如何获取文件名列表并使用gulp进行操作?

时间:2016-07-16 18:45:00

标签: gulp

我想做这样的事情

  return gulp.src(['build/tags/*.tag'])
             .pipe(plugins.filelist('all.js', {
               flatten: true
             }))
             .pipe(plugins.insert.transform(function (content) {
               console.log(content);
               var list = JSON.parse(content);
               var output = "";
               list.forEach(function (file) {
                 output += "import '" + file + "'\n'";
               })
               return output;
             }))
             .pipe(gulp.dest('build'));

那就是我想创建和操作要输出的文件列表。问题是gulp-filelist没有将流更改为文件列表,它只是创建一个列表并传递原始文件流。

我有什么方法可以做我想做的事情吗?

1 个答案:

答案 0 :(得分:1)

gulp-filelist传递原始文件的行为有recently been changed。但是,自npm以来,没有任何新版本被推广到Ask the developers。这意味着如果你运行npm install gulp-filelist,你仍然会得到旧的行为。

这给你留下了三个选择:

  1. gulp-filter gulp-filelist将新版本发布到npm,以便您可以使用新行为。
  2. 直接从github安装当前开发版本的gulp-filelist,而不是使用npm中的版本:

    npm uninstall gulp-filelist
    npm install --save-dev cjroth/gulp-filelist
    
  3. 使用npm中的当前gulp-filelist版本,同时使用this post过滤掉所有不是all.js的文件:

    return gulp.src(['build/tags/*.tag'])
      .pipe(plugins.filelist('all.js', {
         flatten: true
       }))
       .pipe(plugins.filter('all.js')) // only pass all.js through
       .pipe(plugins.insert.transform(function (content) {
          console.log(content);
          var list = JSON.parse(content);
          var output = "";
          list.forEach(function (file) {
            output += "import '" + file + "'\n";
          });
          return output;
       }))
       .pipe(gulp.dest('build'));