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

Preface

Grunt is a task automation tool that provides the platform for an ever-growing ecosystem of plugins. These plugins provide the actual implementation for the tasks that can be automated. File management, testing, deployment, template rendering, code generation, and much more can be automated using easily configurable tasks.

The Grunt platform's key strength lies in the simple methodology it has defined for task configuration, and the fact that all the plugins it supports tend to adhere to this methodology. This provides developers with a faster way to start using tools that would previously have taken more time to learn, while also presenting the configuration in a standard format that can be easily understood, manipulated, and shared.

The Grunt Cookbook aims to provide easy-to-follow instructions to automate all kinds of tasks that your projects need to perform on a regular basis. We'll start with the most basic and common tasks, work our way up to creating our own tasks, and eventually generate entire sites using only Grunt.

The chapters of this book will each focus on a specific topic for which a collection of recipes will be provided. Many of the recipes will explore the automation of similar tasks, using different tools or libraries. Each recipe also explores popular variations on its theme, just in case you have more specific needs.

What this book covers

Chapter 1, Getting Started with Grunt, starts with working though setting up Grunt to be used within a Node.js-based project, and covers some of the more common automations.

Chapter 2, File Management, covers the copying, compressing, linking, concatenation, and download of files. Working with file is probably the most common task that we'll encounter in any software project.

Chapter 3, Templating Engines, covers the rendering, compilation, and packaging of templates using some of the more popular engines. The practice of rendering content from templates is essential in the development of web-based projects.

Chapter 4, Generating CSS and JavaScript, covers the generation of CSS and JavaScript code. Using new languages that compile to JavaScript and CSS preprocessors that generate CSS can save time and improve the flexibility of a project.

Chapter 5, Running Automated Tests, covers the running of test suites and generating code coverage reports. Automated testing has become an essential part of all larger software projects and is an invaluable tool to ensure the stability and quality of code.

Chapter 6, Deployment Preparations, covers the optimization of images, minifying of CSS, ensuring the quality of our JavaScript code, compressing it, and packaging it all together into one source file.

Chapter 7, Deploying to the End User, covers the transfer of files to a network location, refreshing caching services and running commands on remote servers. Once we've got a functioning and optimized web application, it's time to make it accessible to its intended users.

Chapter 8, Creating Custom Tasks, covers all the aspects involved in creating our own custom tasks. At some point, you may encounter something that you'd like to automate but can't find a Grunt task that works exactly the way you need it to. This is when building a custom task can become invaluable to your operations and potentially make a hero out of you.

Chapter 9, Authoring Plugins, covers the process of discovering plugins, contributing to the existing plugin projects, and creating plugin projects of our own. The Grunt project would be nothing without its ecosystem of plugins, and we as developers, can partake in their creation and evolution.

Chapter 10, Static Sites, generates an entire static website using the Assemble plugin. Simpler websites that host content that doesn't change very often can mostly be generated once and uploaded to a hosting service.

What you need for this book

The only requirement for using the recipes in this book is a Node.js installation. This can be done either through downloading and running the installer, compiling from the source code, using a tool such as Node Version Manager (https://github.com/creationix/nvm), or using the package manager supported by your operating system.

If you have never worked with Grunt before, it's highly recommended that you start with Chapter 1, Getting Started with Grunt, as all the later chapters will reference it.

Who this book is for

This book will be useful to anyone who wishes to build anything from a static website to a more modern web application. Some basic JavaScript experience is preferable and some rudimentary knowledge of the Node.js platform could also come in handy.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The simplest way to create a package.json file is to use the npm init command."

A block of code is set as follows:

{
  "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"
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

'curl-dir': {
  weather: {
    router: function (url) {
      var city = url.slice(url.indexOf('=') + 1, url.length);
      return city + '.json';
    },
    src: 'http://api.openweathermap.org/'
      + 'data/2.5/weather?q={London,Paris,Tokyo}',
    dest: 'weather'
  }
}

Any command-line input or output is written as follows:

Running "newer:jshint" (newer) task
Running "newer:jshint:sample" (newer) task
No newer files to process.

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.