简体   繁体   中英

Gulp watch callback task not firing

I'm trying to set up a gulp-watch which triggers a gulp task when some files change. For this purpose I'm using the following code:

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:")
}

Whenever the files change I would like it if the 'FilesChanged' task would fire and I'd see the "Files changed:" text in console. This doesn't happen.

When I change my watchChanges task to this:

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

    return watch(watchPath, FilesChanged);
});

Can anyone explain why the first piece of code does not execute correctly?

Since you are using gulp-watch if you look at its function definition:

watch(glob, [options, callback])

you can see that it will take a callback function which is why your

return watch(watchPath, FilesChanged);

works and

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

does not. gulp-watch will not take an array of tasks like gulp.watch will:

gulp.watch(glob[, opts], tasks)

gulp-watch and gulp.watch are two entirely different things. What you probably want is

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

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

If you then to see which files are being processed, try one of the suggestions from get the current file name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM