Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp-filter is filtering everything

gulp-filter is filtering everything.

I'm trying to use gulp-filter to exclude a single file from gulp-uglify:

var uglify = require('gulp-uglify');
var filter = require('gulp-filter');

var paths = {
    css: 'src/*.css',
    html: 'src/*.html',
    js: 'src/*.js'
};

gulp.task('js', function() {
    // Create a filter for the ToC options file. We don't want to minify that.
    var f = filter(['*', '!src/ToCOptions.js'], {"restore": true});

    return gulp.src(paths.js)
        .pipe(f)
        .pipe(uglify())
        .pipe(f.restore)
        .pipe(gulp.dest('js'));
});

I've read gulp-filter filters out all files, which seems to be the same problem, but the accepted answer doesn't work for me.

I've tried several variations on the filter, including:

  • var f = filter(['*', '!ToCOptions.js'], {"restore": true});
    Nothing processed by gulp-uglify.
  • var f = filter('!ToCOptions.js', {"restore": true});
    Nothing processed by gulp-uglify.
  • var f = filter('!src/ToCOptions.js', {"restore": true});
    Nothing processed by gulp-uglify.
  • var f = filter('src/ToCOptions.js', {"restore": true});
    Only the file I wanted to exclude was processed by gulp-uglify.
  • var f = filter(['*', 'src/ToCOptions.js'], {"restore": true});
    Only the file I wanted to exclude was processed by gulp-uglify.
  • var f = filter(['*', '!/src/ToCOptions.js'], {"restore": true});
    Nothing processed by gulp-uglify.
  • var f = filter(['*', '!/ToCOptions.js'], {"restore": true});
    Nothing processed by gulp-uglify.

What am I doing wrong?

like image 279
Vince Avatar asked Mar 10 '16 09:03

Vince


1 Answers

Apparently, this is an error in the documentation that is explained in issue #55 at the Github repository.

The solution in my case is this:

var f = filter(['**', '!src/ToCOptions.js'], {'restore': true});

As the author of the issue explains, a single asterisk only matches files that are not in subdirectories. All of the files for this task are in a subdirectory, so the first pattern didn't match anything and the second pattern didn't have any results to subtract from.

To understand **, I had to use man bash on a Linux system and search for globstar since the node-glob documentation makes reference to the Bash implementation:

globstar
    If  set,  the  pattern  ** used in a pathname expansion context will
    match all files and zero or more directories and subdirectories.  If the
    pattern is followed by a /, only directories and subdirectories match.
like image 160
Vince Avatar answered Oct 26 '22 23:10

Vince