All of the functionality that can be provided by Grunt is housed in the plugins that are made available in the form of Node.js packages. In this recipe, we'll run through the process of installing a plugin, which will prepare us for all the recipes that are to follow.
For our example, we'll install the contrib-jshint (0.10.0)
plugin. The same steps used to install this plugin can be used to install any of the other plugins available in the plugin package index.
In this example, we'll work with the basic project structure we created in the Setting up Grunt on a project recipe of this chapter Be sure to refer to it if you are not yet familiar with it's contents.
The following steps take us through installing a plugin on our project and loading the tasks it contains:
The first step of installing a plugin is to install the package that contains it in the current project path. For our example, we'll install the
contrib-jshint
plugin that is contained in thegrunt-contrib-jshint
package. We can install this package and add it to our project dependencies by using the following command:$ npm install --save grunt-contrib-jshint
Next, we'll have to load the tasks contained in the plugin package so they can be used in our configuration. This is done using the
loadNpmTasks
function, provided to us by thegrunt
object that is passed to us in the configuration file. After adding this, our configuration file should look similar to the following:module.exports = function (grunt) { grunt.initConfig({}); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', []); };
Now that we have the package installed and its tasks loaded, we can use the loaded tasks in our configuration. For our example, we had the
jshint
task loaded, which enables us to use it in a manner similar to the following:module.exports = function (grunt) { grunt.initConfig({ jshint: { sample: { files: 'src/*.js' } } }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', []); };
As you start to make use of more and more Grunt plugins, you will soon start to wonder whether there is a way to optimize the process a little. Fortunately, someone else has already gone down this road before and created the load-grunt-tasks
utility that automates the loading of tasks from all the packages mentioned in your project dependencies. This means that we no longer need to add a loadNpmTasks
call for each plugin we install.
The following steps illustrate the usage of this utility, continuing from the work we did earlier in the main recipe:
Install the utility's package in the current project path and add it to your dependencies by using the following command:
$ npm install --save load-grunt-tasks
Now we can use it in our configuration file by importing the package and passing the
grunt
object to it. Now that we're making use of this utility, we can also remove all theloadNpmTasks
we were using to load our plugins. This should result in a configuration file similar to the following:module.exports = function (grunt) { require('load-grunt-tasks')(grunt); grunt.initConfig({ jshint: { sample: { files: 'src/*.js' } } }); grunt.registerTask('default', []); };