Grunt is a Javascript task runner. It automates repetitive tasks and steamline workflow. It uses Node JS and can be installed via npm (Node Package Manager)

I have been using Grunt for couple of my projects since last year — to concatenate, minify css and JavaScript files as well as compile SASS, check syntax error and run unit test.

Two important files of Grunt

Project meta data file stores information of all dependencies. When running $npm install, it ensures correct version get installed.

 "name": "my-project-name",
 "version": "0.1.0",
 "devDependencies": {
   "grunt": "~0.4.5",
   "grunt-contrib-compass": "~0.10.0",
   "grunt-contrib-hologram": "~0.4.1",
   "grunt-contrib-uglify": "~0.5.0"

Configuration file stores detail information of each Grunt plugin

module.exports = function(grunt) {
 // Project configuration.
   // Concatenate JavaScript files
   uglify: {
     all: {
          files: {
            'app/assets/js/all.min.js': ['app/assets/js/main/*.js', 'app/assets/js/common.js']
   // Compile SASS to CSS 
  compass: {      
      dist: {                    
        options: {
          sassDir: '../sass',
          cssDir: '../css',
          outputStyle: 'compressed'
   // Generate pattern library  
  hologram: {
      generate: {
        options: {
          config: 'hologram_config.yml'
 // Load the plugin
 // Default task(s).
 grunt.registerTask('default', [

grunt.initConfig — This is where we define configurations of Grunt plugin.

grunt.loadNpmTasks — This is where we load the Grunt plugin.

grunt.registerTask — This is where we register the Grunt task.

Setup Grunt

Make sure you have node.js and npm installed, here is a step by steptutorial. Once completed the installation, let’s start!

Install the CLI

$npm install -g grunt-cli

Install Grunt

$npm install grunt --save-dev

Install Grunt plugin

$npm install [grunt-plugin-name] --save-dev

Run Grunt

Simply type below to run all tasks at once


You can also configure registerTask and run specific task

grunt.registerTask('compass', ['compass']);

then type below to run

$grunt compass

Execute Grunt task, you should be able to see following output display in terminal:

Running "compass:dist" (compass) task
    write /StyleGuide/css/ (6.04s)
    write /StyleGuide/css/crlib.styles.css (6.859s)
  Running "compass:dev" (compass) task
  Running "hologram:generate" (hologram) task
  Custom markdown renderer Cortanamarkdownrenderer loaded.
  Build completed. (-:
  Done, without errors.

Test it with Grunt

Jasmine is a Behavior Driven Development testing framework for JavaScript. It is also called headless testing because it doesn’t rely on browsers, DOM, or any JavaScript framework. Use Grunt to automated testing is super easy.

Install Jasmine

$npm install grunt-contrib-jasmine --save-dev

Configure the task

jasmine : {
      src : 'theme-build/js/*.js',
      options : {
        specs : 'theme-build/specs/*.js'

Add a spec
Create a Jasmine spec, specs/spec.js as below

describe("crlibSlidebarsr", function() {
    it("should be a constructor function", function() {
        expect(typeof window.crlibSlidebars).toBe("function");

Load the plugin


Run the plugin

$grunt jasmine

Terminal should look like…

Running "jasmine:src" (jasmine) task
  Testing jasmine specs via PhantomJS
  >> ReferenceError: Can't find variable: angular at
  >> theme-build/js/main.js:12 
  >> theme-build/js/main.js:17 
    X should be a constructor function
      Expected 'undefined' to be 'function'. (1)
  1 spec in 0.005s.
  >> 1 failures

Watch it with Grunt

Grunt can watch files changes, execute tasks and then reload the page for us. Frist, let’s install Grunt watch

$npm install grunt-contrib-watch --save-dev

Configure the task

// Watch files
  watch: {
    // Watch .scss files
    sass: {
      files: ['css/sass/**/*.scss'],
      tasks: ['sass:dev'],
    // Live reload files
    livereload: {
      options: { livereload: true },
      files: [

Load the plugin


Run the plugin

$grunt watch

Tada! It’s done! Now Grunt will watch for changes to any .scss files, then run the task automatically.

Jessie Wang, Senior frontend developer

Written by Jessie W.

“I have no special talent, I am only passionately curious.“— Albert Einstein

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: