简体   繁体   中英

Is there a way to prevent some variables being renamed via asp.net System.Web.Optimization

I'm using ASP.Net bundling & minification ( System.Web.Optimization ) in an AngularJS project.

Everything works perfectly well apart from one scenario.

We have to, at this time, eval() an expression. We know it's not ideal though at this time there isn't another solution to fit our needs.

So we have this line:

var isValid = eval('globalJson.Permissions.' + attrs.drHasPermissionFor);

Now, because minification has changed the name of our variables etc globalJson doesn't exist, so this line fails.

Is there a way to instruct ASP.Net Minification to not rename specific variables?

Update

Following on from the answer given - we already to use Angulars safe way to inject.

The directive where this is being used, looks like this:

    appDirectives.directive('drHasPermissionFor', ['globalJson', function (globalJson) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                //code .....

                // how can I reference the injected globalJson here - so that i know it's using the
                // newly minified name..
                var isValid = eval('globalJson.Permissions.' + attrs.drHasPermissionFor);

//more code......
            }
        };
    }]);

Ok - so a colleague came up with a solution to this particular issue.

It doesn't really answer the overall "can you tell optimization to leave certain variables alone" question, though it fixes the issue at hand.

Should the initial question be answered i will gladly change the accepted answer.

For now, this can be hacked-around like so:

appDirectives.directive('drHasPermissionFor', ['globalJson', function (globalJson) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {

            // this function will return the minified globalJson
            function returnGlobalJson() {
                return globalJson;
            };

            // call this function as part of the eval - works perfectly :)
            var isValid = eval('returnGlobalJson().Permissions.' + attrs.drHasPermissionFor);

        }
    };
}]);

Angular has a mechanism for dealing with minification this way. You have to use something called inline annotation, as described here .

This works by allowing the minifier to minify the annotation, and then angular will use the annotation to determine what the minified variable name is.

So, you can just inject the variable you want to use and use the minified variable.

There's also a good article here:

http://thegreenpizza.github.io/2013/05/25/building-minification-safe-angular.js-applications/

I am using BuildBundlerMinifier and the solution to use a function did not work since the function was also minified.

The solution I used was to copy the variable to this and use this.value in the eval, then copy the value back to the variable afterwards.

Still a workaround, but might help someone else...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM