System.Web.Optimization.JsMinify生成无效的JavaScript [英] System.Web.Optimization.JsMinify produces invalid JavaScript

查看:64
本文介绍了System.Web.Optimization.JsMinify生成无效的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我认为是标准配置的情况下使用 System.Web.Optimization v1.3来捆绑和缩小MVC 5 Web应用程序中的JavaScript和CSS文件.在大多数情况下,此方法效果很好,但是我发现一种情况是JavaScript的缩小过程损坏了.

I am using System.Web.Optimization v1.3 in what I believe is a standard configuration, to bundle and minify the JavaScript and CSS files in an MVC 5 web application. For the most part, this works very well, but I have discovered a case where JavaScript is corrupted by the minification process.

这是最初AngularJS输入验证指令的简化版本:

Here is a simplified version of what was originally an AngularJS input validation directive:

var myApp;
(function (myApp) {
    myApp.module.directive("validator", [
        function () {
            return {
                link: function (scope, element) {
                    var validators = [
                        { signal: "required", message: "Please enter a value", value: null },
                        { signal: "email", message: "Please enter a valid email address", value: null }
                    ];
                    $("input", element).on("blur", function () {
                        for (var i in validators) {
                            var validator = validators[i];
                            if (scope.$parent.form[scope.modelName].$error[validator.signal]) {
                                element.removeClass("has-success");
                                scope.errorMessage = myApp.Utility.formatString(validator.message, eval(validator.value));
                                break;
                            }
                        }
                    });
                }
            };
        }
    ]);
})(myApp || (myApp = {}));

尽管上面的代码不再有任何用处(因为已被修剪),但它确实演示了缩小问题.缩小后,生成的JavaScript如下:

Although the above code no longer does anything useful (because it has been trimmed), it does demonstrate the minification problem. When minified, the resulting JavaScript is as follows:

var myApp;

function(n){
    n.module.directive("validator",[
        function(){
            return{
                link:function(t,i){
                    var r=[
                        {signal:"required",message:"Please enter a value",value:null},
                        {signal:"email",message:"Please enter a valid email address",value:null}
                    ];

                    $("input",i).on("blur",function(){
                        var i,
                        validator;

                        for(i in r)
                            if(validator=r[i],t.$parent.form[t.modelName].$error[validator.signal]){
                                i.removeClass("has-success");
                                t.errorMessage=n.Utility.formatString(validator.message,eval(validator.value));
                                break
                            }

                    })
                }
            }
        }
    ])
})(myApp||(myApp={}))

请注意,尽管循环变量是事实,但minification如何将参数名称 t i 分配给 link 函数我在原始代码中使用了.

Note how minification has assigned the parameter names t and i to the link function, despite the fact that a loop variable i is used in the original code.

不用说,这会破坏代码.在这种情况下,我可以通过重命名循环变量来解决该问题,但是我担心可能会导致我不知道的 JsMinify 缩小问题产生其他不利后果.

Needless to say, this breaks the code. In this case I can fix it by renaming my loop variable, but I am concerned that there may be other adverse consequences to JsMinify's minification that I am not aware of.

因此,我有3个与此问题相关的问题:

So, I have 3 questions related to this issue:

  • 我正确地认为这是一个缩小错误,如果是的话,在某个地方我应该报告它?
  • 在我的压缩代码中是否有任何实用的方法来查找此问题的其他实例?
  • 是否可以替换由所使用的JavaScript缩小引擎 System.Web.Optimization ,如果这样的话,那将是一个很好的选择替代方案?
  • Am I right to assume that this is a minification bug and, if so, is there somewhere I should report it?
  • Is there any practical way of finding any other instances of this issue in my minified code?
  • Is it possible to replace the JavaScript minification engine used by System.Web.Optimization and, if so, what would be a good alternative?

在此先感谢您的想法.蒂姆

Many thanks, in advance, for your ideas. Tim

更新:经过进一步调查,我发现实际上是 WebGrease 代表 System.Web.Optimization

Update: After some further investigation, I have discovered that it is actually WebGrease that performs minification on behalf of System.Web.Optimization and this issue appears to be the one that I am seeing. This seems to answer my first question, but I would still appreciate advice regarding alternative minifiers.

推荐答案

过去,我已经成功地将 YUI Compressor用于.Net (现在已移至GitHub ),它是Java的.NET端口的YUI Compressor.很棒的是,它基于Mozilla的Rhino JavaScript解释器,这意味着它实际上可以理解代码,而不仅仅是在其上运行正则表达式.因此,如果您遇到JavaScript语法错误,它将使您的构建失败.

In the past, I have successfully used YUI Compressor for .Net (now moved to GitHub), which is a .NET port from Java of YUI Compressor. The great thing about it is that it's based on Mozilla's Rhino JavaScript interpreter, meaning it actually understands the code, and not just runs regular expressions on it. As a result, it will fail your build if you have JavaScript syntax errors.

这篇关于System.Web.Optimization.JsMinify生成无效的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆