gulp 任务是否必须返回任何内容? [英] Does a gulp task have to return anything?
问题描述
在展示 gulp 用法的在线示例中,有些任务会返回流,而有些则不会.
In online examples showing usage of gulp, some tasks return the stream and others don't.
例如,没有返回:
gulp.task('tsc', function()
{
gulp.src('**/*.ts')
// ...
});
同样的代码,有一个返回:
And the same code, with a return:
gulp.task('tsc', function()
{
return gulp.src('**/*.ts')
// ...
});
是否需要返回流?
推荐答案
如果不返回流,那么每个任务的异步结果将不会被其调用者等待,也不会被任何依赖的任务等待.
If you do not return a stream, then the asynchronous result of each task will not be awaited by its caller, nor any dependent tasks.
例如,当不返回流时:
$ gulp scripts
[21:25:05] Using gulpfile ~/my-project/gulpfile.js
[21:25:05] Starting 'tsc'...
[21:25:05] Finished 'tsc' after 13 ms
[21:25:05] Starting 'scripts'...
[21:25:05] Finished 'scripts' after 10 ms
[21:25:05] Compiling TypeScript files using tsc version 1.0.1.0
请注意,scripts
任务依赖于 tsc
任务.它报告 tsc
在 13 毫秒内完成,这绝对快得令人难以置信.然后 scripts
任务似乎在很短的时间内开始并完成.最后,由 tsc
执行的实际操作开始.很明显,tsc
和 scripts
都没有等待编译步骤完成.
Note here that the scripts
task depends upon the tsc
task. It reports that tsc
completes in 13 milliseconds, which is definitely too fast to be reasonably believed. Then the scripts
task appears to start and complete, again in a very small period of time. Finally, the actual operation performed by tsc
commences. Clearly neither tsc
nor scripts
waited for the compilation step to complete.
当这些任务返回它们的流时,输出看起来大不相同:
When these tasks return their streams, the output looks rather different:
$ gulp scripts
[21:42:25] Using gulpfile ~/my-project/gulpfile.js
[21:42:25] Starting 'tsc'...
[21:42:25] Compiling TypeScript files using tsc version 1.0.1.0
[21:42:32] Finished 'tsc' after 6.65 s
[21:42:32] Starting 'scripts'...
[21:42:32] Finished 'scripts' after 204 ms
这里的顺序是有道理的,报告的持续时间符合预期.
Here the sequence makes sense, and the reported durations meet expectations.
这篇关于gulp 任务是否必须返回任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!