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

Importing external data


In most coding practices, it's best practice to keep logic and data separated as much as possible. The same rule applies to the Grunt configuration logic and the data that it makes use of.

A very common use case for using data from an external source is the use of the project information contained in the package.json file. Information such as project names and version numbers might not change too often, but when they do, we'd probably prefer not having to look for them everywhere in the project.

Fortunately, the Grunt framework provides us with functions that allow us to easily read data from external files and store them in the configuration. This stored data can then also be easily used with the help of the string templates that are automatically processed for all configurations.

Getting ready

In this example, we'll work with the basic project structure that 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 making use of data from the package.json file when generating an optimized version of a JavaScript source file.

  1. For our example, we'll make use of the contib-uglify plugin, which can be used to compress JavaScript source files. Let's install it and load its tasks for using the instructions provided in the Installing a Plugin recipe of this chapter.

  2. We'll also need a simple JavaScript source file for the sake of our example. Let's create a file called sample.js in the root of our project directory and fill it with the following code:

    module.exports = function () {
      var sample = 'Sample';
      console.log(sample);
    };
  3. Next, we'll import the data contained in our project's pacakge.json file by making use of the grunt.file.readJSON function and assigning its result to a property called pkg in our configuration. After adding this, our configuration object should look similar to the following:

    {
      pkg: grunt.file.readJSON('package.json')
    }

    Tip

    Note that the property name pkg is just used for this example, and can be pretty much anything except for the names of the tasks that are available for configuration.

  4. Now that we have the data imported form our project package, we can set up an uglify task, which will compress a JavaScript source file called sample.js, using the version number contained in the package.json file as part of the resulting file's name. This is done by adding the following to our configuration:

    uglify: {
      sample: {
        files: {
          'sample_<%= pkg.version %>.js': 'sample.js'
        }
      }
    }

    Tip

    Grunt makes use of the Lo-Dash string template system. You can read more about it at the following URL:

    http://lodash.com/docs/#template

  5. Finally, we can use the grunt uglify command to test our setup, which should produce output similar to the following:

    Running "uglify:sample" (uglify) task
    File sample_0.0.0.js created: 81 B → 57 B
    
  6. We can now also take a look at our newly generated compressed version of the sample.js file in the sample_0.0.0.js file, which should have content similar to the following:

    module.exports = function(){var a="Sample";console.log(a)};

There's more...

The YAML format provides another popular way to store human readable data and can also be easily imported into our configuration. The following example, based on the work we did in the main recipe, demonstrates this functionality:

  1. First, we'll create a simple YAML file for the purpose of our example. Let's create sample.yaml in our project root and give it the following content:

    version: 0.0.0
    
  2. Now all we need to do is change the call to grunt.file.readJSON to import our sample YAML file instead. We do this by changing the pkg configuration to the following:

    pkg: grunt.file.readYAML('sample.yaml')
    
  3. If we now run the Grunt uglify command, we should see the same result as before, with output similar to the following:

    Running "uglify:sample" (uglify) task
    File sample_0.0.0.js created: 81 B → 57 B