There are several tasks defined with gulp.task
in gulpfile.js:
gulp.task('sync-task', () => { ... });
gulp.task('async-task', cb => { ... });
I would like to start one of them programmatically. Preferably in the same process (no exec, etc.), because one of the reasons behind this is the ability to run the script in debugger.
How can this be done?
It looks like there were things like gulp.run
and gulp.start
, but they are deprecated in Gulp 4.
Using your text editor, create a file named gulpfile.js in your project root with these contents: Run the gulp command in your project directory: To run multiple tasks, you can use gulp <task> <othertask>.
The syntax is very elegant and similar to those of other build tools. When Gulp’s started, it creates a dependency tree like the one below. So it realizes that clean is a dependency of two tasks. In this way, it makes sure that it is executed only once. One thing to keep in mind there: All those tasks are executed for maximum concurrency.
It’s functionality is now part of Gulp 4 with the addition of the new task manager “Undertaker”. Gulp 4 drops the dependency parameter completely and replaces them with execution functions that can be used instead: gulp.parallel for parallel execution. Each of those functions allow for parameters of the following kind:
Look at the original execution order: The execution order of the tasks in the original tree are: clean, styles and scripts in parallel, and then the default task. Each step that can be done in concurrent will be combined in a gulp.parallel function. The others are ordered in a gulp.series function.
Adding to the accepted answer:
Yes you can retrieve a task by calling gulp.task
, but rather than calling it directly you can wrap it in a call to gulp.series
(presumably gulp.parallel
would also work) and call that. This has the benefit of producing the expected "Starting/Finished" output. An example would be:
gulp.series(gulp.task('some-task'))();
with the output:
[09:18:32] Starting 'some-task'...
[09:18:32] Finished 'some-task' after 7.96 ms
This was tested just now on gulp v4.0.2.
It appears that a task can be retrieved with gulp.task
getter function and called directly:
gulp.task('sync-task')();
gulp.task('async-or-random-task')(function callbackForAsyncTasks(err) {
if (err)
console.error('error', err);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With