2

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?

Sam Fen
  • 5,074
  • 5
  • 30
  • 56

1 Answers1

0

It's not a bug, the documentation is just wrong. The newest version of gulp is gulp@3.9.1 which uses vinyl-fs@0.3.14. The behavior you're referring to wasn't introduced until vinyl-fs@1.0.0.

In fact, elsewhere the gulp docs explicitly state that glob ordering will be a new feature in gulp@4.0.0:

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'));
});
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • Thank you. It looks like another solution, the one I ended up using, was to use [https://github.com/UltCombo/gulp-src-ordered-globs/](gulp-src-ordered-globs). This acts as a drop-in replacement for `gulp.src` with the expected glob behavior. – Sam Fen Jun 06 '16 at 19:52