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

Setting up Grunt in a project


For a project to make use of the Grunt framework, it will require the installation of its libraries and the setting up a bare minimum configuration file. The libraries provide the framework and tools required by all Grunt plugins, and the configuration file provides a starting point from which we can start loading plugins and adjusting their behavior.

Getting ready

It's usually a good idea for a project to be packaged in a way to help keep track of dependencies, binaries, scripts, maintainers, and other important information. The standard package format for Node.js-based projects is CommonJS.

Tip

To find out more about CommonJS, you can take a look at its specification at the following URL:

http://wiki.commonjs.org/wiki/Packages/1.1

At the heart of the CommonJS package, lies the package.json file. This file contains everything important about the package and is stored in the JSON format. The simplest way to create a package.json file is to use the npm init command. This command will ask a series of questions and generate a package.json file based on the answers provided. Here's an example of the questions that are asked when you run the command:

name: (grunt-book) myproject
version: (0.0.0)
description: My first Grunt project.
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

After these questions are answered, a package.json file will be generated in the current path with the following contents:

{
  "name": "myproject",
  "version": "0.0.0",
  "description": "My first Grunt project.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Tip

Another handy guide to the package.json file can be found at the following URL:

http://browsenpm.org/package.json

How to do it...

The following steps take us through installing the Grunt framework libraries on our project and creating a bare minimum configuration file.

  1. First, we'll install the Grunt libraries in the current project path, and have it added to our project dependencies. This can all be done by using the following command:

    $ npm install --save grunt
    

    Tip

    Due to our use of the --save flag with the install command, the Grunt package will be added to the dependency list of the project's package. This can be confirmed by taking a look at the dependencies property inside the package.json file.

    The --save-dev flag is also available for use with the install command when you'd like the installed packages to be added to the devDependencies property, which lists the dependencies to set up a development environment.

  2. Next, we'll set up an empty configuration file that would, at the very least, allow Grunt to run and also provides a place for future task configurations. Let's create a file called Gruntfile.js in the root directory of our project with the following contents:

    module.exports = function (grunt) {
      grunt.initConfig({});
      grunt.registerTask('default', []);
    };
  3. Now that we have the Grunt libraries installed and a basic configuration file set up, we can use the grunt command to test that it's all working as expected. Running the grunt command in the terminal should now produce output similar to the following:

    Done, without errors.
    

    Tip

    Running the grunt command without specifying any parameters will always try to run the default task, which in the case of our current example, is set to nothing.

How it works...

When the Grunt CLI tool is used, it always looks for the nearest file named Gruntfile.js, from which it then attempts to load configurations. Inside the configuration file, there is an exported function that receives one argument. This argument is an object that provides us with access to the Grunt framework to load, create, and configure tasks.

At this point, we have no tasks loaded or created, and no configurations defined. Our default task is also set to do nothing, so running the grunt command did nothing except report that it was successfully completed.