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

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

问题描述

我有我在 gulpfile 中使用的当前 gulp 任务.路径来自 config.json,一切正常:

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());
});

但我仍然有一个问题,我的项目中的图像数量很大,而且这项任务需要很长时间.我正在寻找一种仅在修改后的文件上运行我的任务的方法.如果我有图像或对其进行修改,imagemin() 将仅在此图像上运行,而不是全部运行.

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.

谢谢.

推荐答案

"gulp-changed" 不是解决这个问题的最佳方案,因为它只查看 "buildType" 文件夹中的修改文件.

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

改用 gulp-cached.

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

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