Gulps gulp.watch没有触发新的或删除的文件? [英] Gulps gulp.watch not triggered for new or deleted files?
问题描述
//监视任务。下面的Gulpjs任务可以正常工作:
gulp.task('watch',['build'],function(){
gulp.watch(src +'/js/**/*.js',['scripts']) ;
gulp.watch(src +'/img//**/*.{jpg,jpeg,png,gif}',['copy:images']);
gulp.watch(src +'/less/*.less',['styles']);
gulp.watch(src +'/templates/**/*.{swig,json}',['html']);
});
//构建任务。
gulp.task('build',['clean'],function(){
return gulp.start('copy','scripts','less','htmlmin');
});
然而,对于新的或已删除的文件它不起作用(它没有被触发)。是否有东西丢失?
编辑:甚至使用grunt-watch插件,它似乎无法正常工作:
gulp.task('scripts',function(){
return streamqueue(
{objectMode:true},
gulp.src([
vendor +'/jquery/dist/jquery.min.js',
vendor +'/bootstrap/dist/js/bootstrap.min.js'
]) ,
gulp.src([
src +'/js/**/*.js'
])。pipe(plugins.uglify())
)
.pipe(plugins.concat(pkg.name +'.min.js'))
.pipe(gulp.dest(dest +'/ js /'));
});
gulp.task('watch',['build'],function(){
plugins.watch({glob:src +'/js/**/*.js' },function(){
gulp.start('scripts');
});
});
编辑:解决了,它是这个问题。以 ./
(即 src
的值)开头的Globs似乎无法使用ATM。
编辑:显然 gulp.watch
现在删除文件。这个问题没有被问到。
我的答案的其余部分仍然是: gulp-watch
通常是一个更好的解决方案,因为它允许您仅对已修改的文件执行特定操作,而 gulp.watch
仅允许您运行完整任务。对于一个合理规模的项目,这将很快变得太慢而无用。
你不会错过任何东西。 gulp.watch
不适用于新的或已删除的文件。这是一个为简单项目设计的简单解决方案。
要获取可以查找新文件的文件,请使用 gulp-watch
插件,它功能更强大。用法如下所示:
var watch = require('gulp-watch');
在任务
watch({glob:< glob或球体数组>>})
.pipe(<<文件任务在这里>>);
//如果您希望重新运行整个任务,您可以这样做:
watch({glob:<< glob或球体数组>>},function ){
gulp.start(<<<任务名称>>);
});
我个人推荐第一个选项。这允许更快,每个文件的进程。只要你不连接任何文件,它在开发过程中效果很好。
www.npmjs.org/package/lazypipe\"> my lazypipe
library ,或者简单地使用函数和 stream-combiner
像这样: var combine = require('stream-combiner');
函数scriptsPipeline(){
return combine(coffeeescript(),uglify(),gulp.dest('/ path / to / dest')); $ {
}
watch({glob:'src / scripts / ** / *。js'})
.pipe(scriptsPipeline());
更新 10月15日,2014
正如下面的@pkyeck所指出的,显然1.0版本的 gulp-watch
稍微改变了格式,所以上面的例子应该是:
var watch = require('gulp-watch');
在任务
中观看(<<<< glob或球体数组>>)
.pipe(<<<<< >);
//如果您希望重新运行整个任务,可以这样做:
watch(<<< glob或球体数组>> ;, function(){
gulp.start(<<<任务名称>>);
});
和
var combine = require('stream-combiner');
函数scriptsPipeline(){
return combine(coffeeescript(),uglify(),gulp.dest('/ path / to / dest'));
$ b $ watch('src / scripts / ** / *。js')
.pipe(scriptsPipeline());
The following Gulpjs task works fine when editing files in the glob match:
// watch task.
gulp.task('watch', ['build'], function () {
gulp.watch(src + '/js/**/*.js', ['scripts']);
gulp.watch(src + '/img//**/*.{jpg,jpeg,png,gif}', ['copy:images']);
gulp.watch(src + '/less/*.less', ['styles']);
gulp.watch(src + '/templates/**/*.{swig,json}', ['html']);
});
// build task.
gulp.task('build', ['clean'], function() {
return gulp.start('copy', 'scripts', 'less', 'htmlmin');
});
However it doesn't work (it's not triggered) for new or deleted files. Is there something I'm missing?
EDIT: even using grunt-watch plugin it seems not working:
gulp.task('scripts', function() {
return streamqueue(
{ objectMode: true },
gulp.src([
vendor + '/jquery/dist/jquery.min.js',
vendor + '/bootstrap/dist/js/bootstrap.min.js'
]),
gulp.src([
src + '/js/**/*.js'
]).pipe(plugins.uglify())
)
.pipe(plugins.concat(pkg.name + '.min.js'))
.pipe(gulp.dest(dest + '/js/'));
});
gulp.task('watch', ['build'], function () {
plugins.watch({glob: src + '/js/**/*.js'}, function () {
gulp.start('scripts');
});
});
EDIT: Solved, it was this issue. Globs starting with ./
(that was the value of src
) seems not working ATM.
Edit: Apparently gulp.watch
does work with new or deleted files now. It did not when the question was asked.
The rest of my answer still stands: gulp-watch
is usually a better solution because it lets you perform specific actions only on the files that have been modified, while gulp.watch
only lets you run complete tasks. For a project of a reasonable size, this will quickly become too slow to be useful.
You aren't missing anything. gulp.watch
does not work with new or deleted files. It's a simple solution designed for simple projects.
To get file watching that can look for new files, use the gulp-watch
plugin, which is much more powerful. Usage looks like this:
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>> );
});
Personally, I recommend the first option. This allows for much faster, per-file processes. It works great during development with livereload as long as you aren't concatenating any files.
You can wrap up your streams either using my lazypipe
library, or simply using a function and stream-combiner
like this:
var combine = require('stream-combiner');
function scriptsPipeline() {
return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));
}
watch({glob: 'src/scripts/**/*.js' })
.pipe(scriptsPipeline());
UPDATE October 15, 2014
As pointed out by @pkyeck below, apparently the 1.0 release of gulp-watch
changed the format slightly, so the above examples should now be:
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());
这篇关于Gulps gulp.watch没有触发新的或删除的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!