Gulp tasks from within other Gulp tasks?

走远了吗. 提交于 2019-12-23 17:03:11

问题


I have a Gulp build task that is made up of other build tasks similar to this:

gulp.task('build', ['build-html', 'build-css', 'build-js', 'build-images']);

The thing I hate about this is that the build task doesn't run until after the dependencies are finished:

Starting 'build-html'...
Finished 'build-html' after 1 s
Starting 'build-css'...
Finished 'build-css' after 1 s
Starting 'build-js'...
Finished 'build-js' after 1 s
Starting 'build-images'...
Finished 'build-images' after 1 s
Starting 'build'...
Finished 'build' after 1 ms

Now obviously, build is not supposed to run at all until it's dependencies are finished, so it's working as expected. But this results in the console saying build took only 1 ms when in reality it should say it took 4 seconds, since all it's dependencies took that long. It would be nice if it looked something like this:

Starting 'build'...
Starting 'build-html'...
Finished 'build-html' after 1 s
Starting 'build-css'...
Finished 'build-css' after 1 s
Starting 'build-js'...
Finished 'build-js' after 1 s
Starting 'build-images'...
Finished 'build-images' after 1 s
Finished 'build' after 4 s

Notice how build is the first thing that "starts", then all the dependencies run, then build finishes.

So what I'm wondering, instead of using task dependencies, can I just call each task one at a time from within the build task? If so, what command do I use to do this?

Is this a bad idea? Is there a better solution to this problem?


回答1:


I guess you could use the runSequence plugin to do what you want.

Since the only purpose of your build task is to launch all the build-* tasks with no specific order, this can look like:

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

gulp.task('build', function (cb) {
  rs(['build-css', 'build-html', 'build-js', 'build-images'], cb);
});


来源:https://stackoverflow.com/questions/27710574/gulp-tasks-from-within-other-gulp-tasks

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