Gulp: Support returning multiple streams from gulp.task

Created on 6 Jan 2014  路  7Comments  路  Source: gulpjs/gulp

Currently when you are dealing with async streams, and want Gulp to know when the task is finished, you have 3 options:

  1. Accept a callback
  2. Return a stream
  3. Return a promise

Could this be extended to include Return an array of streams, so the task is recognized as finished when all the streams are finished.

Currently I am using the following function as workaround:

function getStreamsFinishedPromise(streams){
    var deferred = Q.defer();
    var runningStreams = streams.length;

    streams.forEach(function(stream){
        stream.pipe(es.wait(function(){
            runningStreams--;
            if(runningStreams === 0){
                deferred.resolve();
            }
        }));
    });

    return deferred.promise;
}

Most helpful comment

@contra @sqs, the recipe does not work for me:

// VIEWS
//
var jade = require('gulp-jade');
var safeJade = combine(jade({pretty: true}));
safeJade.on('error', function(err) {
  gutil.log(chalk.red(util.format('Plugin error: %s', err.message)));
});
var wiredep = require('wiredep').stream;
var through2 = require('through2');
var merge = require('merge-stream');

gulp.task('views:src', function() {

  return merge(
    gulp.src(paths.views, {cwd: paths.src, base: paths.src})
      .pipe(changed(paths.tmp))
      .pipe(safeJade)
      .pipe(gulp.dest(paths.tmp))
      .pipe(connect.reload()),
    gulp.src(paths.index, {cwd: paths.src, base: paths.src})
      .pipe(changed(paths.tmp))
      .pipe(safeJade)
      .pipe(through2.obj(function(file, encoding, next) {
        // Fake path for wiredep
        file.path = path.join(__dirname, paths.src, 'index.html');
        file.base = path.dirname(file.path);
        next(null, file);
      }))
      .pipe(wiredep({directory: 'app/bower_components', exclude: [/jquery/, /js\/bootstrap/]}))
      .pipe(gulp.dest(paths.tmp))
      .pipe(connect.reload())
  );

});

The first streams is never completed but killed halfway (only one jade view gets processed). Any ideas?


EDIT: found out that var safeJade = combine(jade({pretty: true})); is the culprit.

All 7 comments

@contra @sqs, the recipe does not work for me:

// VIEWS
//
var jade = require('gulp-jade');
var safeJade = combine(jade({pretty: true}));
safeJade.on('error', function(err) {
  gutil.log(chalk.red(util.format('Plugin error: %s', err.message)));
});
var wiredep = require('wiredep').stream;
var through2 = require('through2');
var merge = require('merge-stream');

gulp.task('views:src', function() {

  return merge(
    gulp.src(paths.views, {cwd: paths.src, base: paths.src})
      .pipe(changed(paths.tmp))
      .pipe(safeJade)
      .pipe(gulp.dest(paths.tmp))
      .pipe(connect.reload()),
    gulp.src(paths.index, {cwd: paths.src, base: paths.src})
      .pipe(changed(paths.tmp))
      .pipe(safeJade)
      .pipe(through2.obj(function(file, encoding, next) {
        // Fake path for wiredep
        file.path = path.join(__dirname, paths.src, 'index.html');
        file.base = path.dirname(file.path);
        next(null, file);
      }))
      .pipe(wiredep({directory: 'app/bower_components', exclude: [/jquery/, /js\/bootstrap/]}))
      .pipe(gulp.dest(paths.tmp))
      .pipe(connect.reload())
  );

});

The first streams is never completed but killed halfway (only one jade view gets processed). Any ideas?


EDIT: found out that var safeJade = combine(jade({pretty: true})); is the culprit.

:+1: for that recipe, fyi

@mgcrea have you ever found a solution? Having the exact same issue with jade compilation..

Linked recipes just redirects to gulp main page...

@klick-barakgall I just clicked the link, it works fine. Not sure what you're talking about.

Was this page helpful?
0 / 5 - 0 ratings