Behold! The 3rd post on automated build/dev processes for the web.
This time we’re looking at Grunt. We’re looking at Grunt because I blew up by Guard setup on Windows (who knew gem update could be a bad idea) and used the opportunity to give Grunt another look. I poured myself a beer, swaggered over to my standing desk, and didn’t move until I figured it out. I’m really glad I did - Grunt is my new best friend.
Grunt is a JavaScript task runner built on Node.js. Node.js is an event-driven platform built on Chrome’s V8 and is blazingly fast. If you need efficient, uber-fast, real-time multi-user shiny, get to know Node.
To get Grunt up and running you need to install Node, then install Grunt via the node package manager (npm): npm install -g grunt-cli. I’m using 5 modules that can also be installed with npm:
You will typically create a package.json file that contains, among other things, your dependencies. If you’ve got one, you can just run npm install and it’ll read it and grab whatever is missing. To create a package.json file from your current dependencies run npm init. A typical package.json file looks like this:
The serious grunt stuff goes in Gruntfile.js. When you look at this one you might throw up in your mouth a little bit. Yes, I was doing all of this in 12 lines of Ruby + 14 lines of shell script. Yes, that’s a lot less than ~120 lines of code. Hang with me here.
With this build script I’m able to do everything I was doing before - watch, livereload, preprocessing, concatenation, minification, uglification, and cache busting (string replacement). It’s all fairly readable JavaScript and easily customizable across projects (this one is for the new version of GeoPortal I’m working on). And it’s node, so it hauls ass.
Here we’re doing some basic setup. We’re requiring 5 grunt modules - watch, uglify, concat, less, and text-replace. We register two tasks: default (simple watch setup) and build (make production ready). So you could type grunt to start the watch process, or grunt build to build that sucker. Last I’m setting an array of the JavaScript files in the order they need to be put together. That’ll get used next.
This section is for JavaScript. concat simply concatenates JavaScript files for development so they’re easy to debug. uglify turns it into the compact unreadable bandwidth-friendly stuff for production. Note that if you ever want to run one of these by itself, just reference it via something like grunt concat:development.
Watch does just that - it watches files and runs tasks when they change. It also can do livereload, which if you’re not doing…I just don’t know what to say to you. Start doing it. Here we’re compiling LESS when *.less changes, concatenating JavaScript when an asset changes, and doing some text replacement when a underscore template changes. Note none of that triggers livereload directly. Instead, livereload watches our public folder for when the changed stuff shows up. That way reload events aren’t firing multiple times.
Finally, some cache busting. These tasks replace a version number with a large random number in a couple of different places so your clients don’t get crap stuck in their cache.