javascript – MVC4 Beta Minifying and Bundling:在瀏覽器中排序檔案和除錯
我已經開始使用MVC4 Beta附帶的捆綁和縮小.我遇到了一些問題:
一方面,如果我使用經典的<script src =“Folder / js”type =“text / javascript”/>捆綁,似乎我必須重新命名我的檔案,以確保它們按照正確的順序捆綁.
假設我有三個javascript檔案:“ants.js”,“bugs.js”,“insects.js”
> ants.js依賴於bugs.js
bugs.js取決於insects.js
>預設捆綁似乎按字母順序捆綁.
>為了使它們正確繫結,我必須將它們重新命名為:“0.insects.js”,“1.bugs.js”,“2.ants.js”
這真的是黑客,必須有一個更清潔的方式.
我遇到的下一個問題是除錯.我喜歡在我的測試瀏覽器中瀏覽javascript,有沒有辦法在DEBUG模式下關閉簡化?
編輯:要清楚,我知道我可以建立捆綁包,並從C#註冊它,它似乎真的很醜,要這樣做.
暫時得到非最小化的輸出使用這個
public class NonMinifyingJavascript : IBundleTransform { public void Process(BundleContext context, BundleResponse bundle) { if(bundle == null) { throw new ArgumentNullException("bundle"); } context.HttpContext.Response.Cache.SetLastModifiedFromFileDependencies(); foreach(FileInfo file in bundle.Files) { HttpContext.Current.Response.AddFileDependency(file.FullName); } bundle.ContentType = "text/javascript"; //base.Process(context, bundle); } }
如果你想要完全基於配置設定,我想象你可以建立一個IBundle變換,根據你的配置設定委派給這個或JsMinify
為了控制javascript檔案的順序,您需要使用BundleFileSetOrdering
var javascriptBundle = new Bundle("~/site/js", new NonMinifyingJavascript()); //controls ordering for javascript files, otherwise they are processed in order of AddFile calls var bootstrapOrdering = new BundleFileSetOrdering("bootstrap"); //The popover plugin requires the tooltip plugin bootstrapOrdering.Files.Add("bootstrap-tooltip.js"); bootstrapOrdering.Files.Add("bootstrap-popover.js"); BundleTable.Bundles.FileSetOrderList.Add(bootstrapOrdering); javascriptBundle.AddDirectory("~/Scripts", "bootstrap-*.js");
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/9657412/mvc4-beta-minification-and-bundling-ordering-files-and-debugging-in-browser