0

Sometimes gulp watch is not working when I save a javascript file. This is my watch task below.

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

I do have a bunch of sub directories. Would it be advisable to make my watch task be more specific like:

gulp.task('watch', function() {
    gulp.watch('./src/*.js', ['js']);
    gulp.watch('./src/dir1/**/*.js', ['js']);
    gulp.watch('./src/dir2/**/*.js', ['js']);
});
jamesRH
  • 420
  • 3
  • 12

1 Answers1

1

You need to make your task slightly more specific.

With './src/**/*.js' you are only watching javascript files in sub folders, not in the first level of ./src/

Having gulp.watch(['./src/**/*.js', './src/*.js'], ['js']) will work. Keep in mind watch can take an array of strings to watch for, don't call it multiple times.

Here's a similar question and answer:

How to Gulp-Watch Multiple files?

Community
  • 1
  • 1
joeyfb
  • 3,044
  • 3
  • 19
  • 25
  • 2
    So far as I can tell this answer isn't correct. Gulp uses [minimatch](https://www.npmjs.com/package/minimatch) library for globbing, so you can test it out: `console.log(minimatch('./src/test.js', './src/**/*.js'))` outputs `true`. Additionally, though I can't find documentation specific to the glob in Gulp, if you look at `**/*` in other contexts you will find this behaviour is normal. E.g. [zsh manual 14.8.6](http://zsh.sourceforge.net/Doc/Release/Expansion.html): "note that this therefore matches files in the current directory as well as subdirectories". – Lauren Aug 04 '17 at 09:57