System.Web.Optimization making function argument names stay the same for certain functions

寵の児 提交于 2019-11-30 05:40:46

问题


With ASP.NET Bundling with a ScriptBundle

function StartController($scope, $location, $rootScope) {}

is transformed to

function StartController(n,t,i) {}

However, as I am using AngularJs, for dependency injection to still work, the argument names must not be changed when minified. How can I ensure $scope, $location and $rootScope keep these names with the minification through a ScriptBundle, but allow argument renaming in other places?


回答1:


This isn't something you can change on the built in bundle types, as we currently don't expose any knobs that you can tweak on the underlying transform classes. The best way to accomplish this is to write your own IBundleTransform which does minification passing in the appropriate settings to not rename variables.

I.e. something like:

public class CustomTransform : IBundleTransform {
    public void process(BundleContext context, BundleResponse response) {
         response.Content = MyMinifier.MinifyWithoutRename(response.Content);
    }
}

BundleTable.Bundles.Add(new Bundle("~/bundles/mybundle", new CustomTransform());



回答2:


This works with System.Web.Optimizations nuget package 1.1

https://gist.github.com/zaus/7436601

(I've included a couple other concepts from along the way)

Essentially, you need to write a new BundleTransform/Minifier that exposes the CodeSettings so you can change the NoAutoRenameCollection.




回答3:


Angular provides a way to deal with minification. If you are defining a controller you can rewrite your code as:

YOUR_APP_MODULE.controller('CONTROLLER_NAME', ['$scope', '$location', '$rootScope', function($scope, $location, $rootScope){
    // DO STUFF
}]);

On Minification, this will become:

YOUR_APP_MODULE.controller('CONTROLLER_NAME', ['$scope', '$location', '$rootScope', function(n, t, i){
}]);

You can do similar stuff with other angular components also.



来源:https://stackoverflow.com/questions/13032721/system-web-optimization-making-function-argument-names-stay-the-same-for-certain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!