Gulp globbing excluding files then unexcluding not working as described

331 Views Asked by At

If I have the files

client/
  a.js
  bob.js
  bad.js

And the gulp task

gulp.task('copy', function() {
  return gulp.src(['client/*.js', '!client/b*.js', 'client/bad.js'])
             .pipe(gulp.dest('public'));
});

then according to the documentation we should copy a.js and bad.js. However, when I run this with gulp v3.9.1, it only copies a.js.

Is this a known bug? Is there a way to do this?

1

There are 1 best solutions below

1
On BEST ANSWER

It's not a bug, the documentation is just wrong. The newest version of gulp is [email protected] which uses [email protected]. The behavior you're referring to wasn't introduced until [email protected].

In fact, elsewhere the gulp docs explicitly state that glob ordering will be a new feature in [email protected]:

globs passed to gulp.src will be evaluated in order, which means this is possible gulp.src(['*.js', '!b*.js', 'bad.js']) (exclude every JS file that starts with a b except bad.js)

That means you could simply use to the current development version of gulp (gulpjs/gulp#4.0) and take advantage of the new feature. Note however that gulp 4.x is radically different from gulp 3.x when it comes to defining tasks.

One workaround would be to keep using gulp 3.x for tasks definitions, but use the newest version of vinyl-fs to create vinyl streams:

var vinylFs = require('vinyl-fs');

gulp.task('copy', function() {
  return vinylFs.src(['client/*.js', '!client/b*.js', 'client/bad.js'])
    .pipe(vinylFs.dest('public'));
});

And if you don't want to do that you can always use merge-stream to combine multiple streams into one stream:

var merge = require('merge-stream');

gulp.task('copy', function() {
  return merge(gulp.src(['client/*.js', '!client/b*.js']),
               gulp.src(['client/bad.js']))
    .pipe(gulp.dest('public'));
});