The Brunch Build Tool

This is a guest post by Michael Schmatz, Lead Developer at CodeCombat

One of the most important parts of CodeCombat’s development workflow is Brunch. In this post I’ll tell you why CodeCombat chose Brunch, run through setting up a basic Brunch project, explain CodeCombat’s Brunch configuration, and compare Brunch to several popular alternative build systems.

CodeCombat loves Brunch

Why CodeCombat uses Brunch

In early 2013, CodeCombat was just getting started. One of the cofounders, Nick, wanted a workflow skeleton to kickstart development. After examining several different tools, including Brunch and Yeoman, he ended up choosing Brunch, mainly because of the awesome project skeletons available.

As time went on and the build times grew and grew, we appreciated one design choice of Brunch more and more: incremental rebuilding and caching. For small projects, the time difference between partial and complete rebuilds isn’t very significant. However, for large projects, long build times can be very detrimental to developer efficiency and happiness. Doing a complete rebuild of CodeCombat can take a few minutes on older computers, whereas partial rebuilds only take a few seconds, if that. Such responsiveness is integral to our development process.

Setting up a basic Brunch project

First, we need to install Brunch. Once you have npm installed, just run:

To get us started, we can use Brunch’s built in skeleton installer. There are many skeletons to choose from; I chose one of the more popular ones that includes AngularJS, CoffeeScript, LESS, and Jade.

All we have to do now is start Brunch. Move into the new directory, and then start the Brunch watcher/server.

Now any time we make a change to our app, Brunch will automatically recompile the files that changed, rebuild the app, and refresh the page. On my machine, this takes only about 70ms.

Let’s change the header on the front page. Open up your favorite IDE and start editing app/partials/todo.jade. In addition, open a web browser to the URL Brunch provided, in my case, http://localhost:3333.

brunch

Notice how, if you change something, for instance the <h2>, the app automatically is rebuilt and the page refreshes.

CodeCombat’s Brunch Configuration

In order to see what Brunch configurations look like for larger projects, let’s look through CodeCombat’s Brunch configuration file (the configuration docs for Brunch can be found here.)

The file starts off by importing the path module and declaring a utility function, startsWith.

Then, we tell Brunch to place all compiled files in the public folder, ignore all paths which start with an underscore, and generate source maps.

This section of code specifies how the various production JavaScript files should be compiled.

This section specifies all of the files that should be loaded before the others are compiled.

This section specifies how the stylesheets and templates should be compiled, as well as the skeleton framework.

This specifies the plugins we use: auto-reload is used to refresh the browser whenever Brunch recompiles, coffeelint-brunch is used to keep our CoffeeScript files clean, and uglify-js-brunch is used to, well, Uglify our code.

The final step is to replace the source map URLs in the compiled files with a small Perl regex.

Why Brunch

One of the first decisions that developers of JavaScript web applications have to make is which build tool to use; they’ve never had so much choice. They can use Grunt, Gulp, Brunch, Broccoli, Mimosa, or Jake, just to name a few. So how does one choose the right tool for the job?

Let’s compare Brunch with Grunt and Gulp, two of the most popular JavaScript build tools.

Grunt

Grunt is arguably the most popular build tool for JavaScript projects. It sports the largest ecosystem and a great amount of flexibility (WebStorm even has built-in Grunt integration!). However, Grunt configuration can often be complicated and result in larger configuration files than Gulp or Brunch. Grunt is great out of the box for small to medium sized projects, but the lack of out-of-the-box incremental builds and file caching makes it necessary to use plugins like grunt-newer to get short build times with very large projects, which may further increase configuration complexity. In addition, Grunt doesn’t have the level of support for project skeletons that Brunch does; often another tool is used, such as Yeoman.

Gulp

Gulp, the newest system of the three, is a bit different from either Brunch or Grunt in that it uses Node streams to achieve high performance. Its ecosystem isn’t as large as Grunt’s, but it’s still pretty large with 600+ plugins. The configuration files are very simple. Like Grunt, it doesn’t have great skeleton support or caching or incremental rebuilds built in, but has well-supported plugins which provide that functionality. Those plugins combined with the streams make Gulp the winner in terms of pure performance.

In Conclusion

You really can’t go wrong these days with build tools as long as you consider the anticipated scope, size, and technology requirements of your projects. Grunt has the largest ecosystem and most flexibility, Gulp has the best performance, and Brunch provides the easiest setup and great out-of-the-box performance. At CodeCombat, we’re very satisfied with Brunch, but can’t wait to see what the future holds in terms of new and improved build tools.

About Ekaterina Prigara

Ekaterina Prigara is WebStorm product marketing manager at JetBrains. She's passionate about new technologies, UX and coffee.
This entry was posted in Cool Feature and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">