Search code examples
npmgulp

How to make gulp.series wait until files are created on previous tasks


I'm currently using something like this:

gulp.task('js', gulp.series('js-a', 'js-b', 'js-c'));

The task js-c requieres js-a and js-b to be executed first and to generate 2 files than then I combine in js-c.

However, no matter if I use gup-series, the function js-c gets executed before the two files from the previous two tasks are created.

How can I tell gulp series to wait for those?

I've read other related issues but they tend to rely on external scripts to accomplish this. I manually managed to fix this by adding a setTimeout on js-b, but seems like a hacky solution.

Is there any proper way to accomplish this with gulp.series?

To put you in context, the task js-a looks like this:

var gp_concat = require('gulp-concat');

gulp.task('js-a', function(done) {
    gulp.src([
            'file1.js',
            'file2.js'
        ])
        .pipe(gp_concat('tmp.js'))
        .pipe(gulp.dest('./'));

        done();
});

And I am now forced to use a timeout to fix this issue:

setTimeout(function(){
    done();
}, 500);

Solution

  • done() shouldn't be used in this case and the task should return the stream (see here). Please try this:

    var gp_concat = require('gulp-concat');
    
    gulp.task('js-a', function() {
        return gulp.src([
                'file1.js',
                'file2.js'
            ])
            .pipe(gp_concat('tmp.js'))
            .pipe(gulp.dest('./'));
    });