是否有任何理由在 BundleConfig 中有多个 ScriptBundle 包?

Is there any reason to have multiple ScriptBundle bundles in BundleConfig?

这里,默认的MVC5 App BundleConfig:

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));
    }
}

只有一个包,其中包含您需要的所有脚本,这不是更简单吗?像这样:

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery.validate*",
                    "~/Scripts/modernizr-*",
                    "~/Scripts/bootstrap.js",
                    "~/Scripts/respond.js"));
    }
}

或者,更简单,像这样(在Script文件夹中你只放你需要的JS文件):

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/scripts").Include("~/Scripts/*"));
    }
}

Wouldn't it be much simpler to have just one bundle, where you will include all the scripts you need?

这可能会稍微简化开发过程,因为您不需要寻找相应的包来包含。但是让我们从另一个角度来看这个。

asp.net forum 上写了以下内容:

Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)

所以我们进行捆绑是为了加快速度。如果您创建的页面具有不需要 bootstrap.js 的简单表单,该怎么办?如果您有兴趣加快一切速度,为什么还要加载它?

最佳性能选项是将您可能需要的所有可能的 js 文件组合放在一个单独的包中。每个页面只发出一个请求来获取它需要的所有 js。然而这种方式很难维护。

所以由你来决定你对哪个方面感兴趣。