0

Using the ASP.NET Web Optimization Framework, I am attempting to load some javascript files up. It works fine, except I am running into a peculiar situation with either the loading order, the loading speed, or its execution. I cannot figure out which.

Basically, I am using ace code editor for javascript, and I also want to include its autocompletion package. This requires two files.

/ace.js

/ext-language_tools.js

This isn't an issue, if I load both of these files the normal way (with <script> tags) it works fine. But when I try to use the web optimization bundles, it seems as if something goes wrong.

Trying this out...

bundles.Add(new ScriptBundle("~/bundles/js") {
   .Include("~/js/ace.js")
   .Include("~/js/ext-language_tools.js")
});

and then in the view ..

@Scripts.Render("~/bundles/js")

I get the error

ace is not defined

This means that the ace.js file hasn't run, or hasn't loaded. Because if I break it apart into two bundles, it starts working.

bundles.Add(new ScriptBundle("~/bundles/js") {
   .Include("~/js/ace.js")
});

bundles.Add(new ScriptBundle("~/bundles/js/language_tools") {
   .Include("~/js/ext-language_tools.js")
});

Can anyone explain why this would behave in this fashion?

4

1 回答 1

1

这在MSDN 杂志:CSS 编程:捆绑和缩小(滚动到更高级捆绑功能部分)中得到了很好的解释。

摘录(我加粗了一些文字):

特别是,BundleCollection类有几个值得一提的特性,尽管它们在捆绑脚本文件(而不是 CSS 文件)时最有用。

第一个功能是排序。BundleCollection 类有一个名为 Orderer 的 IBundleOrderer 类型的属性。看起来很明显,orderer 是一个组件,负责确定您希望捆绑下载文件的实际顺序。默认排序器是DefaultBundleOrderer班级。该类按照通过 FileSetOrderList(BundleCollection 的一个属性)设置的设置的顺序捆绑文件。FileSetOrderList 设计为 BundleFileSetOrdering 类的集合。这些类中的每一个都为文件定义了一个模式(例如,jquery-*),并且将 BundleFileSetOrdering 类添加到 FileSetOrderList 的顺序决定了文件的实际顺序。例如,给定默认配置,所有 jQuery 文件总是在 Modernizr 文件之前捆绑。

DefaultBundleOrderer类的来源在这里

然而,特别令人感兴趣的是BundleCollection类的AddDefaultFileOrderings函数(下面的屏幕截图)。

捆绑集合

现在,回到您的问题,在您的捆绑包中,您拥有ace.jsext-language_tools.js. 后一个文件匹配ext-*,因此将始终包含在您之前捆绑包中文件列表的顶部ace.js

解决方案:

这已经在这里回答了。

MSDN 杂志(顶部的链接)也给出了相同的解决方案。

那里描述的另一种方法是“使用以下代码重置所有排序”:

    bundles.ResetAll();

“在这种情况下,使用前面显示的默认排序器或穷人排序器的效果是相同的。但请注意,ResetAll 也会重置脚本排序。”

于 2014-08-22T19:25:21.207 回答