H23120
H23120

Reputation: 387

How does gulp resolve dependencies before running?

Assumed there are four tasks 'a', 'b', 'c', 'd' (b depends on c and d; both c and d depend on a), so tasks are run in the following order:

a-> (c, d) -> b

Here's the gulpfile.js which works accordingly:

gulp.task('a');
gulp.task('c', ['a']);
gulp.task('d', ['a']);
gulp.task('b', ['c', 'd']);

In spite of task 'c' and 'd' both depend on task 'a', gulp is smart enough to have task 'a' run only once. I'm wondering since tasks run with maximum concurrency, how does gulp resolve dependencies before running?

Upvotes: 6

Views: 3253

Answers (2)

robrich
robrich

Reputation: 13205

Perhaps a better way to document it is "gulp (orchestrator) runs with maximum possible concurrency after accounting for all specified dependencies."

So in your scenario, do c and d actually run "in parallel"? Well, yes and no. Because JavaScript is single threaded, technically the processor is doing either c or d but not both. Gulp (orchestrator) will launch a, wait for it to finish, then launch both c and d, and wait for both to finish, then launch b.

If tasks c and d are at all asynchronous, you'll see both execute simultaneously. (Note the gulp timing outputs get intertwined.) If the tasks are completely synchronous, technically they won't run "in parallel" ... only because JavaScript is single threaded.

It's the maximum possible concurrency, accounting for all your specified restrictions.

Upvotes: 7

Shuhei Kagawa
Shuhei Kagawa

Reputation: 4777

gulp tasks don't seem to always run with maximum concurrency if tasks have dependencies. gulp's documentation says:

Note: By default, tasks run with maximum concurrency -- e.g. it launches all the tasks at once and waits for nothing. If you want to create a series where tasks run in a particular order, you need to do two things:

  • give it a hint to tell it when the task is done,
  • and give it a hint that a task depends on completion of another.

gulp uses orchestrator to run tasks and orchestrator uses sequencify to arrange tasks in sequence.

For example, the tasks in your example will be arranged to a -> c -> d -> b or a -> d -> c -> b by sequencify and executed in sequence by orchestrator.

If I understand correctly, c and d could be executed in parallel in theory but not with the current implementation of gulp.

EDIT

orchestrator seems to run c and d in parallel. It loops through the tasks in sequence and runs tasks that are not done or running and whose dependencies are all done. It repeats that when each task is done until all tasks are done. See Orchestrator.prototype._runStep for more detail.

Upvotes: 2

Related Questions