关于javascript:ASP.NET捆绑软件在部署时不起作用(debug =” false”)

ASP.NET bundle does not work when deployed (debug=“false”)

在开发中,捆绑对于未合并和未压缩的文件可以正常工作,但是在部署了具有web.config设置的站点之后,启用捆绑

1
<compilation debug="false" targetFramework="4.5" />

获得捆绑请求的结果可能在顶部包含类似于以下内容的注释

1
2
/* Minification failed. Returning unminified contents.
.. errors like JS1002 or JSxxxx errors

在其他情况下,缩小不会引发任何错误,但某些javascript无法运行或在执行过程中出错。

捆绑后,可以正常工作的JavaScript中的哪种语法可能导致此行为?


可能导致这种情况的一种情况是单行注释//作为javascript文件的最后一行。这将导致添加的下一个文件至少具有第一行也已注释掉

例如,如果您有捆绑包

1
2
3
bundles.Add(New ScriptBundle("~/bundles/test").Include(
           "~/Scripts/adder.js",
           "~/Scripts/printer.js"))

adder.js

1
2
3
4
function adder(a, b) {
    return a + b;
}
//this is the adder.js

printer.js

1
2
3
4
5
6
printer = true;

if (printer) {
    alert("It works");
    document.getElementById("itWorked").textContent ="It worked";    
}