Using Grunt to automate development tasks: Part II

  • 0
  • March 5, 2014

If you’re new to Grunt, or don’t know what it is, check out the first article I wrote which introduces it: Using GRUNT to automate development tasks: Part I

As explained in that first article, Grunt is a javascript task runner. It allows us automate all sorts of tasks, however, we’re still having to trigger a manual process by running Grunt every time we want it to do something.

Using watch, we can get Grunt to run tasks whenever something changes instead. So, for example, every time we update our JS, Grunt will automatically concatenate and minify it to an output file instead of waiting for us to run the file manually.

Watching happens through the official grunt-contrib-watch plugin.

Let’s get this installed as we have done in our previous post (it’s exactly the same process) – npm install grunt-contrib-watch--save-dev.

We configure watch by giving it specific file, or folder (or both) to monitor, then we tell it what tasks we want to run when it detects a change.

So, let’s take a look. I think it would be safe to say that we’d like Grunt to concatinate and minify our JS whenever anything in the js folder changes – not just when we run it; and not when we change some images or CSS.

This should be fairly straight forward. spawn stops Grunt spawning child processes and keeps the context within the main watch function.

Don’t forget to load the NPM Task, and also to register the task.


grunt.registerTask('default', ['concat', 'uglify', 'imagemin', 'cssmin', 'watch']);

There are a lot of other resources and extensions around Grunt. So, some further reading:

Douglas Radburn

About Douglas Radburn

As our Head of Technical Doug has played a key role within the Development team since 2009. A founding developer of our Twitition and Competwition platforms, Doug has also been lead in our Open Source Projects on Magento ecommerce solutions and Wordpress CMS platforms.

  • Bret Kerr

    Thanks for the article Douglas. Quick question – do you think it would be possible to automtae, via Grunt, the task of updating .less and .css files, for a Sitecore based site, on 5 different production servers (we code on Macs and upload to 4 different production servers via windows remote desktop).

  • Douglas Radburn

    Hi Brett, Sorry for the delay getting back to you. Our .NET team aren’t currently using GRUNT, as they integrate with LESS in a different way on their Sitecore sites, but let me find out!

Like what you see? Talk to an Expert