2016-04-22 51 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"));