在流的索引处插入特定文件

时间:2016-04-22 12:08:04

标签: gulp

基本上这个gulp脚本找到了' file1,file2,file3'在文件夹中,构建其依赖项(位于同一文件夹中)并将其全部添加到1个文件中。

现在,我的问题是,我有一个自定义文件,我必须放在file2之后。我怎样才能做到这一点?

function gatherAMD(stream, file) {
  var moduleId = file.path.match(/file\.(.+)\.js/)[1];
  return stream.pipe(amdOptimize(`file.${moduleId}`, {
      baseUrl: "fileslocation/location",
      exclude: [ 'jquery' ]
  }));
}



gulp.task("js", function() {
  var included = [];
  var files = "file1,file2,file3"
  var stream = null;

  if (files.indexOf(",") == -1) {
    stream = gulp.src('fileslocation/location/file.'+ files+ '.js', { base: "fileslocation/location"});
  } else {
    stream =  gulp.src(`fileslocation/location/file.{${files}}.js`, { base: "fileslocation/location"});
  }

  return stream.pipe(debug())
          .pipe(foreach(gatherAMD))
          .pipe(filter(function(file) {
              if (included.indexOf(file.path) === -1) {
                  included.push(file.path);
                  return true;
              }  else {
                  return false;
              }
          }))
          .pipe(concat({path: 'fileslocation/custom.js', base: 'fileslocation'}))
          .pipe(gulp.dest("fileslocation"));
});

1 个答案:

答案 0 :(得分:0)

您可以使用map检查流中的当前文件,然后从某个逻辑添加它,然后使用gulp-inject将文件添加到流中。

类似的东西,

return stream.pipe(debug())
          .pipe(foreach(gatherAMD))
          .pipe(filter(function(file) {
              if (included.indexOf(file.path) === -1) {
                  included.push(file.path);
                  return true;
              }  else {
                  return false;
              }
          }))
          .pipe(map(function(file, callback) {
              if (file.relative === 'file2.js') {
                   // inject file to the stream here
              }
              callback(null, file);
          }))
          .pipe(concat({path: 'fileslocation/custom.js', base: 'fileslocation'}))
          .pipe(gulp.dest("fileslocation"));