如何让Gulp 4 不对运行 一个任务多次?

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

我有两个任务。他们有一个共同的任务,应该在任务之前执行。

使用 Gulp 3 我是这样实现的:

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']);

而当我 运行 gulp default compile 任务 运行 只有 1 次。

Gulp 4中我是这样实现的:

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'));

而当我 运行 gulp defaultcompile 任务是 运行 2 次,这是不希望的,因为完成了一项备用工作。如何使任务 运行 仅执行 1 次,同时保持 运行 独立完成 testminify 任务的能力?

由于您正在尝试 运行 并行测试和缩小,因此无法使 运行 仅编译一次,因为它将成为顺序操作。你可以,

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'));

这种方法将允许您 运行 单独的操作,并使测试和缩小操作并行进行,同时只执行一次编译。

您可以阅读更多详细信息here