Multiple synchronous streams in one Gulp task?

I know that one commonly used method for executing a Gulp task is return

output:

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

      

But what will you do if there are multiple threads in one 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 are you going to make this task synchronous? Currently, Gulp reports that the task completes immediately, and the threads start at the same time and terminate when the task completes. Obviously this is because no streams are returned. Should I just return the last thread to the task? Or is it more correct to return the result of both tasks? If so, how do you do it?

+3


source to share


1 answer


The recommended way to use multiple pipelines in gulp is with 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);
});

      

+7


source







All Articles