编辑: 显然
gulp.watch现在可以处理新文件或删除的文件。询问问题时没有。
我剩下的答案仍然
gulp-watch是:通常是一个更好的解决方案,因为它使您仅可以对已修改的文件执行特定的操作,而
gulp.watch仅允许您运行完整的任务。对于合理大小的项目,这将很快变得太慢而无法使用。
您什么都不会错过。
gulp.watch不适用于新文件或删除的文件。这是专为简单项目设计的简单解决方案。
要查看可以查找新文件的文件,请使用功能更强大的
gulp-watchplugin。用法如下所示:
var watch = require('gulp-watch');// in a taskwatch({glob: <<glob or array of globs>> }) .pipe( << add per-file tasks here>> );// if you'd rather rerun the whole task, you can do this:watch({glob: <<glob or array of globs>>}, function() { gulp.start( <<task name>> );});就个人而言,我建议第一种选择。这样可以加快每个文件的处理速度。只要您不连接任何文件,它在使用livereload进行开发的过程中就可以很好地工作。
您可以使用我的
lazypipe库,也可以仅使用函数来包装流,
stream-combiner如下所示:
var combine = require('stream-combiner');function scriptsPipeline() { return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));}watch({glob: 'src/scripts*.js' }) .pipe(scriptsPipeline());更新 2014年10月15日
如下面的@pkyeck所指出的,显然1.0版对
gulp-watch格式进行了稍微的更改,因此上述示例现在应为:
var watch = require('gulp-watch');// in a taskwatch(<<glob or array of globs>>) .pipe( << add per-file tasks here>> );// if you'd rather rerun the whole task, you can do this:watch(<<glob or array of globs>>, function() { gulp.start( <<task name>> );});和
var combine = require('stream-combiner');function scriptsPipeline() { return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));}watch('src/scripts*.js') .pipe(scriptsPipeline());


