0

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'

1
  • Do you need both to be executed in the task or each language needs his own task? Commented Apr 15, 2016 at 11:10

1 Answer 1

7

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:

$ npm install event-stream

And use it to pipe Gulp streams

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.