Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 682
Drew Noakes Avatar asked Sep 27 '14 20:09

Drew Noakes


People also ask

What is a gulp task?

What is Gulp? Gulp is a task runner that uses Node. js as a platform. Gulp purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. It builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.

What is CB () in gulp?

I'd say that sometimes giving cb a more intuitive name (e.g. "operationComplete") can help with confusion, which I think might be the use case you're referring to in gulp. – Joshua. Aug 3, 2015 at 7:33. 2. so cb is just short for callback, and can be replaced with any other variable names?

How do you signal async completion gulp?

To indicate to gulp that an error occurred in a task using an error-first callback, call it with an Error as the only argument. However, you'll often pass this callback to another API instead of calling it yourself.


1 Answers

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.

like image 98
Drew Noakes Avatar answered Oct 08 '22 17:10

Drew Noakes