About
grunt is a task runner that permits to perform build operations.
Installation
Global
- Install node
npm update -g npm
- Install the Grunt CLI. It will run the version of Grunt required by a Gruntfile
npm install -g grunt-cli
Project
See also the grunt-init
Package.json
- Minimal package.json
{
"name": "my-project-name",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0"
}
}
or
npm install grunt --save-dev
npm install grunt-contrib-jshint --save-dev
npm install grunt-contrib-uglify --save-dev
...
Gruntfile
This file is named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins.
Plugins:
- config: Most Grunt tasks rely on configuration data defined in an object passed to the grunt.initConfig method.
- must be specified in package.json as a dependency.
module.exports = function(grunt) {
"use strict";
// Do grunt-related things in here
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Default task(s).
// It will run the uglify task
// same as
// grunt uglify
// or
// grunt default
grunt.registerTask('default', ['uglify']);
// A very basic custom default task.
grunt.registerTask('default', 'Log some stuff.', function() {
grunt.log.write('Logging some stuff...').ok();
});
};
Custom project-specific tasks don't need to be defined in the Gruntfile; they may be defined in external .js files and loaded via the grunt.loadTasks method.
Task
grunt –help command will list all available tasks.