2

I want to use one source file to generate multiple destination files with different characteristics. Here's a watered down version of what I want to do:

gulp.task('build', function() {
  var src = gulp.src('myfile.txt')

  for (var i = 0; i < 10; i++) {
    src
      .pipe(someplugin(i))
      .pipe(rename('myfile-' + i + '.txt'))
      .dest('./build')
  }
});

Presumably, "someplugin" would change the file contents based on the index passed to it, so the code above would generate 10 files, each with slightly different content.

This doesn't work, and I didn't expect it to, but is there another way to achieve this?

Stephen Sorensen
  • 11,455
  • 13
  • 33
  • 46
  • You should be more specific in what you are trying to do. It's almost impossible to suggest a useful solution based on what you have provided here. – OverZealous May 16 '14 at 17:08

2 Answers2

2

I ended up creating a function that builds my tasks for me. Here's an example:

function taskBuilder(i) {
  return function() {
    gulp.src('*.mustache')
      .pipe(someplugin(i))
      .pipe(rename('myfile-' + i + '.txt'))
      .dest('./build');
  };
}

var tasks, task, i;
for (i = 0; i < 10; i++) {
  taskName = 'tasks-' + i;
  gulp.task(taskName, taskBuilder(i));
  tasks.push(task);
}

gulp.task('default', tasks);
Stephen Sorensen
  • 11,455
  • 13
  • 33
  • 46
1

Maybe you should check this :

How to save a stream into multiple destinations with Gulp.js?

If it doesn't answer your question, I believe a simple solution would be to have many tasks all running one after the other. For example :

gulp.task('build', function() {
  return gulp.src('*.txt')
      .pipe(someplugin(i))
      .pipe(rename('myfile-1.txt'))
      .dest('./build')
  }
 });

gulp.task('build2', [build], function() {
  return gulp.src('*.txt')
      .pipe(someplugin(i))
      .pipe(rename('myfile-2.txt'))
      .dest('./build')
  }
});

Running task 'build2' will run 'build' first, and then 'build2' when it's complete. If you don't have to wait for 'build2' to be finished, just remove the "return" and they will run at the same time.

Community
  • 1
  • 1
soenguy
  • 1,332
  • 3
  • 11
  • 23
  • The issue you're missing in both of those proposed solutions (any maybe the question wasn't clear enough) was that I don't necessarily know how many versions I need. In the example above, I needed 10, and writing that code 10 times would be awful. In reality, I don't know how many I need, which makes code reuse not just convenient, but mandatory. I can't just type it multiple times. – Stephen Sorensen Aug 27 '14 at 17:39