Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple synchronous streams in one Gulp task?

I know that one commonly used method to make a Gulp task synchronous is to return the output:

gulp.task('mytask', function(){
  return gulp.src('myfile.css')
    .pipe(dosomething())
    .pipe(gulp.dest('./css'));
});

But what do you do if there are multiple streams that occur in a single task?

gulp.task('anothertask', function(){
  gulp.src('file1.css')
    .pipe(dosomething())
    .pipe(gulp.dest('./css'));

  gulp.src('file2.css')
    .pipe(dosomethingElse())
    .pipe(gulp.dest('./css'));
});

How do you go about making this task synchronous? Currently, Gulp reports that the task finishes immediately and the streams are running at the same time and finish after the task is "finished". Obviously this is because no streams are being returned. Should I just return the last stream in the task? Or is it more proper to return the result of both tasks? If so, how do you do that?

like image 612
Jake Wilson Avatar asked Dec 30 '14 20:12

Jake Wilson


1 Answers

The recommended way to use multiple pipelines in a gulp task is by using merge-stream:

var merge = require('merge-stream');

gulp.task('anothertask', function(){
  var stream1 = gulp.src('file1.css')
    .pipe(dosomething())
    .pipe(gulp.dest('./css'));

  var stream2 = gulp.src('file2.css')
    .pipe(dosomethingElse())
    .pipe(gulp.dest('./css'));

  return merge(stream1, stream2);
});
like image 50
Ben Avatar answered Oct 20 '22 14:10

Ben