Better Builds Begin with Broccoli

Build tools have come a long way since the days of writing bash scripts. The newest member to the ever growing community of build tools is called Broccoli and it’s written by the excellent open-source developer @jo_liss. Its aim is to not only deliver < 0.5 second rebuild times but to also simplify your build script. It accomplishes this by implementing an asset pipeline (a-la Rails) and by doing incremental builds instead of building the entire project (like Grunt).

Not a Replacement for Grunt

Note that Broccoli does not attempt to replace Grunt at all. In fact, it’s a grunt plugin. The idea here is to let Grunt do what it does best – run tasks and leave the building of your app to a tool that was designed to build – Broccoli. Simply install the plugin and replace all of the “hoop-jumping-multiple-tempfile-slow-building” mess in your Gruntfile with one fast, straightforward build task. Then you can use Grunt for the things it’s better at, like scaffolding and deploying.

Why Broccoli is Good for You

If you’ve spent any time on a medium sized project with Grunt then you are familiar with how slow builds can become. After a few months of development your productivity begins to shrink. One day, you change a single file, refresh the browser, and wait… and wait… 12 seconds later, your app loads. What was once a responsive feedback loop has become an intolerable double digit delay.

Never fear, Broccoli will get that to under 0.5 seconds. Do you have 100+ JavaScript files? No problem. But wait. “We use CoffeeScript, Sass, and ES6 Modules in our app. That stuff understandably takes time to build”, you say. Not anymore they don’t.

The beauty of Broccoli lies in three places:

Trees. Instead of passing around File System Directories (Grunt) or Memory Streams (Gulp), it passes around Trees. Broccoli isn’t built around individual files. Its unit of work (input and output) is a tree. This makes n:1 compilers like Sass a breeze to deal with (tree pun).

Chainable Plugins. Since all plugins return a tree, they can be easily chained.

Caching. Broccoli’s approach to incremental builds is pretty simple. Rather than do a partial rebuild (Rails, Brunch), it gets out of the way of the plugins and allows them to cache their output as appropriate. Broccoli simply does a fresh rebuild every time and the plugins return the majority of the build from their caches.

Where Broccoli Fits in

There are some places where Broccoli might not be the best choice and/or might not be a fit at all. If you’re building Sencha Ext JS or Sencha Touch apps then Broccoli will be a poor fit for that ecosystem.

However, if you are already using Grunt to do your builds then Broccoli could be a great fit. Also (and obviously), if you are using Ember then Broccoli is the perfect, if not the de-facto choice in build tools. That’s especially true given that the new Ember CLI uses the broccoli pipeline for asset compilation.

So, let’s go ahead and see what an actual build file looks like.

How Do I Use Broccoli

Installation is very straightforward. Since Broccoli runs on Node, we can use npm to install what we need. Note that you may need to run the broccoli-cli install as root/Administrator:

npm install --save-dev broccoli
npm install --global broccoli-cli

Then install any broccoli plugins you intend to use in your build file:

You can either start up the built-in watch server with broccoli serve and it will automatically know what needs to be rebuilt and when to rebuild it. Point your browser to http://localhost:4200 and you will see your newly built app.

You can use the CLI to build your app into whatever directory you choose. For example – broccoli build ‘public’ will build your app into the ‘public’ directory of your workspace.

<

p>Broccoli is currently in its Beta release (0.12.3) and although it’s still a bit rough around the edges, the tool itself is extremely fast and the community around it is growing. I think we should all get out there and give Broccoli a try. Our refresh buttons will thank us.

Share this article

A senior engineer at Modus Create, Timothy Eagan is an accomplished software developer with over 20 years of experience in creating customized business solutions. He can be found in Providence, RI, where he organizes and hosts the Providence JavaScript (Prov.JS) meetup group. Tim is also an OIF veteran, former paperboy and part-time comedian.