Gulp Watch 只运行一次 [英] Gulp Watch only run once
问题描述
我正在使用这个 Gulp Watch 示例:https://github.com/floatdrop/gulp-watch/blob/master/docs/readme.md#starting-tasks-on-events.
I'm using this Gulp Watch sample: https://github.com/floatdrop/gulp-watch/blob/master/docs/readme.md#starting-tasks-on-events.
var gulp = require('gulp');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
gulp.task('build', function () { console.log('Working!'); });
gulp.task('watch', function () {
watch('**/*.js', batch(function () {
gulp.start('build');
}));
});
当我在我的 Windows 8 机器上运行它时,它只在我第一次更改文件时运行:
When I run it on my Windows 8 machine, it only runs the first time I change a file:
C: est>gulp watch
[08:40:21] Using gulpfile C: estgulpfile.js
[08:40:21] Starting 'watch'...
[08:40:21] Finished 'watch' after 2.69 ms
[08:40:31] Starting 'build'...
Working!
[08:40:31] Finished 'build' after 261 µs
下次什么都没有发生.为什么?
Next time nothing happens. Why?
推荐答案
如果您仔细阅读文档,您会看到以下短语:
If you read the documentation closely, you see the following phrase:
您可以传递普通回调,它将在每个事件上调用或将其包装在 gulp-batch 中以运行它一次
You can pass plain callback, that will be called on every event or wrap it in gulp-batch to run it once
所以,这基本上就是 gulp-batch
的处理.要持续观看,只需删除批处理调用:
So, that's basically the deal with gulp-batch
. To constantly watch it, just remove the batch call:
gulp.task('build', function (done) {
console.log('Working!');
done();
});
gulp.task('watch', function () {
watch('app/*.js', function () {
gulp.start('build');
});
});
(并将完成"回调添加到 build
以让 Gulp 知道您何时完成).
(and add the 'done' callback to build
to let Gulp know when you're finished).
顺便说一句...我不确定,但我认为 gulp-watch
不仅意味着观看文件,而且还直接返回乙烯基对象.所以实际使用内置的 gulp.watch
应该也是一样的效果:
Btw... I'm not sure, but I think gulp-watch
is meant to not only watch files, but also directly returning a vinyl object. So actually using the built-in gulp.watch
should have the same effect:
gulp.task('watch', function () {
gulp.watch('app/**/*.js', ['build']);
});
这篇关于Gulp Watch 只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!