jonkemp / gulp-useref

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.
MIT License
705 stars 93 forks source link

Gulp task chaining #183

Closed pbakondy closed 8 years ago

pbakondy commented 8 years ago

Hello,

I would like to chain useref task with other gulp task but I can not solve it.

First version with return

gulp.task('useref', function () {
    return gulp.src('index.html')
        .pipe(useref())
        .pipe(gulp.dest('dist'));
});

gulp.task('other', function () {
    /* codes */
});
gulp.task('build', ['useref', 'other']);

Second version with callback (same result)

gulp.task('useref', function (done) {
    gulp.src('index.html')
        .pipe(useref())
        .pipe(gulp.dest('dist'))
        .on('end', done);
});

gulp.task('other', function (cb) {
      /* codes */
});
gulp.task('build', ['useref', 'other']);

running:

> gulp build
Using gulpfile gulpfile.js
Starting 'useref'...
Starting 'other'...
Finished 'other' after 1ms
Finished 'useref' after 500ms
Starting 'build'...
Finished 'build' after 5us

expected:

> gulp build
Using gulpfile gulpfile.js
Starting 'useref'...
Finished 'useref' after 500ms
Starting 'other'...
Finished 'other' after 1ms
Starting 'build'...
Finished 'build' after 5us
jonkemp commented 8 years ago

That's how gulp works. This doc may explain it for you.

https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md

In short, tasks started that way all run at once. If you want other to wait on useref, you need to write it this way.

gulp.task('other', ['useref'] function (cb) {
      /* codes */
});
pbakondy commented 8 years ago

Nice, thank you