Node.js的/咕嘟咕嘟 - 通过循环任务咕嘟咕嘟 [英] Node.js / Gulp - Looping Through Gulp Tasks

查看:206
本文介绍了Node.js的/咕嘟咕嘟 - 通过循环任务咕嘟咕嘟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过对象循环和传递的文件路径的数组gulp.src在每次迭代,然后做对这些文件进行一些处理。在code以下是用于说明目的,实际上不会工作,因为return语句杀死在第一轮循环。

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?

推荐答案

我能够把这个功能使用合并流。如果任何人的兴趣,这里的code。我们的想法是创建流数组的循环中,并把它们合并完成迭代时:

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

});

这篇关于Node.js的/咕嘟咕嘟 - 通过循环任务咕嘟咕嘟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆