Edit:显然gulp.watch
现在可以处理新的或删除的文件。当问题被问到时却没有。
我的答案的其余部分仍然有效:gulp-watch
通常是更好的解决方案,因为它允许您仅对已修改的文件执行特定操作,而gulp.watch
只允许您运行完整的任务。对于一个合理规模的项目,这很快就会变得太慢而无用。
你没有遗漏任何东西。gulp.watch
不适用于新文件或已删除的文件。这是一个专为简单项目设计的简单解决方案。
要获得可以查找新文件的文件监视,请使用the gulp-watch plugin,这更强大。用法如下:
var watch = require('gulp-watch');
// in a task
watch({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 就可以很好地工作。
您可以使用以下方法来结束您的流my lazypipe library,或者简单地使用一个函数和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());
UPDATE2014 年 10 月 15 日
正如下面@pkyeck 所指出的,显然 1.0 版本gulp-watch
稍微改变了格式,所以上面的例子现在应该是:
var watch = require('gulp-watch');
// in a task
watch(<<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>> );
});
and
var combine = require('stream-combiner');
function scriptsPipeline() {
return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));
}
watch('src/scripts/**/*.js')
.pipe(scriptsPipeline());