如何指定显式的 ScriptBundle 包含顺序? [英] How can I specify an explicit ScriptBundle include order?
问题描述
我正在尝试 MVC4 System.Web.Optimization1.0 ScriptBundle 功能.
我有以下配置:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
// shared scripts
Bundle canvasScripts =
new ScriptBundle(BundlePaths.CanvasScripts)
.Include("~/Scripts/modernizr-*")
.Include("~/Scripts/json2.js")
.Include("~/Scripts/columnizer.js")
.Include("~/Scripts/jquery.ui.message.min.js")
.Include("~/Scripts/Shared/achievements.js")
.Include("~/Scripts/Shared/canvas.js");
bundles.Add(canvasScripts);
}
}
以及以下视图:
<script type="text/javascript" src="@Scripts.Url(BundlePaths.CanvasScripts)"></script>
其中 BundlePaths.CanvasScripts
是 "~/bundles/scripts/canvas"
.它呈现这个:
where BundlePaths.CanvasScripts
is "~/bundles/scripts/canvas"
. It renders this:
<script type="text/javascript" src="/bundles/scripts/canvas?v=UTH3XqH0UXWjJzi-gtX03eU183BJNpFNg8anioG14_41"></script>
到目前为止一切顺利,除了 ~/Scripts/Shared/achievements.js
是捆绑源中的第一个脚本.它取决于 ScriptBundle
中包含在它之前的每个脚本.如何确保它遵守我向包中添加包含语句的顺序?
So far so good, except ~/Scripts/Shared/achievements.js
is the first script in the bundled source. It depends on every script included before it in the ScriptBundle
. How can I ensure that it honors the order in which I add include statements to the bundle?
更新
这是一个相对较新的 ASP.NET MVC 4 应用程序,但它引用了优化框架预发布包.我删除了它并从 http://nuget.org/packages/Microsoft 添加了 RTM 包.AspNet.Web.优化.对于 web.config 中带有 debug=true 的 RTM 版本,@Scripts.Render("~/bundles/scripts/canvas")
以正确的顺序呈现各个脚本标签.
This was a relatively new ASP.NET MVC 4 application, but it was referencing the optimization framework pre release package. I removed it and added the RTM package from http://nuget.org/packages/Microsoft.AspNet.Web.Optimization. With the RTM version with debug=true in web.config, @Scripts.Render("~/bundles/scripts/canvas")
renders the individual script tags in the correct order.
在 web.config 中使用 debug=false 时,组合脚本首先有成就.js 脚本,但由于它是稍后调用的函数定义(对象构造函数),因此它运行没有错误.也许 minifier 足够聪明,可以找出依赖关系?
With debug=false in web.config, the combined script has the achievements.js script first, but since its a function definition (object constructor) that's called later, it runs without error. Perhaps the minifier is smart enough to figure out dependencies?
我还尝试了 Darin Dimitrov 建议使用带有两个调试选项的 RTM 的 IBundleOrderer
实现,并且它的行为相同.
I also tried the IBundleOrderer
implementation that Darin Dimitrov suggested with RTM with both debug options and it behaved the same.
所以缩小版本不是我期望的顺序,但它可以工作.
So the minified version is not in the order I expect, but it works.
推荐答案
我在 RTM 位上没有看到这种行为,您是否使用 Microsoft ASP.NET Web Optimization Framework 1.0.0 位:http://nuget.org/packages/Microsoft.AspNet.Web.Optimization ?
I'm not seeing this behavior on the RTM bits, are you using the Microsoft ASP.NET Web Optimization Framework 1.0.0 bits: http://nuget.org/packages/Microsoft.AspNet.Web.Optimization ?
我根据一个新的 MVC4 Internet 应用程序网站对您的示例使用了类似的复制.
I used a similar repro to your sample, based off of a new MVC4 Internet application website.
我添加到 BundleConfig.RegisterBundles:
I added to BundleConfig.RegisterBundles:
Bundle canvasScripts =
new ScriptBundle("~/bundles/scripts/canvas")
.Include("~/Scripts/modernizr-*")
.Include("~/Scripts/Shared/achievements.js")
.Include("~/Scripts/Shared/canvas.js");
bundles.Add(canvasScripts);
然后在默认的索引页面中,我添加了:
And then in the default index page, I added:
<script src="@Scripts.Url("~/bundles/scripts/canvas")"></script>
并且我验证了在包的缩小后的 javascript 中,成就.js 的内容是在 Modernizr 之后...
And I verified that in the minified javascript for the bundle, the contents of achievements.js was after modernizr...
这篇关于如何指定显式的 ScriptBundle 包含顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!