gpt4 book ai didi

javascript - 如何在循环中的 GULP 任务中传递数组

转载 作者:行者123 更新时间:2023-11-28 12:21:39 24 4
gpt4 key购买 nike

我不确定是否使下面给出的代码正常工作:

var language = ['en', 'sp'];

gulp.task('jsonResourcesConcat', function() {
return gulp.src('path/to/json/' + language + '/resources.json')
.pipe(insert.prepend('window.resourcesData.' + language + ' = '))
.pipe(concat('resources-' + language +'.js'))
.pipe(gulp.dest('./www/assets/json2js/'));
});

我需要将文件输出到文件夹./www/assets/json2js/,文件名为“resources-en.js”和“resources-sp.js”

最佳答案

好的,让我们从您的脚本开始:

var languages = ['en', 'sp'];

// this is the function that does all the work
function buildJson(language) {
return gulp.src('path/to/json/' + language + '/resources.json')
.pipe(insert.prepend('window.resourcesData.' + language + ' = '))
.pipe(concat('resources-' + language +'.js'))
.pipe(gulp.dest('./www/assets/json2js/'));
});

现在安装EventStream轻松处理流的模块:

$ npm install event-stream

并用它来管理 Gulp 流

var es = require('event-stream');

// now define the gulp task
gulp.task('jsonResourcesConcat', function gulpTask(){
// build the streams
var streams = languages.map( buildJson );

// now merge the streams together
// see the doc here: https://www.npmjs.com/package/event-stream#merge-stream1streamn-or-merge-streamarray
return es.merge( streams );
});

类似的答案也可以在这里找到:How to batch similar gulp tasks to reduce code repetition

关于javascript - 如何在循环中的 GULP 任务中传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com