gulp: Can't exclude directories using src glob
I can’t exclude a directory from being copied over when using a src glob. Here’s my task:
gulp.task('copy', function() {
return gulp.src(['app/**', '!app/_tmp/**'])
.pipe(gulp.dest('dist'));
});
Gulp copies over app/**
, but app/_tmp/
is still being copied.
Expected:
+ app/
+ tmp/
file1
file2
+ css/
styles.css
+ js/
app.js
+ dist/
+ css/
styles.css
+ js/
app.js
Gulp output:
+ app/
+ tmp/
file1
file2
+ css/
styles.css
+ js/
app.js
+ dist/
+ tmp/
+ css/
styles.css
+ js/
app.js
Update: Clarified that the dist/_tmp/
directory does not contain any files
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 28 (9 by maintainers)
@aaronbushnell
why?
!app/_tmp
would match only the folder, but not any file within it - since it’s not a globbing expression (a bug?)!app/_tmp/**
would match the files IN the_tmp
folder, but not the folder itself, so the empty folder would be createdusing the
!app/{_tmp,_tmp/**}
pattern uses a pattern to combine the both requirements and says:😃
The way I’ve made this work is to exclude the directory and the contents like so:
gulp.src(['app/**', '!app/_tmp', '!app/_tmp/**'])
It can be golfed further to
!app/_tmp{,/**}
so you don’t need to repeat the directory name.@chovy You have a space there that messes things up:
{vendor, vendor/**}
.Just as a note, I used to use:
But then converting it to:
Shaved off at least 3 whole seconds from my task. Just a heads up, you might want to test your task if you care about performance; it would appear the
dir{**}
notation is really slow.I want to match everything except bower_components and anything inside. So, I used the following code as suggested, but this still matches any folder that has .html files inside, together with those files.
Any suggestions?
Thank you, @ktstowell, your solution worked really well. I’m working with Gulp Zip. It was really cumbersome to exclude a few folders/files and keep everything else including the project tree as it is.
@stryju Thank you for your solution of using
!{_tmp,_tmp/**}
method. I was using!(.gitignore,.sftp-config.json)
to exclude a few files, but that didn’t work.Here’s my code:
['!node_modules', '!node_modules/**', './**']
Worked for me, though I do like @aaronbushnell 's proposition in https://github.com/isaacs/minimatch/issues/25This works well for me:
@sindresorhus No dice. Now the
dist/_tmp/
directory has the files fromapp/_tmp/
inside it.You’re globbing for files, if you want to ignore the dir you need to:
gulp.src(['app/**', '!app/_tmp/'])