问题
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:
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 ab
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'));
});
来源:https://stackoverflow.com/questions/37664138/gulp-globbing-excluding-files-then-unexcluding-not-working-as-described