Getting Started With Grunt Task Runner

Grunt

About Task Runner

There are certain tasks which front-end developers are often asked to do. Tasks like:

  • CSS file compression
  • JS minification or obfuscation
  • HTML/JSON file minification
  • Image optimisation
  • Compile SASS or LESS files to generate CSS files
  • Unit testing
  • SVN commit
  • Build deployment etc

To do the above tasks we need so many different reliable tools and it is a tedious task to find such types of reliable tools online. And I don’t think any such tool is available which can do all the above tasks in one click.

What if these tasks can be automated and can be done in one key press? To do this, we would need to write few tasks in JS file and run Grunt.

So, what is Task Runner? We can say task runner is in one word Automation. Task runners help us to automate these tasks and perform these tasks synchronously or asynchronously.

Grunt

Grunt is a JavaScript Task Runner and this is a command line tool which runs on NodeJS. It helps us to do all the above tasks very easily with a minimum effort. Grunt ecosystem and its huge list of plugins help us get all front-end code production ready.

Prerequisites

Grunt and Grunt plugins are installed and managed by NodeJS Package Manager (npm). To run grunt commands, it is required to have NodeJS installed in your system.

To test if node and npm are installed in your system, followthe  below steps:

  • Open terminal
  • Type “node –v” in command line and press enter. It should show node version.
  • Type “npm” in command line and press enter. It should show npm details.

Install Command Line Interface (CLI)

To work with Grunt, it is required to install Grunt’s Command Line Interface (CLI). To install run below command in command line (or terminal in Mac).

npm install -g grunt-cli

This will put the grunt command in your system path, allowing it to be run from any directory.

In Mac it may be required to use sudo to run npm commands. So, in that case, command should look like:

sudo npm install -g grunt-cli

Start with a new Grunt Project

Each grunt project requires two files in your project folder:

  • package.json
  • Gruntfile.js

Create package.json

As mentioned in official website of Grunt, this file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file. To create package.json:

  1. Change to Project’s root directory in the command line.

  2. Run below command in command line:

    npm init

This command will askfor a  few inputs. It is not necessary to enter values for any option. Remember to type “Gruntfile.js” when it asks “entry point”. Then continue pressing enter. It will create a package.json file in your project directory. package.json should look like:

  1. {  
  2.   "name""sampleproject",  
  3.   "version""1.0.0",  
  4.   "description""",  
  5.   "main""Gruntfile.js",  
  6.   "scripts": {  
  7.     "test""echo \"Error: no test specified\" && exit 1"  
  8.   },  
  9.   "author""",  
  10.   "license""ISC"  
  11. }  
Create Gruntfile.js

Gruntfile.js is a JavaScript file which will be used to configure or define tasks and load Grunt plugins. So, to create a Gruntfile.js: 
  1. Create a new JavaScript file with name Gruntfile.js in your project directory.

  2. Copy below code to Gruntfile.js and save:
    1. module.exports = function(grunt) {  
    2.   
    3.   grunt.initConfig({  
    4.     pkg: grunt.file.readJSON('package.json'),  
    5.   });  
    6.   grunt.registerTask('default', []);  
    7. };  

Install Grunt

To install grunt in your project folder, run the below command in command line:

npm install grunt --save-dev

Appending --save-dev in any npm command, it adds installed plugin in devDependencies object of the package.json.

package.json should look like:
  1. {  
  2.   "name""sampleproject",  
  3.   "version""1.0.0",  
  4.   "description""",  
  5.   "main""Gruntfile.js",  
  6.   "scripts": {  
  7.     "test""echo \"Error: no test specified\" && exit 1"  
  8.   },  
  9.   "author""",  
  10.   "license""ISC",  
  11.   "devDependencies": {  
  12.     "grunt""^0.4.5"  
  13.   }  
  14. }  
Run Grunt command

In command line, type grunt and press enter. Below message should appear in your command line:

Done, without errors.

If above message is appearing in the command line, it means your grunt project is ready and we are good to move forward to add plugins for automation tasks in our Grunt file.

How to Add Tasks in Grunt file

Let’s start with how tasks work by adding a “test” task in our Gruntfile.js. Add below code in your Gruntfile.js:
  1. grunt.registerTask('test''My "test" task description.', function() {  
  2.    grunt.log.writeln('Currently running the "test" task.');  
  3. });  
After adding above task in Gruntfile.js, it should look like:
  1. module.exports = function(grunt) {  
  2.   
  3.   grunt.initConfig({  
  4.     pkg: grunt.file.readJSON('package.json'),  
  5.   });  
  6.   
  7.   grunt.registerTask('test''My "test" task description.', function() {  
  8.     grunt.log.writeln('Currently running the "test" task.');  
  9.   });  
  10.   
  11.   grunt.registerTask('default', []);  
  12. };  
Now type “grunt test” in your command line and below message should appear in your command line:

Let’s start with a real project. Here we will be discussing a few common tasks which we need while working in a front-end project. These tasks are: 
  • HTML file minification
  • JavaScript file minification
  • CSS file compression
  • A task to create final build

I have created a sample project with the below folder structure:

Folder

Root folder contains app, dist, node_modules, Gruntfile.js and package.json:

  • App folder is used as the development folder.
  • Dist will be used as the distribution folder where all optimized folder will be stored. Initially its blank.
  • Node_modules, Gruntfile.js and package.json are required to run Grunt command.

Now, let’s add tasks to Gruntfile.js

Remove all contents of “dist” folder

  1. Run below command in command line:

    npm install grunt-contrib-clean --save-dev

  2. Add below code in Gruntfile.js:
    1. grunt.registerTask('removeAllContents', ‘Removes all files/folders of “dist” folder’, function(){  
    2.     grunt.config('clean', {  
    3.         contents: ['dist/*']  
    4.     });  
    5.     grunt.task.run('clean');  
    6.   });  
    7.   
    8.   grunt.loadNpmTasks(‘grunt-contrib-clean’);  
  3. Run below command in command line:

    grunt removeAllContents

This will remove all contents of the “dist” folder.

Copy all folders from “app” to “dist”

  1. Run below command in command line:

    npm install grunt-contrib-copy --save-dev

  2. Add below code in Gruntfile.js:
    1. grunt.registerTask('createFolderStructure'"Copies all subdirectories to dist folder", function(){  
    2.     grunt.config('copy', {  
    3.         main: {  
    4.             expand: true,  
    5.             cwd: 'app',  
    6.             src: '**',  
    7.             dest: 'dist/',  
    8.             filter: 'isDirectory'  
    9.         }  
    10.     });  
    11.     grunt.task.run('copy');  
    12.   });  
    13.   
    14. grunt.loadNpmTasks(‘grunt-contrib-copy);  
  3. Run below command in command line:

    grunt createFolderStructure

    This command should copy css and js folders to “dist” folder without copying its inner finesl. We don’t need inner files as we need minified version of css and js files.

Minify CSS file

  1. Run below command in command line:

    npm install grunt-contrib-cssmin--save-dev

  2. Add below code in Gruntfile.js:
    1. grunt.registerTask('minifyCSSFiles'"Minifies all CSS files", function(){  
    2.       grunt.config('cssmin', {  
    3.         target: {  
    4.           files:{  
    5.             'dist/css/style.min.css': ['app/css/style.css']  
    6.           }  
    7.         }  
    8.       });  
    9.       grunt.task.run('cssmin');  
    10.   });  
    11.   
    12.   grunt.loadNpmTasks('grunt-contrib-cssmin');  
  3. Run below command in command line:

    grunt minifyCSSFiles

    This command should create a new minified CSS file named style.min.css in dist/css folder. For more on CSS Minification using Grunt, please visit official site.

Minify JavaScript file

  1. Run below command in command line:

    npm install grunt-contrib-uglify--save-dev

  2. Add below code in Gruntfile.js:"
    1. grunt.registerTask('minifyJSFiles'"Minifies all JS files", function(){  
    2.       grunt.initConfig({  
    3.         uglify: {  
    4.           my_target: {  
    5.             files: {  
    6.               'dist/js/main.min.js': ['app/js/main.js']  
    7.             }  
    8.           }  
    9.         }  
    10.       });  
    11.       grunt.task.run('uglify');  
    12.   });  
  3. Run below command in command line:

    grunt minifyJSFiles

    This command should create a new minified JS file named main.min.js in dist/js folder. For more on JS minification, please visit the official site.

Prepare index.html file

  1. Run below command in command line:

    npm install grunt-htmlcompressor--save-dev

  2. Add below code in Gruntfile.js:
    1. grunt.registerTask('minifyHTMLFiles'"Minifies all JS files", function(){  
    2.     var strData = grunt.file.read('app/index.html', {encoding: 'utf8'});  
    3.   
    4.     objReg = /css\/style.css/gi;  
    5.     strData = strData.replace(objReg, 'css/style.min.css');  
    6.   
    7.     objReg = /js\/main.js/gi;  
    8.     strData = strData.replace(objReg, 'js/main.min.js');  
    9.   
    10.     grunt.file.write('dist/index.html' , strData );  
    11.   
    12.     grunt.initConfig({  
    13.       htmlcompressor: {  
    14.         compile: {  
    15.           files: {  
    16.             'dist/index.html''dist/index.html'  
    17.           },  
    18.           options: {  
    19.             type: 'html',  
    20.             preserveServerScript: true  
    21.           }  
    22.         }  
    23.       }  
    24.     });  
    25.     grunt.task.run('htmlcompressor');  
    26.   });  
    27.   
    28. grunt.loadNpmTasks('grunt-htmlcompressor');  
  3. Run below command in command line:

    grunt minifyHTMLFiles

    This command should:

      • Replace main.js to main.min.js in the script tag of index.html.
      • Replace style.css to style.min.css in the style tag of index.html.
      • minify and create index.html in “dist” folder.

For more on “HTML minification” please visit the official site.

How to run all commands at once

To run all the tasks on one enter, we need to arrange them sequentially. Update the code in Gruntfile.js as shown below:

  1. grunt.registerTask('default', ['removeAllContents',   
  2. "createFolderStructure",   
  3. 'minifyCSSFiles',   
  4. 'minifyJSFiles',   
  5. 'minifyHTMLFiles']);  
Now run below command in command line:

grunt

This command will run all the commands sequentially and perform all the tasks to prepare your build.

To know more Grunt plugins please visit official site of Grunt plugins.
 
Read more articles on Node.js:

Up Next
    Ebook Download
    View all
    Learn
    View all