This section provides an overview of what gruntjs is, and why a developer might want to use it.
It should also mention any large subjects within gruntjs, and link out to the related topics. Since the Documentation for gruntjs is new, you may need to create initial versions of those related topics.
Version | Release Date |
---|---|
0.3.0 | 2012-07-30 |
0.4.0 | 2013-02-17 |
0.4.1 | 2013-03-12 |
0.4.2 | 2013-11-20 |
0.4.3 | 2014-03-06 |
0.4.4 | 2014-03-13 |
0.4.5 | 2014-05-11 |
1.0.0 | 2016-04-04 |
1.0.1 | 2016-04-05 |
Run grunt -h to see the following:
Grunt requires Node.js and npm to be installed. If you don’t have Node.js and/or npm installed on your machine, go to https://nodejs.org and download the installer or package for your operating system.
If you're installing Grunt for the first time, you'll first have to install the Grunt command-line interface package grunt-cli
globally.
npm install -g grunt-cli
This installs the command-line interface for Grunt globally so you can run the local version of Grunt in your project.
You can verify that you have grunt-cli
package installed by running the following command:
grunt --version
This should print at least the current version of your grunt-cli
package.
After you have grunt-cli
up and running, you can install the actual grunt
task runner and your first Grunt package grunt-contrib-jshint
:
npm install grunt --save-dev npm install grunt-contrib-jshint --save-dev
This downloads the packages from NPM package manager and saves them as devDependencies
to your package.json
file.
Next you need a Gruntfile.js
in your project root that acts as a config file for Grunt tasks:
module.exports = function(grunt) { grunt.initConfig({ jshint: { files: ['Gruntfile.js'], } }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint']); };
This file does three things:
grunt-contrib-jshint
task from NPM packagejshint
task to run against the file Gruntfile.js
default
that runs the jshint
taskAfter you have set up your project you can run the default
task of Grunt by calling:
grunt
This fires up grunt-cli
that runs the local grunt
which looks for a Grunt task named default
which is configured to run the task called jshint
.
All attributes of grunt.initConfig
are valid tasks, so if your Gruntfile looks like this:
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
files: ['Gruntfile.js'],
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('default', ['jshint']);
};
The shell command $ grunt jshint
will run the jshint
task.
Tasks can have different targets. Take this snippet of code for example:
grunt.initConfig({
jshint: {
gruntfile: {
files: ['Gruntfile.js']
},
project: {
files: 'src/**/*.js'
}
}
});
Here, jshint can target the gruntfile or all JavaScript files of your project. If we run $ grunt jshint
both targets will be used, but if we run $ grunt jshint:gruntfile
the linter will only be applied to the gruntfile.
The default tasks registered like this grunt.registerTask('default', ['jshint']);
will run with the shell command $ grunt
.
New registered tasks will run passing its name as a command line argument to grunt. For example:
grunt.registerTask('gruntfile', ['jshint:gruntfile']);
Will runt with $ grunt gruntfile
.