如何仅使用Gulp手表在修改的文件上运行任务 [英] How to run a task ONLY on modified file with Gulp watch

查看:101
本文介绍了如何仅使用Gulp手表在修改的文件上运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个我在gulpfile中使用的gulp任务。路径来自config.json并且一切正常:


$ b

  //更多的代码和变量... 

gulp.task('watch',function(){
gulp.watch([config.path.devfolder +/ ** / * .png,config.path.devfolder +/ ** / *。jpg,config.path.devfolder +/ ** / * .gif,config.path.devfolder +/ ** / *。jpeg] ,['imagemin-cfg']);
})

//更多代码...

gulp.task('imagemin-cfg', function(){
return gulp.src([config.path.devfolder +/ ** / *。png,config.path.devfolder +/ ** / *。jpg,config.path.devfolder + / ** / * .gif,config.path.devfolder +/ ** / *。jpeg],{read:false})
.pipe(imagemin({
progressive:true,
svgoPlugins:[{removeViewBox:false}],
use:[pngcrush()]
}))
.pipe(gulp.dest(buildType))
。 pipe(connect.reload());
});

但是我仍然有一个问题,我的项目中的图片数量很大,这个任务需要很长时间。我正在寻找一种方法来只在修改的文件上运行我的任务。如果我有图像或修改它, imagemin()只会在这张图片上运行,而不是全部。



<再次,一切工作都很完美,但运行时间很长。

谢谢。

解决方案

gulp-changed不是这样做的最佳解决方案,因为它只能在buildType文件夹中观看修改过的字段。

尝试吞食缓存。


I have the current gulp task which I use in a gulpfile. Path are from a config.json and everything works perfectly:

//Some more code and vars...

gulp.task('watch', function() {
    gulp.watch([config.path.devfolder+"/**/*.png", config.path.devfolder+"/**/*.jpg", config.path.devfolder+"/**/*.gif", config.path.devfolder+"/**/*.jpeg"], ['imagemin-cfg']);
})

//Some more code ...

gulp.task('imagemin-cfg', function () {
    return gulp.src([config.path.devfolder+"/**/*.png", config.path.devfolder+"/**/*.jpg", config.path.devfolder+"/**/*.gif", config.path.devfolder+"/**/*.jpeg"], {read: false})
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],
            use: [pngcrush()]
        }))
        .pipe(gulp.dest(buildType))
        .pipe(connect.reload());
});

But I still have an issue, the number of images in my project is huge and this task takes ages. I'm looking for a way to run my task ONLY on modified files. If I had an image or modify it, imagemin() will only run on this image, and not on all.

Once again everything is working perfectly fine, but the run time is really long.

Thanks.

解决方案

"gulp-changed" is not a best solution for doing this, because it watch only modified filed in "buildType" folder.

Try gulp-cached instead.

这篇关于如何仅使用Gulp手表在修改的文件上运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆