問(wèn)題描述
我想遍歷一個(gè)對(duì)象,并在每次迭代時(shí)將一組文件路徑傳遞給 gulp.src,然后對(duì)這些文件進(jìn)行一些處理.下面的代碼僅用于說(shuō)明目的,實(shí)際上并不能正常工作,因?yàn)?return 語(yǔ)句在第一遍就終止了循環(huán).
I'd like to loop through an object and pass an array of file paths to gulp.src on each iteration and then do some processing on those files. The code below is for illustration purposes and won't actually work since the return statement kills the loop on the first pass.
gulp.task('js', function(){
for (var key in buildConfig.bundle) {
return gulp.src(bundleConfig.bundle[key].scripts)
.pipe(concat(key + '.js'));
// DO STUFF
}
});
這是基本的想法.關(guān)于如何做到這一點(diǎn)的任何想法?
That's the basic idea. Any ideas on how to do this?
推薦答案
我能夠使用合并流解決這個(gè)問(wèn)題.如果有人有興趣,這里是代碼.這個(gè)想法是在循環(huán)中創(chuàng)建一個(gè)流數(shù)組,并在完成迭代時(shí)合并它們:
I was able to pull this off using merge-streams. If anyone's interested, here's the code. The idea is to create an array of streams inside your loop and merge them when finished iterating:
var merge = require('merge-stream');
gulp.task('js', function(){
// Init vars
var jsBundleStreams = [];
var i = 0;
// Create array of individual bundle streams
for (var key in buildConfig.bundle) {
jsBundleStreams[i] = gulp.src(bundleConfig.bundle[key].scripts)
.pipe(concat(key + '.js'))
.pipe(gulp.dest('./public/papasteftest/'));
i++;
}
// Merge and return streams
return merge.apply(this, jsBundleStreams);
});
這篇關(guān)于Node.js/Gulp - 循環(huán)執(zhí)行 Gulp 任務(wù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!