Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Gulp 4 task programmatically

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.

like image 253
Estus Flask Avatar asked Feb 15 '17 19:02

Estus Flask


People also ask

How do I run multiple tasks using Gulp in Python?

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>.

What is Gulp’s syntax?

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.

What is new in Gulp 4?

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:

How do I know if a gulp task is concurrent?

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.


2 Answers

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.

like image 108
tuck182 Avatar answered Sep 21 '22 09:09

tuck182


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);
});
like image 33
Estus Flask Avatar answered Sep 17 '22 09:09

Estus Flask