AngularJS Essentials
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
[ 147 ]<br />
Chapter 8<br />
Creating a distribution package<br />
Before being ready for the production environment, you need to create an optimized<br />
distribution package of your product. This is very important for the following reasons:<br />
• Performance: Through the concatenation step, you could drastically reduce<br />
the amount of requests that the browser needs to perform every time the<br />
application is loaded. All the scripts of the application are concatenated in<br />
just one file. After that, the minifying step removes all the white spaces,<br />
line breaks, and comments, and replaces the names of the local variables<br />
and functions and makes them shorter than the original. It contributes to<br />
improving the performance by reducing the amount of bytes that need to be<br />
transferred. Also, the HTML and CSS files can be minified, and the images<br />
can be optimized by specific processors. Grunt has a lot of plugins for<br />
performing its tasks.<br />
• Security: By removing the white spaces, line breaks, comments, and<br />
replacing the local variable names, the JavaScript files become much<br />
harder to understand. However, take care before submitting an <strong>AngularJS</strong><br />
application to this kind of process. As we saw in Chapter 4, Dependency<br />
Injection and Services, we should apply the array notation for the dependency<br />
injection mechanism by declaring each dependency as a string; otherwise, the<br />
framework will not find the expected dependency, throwing an error.<br />
• Quality: There are two steps that improve the quality of the distribution. The<br />
first one is the validating step. With tools such as JSLint or JSHint, the code<br />
is validated against rules that verify things such as the absence of semicolons,<br />
wrong indentation, undeclared or unused variables and functions, and many<br />
others. Also, the tests are executed, preventing the process from proceeding<br />
in the case of errors.<br />
In order to follow each step of our workflow, we are now going to discover how to<br />
install and configure each plugin:<br />
1. Cleaning step: The first step is about cleaning the files that were created in<br />
the last distribution. This can be done through the grunt-contrib-clean<br />
plugin. Take care about which directory will be configured, avoiding any<br />
accidental deletion of the wrong files. To install this plugin, type in the<br />
following command:<br />
npm install grunt-contrib-clean --save-dev<br />
After this, we just need to configure it inside the Gruntfile.js file,<br />
as follows:<br />
Gruntfile.js<br />
module.exports = function (grunt) {<br />
www.it-ebooks.info