Sync Gulp Tasks

本文介绍了Gulp中确保任务按顺序执行的两种方法:使用gulp-sequence插件及利用任务依赖性。此外还讨论了如何避免插件并行运行导致的问题,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have already known that Gulp runs tasks in parallel and there’re several ways to ensure tasks run in order.

  • The 1st one is use the gulp-sequence plugin. This plugin takes an array of tasks and perform them one by one.
  • The 2nd one is leveraging the dependencies of the tasks: the 2nd parameter of the task could be an array of other tasks that are the prerequisite of current task which means that current task won’t start
    until those tasks in the array are finished.

However there’s one thing should be noticed that those prerequisite tasks might not actually complete as some plugins called inside those tasks may run in parallel.

And for avoiding this problem, check whether the plugins leveraged in tasks have a callback that is invoked after its execution if those do, put a parameter as the callback for task’s completion and invoke this
parameter in the plugin’s ‘done’ callback like following snippet:

gulp.task('initialize', (cb) => {

  iterateFiles(config.TASKDIR, (fn) => { require(fn); }, (err) => {console.log('all tasks have been done');cb();});

});

Plus, if gulp stream is being used in the task function, make sure return the statement as below:

gulp.task('clean', function() {
    return gulp.src('dist/*').pipe(rm());
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值