简体   繁体   中英

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

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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