2016-04-22 53 views
0

基本上,这个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")); 
}); 

回答

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