You are previewing Grunt Cookbook.
O'Reilly logo
Grunt Cookbook

Book Description

Over 80 hands-on recipes for streamlining development, management, and deployment with Grunt

In Detail

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.

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.

What You Will Learn

  • Automate CSS generation

  • Manage asynchronous work inside a task

  • Automate HTML and CSS template compilation

  • Manage entire web applications using Grunt

  • Automate various levels of testing

  • Build and publish custom tasks and plugins

  • Automate CoffeeScript and LiveScript code compilation

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. 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.

    Table of Contents

    1. Grunt Cookbook
      1. Table of Contents
      2. Grunt Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Grunt
        1. Introduction
        2. Installing the Grunt CLI
          1. How to do it...
          2. How it works...
        3. Setting up Grunt in a project
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Installing a plugin
          1. Getting ready
          2. How to do it...
          3. There's more...
        5. Setting up a basic web server
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Opening the default web browser on the default URL
            2. Opening a specific web browser at a specific URL
            3. Using a specific port
            4. Automatically selecting an available port
            5. Using a specific hostname
            6. Serving files from multiple directories
            7. Using middleware
            8. Adding functionality to the created server
        6. Watching files for changes
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Watching more than one file
            2. Running a series of tasks
            3. Preventing process spawning for task runs
            4. Enabling the interruption of task runs
            5. Specifying the waiting period before rerunning tasks
            6. Run tasks only on specific file events
            7. Allowing tasks to kill the watcher process
            8. Running tasks once at startup of the watcher
        7. Setting up LiveReload
          1. Getting ready
          2. How to do it...
        8. Processing only changed files
          1. Getting ready
          2. How to do it...
          3. There's more...
        9. Importing external data
          1. Getting ready
          2. How to do it...
          3. There's more...
      9. 2. File Management
        1. Introduction
        2. Copying files
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Processing file contents
            2. Copying the source file's permissions
            3. Setting the permissions of the destination file
        3. Compressing files
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Archiving a collection of files
            2. Specifying the compression mode
            3. Specifying the archive compression level
        4. Creating symbolic links
          1. Getting ready
          2. How to do it...
        5. Concatenating files
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Joining files with a custom separator
            2. Stripping banners before concatenation
            3. Adding a banner and footer to the concatenated result
            4. Processing the source file's contents before concatenation
        6. Fetching a single URL
          1. Getting ready
          2. How to do it...
          3. There's more...
        7. Fetching multiple URLs
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Downloading similar URLs
            2. Saving downloaded files to altered filenames
            3. Downloading files using special request options
      10. 3. Templating Engines
        1. Introduction
        2. Rendering Jade templates
          1. Getting ready
          2. How to do it...
        3. Using data in a Jade template
          1. Getting ready
          2. How to do it...
          3. There's more...
        4. Using custom filters in a Jade template
          1. Getting ready
          2. How to do it...
        5. Compiling Jade templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Specifying the namespace of compiled templates
            2. Indicating how template names should be derived from filenames
            3. Compiling templates with debug support
        6. Compiling Handlebars templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Specifying the namespace for compiled templates
            2. Indicating how template names should be derived from file names
        7. Compiling Underscore templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Specifying the namespace for compiled templates
            2. Indicating how template names should be derived from file names
        8. Using partials in Handlebars templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Making partials available in the namespace
            2. Indicating how partial templates should be identified
            3. Indicating how partial template names should be derived
        9. Wrapping Jade templates in AMD modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Wrapping Handlebars templates in AMD modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Wrapping Underscore templates in AMD modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Wrapping Handlebars templates in CommonJS modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Altering Jade templates before compilation
          1. Getting ready
          2. How to do it...
        14. Altering Handlebars templates before compilation
          1. Getting ready
          2. How to do it...
        15. Altering Underscore templates before compilation
          1. Getting ready
          2. How to do it...
      11. 4. Generating CSS and JavaScript
        1. Introduction
        2. Compiling LESS to CSS
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying a root path for resources
            2. Minifying the resulting output
            3. Rewriting URLs to be relative
            4. Forcing evaluation of imports
            5. Specifying extra import paths
            6. Adding a banner to the resulting output
        3. Compiling Sass to CSS
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Changing the output style
            2. Specifying a path for images
            3. Specifying extra import paths
        4. Compiling Stylus to CSS
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Disabling compression of the resulting CSS
            2. Specifying extra import paths
            3. Defining global variables
            4. Enabling the inclusion of imported CSS files
            5. Adding a banner to the resulting output
        5. Compiling CoffeeScript to JavaScript
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Compiling without the top-level safety wrapper
            2. Concatenating multiple targets before compilation
        6. Compiling LiveScript to JavaScript
          1. Getting ready
          2. How to do it...
        7. Generating source maps for LESS
          1. Getting ready
          2. How to do it...
          3. There's more...
          4. How it works...
        8. Generating source maps for Sass
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Generating source maps for CoffeeScript
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Defining custom functions with LESS
          1. Getting ready
          2. How to do it...
        11. Using Stylus plugins
          1. Getting ready
          2. How to do it...
      12. 5. Running Automated Tests
        1. Introduction
        2. Running Jasmine tests
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Loading helpers to be used in tests
            2. Loading libraries before running tests
            3. Loading styles required by tests
            4. Providing a custom template for the specification runner
        3. Running QUnit tests
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Loading tests from a web server
            2. Continuing execution after failed tests
            3. Suppressing the PhantomJS browser console output
            4. Passing arguments to the PhantomJS instance
        4. Running NodeUnit tests
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Using an alternative reporter
            2. Sending reporter output to a file
        5. Running Mocha client-side tests
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Loading tests from a web server
            2. Sending reporter output to a file
            3. Displaying the PhantomJS browser's console output
            4. Displaying source errors
            5. Specifying options for the Mocha test runner
            6. Using an alternative reporter
        6. Running Mocha server-side tests
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Using an alternative reporter
            2. Selecting tests using a regular expression
            3. Checking for global variable leaks
            4. Sending reporter output to a file
            5. Loading extra modules into the testing environment
        7. Generating coverage reports for server-side code using Mocha and Blanket
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Outputting coverage results in the LCOV format
        8. Generating coverage reports for client-side code using Mocha and Blanket
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying a success threshold for the global average
            2. Specifying success thresholds for particular files
            3. Specifying success thresholds for particular modules
        9. Generating coverage reports for client-side code using QUnit and Istanbul
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying a report output destination
            2. Specifying coverage thresholds at various levels
      13. 6. Deployment Preparations
        1. Introduction
        2. Minifying HTML
          1. Getting ready
          2. How to do it...
        3. Minifying CSS
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Prefixing a banner
            2. Removing special comments
            3. Reporting on gzipped results
        4. Optimizing images
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Adjusting the PNG compression level
            2. Disabling the progressive JPEG generation
            3. Disabling the interlaced GIF generation
            4. Specifying SVGO plugins to be used
            5. Using the 'imagemin' plugin framework
        5. Linting JavaScript code
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying standard JSHint options
            2. Specifying globally defined variables
            3. Sending reported output to a file
            4. Preventing task failure on JSHint errors
        6. Uglifying JavaScript Code
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying standard UglifyJS options
            2. Generating source maps
            3. Wrapping generated code in an enclosure
        7. Setting up RequireJS
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Using RequireJS optimizer options
            2. Generating a source map
      14. 7. Deploying to the End User
        1. Introduction
        2. Deploying to Rackspace Cloud Files
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Uploading the contents of a directory
            2. Uploading to a destination directory
        3. Deploying to AWS S3
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Specifying the accessibility of uploaded files
            2. Enabling concurrent uploads
        4. Deploying over FTP
          1. Getting ready
          2. How to do it...
        5. Deploying over SFTP
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Using a private key and passphrase
            2. Using an SSH agent
        6. Deploying to GitHub Pages
          1. Getting ready
          2. How to do it...
        7. Invalidating an AWS CloudFront distribution
          1. Getting ready
          2. How to do it...
        8. Running commands over SSH
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Using a private key and passphrase
            2. Using an SSH agent
      15. 8. Creating Custom Tasks
        1. Introduction
        2. Creating an alias task
          1. Getting ready
          2. How to do it...
          3. There's more...
        3. Creating a basic task
          1. Getting ready
          2. How to do it...
        4. Accessing project configuration
          1. Getting ready
          2. How to do it...
        5. Checking for required configurations
          1. Getting ready
          2. How to do it...
        6. Checking for the successful execution of other tasks
          1. Getting ready
          2. How to do it...
        7. Running non-blocking code in a task
          1. Getting ready
          2. How to do it...
        8. Failing a task
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. Failing a non-blocking task
            2. Aborting a task immediately on failure
        9. Using command-line parameters
          1. Getting ready
          2. How to do it...
        10. Enqueuing tasks to run
          1. Getting ready
          2. How to do it...
        11. Creating a multi-task
          1. Getting ready
          2. How to do it...
        12. Using options in a task
          1. Getting ready
          2. How to do it...
        13. Using files in a task
          1. Getting ready
          2. How to do it...
          3. There's more...
      16. 9. Authoring Plugins
        1. Introduction
        2. Finding plugins
          1. Getting ready
          2. How to do it...
        3. Contributing to a plugin
          1. Getting started
          2. How to do it...
        4. Setting up a basic plugin project
          1. Getting started
          2. How to do it...
        5. Creating a plugin task
          1. Getting started
          2. How to do it...
        6. Writing tests for a plugin task
          1. Getting started
          2. How to do it...
        7. Adding documentation for a plugin
          1. Getting started
          2. How to do it...
        8. Publishing a plugin
          1. Getting started
          2. How to do it...
      17. 10. Static Sites
        1. Introduction
        2. Setting up a basic site project
          1. Getting started
          2. How to do it...
        3. Adding a page to the site
          1. Getting started
          2. How to do it...
        4. Adding content to the site
          1. Getting started
          2. How to do it...
        5. Adding data to the site
          1. Getting started
          2. How to do it...
        6. Creating and using a site layout
          1. Getting ready
          2. How to do it...
        7. Generating pages from a collection
          1. Getting started
          2. How to do it...
          3. Creating a template helper
          4. Getting started
          5. How to do it...
        8. Using a plugin
          1. Getting ready
          2. How to do it...
      18. Index