Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp.js: task based on forEach loop

I have an array of objects that looks like the following.

var bundles = [
  {
    src: 'js/my-component/*.js',
    bundleName: 'my-component.js'
  },
  {
    src: 'js/my-other-component/*.js',
    bundleName: 'my-other-component.js'
  }
]

I want the gulp task to process/concat every entry in the array, but it doesn't seem to work.

gulp.task('bundlejs', function(){
    return bundles.forEach(function(obj){
      return gulp.src(obj.src)
      .pipe(concat(obj.bundleName))
      .pipe(gulp.dest('js/_bundles'))
    });
});
like image 431
gkiely Avatar asked Aug 17 '15 08:08

gkiely


1 Answers

You should probably be merging the streams and returning the result, so that the task will complete at the appropriate time:

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

gulp.task('bundlejs', function () {
  return es.merge(bundles.map(function (obj) {
    return gulp.src(obj.src)
      .pipe(concat(obj.bundleName))
      .pipe(gulp.dest('js/_bundles'));
  }));
});
like image 154
Ben Avatar answered Sep 21 '22 14:09

Ben