Including/excluding globs for gulp.src

时间秒杀一切 提交于 2020-01-13 19:01:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/26563048/including-excluding-globs-for-gulp-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!