简体   繁体   English

如何在gulp任务之间共享参数?

[英]How to share parameters between gulp tasks?

I'd like to share a set of parameters that describe the current execution flow. 我想分享一组描述当前执行流程的参数。 This is how I am doing it at the moment: 这是我目前的做法:

let taskError;

gulp.task('pre-watch', () => {
    taskError = false; 
});

gulp.task('task-a', () => {
    console.log('OK');
});

gulp.task('task-b', () => {
    console.log('Error');

    taskError = true;
});

gulp.task('task-c', () => {
    console.log('Error', taskError);

    if (!taskError) {
        throw new Error('Was expecting taskError to be true.');
    }
});

gulp.task('watch', () => {
    gulp.watch('./**/*', [
        'pre-watch',
        'task-a',
        'task-b',
        'task-c'
    ]); 
});

This approach works great until we run into issues with parallelism, ie 这种方法非常有效,直到我们遇到并行性的问题,即

  • gulp.watch detected a change. gulp.watch检测到更改。
  • pre-watch done, task-a done, task-b done ... 完成pre-watch完成task-a完成task-b ...
  • gulp.watch detected a change. gulp.watch检测到更改。
  • pre-watch resets taskError . pre-watch重置taskError
  • task-c throws Error . task-c抛出Error

Is there a way to pass parameters within the current task execution flow? 有没有办法在当前任务执行流程中传递参数?

As you have found out gulp tasks by default run in parallel. 正如您所发现的,gulp任务默认是并行运行的。 The best gulp plugin ever is run-sequence 有史以来最好的Gulp插件是运行顺序

npm i -SD run-sequence npm i -SD运行顺序

https://www.npmjs.com/package/run-sequence https://www.npmjs.com/package/run-sequence

Very popular, 4k+ downloads a day. 非常受欢迎,每天下载4k +。 Its a temp fix until gulp 4 is released which will give more control over parallelism. 在gulp 4发布之前,它是一个临时修复程序,它将为并行性提供更多控制。

It allows you to do ... 它可以让你做...

'task1', ['task2', 'task3'], 'task4' 'task1',['task2','task3'],'task4'

That is ... 那是 ...

Run task 1 运行任务1

Then run task 2 and task 3 in parallel 然后并行运行任务2和任务3

When both 2 and 3 are complete then run task 4 当2和3都完成后,运行任务4

This fixes your parallelism issue :) 这可以解决您的并行性问题:)

Not sure this is the best way of sharing params. 不确定这是共享参数的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM