I am here to continue the discussion around AngularJS. Today, we will discuss how to perform automatic workflow for AngularJS projects using GRUNT. Also in case you have not had a look at our previous articles of this series, go through the following links:
- Learn AngularJS From Beginning: Basic - Part One
- Learn AngularJS From Beginning: Filter - Part Two
- Learn AngularJS From Beginning: Service - Part Three
- Learn AngularJS From Beginning: Directive - Part Four
- Learn AngularJS From Beginning: Form Data Binding - Part Five
- Learn AngularJS From Beginning: Global Object Service - Part Six
- Learn AngularJS From Beginning: Http Request or Ajax - Part Seven
- Learn AngularJS From Beginning: Rest API - Part Eight
- Learn AngularJS From Beginning: Route Url - Part Nine
- Learn AngularJS From Beginning: Animation - Part Ten
- Learn AngularJS From Beginning: Injection - Part Eleven
- Learn AngularJS From Beginning: Localisation - Part Twelve
- Learn AngularJS From Beginning: Unit Test of AngularJS Controller - Part Thirteen
- Learn AngularJS From Beginning: Unit Test of AngularJS Component - Part Fourteen
In this article, we will discuss how to setup and deploy an angular project.
Every time we perform the same activity over and over again, it can be considered waste, and we stand to lose a great opportunity to automate it. Depending on the complexity of the workflow of our product, there are many steps that have to be performed, such as cleaning the temporary files from the last distribution, validating the code, concatenating and minifying the JavaScript files, copying resources such as images and configuration files, running the tests, and many others. Also, based on the environment, the workflow could be very different—development, staging, or production.
Automating the workflow with Grunt
Grunt is a JavaScript task runner that automates repetitive and boring tasks. Also, it can group the desired tasks in any sequence, thus creating a workflow. It works over plugins, and there is a huge ecosystem with thousands of choices just waiting to be downloaded! We also have the opportunity to create our own plugins and share them within the community.
Installation
Just like Karma, Grunt requires NodeJS and the Node Package Manager to be installed. To proceed with the installation, we just need to type in the following command:
- npm install -g grunt-cli grunt
After this, we are ready to configure it!
Configuration
The Gruntfile.js file is the JavaScript file that defines the configuration of each task, plugin, and workflow. Let's take a look at its basic structure:
- module.exports = function(grunt)
- {
- // 1 – Configuring each task
- grunt.initConfig({});
- // 2 - Loading the plug-ins
- grunt.loadNpmTasks('plugin name');
- // 3 - Creating a workflow by grouping tasks together
- grunt.registerTask('taskName', ['task1', 'task2', 'task3']);
- }
- npm init
After this, you are ready to create your distribution package!
Creating a distribution package
Before being ready for the production environment, you need to create an optimized distribution package of your product. This is very important for the following reasons:
- Performance
Through the concatenation step, you could drastically reduce the amount of requests that the browser needs to perform every time the application is loaded. All the scripts of the application are concatenated in just one file. After that, the minifying step removes all the white spaces, line breaks, and comments, and replaces the names of the local variables and functions and makes them shorter than the original. It contributes to improving the performance by reducing the amount of bytes that need to be transferred. Also, the HTML and CSS files can be minified, and the images can be optimized by specific processors. Grunt has a lot of plugins for performing its tasks. - Security
By removing the white spaces, line breaks, comments, and replacing the local variable names, the JavaScript files become much harder to understand. However, take care before submitting an AngularJS application to this kind of process. As we saw in dependency injection section, we should apply the array notation for the dependency injection mechanism by declaring each dependency as a string; otherwise, the framework will not find the expected dependency, throwing an error. - Quality
There are two steps that improve the quality of the distribution. The first one is the validating step. With tools such as JSLint or JSHint, the code is validated against rules that verify things such as the absence of semicolons, wrong indentation, undeclared or unused variables and functions, and many others. Also, the tests are executed, preventing the process from proceeding in the case of errors.
In order to follow each step of our workflow, we are now going to discover how to install and configure each plugin:
Step 1 - Cleaning step
The first step is about cleaning the files that were created in the last distribution. This can be done through the grunt-contrib-clean plugin. Take care about which directory will be configured, avoiding any accidental deletion of the wrong files. To install this plugin, type in the following command:
npm install grunt-contrib-clean --save-dev
After this, we just need to configure it inside the Gruntfile.js file, as follows:
- module.exports = function(grunt)
- {
- grunt.initConfig
- ({
- clean:
- {
- dist: ["dist/"]
- }
- });
- grunt.loadNpmTasks("grunt-contrib-clean");
- grunt.registerTask("dist", ["clean"]);
- }
Step 2 - Validating step
Now, it's time to configure the validation step. There is a plugin called grunt-contrib-jshint that can be installed with the following command:
npm install grunt-contrib-jshint --save-dev
Next, we need to configure it inside our Gruntfile.js file as follows:
- module.exports = function(grunt)
- {
- grunt.initConfig
- ({
- clean:
- {
- dist: ["dist/"]
- },
- jshint:
- {
- all: ['Gruntfile.js', 'js/**/*.js', 'test/**/*.js']
- }
- });
- grunt.loadNpmTasks("grunt-contrib-clean");
- grunt.loadNpmTasks("grunt-contrib-jshint");
- grunt.registerTask("dist", ["clean", "jshint"]);
- }

Vignesh ManiPosted May 25, 2016, 8:27 AM
Nice
Pritam ZopePosted May 25, 2016, 5:24 AM
Nice...........................................!
Thiruppathi RPosted May 25, 2016, 4:33 AM
Nice Sharing..
Gowtham RajamanickamPosted May 25, 2016, 1:15 AM
Good one..
Munesh SharmaPosted May 25, 2016, 12:27 AM
good one
Sonu ChaudharyPosted May 24, 2016, 12:47 PM
Nice Sharing..
Kuppurasu NagarajPosted May 24, 2016, 12:05 PM
Nice Sharing..