Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The first usage example is incorrect and gives the wrong impression about how gulp-filter matches files. #55

Closed
lddubeau opened this issue Oct 22, 2015 · 6 comments

Comments

@lddubeau
Copy link

I'm referring to this

var gulp = require('gulp');
var jscs = require('gulp-jscs');
var gulpFilter = require('gulp-filter');

gulp.task('default', function () {
    // create filter instance inside task function
    var filter = gulpFilter(['*', '!src/vendor']);

    return gulp.src('src/*.js')
        // filter a subset of the files
        .pipe(filter)
        // run them through a plugin
        .pipe(jscs())
        .pipe(gulp.dest('dist'));
});

The intent seems to process everything in src but to exclude the files in src/vendor. Problems:

  1. The filter does no work because src/*.js won't go into subdirectories in the first place. You could remove the filter and you'd still not get anything from src/vendor into dest.
  2. If we amend to src/**/*.js then this pattern will cover every .js file in src, including subdirectories but the filter still gives the wrong impression. It does the work of excluding files in src/vendor not because of !src/vendor but because of *, which matches only files that are not in a subdirectory. You could reduce the filter to gulpFilter('*') and it would still exclude src/vendor and everything in it.
  3. If we amend the filter so that we have ** instead of *, then the filter no longer works because !src/vendor does not match anything. Why? Because gulp-filter matches against the file's relative path.

Ultimately, the code I see doing what the example seems to be illustrating should be:

var gulp = require('gulp');
var jscs = require('gulp-jscs');
var gulpFilter = require('gulp-filter');

gulp.task('default', function () {
    // create filter instance inside task function
    var filter = gulpFilter(['**', '!vendor/**']);

    return gulp.src('src/**/*.js')
        // filter a subset of the files
        .pipe(filter)
        // run them through a plugin
        .pipe(jscs())
        .pipe(gulp.dest('dist'));
});

The examples based off of this one should also be fixed.

@mix3d
Copy link

mix3d commented Mar 2, 2016

THANK YOU!!!! This finally fixed the problem's I'd been having trying to use this plugin, and was very confused why a single * would work, since both glob patterns didn't match the rest of gulp's glob styles.

@leocaseiro
Copy link
Contributor

Perfect!
This fix my issue as well. The documentation should be updated.

👍

@nfroidure
Copy link
Contributor

I agree, submitting a PR would help to clean it quicker.

leocaseiro added a commit to leocaseiro/gulp-filter that referenced this issue Mar 3, 2016
The examples was incorrected.

* From `'src/*.js'` to `'src/**/*.js'`
* From `'*'` to `'**'`
sindresorhus added a commit that referenced this issue Mar 4, 2016
Fix documents with references from #55
@VAggrippino
Copy link

Hah! One lousy asterisk caused my problem and it took me several hours to figure it out. Thank you, @lddubeau, for posting this issue. It helped me solve a problem that was driving me crazy.

ref: http://stackoverflow.com/q/35911876/2948042

@sindresorhus
Copy link
Owner

This was fixed in leocaseiro@922496e.

@ghost
Copy link

ghost commented Sep 19, 2016

The "Usage" section on the npm page still shows the example with a single asterisk: https://www.npmjs.com/package/gulp-filter#filter-only

const f = filter(['*', '!src/vendor']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants