简体   繁体   English

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

[英]How to pass array in GULP task in loop

I am not sure on to make the below given code to work: 我不确定要使以下给出的代码起作用:

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/'));
});

I need the file should be output to folder ./www/assets/json2js/ with file names 'resources-en.js' and 'resources-sp.js' 我需要将文件输出到文件夹./www/assets/json2js/ ,文件名为“ resources-en.js”和“ resources-sp.js”

Ok, let's start from your script: 好的,让我们从您的脚本开始:

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/'));
});

Now install the EventStream module to handle streams easily: 现在安装EventStream模块以轻松处理流:

$ npm install event-stream

And use it to pipe Gulp streams 并用它来输送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 );
});

A similar answer can also be found here: How to batch similar gulp tasks to reduce code repetition 在这里也可以找到类似的答案: 如何批处理类似的gulp任务以减少代码重复

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

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