gulp main-bower-files regular expression filter no

2019-06-21 04:30发布

问题:

Why is the second array, bowerFiles, not filtered to just javascript files.

var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');

gulp.task('default', function () {

  var unfiltered = mainBowerFiles(); 
  console.log('unfiltered files:', unfiltered); // 11 FILES

  //var jsRegEx = /js$/i; // tried this way too...
  var jsRegEx = new RegExp('js$', 'i');
  var bowerFiles = mainBowerFiles(jsRegEx);
  console.log('bower files:', bowerFiles); // 11 FILES

});

I have tried to mimic what bower-main-files is doing here, and it works.

回答1:

You don't need to create a RegExp to filter for files with main-bower-files.

Indeed, you can simply pass an array or a string of glob to check only for .js files:

gulp.task('default', function () {
  var bowerFiles = mainBowerFiles('**/*.js');
  console.log('bower files: ', bowerFiles);
});

If you really want to use the regex, you have to use the filter option, you can't directly pass it as an argument:

gulp.task('default', function () {
  var bowerFiles = mainBowerFiles({ filter: new RegExp('.*js$', 'i') });
  console.log('bower files: ', bowerFiles);
});