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

966

被折叠的 条评论
为什么被折叠?



