2013-07-24



In a series of tutorials, I’m going to help you get started with Grunt, an open source JavaScript task runner that will automate some of your web development tasks.

This nifty JavaScript library will notably speed up and improve your web development processes, and also help you sidestep common human errors.

My intention with this Grunt tutorial series is to get you to experience the same productivity and efficiency improvements I’ve gained through this useful, but little-known open source JavaScript tool.

Here’s the outline of this three-part Grunt tutorial series:

Speed Up Your Web Development Workflow with Grunt: We’ll cover basic information about Grunt; what Grunt is, how to install Grunt, how to use Grunt in your web projects, and a brief overview of gruntplugins.

Create a test project using modern front-end development techniques (will be posted on a later date)

Using Grunt and Jasmine to automate testing and enforce code quality (will be posted on a later date)

Let’s start this tutorial series with some basic information about Grunt and how it can help speed up your web development workflow.

What is Grunt?



When I started my job as a front-end web developer at CDNify (we’re a content delivery network aimed at web developers, startups, and digital agencies) I had absolutely no idea what Grunt was or how it could significantly improve my development workflow.

Months down the road, and I’m now using Grunt every single day. I can’t imagine my front-end web development workflow without it now.

In short, Grunt is an open source JavaScript project that automates repetitive tasks you often have to do as a web developer.

Grunt can automate tasks like minification, unit testing, and preparing your files and website assets for production use. Grunt does this by wrapping these processes up into tasks.

A few more examples of common Grunt tasks are:

Optimizing your web images through a service like Smush.it

Concatenating JavaScript files into one big file for faster web page load times

Running scripts that analyze your code for potential errors (often referred to as linting)

Enforcing your coding style guides for uniformity and readability throughout your project’s code base

Compiling your CSS from LESS/Sass

Anything that has the potential to be automated can be done with Grunt.



You can configure Grunt to watch certain files for changes, and then build the results on the fly.

Using Grunt in Your Web Development Team

When used in a team environment, Grunt can help every person in that team write code that adheres to the same standards/style guides.

Details such as controlling the indentation of code can now be a strict process, as a whole build will fail if any part throws an error.

For example, when indenting code, you could automatically enforce the use of spaces instead of tabs (or vice versa, depending on your preferences), which will ensure that the whole team has the same configuration. When they push code to the repository, it’s in the expected format.

Used in conjunction with your favorite code-quality tools (for example, JSHint), Grunt will help you catch sloppy code such as missing semicolons, braceless control statements, unused variables, and trailing whitespace. This is excellent for discovering human errors as well as disallowing valid, but badly written, JavaScript.

Prerequisites

To get the most out of Grunt, you should first know (or learn about) the following.

Command-line Interface

In order to use a tool such as Grunt effectively, you will need to have at least a basic understanding of the terminal. You’ll need to be familiar with the command line interface (CLI) for the file system you use.

You should be able to navigate confidently to a folder on your system and run commands within it.

Here are tutorials and resources that will get you started on learning how to use CLIs on your own computer:

Windows: Windows Command Prompt in 15 Minutes (cs.princeton.edu)

Mac: Introduction to the Mac OS X Command Line (teamtreehouse.com)

Linux: tuXfiles (tuxfiles.org)

However, note that you’ll need to be familiar with the CLI of the SSH client you use to connect to your remote web server if you want to use Grunt on web projects. One popular SSH client for Windows and Linux is PuTTY. (Learn how to use PuTTY by reading its docs).

Don’t be put off; you’ll see that the CLI is very simple once you start using it. It’s also a very powerful tool, and knowing how to use it effectively can speed up your workflow on its own.

Optional: Version Control System

Although not technically required in order to use Grunt, Grunt works best in an environment where version control is important.

Using Grunt for a small static website that is likely to be updated infrequently may be considered overkill. Any web development project that’s larger than that should be version-controlled. And large web development projects are where Grunt becomes critical and extremely useful.

Read this list of Git tutorials for beginners or this introductory guide to Git to help you get started with Git, a popular version control system.

Get Started with Grunt

I’ll run through the general steps of installing Grunt, which is a process that relies on Node.js (an open source development platform for network applications).

Install Node.js

In order to install Grunt, Node.js must first be installed or available in your dev environment (which could your computer) or your web server.

Currently, Node.js version 0.8.0 and up is the supported version.

Download and install Node.js by first going to:

Node.js Download Page (nodejs.org)

The official Node.js Download Page has various options for you, such as a Windows installer, a Mac OS installer, Linux binaries, and the source code.

If you need help installing Node.js, read the following tutorial:

How to Install Node.js (howtonode.org)

Installing Node.js allows us to install Grunt using Node.js’s package manager, called npm.

Install Grunt

Next, you need to install Grunt and its dependencies. We will install Grunt using Node.js’s npm.

Learn how to install Grunt with npm by reading this tutorial:

Installing Grunt (gruntjs.com)

Install Grunt’s Command Line Interface (CLI)

Grunt’s Command Line Interface (CLI) allows you to run Grunt in any directory.

We can install Grunt’s CLI with this npm command:

You may need to prefix the command with sudo on OS X, *nix, BSD, etc., or run your command shell as Adminstrator on Windows to install Grunt’s CLI.

Note that installing the CLI doesn’t install the Grunt task runner itself, as each project may wish to use a different version of Grunt. (In this tutorial, we’ll be using Grunt 0.4x.)

Instead, the CLI acts as a bridge between your Gruntfile (more on this in a bit) and the version of Grunt installed locally to your project.

Using Grunt in a Web Development Project

Now that you have Grunt installed, let’s go over the basics of how to use it in a web development project.

To use Grunt in a web development project, we need two files: package.json and a Gruntfile (e.g. Gruntfile.js).

package.json

package.json is a JSON file. This file should be located in your project’s root directory.

Project information and settings are specified in package.json, such as the project name, version, author, and if it’s a private project or not.

package.json also contains what are known in the Node.js nomenclature as devDependencies. devDependencies are items you need for your project. In this sense, Grunt will be an item listed under devDependencies, along with the Grunt plugins you want to use for the project (I’ll talk about this later).

Here’s an example template for a package.json file:

By specifying the project’s dependencies in package.json, we can use npm to install them for us automatically simply by running the following command in our project’s directory:

Running that command will give us an output like this:

Gruntfile

The Gruntfile is the main configuration file for the project, and specifies what tasks Grunt should run and what files in the project they affect.

Your project’s Gruntfile can be a JavaScript file (Gruntfile.js) or CoffeeScript file (Gruntfile.coffee).

In this tutorial, we’ll be using JavaScript.

At its most basic form, the Gruntfile should contain the following:

initConfig is where the dependency options are specified. Each Grunt plugin is configured using a JSON object (with some exceptions).

Some plugins allow more than one configuration to be loaded. For example, there may be a specific set of experimental JavaScript that uses a different library to jQuery, and so that library must be predefined instead of jQuery in the JSHint configuration. (We will look at this in more detail in the third part of this Grunt tutorial series.)

registerTask can be specified more than once. The default task is run when Grunt is executed in the command line, and so this should contain common setup tasks.

At this point, if you run Grunt successfully, it will generate the following output:

Now we have a project skeleton which will become useful once we use some plugins.

Using Grunt Plugins

A key feature of Grunt is the use of Grunt plugins. Grunt plugins are referred to as gruntplugins in the Grunt nomenclature.

gruntplugins are user-contributed modules that will help you automate tasks without having to write your own task scripts.

For example, you can use the grunt-contrib-compress gruntplugin to compress and optimize the file sizes of your project files.

To use the plugin in a project, these are the steps to take:

List it as a devDependency object in package.json

Load it using the loadNpmTasks function in the project’s Gruntfile

Register the task by using the registerTask function in the project’s Gruntfile

Run npm install to install Grunt and the plugin

Here’s the sample source code for using the grunt-contrib-compress gruntplugin in your project.

package.json

Gruntfile

Grunt plugins are actually just npm modules that follow the gruntplugin template. You can find gruntplugins on the npm registry by browsing modules tagged with "gruntplugin" or at the official Grunt Plugins page. There are currently over 300 listed gruntplugins.

Summary

In this tutorial, this is what we covered:

What Grunt is, and how it can help web developers speed up their workflow.

Installation of Node.js

Installation of Grunt

Installation of Grunt’s CLI

General usage of Grunt in web development projects

How to use a gruntplugin

In the next tutorial in this Grunt tutorial series, we will go hands-on and apply what we’ve discussed here by creating a simple project using modern front-end development techniques paired with Grunt.

This is the three-part Grunt tutorial series outline:

Speed Up Your Web Development Workflow with Grunt

Create a test project using modern front-end development techniques (will be posted on a later date)

Using Grunt and Jasmine to automate testing and enforce code quality (will be posted on a later date)

To ensure that you get notified when the next parts of this tutorial series is published, follow @sixrevisions on Twitter and/or Like the Six Revisions Facebook page.

Related Content

Introductory Guide to Git Version Control System

20 Free Online Tools for Website Speed Testing

A Comparison of Methods for Building Mobile-Optimized Websites

Related categories: JavaScript and Web Development

About the Author

Ben Briggs is a front-end developer at CDNify, a content delivery network designed for startups and developers. He’s passionate about modular CSS, preprocessors and optimization. Find Ben on Twitter: @ben_eb.

The post Speed Up Your Web Development Workflow with Grunt appeared first on Six Revisions.

Show more