gulp main-bower-files regular expression filter not working

旧街凉风 提交于 2019-12-10 13:59:24

问题


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);
});


来源:https://stackoverflow.com/questions/28749749/gulp-main-bower-files-regular-expression-filter-not-working

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