简体   繁体   English

Node.js / Gulp-遍历Gulp任务

[英]Node.js / Gulp - Looping Through Gulp Tasks

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. 我想遍历一个对象,并在每次迭代时将文件路径数组传递给gulp.src,然后对这些文件进行一些处理。 The code below is for illustration purposes and won't actually work since the return statement kills the loop on the first pass. 下面的代码仅用于说明目的,因为return语句会在第一次通过时终止循环,因此实际上将无法工作。

gulp.task('js', function(){
    for (var key in buildConfig.bundle) {
        return gulp.src(bundleConfig.bundle[key].scripts)
            .pipe(concat(key + '.js'));
            // DO STUFF
    }
});

That's the basic idea. 这是基本思想。 Any ideas on how to do this? 有关如何执行此操作的任何想法?

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM