2013-09-25 78 views
10

我對捆綁腳本和樣式文件的正確方法感到有點困惑。目前,我的BundleConfig.cs看起來是這樣的:在MVC4中捆綁的正確方法

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
      "~/Scripts/jquery-{version}.js")); 

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
      "~/Scripts/jquery-ui-{version}.js")); 

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
      "~/Scripts/jquery.unobtrusive*", 
      "~/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 StyleBundle("~/Content/css").Include("~/Content/site.css")); 

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
      "~/Scripts/knockout-{version}.js", 
      "~/Scripts/knockout-{version}.debug.js", 
      "~/Scripts/knockout-sortable.js")); 

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
      "~/Content/themes/base/jquery.ui.core.css", 
      "~/Content/themes/base/jquery.ui.resizable.css", 
      "~/Content/themes/base/jquery.ui.selectable.css", 
      "~/Content/themes/base/jquery.ui.accordion.css", 
      "~/Content/themes/base/jquery.ui.autocomplete.css", 
      "~/Content/themes/base/jquery.ui.button.css", 
      "~/Content/themes/base/jquery.ui.dialog.css", 
      "~/Content/themes/base/jquery.ui.slider.css", 
      "~/Content/themes/base/jquery.ui.tabs.css", 
      "~/Content/themes/base/jquery.ui.datepicker.css", 
      "~/Content/themes/base/jquery.ui.progressbar.css", 
      "~/Content/themes/base/jquery.ui.theme.css")); 

bundles.Add(new StyleBundle("~/bundles/BootStrapcss").Include(
      "~/BootStrap/css/bootstrap.css", 
      "~/BootStrap/css/bootstrap-fileupload.css")); 

bundles.Add(new StyleBundle("~/bundles/BootStrap").Include(
      "~/BootStrap/tpg-main.css", 
      "~/BootStrap/tpg-internal.css")); 

bundles.Add(new ScriptBundle("~/bundles/BootStrapjs").Include(
      "~/BootStrap/js/bootstrap-fileupload.js", 
      "~/BootStrap/js/bootstrap.js")); 

BundleTable.EnableOptimizations = true; 

應該留與我有什麼,或者我的所有腳本文件捆綁爲一個ScriptBundle,和我所有的樣式合併到一個StyleBundle?我想盡可能達到最佳性能。

回答

14

如果您總是使用所有文件,而不是繼續,並將它們粘在兩個包中;一個用於JavaScript,一個用於樣式。捆綁越少意味着對服務器獲取資源的請求就越少,這可能會導致首次訪問時的性能稍微提高;隨後文件將被瀏覽器緩存。

如果你並不總是使用所有的文件,那麼把它們分成更多的文件夾更有意義。

+6

爲了澄清他的觀點,您可能只需要在一兩頁上使用Knockout,而不是整個網站。因此,您不應該將其與其他所有內容捆綁在一起,因爲它會在未使用或不需要的頁面上加載。 –

+0

@Moozhe是的,謝謝你,我應該擴大一點爲完整性。 – asymptoticFault

+0

如何將jquery,validate和modernizr打包成一個? –