Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minifying angular

I have this interest in automate/simplify angular project with a compiler tool, which might work on everything else, but angular inject and namespacing is awkward enough to escape compiler knowledge. What is the best/professional method for doing this?

thanks, just one last thing,

app.controller('ctrl',['$rootScope',function($rootScope){
    ...
}]);

works when minified, but how do I minify

app.config(['$routeProvider', function($routeProvider){

}]);

and does it work when I minify successive actions?

app.controller(...).directive(...).run(...)
like image 856
user2167582 Avatar asked Mar 29 '13 19:03

user2167582


People also ask

What is the use of Minifying?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

Is Minifying CSS worth it?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

What does Minifying CSS mean?

What is CSS minification? CSS minification is the process of removing unneeded code from CSS source files, with the goal of reducing file size without changing how the CSS file executes in the browser.

How much does Minifying help?

In fact, minifying code can reduce the file size by 30-40%. Sometimes even as much as 50%. Concatenating files also helps reduce the load on your server and network. Combining multiple files into one lets a server send more data in a fewer number of connections.


1 Answers

In Angular, you need to annotate functions for the injector to know which dependencies you want to inject in your function. There are basically three ways to inject dependencies in your function which are being described on official angular website. The three ways are:

1.Use the inline array annotation

 yourModule.controller('yourController', ['$scope', function($scope) {}]);   

2.Use the $inject property annotation

var yourController = function($scope) {};

yourController.$inject = ['$scope'];

yourModule.controller('yourController', yourController);

3.Implictly from the function parameter names

yourModule.controller('yourController', function($scope) {});

Now when you minify your project, your dependencies names will get renamed. In first case your code will be like

yourModule.controller('yourController', ['$scope', function(e) {}]); 

In third case your code will be like

yourModule.controller('yourController',  function(e) {});

It will break your app because angular has no way to recognize your dependency name. So it is advised never to use implicit dependency injection in your project. From the above two inline array annotation is the most popular way amongst programmers.

like image 113
Himanshu Mittal Avatar answered Nov 16 '22 01:11

Himanshu Mittal