Skip to main content

Grunt

Grunt is a JavaScript library , which is used to automatically perform repetitive tasks such as minification, compilation, unit testing, linting, etc .It is a task runner that can do most of the mundane work for you and your team with basically zero effort.

It uses a command-line interface(CLI) to run custom tasks defined in a file (known as a Gruntfile ). Grunt was created by Ben Alman and is written in Node.js. It is distributed via npm.

It is an open source tool and a link to Grunt open source repository can be found here.

Why use Grunt ?

The Grunt ecosystem is huge and it's growing every day. Grunt has a library of more than 5000 plugins.

You can use Grunt to automate just about anything with a minimum of effort. If you didn't find a plugin that you need, authoring and publishing your own Grunt plugin to npm is relatively easier.

Advantages of using Grunt :

  • All task runners have the following properties: consistency, effectiveness, efficiency, repeatability, etc.
  • Access to many predefined plugins that can be used to work with JavaScript tasks and on static content.
  • Allows users to customize tasks using predefined plugins.
  • Prefers the configuration approach to coding.
  • Allows users to add their own plugins and publish them to npm.

Installation:

Latest Version

Stable: v1.4.1 (npm)

Prerequisties:-

First of all, please make sure that Node.js is installed on your system. The current Long Term Support (LTS) release is an ideal starting point. Grunt 0.4.x requires stable Node.js versions >= 0.8.0 , older versions are not supported anymore.

Before setting up Grunt ensure that your npm is up-to-date by running npm update -g npm (this might require sudo on certain systems).

Installation:-

To install the grunt CLI:

/* with npm */
npm install -g grunt-cli

To install the Grunt and Grunt Plugins:

/*  installing Grunt */
npm install grunt --save-dev

/* the same can be used to install a grunt plugin ( for eg JSHint task module) */
npm install grunt-contrib-jshint --save-dev

GruntFile

The Gruntfile.js or Gruntfile.coffee file is a valid JavaScript or CoffeeScript file that belongs in the root directory of your project, next to the package.json file, and should be committed with your project source.

A Gruntfile is comprised of the following parts:

  • The "wrapper" function
  • Project and task configuration
  • Loading Grunt plugins and tasks
  • Custom tasks

Creating Tasks

Tasks are the backbone of grunt . The stuff you do most often, like jshint or nodeunit. Every time Grunt is run, you specify one or more tasks to run, which tells Grunt what you'd like it to do.

If you don't specify a task, but a task named "default" has been defined, that task will run by default.

for alias task

grunt.registerTask(taskName, [description, ] taskList)

for multi tasks

most of the contrib tasks are multi tasks

grunt.registerMultiTask(taskName, [description, ] taskFunction)
  1. grunt-contrib-clean ( Clean files and folders )
  2. grunt-contrib-watch ( Runs predefined task whenever watched files are added , deleted or edited )
  3. grunt-contrib-copy ( Copy files and folders )
  4. grunt-contrib-uglify ( Minify JavaScript with UglifyJs )
  5. grunt-contrib-concat ( Concatenate files )

Top Companies that are using Grunt :

References: