Book Image

Grunt Cookbook

By : Jurie-Jan Botha
Book Image

Grunt Cookbook

By: Jurie-Jan Botha

Overview of this book

<p>A web application can quickly turn into a complex orchestration of many smaller components, each one requiring its own bit of maintenance. Grunt allows you to automate all the repetitive tasks required to get everything working together by using JavaScript, the most popular programming language.</p> <p>Grunt Cookbook offers a host of easy-to-follow recipes for automating repetitive tasks in your web application's development, management, and deployment processes. This book will introduce you to methods that can be used to automate basic processes and your favorite tools. By following the recipes, you will soon be comfortable using Grunt to perform a wide array of advanced tasks in a range of different scenarios.</p>
Table of Contents (17 chapters)
Grunt Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing a plugin


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.

Getting ready

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.

How to do it...

The following steps take us through installing a plugin on our project and loading the tasks it contains:

  1. 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 the grunt-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
    
  2. 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 the grunt 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', []);
    };
  3. 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', []);
    };

There's more...

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:

  1. 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
    
  2. 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 the loadNpmTasks 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', []);
    };

    Tip

    The load-grunt-tasks plugin will, by default, only load plugins that have names with grunt at their start. This behavior can be customized by using the pattern option. To find out more about the load-grunt-tasks plugin, refer to the plugin's page at the following URL:

    https://github.com/sindresorhus/load-grunt-tasks