Node.js/Gulp - 循环执行 Gulp 任务

Node.js / Gulp - Looping Through Gulp Tasks(Node.js/Gulp - 循环执行 Gulp 任务)
本文介绍了Node.js/Gulp - 循环执行 Gulp 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个对象,并在每次迭代时将一组文件路径传递给 gulp.src,然后对这些文件进行一些处理.下面的代码仅用于说明目的,实际上并不能正常工作,因为 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?

推荐答案

我能够使用合并流解决这个问题.如果有人有兴趣,这里是代码.这个想法是在循环中创建一个流数组,并在完成迭代时合并它们:

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/Gulp - 循环执行 Gulp 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)