Search code examples
gulptaskwatch

Gulp: Is there any difference with .on('change') and without?


Is there any difference between:

gulp.watch("src/sass/**/*.{scss,sass}").on("change", gulp.series("style")); 

and

gulp.watch("src/sass/**/*.{scss,sass}", gulp.series("style"));

?

Looks like it work the same.


Solution

  • There are more events that can be watched, see chokidar watch events.

    In my testing if I use

    gulp.watch("src/sass/**/*.{scss,sass}").on("change", gulp.series("style")); 
    

    the task will not trigger if I add folders or files or copy a file into a pre-existing folder that is being watched (probably also deleting - I just didn't test that).

    If I use

    gulp.watch("src/sass/**/*.{scss,sass}", gulp.series("style"));
    

    all the above - adding and copying events do trigger the task. So apparently, not specifying a watch event is the same as specifying on("all", ...). Which is probably what you want unless you want different behaviour when you add, delete or copy a file vs. when you modify an existing one. The events just give you more fine-grained control.