【问题标题】:How to make Gulp 4 not to run a single task multiple times?如何让 Gulp 4 不多次运行单个任务?
【发布时间】:2018-10-28 09:58:03
【问题描述】:

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

使用 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 default 时,compile 任务运行了 2 次,这是不可取的,因为已经完成了一项备用工作。如何使任务只运行 1 次,同时保持 testminify 任务独立运行的能力?

【问题讨论】:

    标签: javascript gulp gulp-4


    【解决方案1】:

    由于您尝试并行运行 test 和 minify,因此不可能只使 run 编译一次,因为它将成为顺序操作。可以的,

    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

    【讨论】:

    • 该操作可以并行完成,特别是(首先编译,然后并行测试和缩小),在我的示例中,Gulp 3 自动完成。但是 Gulp 4 出于某种原因失去了这种能力。
    • Gulp 3 使用 orchestrator 来编写任务,而 Gulp 4 已切换到承担者。 Undertaker 的 API 中没有编排器的依赖概念。参考:github.com/gulpjs/gulp/issues/1392
    猜你喜欢
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2016-04-14
    相关资源
    最近更新 更多