Gulp Watch只运行一次

时间:2015-03-25 07:45:16

标签: gulp gulp-watch

我正在使用这个Gulp Watch样本: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计算机上运行它时,它仅在我第一次更改文件时运行:

C:\test>gulp watch
[08:40:21] Using gulpfile C:\test\gulpfile.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

下次没有任何反应。为什么呢?

5 个答案:

答案 0 :(得分:17)

如果您仔细阅读文档,则会看到以下短语:

  

您可以传递普通回调,将在每个事件上调用或将其包装在gulp-batch中以运行一次

所以,这基本上是与gulp-batch的交易。要不断观看,只需删除批量调用:

gulp.task('build', function (done) { 
    console.log('Working!'); 
    done();
});

gulp.task('watch', function () {
    watch('app/*.js', function () {
        gulp.start('build');
    });
});

(并在build添加'完成'回调,让Gulp知道你何时完成。

顺便说一句......我不确定,但我认为gulp-watch不仅意味着观看文件,还意味着直接返回一个乙烯基对象。因此,实际使用内置gulp.watch应具有相同的效果:

gulp.task('watch', function () {
    gulp.watch('app/**/*.js', ['build']);
});

答案 1 :(得分:6)

我遇到了同样的问题并使用了与ddprrt相同的问题。区别在于使用目录通配符作为绝对路径。

我改变了这个:

 gulp.task('watch', function() {
   gulp.watch('sass/shortcutcss/*.scss', ['sass'])
 });

到此:

 gulp.task('watch', function() {
   gulp.watch('sass/**/*.scss', ['sass'])
 });

答案 2 :(得分:6)

对我来说,它为任务添加了“返回”:

gulp.task('styles', function(){
 return gulp.src('css/styles.css')
    .pipe(autoprefixer())
    .pipe(gulp.dest('build'));
});

答案 3 :(得分:1)

This problem made me crazy for a weekend. I tried all:

  • Add done()-Event
  • Rename/Touch/Clear target CSS
  • Be more specific with the filepaths

But the (reason for this problem and) the solution was so easy that I felt pathetic after that:

Just update your nodejs installation, mine was 0.12.x! No wonder that this doesn't worked.

After that the watch event works again. Sometimes it goes wrong again, too. But in this cases just save your file a second time and it get recognized. (I think foundation/gulp watch checks for changes to fast and while your file get replaced with the new uploaded one)

答案 4 :(得分:0)

长话:
对我来说,它长时间不起作用。一切似乎都设置正确,但只运行一次。

但突然间我开始在另一个项目上使用 Parcel js,同样的事情发生在他们的内置手表上。于是我寻找答案,发现问题出在我的 Vim 设置上。

回答对我有帮助的是@acobster https://stackoverflow.com/a/55435197/2686510

简而言之:
通过添加 .vimrc

更新 set backupcopy=yes
相关问题