ASP.NET捆绑-忽略第二个JS文件 [英] ASP.NET Bundling - Ignoring second JS file
问题描述
我已经使用ASP.NET的捆绑和缩小功能进行了简单的测试。我有两个非常简单的js文件:
I have set up a simple test using ASP.NET's bundling and minification feature. I have two very simple js files:
〜/ JS / Site.js
(function echo1() {
alert("this is site.js");
})();
〜/ JS / test.js
(function echo2(value) {
alert("and this is test.js");
})();
我创建了一个捆绑软件,如下所示:
I've created a bundle as follows:
bundles.Add(new ScriptBundle("~/bundles/scripts/site-globals").Include(
"~/JS/Site.js",
"~/JS/test.js"));
,并使用 @ Scripts.Render(〜 / bundles / scripts / site-globals)
当我在调试下运行站点时(web.config中的< compilation debug = true targetFramework = 4.5 />
)我得到了预期的结果-两个警报框一个接一个地显示。
When I run the site under debug (<compilation debug="true" targetFramework="4.5" />
in web.config) I get the expected result - two alert boxes show one after the other.
但是,当我运行更改以发布(< compilation debug = false targetFramework = 4.5 />
)-我只看到第一个警报框,并且发送到浏览器的合并JS文件已完全忽略了test.js的内容。
However, when I run change to release (<compilation debug="false" targetFramework="4.5" />
) - I only see the first alert box, and the combined JS file that gets sent to the browser has completely ignored the content of test.js.
合并和缩小的输出如下,并且错误地仅包含Site.js中的代码:
The "combined and minified" output is as below, and wrongly only includes the code from Site.js:
(function(){alert("this is site.js")})()
任何关于为什么发生这种情况的想法将不胜感激!谢谢
Any ideas on why this is happening would be much appreciated! Thanks
推荐答案
我发现了导致此问题的原因。如果任何javascript文件的最后一行都带有注释,则它们将被组合在一起而没有换行符,从而导致下一个文件的第一行被注释掉。
I've found out what was causing this problem. If any of the javascript files have a comment as the last line, they will be combined together without a newline, causing the first line of the next file to be commented out.
这里是指向堆栈溢出的另一个问题的链接,它证明了这一点: https: //stackoverflow.com/a/14223945/1145963 1
Here's a link to another question on stack overflow which demonstrates this: https://stackoverflow.com/a/14223945/11459631
就我而言,我正在使用 Web Essentials Visual Studio插件来缩小我的JavaScript文件。它正在为每个JS文件创建一个.js.map文件,并且在每个.min.js文件的末尾都是这样的注释行,原来是引起问题的原因:
In my case, I was using the Web Essentials Visual Studio plugin to minify my javascript files. It was creating a .js.map file for each JS file, and at the end of each .min.js file was a commented line like this, which turned out to be causing the problem:
//@ sourceMappingURL=somefile.min.js.map
由于我不需要映射文件,因此使用 Options->关闭了此功能。 Web Essentials-> Javascript->将生成源地图(.map)设置为false
Since I didn't need the mapping files, I turned this feature off using Options -> Web Essentials -> Javascript -> Set Generate source maps (.map) to false
希望这对发现此问题的人有所帮助!
Hope this helps anyone who finds this problem!
这篇关于ASP.NET捆绑-忽略第二个JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!