How to make Gulp 4 not to run a single task multiple times?

649 views Asked by At

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?

1

There are 1 answers

2
Low Flying Pelican On BEST ANSWER

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.