【问题标题】:Gulp watch callback task not firingGulp watch 回调任务未触发
【发布时间】:2018-01-01 11:54:29
【问题描述】:

我正在尝试设置一个 gulp-watch,它会在某些文件更改时触发 gulp 任务。为此,我使用以下代码:

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    return watch(watchPath, ['FilesChanged']);
});

gulp.task('FilesChanged', function (cb){
    FilesChanged();
    cb();
})

function FilesChanged()
{
    console.log("Files changed:")
}

每当文件更改时,我希望“FilesChanged”任务会触发并且我会在控制台中看到“文件已更改:”文本。这不会发生。

当我将 watchChanges 任务更改为:

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    return watch(watchPath, FilesChanged);
});

谁能解释为什么第一段代码没有正确执行?

【问题讨论】:

  • 您是否有自己的手表功能正在调用或打算使用 gulp.watch?如果你有一个手表功能,它是什么样子的?你在那里使用 gulp-watch 吗?我们需要更多信息。 Gulp-watch 可以像在工作示例中那样传递一个函数,但不能传递一个任务名称列表。
  • @Mark 我正在使用 gulp-watch。我忘了在示例中包含我的一部分代码:var gulp = require('gulp'); var watch = require('gulp-watch');

标签: node.js gulp gulp-watch gulp-task


【解决方案1】:

因为你使用的是 gulp-watch,如果你看一下它的函数定义:

watch(glob, [options, callback])

你可以看到它需要一个回调函数,这就是为什么你的

return watch(watchPath, FilesChanged);

工作和

return watch(watchPath, ['FilesChanged']);

没有。 gulp-watch 不会像 gulp.watch 那样执行一系列任务:

gulp.watch(glob[, opts], 任务)

gulp-watch 和 gulp.watch 是两个完全不同的东西。你可能想要的是

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    gulp.watch(watchPath, ['FilesChanged']);
});

如果您想查看正在处理哪些文件,请尝试get the current file name 的建议之一。

【讨论】:

  • 感谢您的回复。我确实查看了 watch 和 gulp-watch 的函数定义。我已将我的代码更正为您的上一个示例,但我仍然没有得到所需的结果。所以这行得通:watch(watchPath, FilesChanged) .pipe(debug()); 但这行不通:gulp.watch(watchPath, ['FilesChanged']) .pipe(debug());
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多