首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gulp changed/gulp newer+gulp rename无效

gulp changed/gulp newer+gulp rename无效
EN

Stack Overflow用户
提问于 2015-11-03 04:04:41
回答 2查看 1.2K关注 0票数 2

我试图绕过缩小,调整大小和重命名已经处理过的图像,添加'gulp- changed‘对我没有任何影响;所有的文件都被处理了,е非常及时。我试了试“gulp newer”,但还是没有成功。

后来,我发现-如果我愿意的话-重命名,吞咽-更改的工作很好。在任务中使用gulp-rename -它不会。但无论如何我需要gulp rename...

代码语言:javascript
复制
var gulp        = require('gulp');
var changed     = require('gulp-changed');
var imagemin    = require('gulp-imagemin');
var pngquant    = require('imagemin-pngquant');
var imageResize = require('gulp-image-resize');
var rename      = require('gulp-rename');

var img_src = ['_img/**/*.jpg', '_img/**/*.png'];
var img_dest = '_site/img';

gulp.task('resize-xl', function () {
    return gulp.src(img_src)
    .pipe(changed(img_dest))
    .pipe(imageResize({
      width : 2048,
      crop : false,
      upscale : true,
      sharpen: false,
      quality: 1
    }))
    .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],
            use: [pngquant()]
        }))
        .pipe(rename(function (path) {
          path.basename += "-2048";
        }))
        .pipe(gulp.dest(img_dest));
});
EN

回答 2

Stack Overflow用户

发布于 2016-02-24 19:19:29

所有的文件都会被处理,因为任务中的gulp-changed (或gulp-newer)会检查名称为gulp.src(img_src)的文件的更改,所以е的时间非常长。由于img_dest目录中没有原始名称的文件,因此将对img_src目录中的所有文件执行任务。

要解决此问题,可以对修改后的文件使用暂存目录。例如:

1)新建目录'_resize‘。

2)修改gulpfile.js:

代码语言:javascript
复制
var img_resize = '_resize';

gulp.task( 'resize-xl', function () {
    return gulp.src( img_src )
       .pipe( changed( img_resize ) )

       // add here your image plugins: imageResize, imagemin, ..

       // save the modified files with original names on '_resize' dir
       .pipe( gulp.dest( img_resize ) )

       .pipe( rename( { suffix: '-2048' } ) )
       // save the modified files with new names on destanation dir
       .pipe( gulp.dest( img_dest ) );
} );

首次运行后,此任务将仅处理新文件和更改的文件

票数 1
EN

Stack Overflow用户

发布于 2016-11-25 01:32:54

您还可以在通过管道传输更改后的插件之前重命名文件,以便插件获得具有新名称的源文件:

代码语言:javascript
复制
gulp.task( 'resize-xl', function () {
return gulp.src( img_src )
   // change name first
   .pipe( rename( { suffix: '-2048' } ) )
   .pipe( changed( img_dest ) )

   // add here your image plugins: imageResize, imagemin, ..

   .pipe( gulp.dest( img_dest ) );
} );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33485910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档