Several Gulp tasks wait for another one

倖福魔咒の 提交于 2019-12-11 17:31:30

问题


I need to build a sequence of Gulp tasks like this:

Task1 -> Task2A, Task2B, Task2C -> Task3,

where tasks 2A,2B,2C run in parallel(but task1 should be completed before, and completed just once).

What I tried:

gulp.task('Task1', []);

gulp.task('Task2A', ['Task1']);

gulp.task('Task2B', ['Task1']);

gulp.task('Task2C', ['Task1']);

gulp.task('Task3', ['Task2A', 'Task2B', 'Task2C']);

Looks like it's working, but I'm not sure, does it guarantee that Task1 will be executed only 1 time, or it can be triggered multiple times?

Thank you.


回答1:


Perhaps the simplest way to do this (without using gulp4.0) is the run-sequence plugin.

For your case:

var runSequence = require('run-sequence');

gulp.task('build', function(callback) {
   runSequence( 'Task1',
               ['Task2A', 'Task2B', 'Task2C'],
                'Task3',
                callback);
});

Make sure you have return statements in all your tasks.

Please Note

This was intended to be a temporary solution until the release of gulp 4.0 which should have support for defining task dependencies similarly.

Given that Gulp 4 appears to never be fully released, take that for what you will. Be aware that this solution is a hack, and may stop working with a future update to gulp.




回答2:


Try this structure

gulp.task('Task1', []);

gulp.task('Task2A', ['Task1']);

gulp.task('Task2B', ['Task2A']);

gulp.task('Task2C', ['Task2B']);

gulp.task('Task3', ['Task2C']);

Also, you can run task inside another task, like this:

gulp.task('task1', function(){
  gulp.start('task2');
})


来源:https://stackoverflow.com/questions/46409698/several-gulp-tasks-wait-for-another-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!