Including/excluding globs for gulp.src

六眼飞鱼酱① 提交于 2019-12-06 01:16:08

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.

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