sindresorhus / gulp-filter

Filter files in a `vinyl` stream
MIT License
315 stars 37 forks source link

Unable match on a per-directory basis #53

Closed notpeelz closed 7 years ago

notpeelz commented 8 years ago

Sample gulpfile:

var gulp = require('gulp'),
    debug = require('gulp-debug'),
    filter = require('gulp-filter');

gulp.src('./build/css/**/*.css')
    .pipe(debug({ title: 'base-pre' }))
    .pipe(filter('build/css/**/*.css'))
    .pipe(debug({ title: 'base-post' }));

gulp.src('./build/css/**/*.css')
    .pipe(debug({ title: 'glob-pre' }))
    .pipe(filter('**/*.css'))
    .pipe(debug({ title: 'glob-post' }));

Output:

[14:29:32] base-pre ~/path/to/project/build/css/site.css
[14:29:32] base-pre ~/path/to/project/build/css/dummy.css
[14:29:32] base-pre 2 items
[14:29:32] base-post 0 items
[14:29:32] glob-pre ~/path/to/project/build/css/site.css
[14:29:32] glob-pre ~/path/to/project/build/css/dummy.css
[14:29:32] glob-post ~/path/to/project/build/css/site.css
[14:29:32] glob-post ~/path/to/project/build/css/dummy.css
[14:29:32] glob-pre 2 items
[14:29:32] glob-post 2 items

I'm not quite sure as to what's causing this issue; whether it has to do with streamfilter, multimatch or minimatch, non-glob patterns are clearly not being applied correctly.

Related issue: #52


Upon further analysis, I have come to the conclusion that this really only has to do with the way Gulp handles streams works (as opposed to actually being a bug; see gulpjs/gulp#699).

All things considered, I ended up setting the base directory to ./ to keep it from interpreting the build directory as the base: gulp.src('./build/css/**/*.css', { base: './' })

It could be a good idea to include a similar example in the README.md so to help out other users facing this issue.

kewwwa commented 8 years ago

I also came up with a strange combo with main-bower-files 2.11.0.

main-bower-files + gulp-filter = not working

gulp.src(mainBowerFiles())
    .pipe(gulpFilter('**/fonts/*'))
    .pipe(gulp.dest('./demo/assets/fonts'));

main-bower-files + basedir + gulp-filter = working

gulp.src(mainBowerFiles(), {base: './'})
    .pipe(gulpFilter('**/fonts/*'))
    .pipe(gulp.dest('./demo/assets/fonts'));

main-bower-files with filter = working

gulp.src(mainBowerFiles('**/fonts/*'))
    .pipe(gulp.dest('./demo/assets/fonts'));

simple gulp.src + gulp-filter = working

gulp.src('bower_components/**')
    .pipe(gulpFilter('**/fonts/*'))
    .pipe(gulp.dest('./demo/assets/fonts'));
notpeelz commented 8 years ago

@kewwwa That's definitely odd; I remember encountering the same issues while toying around with my gulpfile, but I couldn't quite figure out where the inconsistency stemmed from.

My hypothesis as to what's causing the issue at hand is that, when sourcing mainBowerFiles() directly (i.e. gulp.src(mainBowerFiles())), the base directory remains unchanged, so any further glob patterns will be matched against the (wrong) root directory.

In your case, it seems that main-bower-files is to blame... but it doesn't quite relate to the original issue. Could this be the intended behavior after all—however unintuitive it is?

nweldev commented 8 years ago

+1 ... Here is my code :

gulp.src(modJsFiles)
                .pipe(debug({title : 'modJsFiles'}))
                .pipe(filter(['app/**/*.js']))
                .pipe(debug({title : 'app js filtered'}))

and its output :

[16:29:51] modJsFiles app\layout\layout-footer_component.js
[16:29:51] modJsFiles gulp-eslintrc.json
[16:29:51] modJsFiles gulpfile.babel.js
[16:29:51] modJsFiles package.json
[16:29:51] modJsFiles scripts\gulp-utils\gitUtil.js
[16:29:51] modJsFiles 5 items
[16:29:51] app js filtered 0 items
notpeelz commented 8 years ago

@noelmace Where does modJsFiles come from? You would have to set the base directory on that stream so that gulp-filter can match the pattern correctly.

mix3d commented 8 years ago

Another thing that i noticed, was the filter has forward slashes, while the output of modJsFiles has backward slashes. Depending on your operating system this could cause a mismatch