Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

javascript - Does a gulp task have to return anything?

In online examples showing usage of gulp, some tasks return the stream and others don't.

For example, without a return:

gulp.task('tsc', function()
{
    gulp.src('**/*.ts')
        // ...
});

And the same code, with a return:

gulp.task('tsc', function()
{
    return gulp.src('**/*.ts')
        // ...
});

Is it necessary to return the stream?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

For example, when not returning streams:

$ 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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...