0

I have a gulp task that I would like to run on multiple sets of files. My problem is pretty much similar to what is described here except that I define my sets of files in an extra config.

What I've come up with so far looks like the following:

config.json

{
  "files": {
    "mainScript": [
      "mainFileA.js",
      "mainFileB.js"
    ],
    "extraAdminScript": [
      "extraFileA.js",
      "extraFileB.js"
    ]
  }
}

gulpfile.js

var config = require ('./config.json');
...
gulp.task('scripts', function() {
  var features = [],
      dest = (argv.production ? config.basePath.compile : config.basePath.build) + '/scripts/';

  for(var feature in config.files) {
    if(config.files.hasOwnProperty(feature)) {
      features.push(gulp.src(config.files[feature])
        .pipe(plumper({
          errorHandler: onError
        }))
        .pipe(jshint(config.jshintOptions))
        .pipe(jshint.reporter('jshint-stylish'))
        .pipe(sourcemaps.init())
          .pipe(concat(feature + '.js'))
          .pipe(gulpif(argv.production, uglify()))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(dest))
      );
    }
  }

  return mergeStream(features);
});

My problem is that this doesn't seem to work. The streams are not combine or at least nothing really happens. Some while ago others ran into a similar problem, see here, but even though it should have been fixed it's not working for me.

By the way I've also tested merging the streams in this way:

return es.merge(features)
return es.merge.apply(null, features)

And if I just run the task on a single set of files it works fine.

Motivation

The reason why I want to do this is that at some point concatenating and minifying ALL scripts into one final file doesn't make sense when the sheer number of files is too large. Also, sometimes there is no need to load everything at once. For example all scripts related to an admin interface doesn't need to be load by every visitor.

F Lekschas
  • 12,481
  • 10
  • 60
  • 72
  • I'm not sure what you mean by "if I run the task on a single file it works fine". Does it work if you make no change to the gulpfile but have only one set in config? – Sze-Hung Daniel Tsui May 26 '15 at 23:28
  • Try pushing `features.push(gulp.src(config.files[feature]) )` first. Then using `es.merge(features)` to merge the streams (I'm not sure if mergeStream can take an array). Then doing all of your work after (from ` .pipe(plumper({`) on the merged stream. I think that will work. If so, feel free to comment and I'll turn it into an answer. – Sze-Hung Daniel Tsui May 26 '15 at 23:40
  • With a single set of files I mean if I just run the whole task with `gulp.src(['fileA.js', 'fileB.js'])...`. Merging beforehand doesn't make sense to me as I want my set of files to be concatenated and minified by features like `main` and `extra`. – F Lekschas May 27 '15 at 00:14
  • Another way of solving your problem is creating parent and children tasks (eliminates merging which seems not working properly). So you will have a task `gulp.task('scripts', childrenTasks)` where childrenTasks is an array of task names you need to process before. You can generate children tasks similarly as you do now. – zaynetro May 27 '15 at 06:34
  • possible duplicate of [creating tasks using a loop \[gulp\]](http://stackoverflow.com/questions/22968516/creating-tasks-using-a-loop-gulp) – F Lekschas May 27 '15 at 13:19
  • This is the answer: http://stackoverflow.com/a/22970070/981933 – F Lekschas May 27 '15 at 13:20

0 Answers0