Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including/excluding globs for gulp.src

I'm trying to setup a glob array for my javascript concat build task in gulp. The directory structure looks as follows:

├── about
│   └── about.js
├── assets
├── contact
├── core
│   ├── navbar
│   │   ├── navbar.js
│   │   └── navbar.test.js
│   ├── routing.js
│   ├── routing.test.js
│   ├── utils.js
│   └── utils.test.js
├── generated
│   ├── footer.js
│   ├── header.js
│   └── templates.js
├── home
├── app.js
└── config.js

The order of the files is important:

  1. generated/header.js
  2. app.js
  3. any of the *.js files, except those here below
  4. generated/templates.js
  5. generated/footer.js

I've wildly tried all kinds of wildcards combination, but the globbing isn't strong with me.

var inputFiles = [
  'generated/header.js',
  'app.js',
  '!(generated)**/*.js',    // <=---- ???
  'generated/templates.js',
  'generated/footer.js',
  '!**/*.test.js'
];

So how do I include all *.js files except those from a subdirectory?

Thanks.

like image 308
null Avatar asked Mar 16 '26 11:03

null


1 Answers

The best I came up with:

var gulp = require('gulp');
var tap = require('gulp-tap');

gulp.task('default', function() {
    return gulp.src([
        'generated/header.js',
        'app.js',
        '*.js',
        './!(generated)/**/*.js', // <- All subdirs except 'generated'
        'generated/{templates,footer}.js',
        '!**/*.test.js',
        '!node_modules/**'
    ]).pipe(tap(function(file) {
        console.log(file.path);
    }));
});

Running it:

∴ glob-test gulp
[20:07:51] Using gulpfile ~/Desktop/glob-test/gulpfile.js
[20:07:51] Starting 'default'...
/Users/heikki/Desktop/glob-test/generated/header.js
/Users/heikki/Desktop/glob-test/app.js
/Users/heikki/Desktop/glob-test/config.js
/Users/heikki/Desktop/glob-test/gulpfile.js
/Users/heikki/Desktop/glob-test/about/about.js
/Users/heikki/Desktop/glob-test/core/routing.js
/Users/heikki/Desktop/glob-test/core/utils.js
/Users/heikki/Desktop/glob-test/core/navbar/navbar.js
/Users/heikki/Desktop/glob-test/generated/templates.js
/Users/heikki/Desktop/glob-test/generated/footer.js
[20:07:51] Finished 'default' after 326 ms

The main trick is avoiding the "!" character at the beginning of glob when including files.

https://github.com/isaacs/minimatch#comparisons-to-other-fnmatchglob-implementations

"If the pattern starts with a ! character, then it is negated."

ps. Placement of the negated globs doesn't matter. They are always moved to the end behind the scenes.

like image 145
Heikki Avatar answered Mar 19 '26 01:03

Heikki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!