61

I would like to watch all, but the .min.ext files in my directories with gulp.js. What is the best way to filter out those?

Example:

gulp.task('watch', function(e) {
   gulp.watch('./js/*.js', ['css']); // don't want to watch .min.js files. what is best way?
});

EDIT: If can't be done without external packages, which one is the most reputable?

i--
  • 4,349
  • 2
  • 27
  • 35

2 Answers2

101

gulp.watch internally uses vinyl-fs (see source), which uses gaze, which uses itself minimatch, so you should be able to ignore some files using !./js/*.min.*.

In fact, this is even described in vinyl-fs's README:

fs.src(["./js/**/*.js", "!./js/vendor/*.js"])
[…]
Paul Mougel
  • 16,728
  • 6
  • 57
  • 64
  • 17
    This works too `./js/!(vendor)/**/*.js`, so you only need one entry in the watch – guzart Nov 14 '14 at 00:13
  • `gulp@3.9.1` uses `vinyl-fs@0.3.0` which uses `glob-watcher@^0.0.6` (best match is `0.0.8`) which uses `gaze@^0.5.1` (best match is `0.6.4`) which uses `globule~0.2.0` which uses `minimatch@~0.2.11`AND `glob~3.2.7` which produces interesting results... Any match not starting with `!` will return ALL paths from that match, and any match starting with `!` will then filter the resulting array. Be wary if you use `**/*` and then filter it as it will take a long time. [Relevant code](https://github.com/cowboy/node-globule/blob/v0.2.0/lib/globule.js#L23-L39) – Cobertos Dec 18 '18 at 22:10
0

Faced the same issue in gulp 4, the fix is as follows

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

css is gulp.series/gulp.parallel or any function that you want to run.

Suraj Mandal
  • 221
  • 2
  • 9