简体   繁体   中英

Task Dependency in Gulp

I have a task in gulp that need to execute other two tasks consecutively and in order.

So when I run build , build run first task-a , when task-a is completed task-b is performed.

At the moment I am using this script, I would like to have confirmation the is correct.

 gulp.task('build', [
        'task-a',
        'task-b'
    ]);

As stated at the official documentation with that format the tasks will run in parallel (all at once), so don't assume that the tasks will start/finish in order .

You have to explicitly define the order of tasks as follow to make Gulp workout the right order:

gulp.task('task-a', function(){ ... });

gulp.task('task-b', ['task-a'], function(){ ... });

gulp.task('build', ['task-a', 'task-b'], function(){ ... });

At this point when you'll try to run build Gulp will build the dependency tree of the tasks and workout that task-b relies on task-a to be completed before executing.

A more complete example can be found here: https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md

The current gulp task you have at the moment will run correctly, it'll run task-a then run task-b . For whatever reason, it's not giving you back what you want. There's also an npm package called run-sequence ( https://www.npmjs.com/package/run-sequence ) you can use.

Your gulp task would look something like :

gulp.task('runTasks', function () {
    runSequence('task-a', 'task-b', 'task-c');
});

And that'll execute your tasks in the order sequence you pass it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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