Gulp.js: task based on forEach loop
Asked Answered
F

2

10

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'))
    });
});
Fortyish answered 17/8, 2015 at 8:58 Comment(0)
I
13

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'));
  }));
});
Impala answered 19/8, 2015 at 4:37 Comment(2)
Thanks Ben, I will try this.Fortyish
I marked this as the answer as it highlighted a best practice for structuring the code I had submitted. The issue I had was typo as detailed below.Fortyish
F
0

This solution does work, I had the wrong directory name. Doh.

https://github.com/adamayres/gulp-filelog helped me find the problem.

Fortyish answered 17/8, 2015 at 23:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.