Gulp tasks from within other Gulp tasks?

2019-07-04 14:05发布

问题:

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);
});