{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/minificates-angularjs-with-less-worries/","result":{"data":{"site":{"siteMetadata":{"title":"Manuel Rueda Blog","author":"Manuel Rueda","keywords":["Manuel Rueda","development","software","JavaScript","js","Node.js","Typescript","Argentina","Buenos Aires","Ann Arbor","Michigan","United States"]}},"allMarkdownRemark":{"edges":[{"node":{"id":"1d141cb5-7e95-5548-bb9c-360863a5c086","excerpt":"DISCLAMER DATE: February 2019 This is a blog entry that I wrote before I change my blog, most of it may be old and probably outdated. The pain! We (the web devs…","html":"<blockquote>\n<p><strong>DISCLAMER</strong></p>\n<p><strong>DATE: February 2019</strong></p>\n<p><strong>This is a blog entry that I wrote before I change my blog, most of it may be old and probably outdated.</strong></p>\n</blockquote>\n<h2>The pain!</h2>\n<p>We <em>(the web devs at least)</em> know the fears and how careful we must be when we code to avoid error in the minified version of the code.</p>\n<p>You forget a semicolon and half of a day goes into an endless search of the point of the error. With time and practice, this half day can be reduce to minutes, but not all the developers have that experience and less when must work with teams with different backgrounds.</p>\n<p>To that, there is no so much to do… keep being careful! (There are linters and stuff…)</p>\n<h2>A new angle of pain</h2>\n<p>With angular.js and his dependency injection <em>(DI for now on)</em>, a new things came to bother us.</p>\n<p>The most widespread way of use the DI is the simplest way but this it is impossible to minify.\nLets see this example:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">angular\n  <span class=\"token punctuation\">.</span><span class=\"token function\">module</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SuperModule'</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">.</span><span class=\"token function\">controller</span><span class=\"token punctuation\">(</span><span class=\"token string\">'AwesomeCrtl'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">$scope<span class=\"token punctuation\">,</span> $timeout</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>This code is clean and nice but there is a problem. angular.js, to execute DI, parses the function as a string to extract the parameter’s name and use them to import the services, values, etc.\nWhen you minify the code, the names are replaced with letters and that names have no meaning to angular.js.</p>\n<p>To perform a minification to the code you must use the DI with one of the following method.</p>\n<h4>Dependencies Array</h4>\n<p>This way it’s the most spreaded in tutorials and blogs. It’s simple to understand, but it’s hard to read and follow when there is a lot of injections.\nThe idea is to put the dependencies’s names as string in the array and then the function.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">angular\n  <span class=\"token punctuation\">.</span><span class=\"token function\">module</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SuperModule'</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">.</span><span class=\"token function\">controller</span><span class=\"token punctuation\">(</span><span class=\"token string\">'AwesomeCrtl'</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'$scope'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'$timeout'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">$scope<span class=\"token punctuation\">,</span> $timeout</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h4>The $inject</h4>\n<p>This method uses a specific property of the controller’s function to specify, as an array of string, the names of the dependencies. Works with the same fundaments of the dependencies array, but it’s most clean.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">AwesomeCrtl</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">$scope<span class=\"token punctuation\">,</span> $timeout</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\nAwesomeCrtl<span class=\"token punctuation\">.</span>$inject <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'$scope'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'$timeout'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\nangular<span class=\"token punctuation\">.</span><span class=\"token function\">module</span><span class=\"token punctuation\">(</span><span class=\"token string\">'SuperModule'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">controller</span><span class=\"token punctuation\">(</span><span class=\"token string\">'AwesomeCrtl'</span><span class=\"token punctuation\">,</span> AwesomeCrtl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2>Share the pain</h2>\n<p>When you have to manage a work team with different backgrounds, as i mentioned before, this kind of thing can create a problem. Some developers forget this rules and send code to CI that breaks everything.</p>\n<p>My first attempt to resolve that, was an awareness meeting of the subject. Worked for some but others keep breaking the CI environment.</p>\n<h4>My (almost) solution</h4>\n<p>Last week the solution came to me in a JavaScript news feed. A module, <a href=\"https://github.com/olov/ng-annotate\">ng-annotate</a> is a processor of angular.js code that searches for unsecure DI functions and replace it with the dependencies array method.</p>\n<p>This module doesn’t solve all the problems, can detect a lot of DI examples but there are some very weird ones that escape from it.\nStill is an excellent idea implement it in your development flow.</p>\n<p>There is plugins for the most common task runners and asset pipeline:</p>\n<ul>\n<li>Grunt: <a href=\"https://www.npmjs.com/package/grunt-ng-annotate\">grunt-ng-annotate</a></li>\n<li>Gulp: <a href=\"https://www.npmjs.com/package/gulp-ng-annotate/\">gulp-ng-annotate</a></li>\n<li>Broccoli: <a href=\"https://www.npmjs.com/package/broccoli-ng-annotate\">broccoli-ng-annotate</a></li>\n</ul>\n<p>There is more… check the GitHub page of ng-annotate.</p>\n<h2>A gift to test it</h2>\n<p>This week i made a very small implementation of this module with a browserified version of it.\nYou can put you angular.js code and view how will be the DI output of ng-annotate.</p>\n<h4>Check it at : <a href=\"http://manrueda.github.io/ng-annotate-online/\">ng-annotate-online</a></h4>","timeToRead":3,"fields":{"slug":"/blog/minificates-angularjs-with-less-worries/"},"frontmatter":{"lang":"en","title":"Minificates angular.js with less worries","date":"2015-11-20T00:18:41.505+00:00","keywords":[],"draft":null,"styles":null}}}]}},"pageContext":{"slug":"/blog/minificates-angularjs-with-less-worries/","uniqueSlug":"/blog/minificates-angularjs-with-less-worries","previous":{"fields":{"slug":"/blog/update-without-thinking/","uniqueSlug":"/blog/update-without-thinking"},"frontmatter":{"lang":"en","title":"Update without thinking"}},"next":{"fields":{"slug":"/blog/typescript-complex-types/","uniqueSlug":"/blog/typescript-complex-types"},"frontmatter":{"lang":"en","title":"TypeScript Complex Types"}}}}}