How can I use a glob to ignore files that start wi

2019-01-24 03:22发布

问题:

I am using this file matching in gulp:

'content/css/*.css'

But I would like to only include the files that do not start with an underscore. Can someone tell me how I can do this?

Here's the code:

gulp.task('less', function () {
    gulp.src('content/less/*.less')
        .pipe(less())
        .pipe(gulp.dest('content/css'));
});

回答1:

If Gulp wildcards are like shell wildcards:

content/css/[^_]*.css

The ^ at the beginning of a character set means to match any characters not in the set.



回答2:

You can add a second glob whitch will filter files or folders with an underscore.

For ignore folders I use:

gulp.src(['./src/**/*.html', '!**/_*/**'])

'!**/_*/**' will filter folders that start with an underscore.



回答3:

Use this plugin https://github.com/robrich/gulp-ignore

var gulpIgnore = require('gulp-ignore');
var condition = '_*.css'; //exclude condition

gulp.task('less', function() {
  gulp.src('content/less/*.less')
    .pipe(gulpIgnore.exclude(condition))
    .pipe(less())
    .pipe(gulp.dest('/dist/'));
});

I think gulp supports file glob not full-regex, But You can give a try to content/css/[^_]*.less



回答4:

You could use this regex.

content/css/[^_].*\.css

Add $ at the last if necessary.



标签: gulp glob