3

I have two tasks. They have a common task which should be executed before the tasks.

With Gulp 3 I implement them this way:

gulp.task('compile', () => {
    // Compiling the TypeScript files to JavaScript and saving them on disk
});

gulp.task('test', ['compile'], () => {
    // Running tests with the compiled files
});

gulp.task('minify', ['compile'], () => {
    // Minifying the compiled files using Uglify
});

guls.task('default', ['test', 'minify']);

And when I run gulp default the compile task is run only 1 time.

In Gulp 4 I implement them this way:

gulp.task('compile', () => {
    // Compiling the TypeScript files to JavaScript and saving them on disk
});

gulp.task('test', gulp.series('compile', () => {
    // Running tests with the compiled files
}));

gulp.task('minify', gulp.series('compile', () => {
    // Minifying the compiled files using Uglify
}));

guls.task('default', gulp.parallel('test', 'minify'));

And when I run gulp default the compile task is run 2 times which is undesirable because a spare job is done. How to make the task run only 1 time keeping the ability to run the test and minify tasks independently?

Finesse
  • 9,793
  • 7
  • 62
  • 92

1 Answers1

2

Since you are trying to run test and minify in parallel, it's not possible to make run compile only once, since it will become a sequential operation. You could do,

gulp.task('compile', () => {
    // Compiling the TypeScript files to JavaScript and saving them on disk
});

gulp.task('test',() => {
    // Running tests with the compiled files
}));

gulp.task('minify',=> {
    // Minifying the compiled files using Uglify
}));

gulp.task('compile-and-test', gulp.series('compile','test'));

gulp.task('compile-and-minify', gulp.series('compile','minify'));

guls.task('default', gulp.series('compile', gulp.parallel('test', 'minify'));

This approach will allow you to run individual operations, and make the test and minify operation parallel while executing the compilation only once.

You can read more details here.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • The operation can be done in parallel particularly (first compile and then test and minify in parallel) and Gulp 3 **does** it automatically in my example. But Gulp 4 lost this ability for some reason. – Finesse May 15 '18 at 04:55
  • Gulp 3 used orchestrator to compose tasks, while Gulp 4 has switched to undertaker. Undertaker does not have orchestrator's concept of dependencies baked into its API. Ref : https://github.com/gulpjs/gulp/issues/1392 – Low Flying Pelican May 15 '18 at 05:03