Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc4 bundling, minification and AngularJS services

Is there a way to customize the way Asp.Net MVC4 bundling&minification feature minifies the js files?

Meaning, I don't want to completely turn off minification, but "as is" it just breaks AngularJs.

Since AngularJs uses DI and IoC approach for injecting services in controllers, the following:

function MyController($scope) { }

Once minified, becomes:

function MyController(n) { }

Normally that wouldn't be a problem, but AngularJs uses the parameter names to understand which service to inject. So $scope should remain $scope, as well as any other parameter in angular controllers. Everything else, like local variables, etc, should be minified normally.

I can't find any clear documentation on how to configure Mvc4 minification, and it seems rather dumb for it to be "all or nothing" so I think I'm missing something.

Thanks.

like image 385
Matteo Mosca Avatar asked Feb 16 '13 11:02

Matteo Mosca


People also ask

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.

Can we use bundling and minification with ASP NET web forms like MVC?

To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.

How bundling and minification works in MVC?

Bundling and Minification are two performance improvement techniques that improves the request load time of the application. Most of the current major browsers limit the number of simultaneous connections per hostname to six. It means that at a time, all the additional requests will be queued by the browser.

What are the two types of bundles in MVC 5?

Bundle TypesScriptBundle: ScriptBundle is responsible for JavaScript minification of single or multiple script files. StyleBundle: StyleBundle is responsible for CSS minification of single or multiple style sheet files.


2 Answers

Actually you can (and should!) write AngularJS code so it is "minification safe". Details are described in the "Dependency Annotation" section of http://docs.angularjs.org/guide/di but in short, for globally defined controllers you can write:

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

Please note that globally defined controllers are polluting global namespace (see this for more details) and should be avoided. If you declare a controller on a module level you can make it minification-safe as well:

angular.module('mymodule', []).controller('MyController', ['$scope', function($scope){
//controller code goes here
}]);
like image 77
pkozlowski.opensource Avatar answered Oct 22 '22 00:10

pkozlowski.opensource


if you still want to control what to minify and what not (or if you want to include an already minified version by the plugin vendor) just declare two bundles, and only minify one of them on your BundleConfig.cs:

var dontMinify = new Bundle("~/bundles/toNotMinify").Include(
                        "~/Scripts/xxxxx.js");
bundles.Add(dontMinify);

var minify = new Bundle("~/bundles/toNotMinify").Include(
                        "~/Scripts/yyyyyy.js");
minify.Transforms.Add(new JsMinify());
bundles.Add(minify);
like image 1
amhed Avatar answered Oct 21 '22 22:10

amhed