gulp-di: a modular approach to Gulp using dependency injection
If you have used Gulp, chances are pretty high that you have at least
modified an existing Gulpfile. This post explains in brief what a Gulpfile is
and introduces into my dependency injection framework "gulp-di" which was
created in order to help writing way shorter, more modular Gulpfiles.
Gulp is a so-called streaming building tool for developers which allows to automate certain tasks. In contrast to more traditional systems like Grunt, it fully embraces UNIX-style Node.js streams.
Therefore, its users can modify data on channels instead on simple files. This is more time- and memory-efficient, but also more flexible, as the origin of a stream could be also f.e. a network download.
The describing file for such tasks is called a "Gulpfile" which usually contains all needed tasks declarations.
Build configurations are growing with the projects' size. They exceed 1000 lines of code easily.
A simple example would be to copy several files together (GitHub Gist). As you can see, the "concat-all-texts" task is pretty hard-wired for a specific use case. If we want to extend this to concatenating other file types, we could use wildcards when using gulp.src(), but then we would end with concatenating several types together. We need to set up a task for each file extension like in this GitHub Gist.
It is common that such build configurations are growing with the projects' size. Larger projects like Google's Angular.js 2.0 (which recently reached its beta state) have Gulpfiles which exceed 1000 lines of code easily.
In my opinion, it is not a good style to include everything in a single file when dealing with a complex build architecture.
Complexity increases friction costs.
Complexity increases friction costs, especially as JavaScript build environments like Gulp/Grunt are a necessity these days in production use when dealing with new languages like Microsoft's TypeScript or upcoming, but not yet-available standards like ECMAScript 2015 (previously ES6).
When concerns like defining constants, helper functions and building tasks are
being separated more properly, it should be easier to comprehend projects of any size.
After writing a DI framework for Node.js and Browsers called dijs, I thought
it would be good to apply its patterns to Gulp as well.
One solely needs to define dependencies at a central point, then they become available everywhere you need it.
DI might be especially handy when prototyping applications, as one solely needs to define dependencies at a central point, they become available instantly everywhere you need it. This pattern has become popular with Angular.js itself in the recent years.
My framework is called gulp-di and is available as Open Source Software on Github.
The example above could be split into several files. As gulp-di includes the majority gulp plugins automatically, you just need to install "gulp-concat" in order to make the "concat" plugin available in your tasks like in this Gist.
In order to proof the basic concept, I needed to apply the idea to a real-world
scenario and rewrote the Gulpfile of the great angularjs-in-patterns repository.
I would be glad if you'd give it a spin if you've read so far!
Matthias, thanks for sharing! Any good events for sales and marketing executives coming up for you? We are hosting a CRO/CEO/Founder's Roundtable Mastermind on every 2nd and 4th Tuesday of each month at 11am EST covering the “CRO/Founders Revenue Pipeline Best Practices - Tips, Tactics and Strategies" We would love to have you be one of our special guests! Please join us by using this link to register for the zoom: https://www.eventbrite.com/e/crofounders-revenue-pipeline-best-practices-tips-tactics-and-strategies-tickets-1249362740589
Matthias, thanks for sharing!