Asp.Net Mvc ScriptBundle 指令碼檔案捆綁壓縮 導致 指令碼出錯的問題

黑白、發表於2018-07-16

由於捆綁壓縮會對所有包含的檔案進行壓縮,無法設定忽略對某個js檔案的壓縮。導致壓縮該js後,指令碼出錯的問題。

解決方式:

  重寫 ScriptBundle 的 GenerateBundleResponse 。程式碼如下

  

public class ScriptBundleFileIgnoreZip: ScriptBundle
        {
 
            readonly HashSet<string> bundles = new HashSet<string>();
 
            /// <summary>
            /// 建構函式
            /// </summary>
            /// <param name="virtualPath"></param>
            public ScriptBundleFileIgnoreZip(string virtualPath) : base(virtualPath) { }
 
            /// <summary>
            /// 忽略壓縮包含檔案
            /// </summary>                                
            /// <param name="virtualPaths">檔案組</param>
            /// <returns></returns>
            public Bundle IgnoreMinInclude(params string[] virtualPaths)
            {
                foreach (string path in virtualPaths)
                {
                    string cPath = path.TrimStart(`~`);
                    if (!bundles.Contains(cPath))
                    {
                        bundles.Add(cPath);
                    }
                }
 
                return base.Include(virtualPaths);
            }
 
            /// <summary>
            /// 生成壓縮捆綁響應文
            /// </summary>
            /// <param name="context">捆綁上下文</param>
            /// <returns></returns>
            public override BundleResponse GenerateBundleResponse(BundleContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                IEnumerable<BundleFile> enumerable = this.EnumerateFiles(context);
                enumerable = context.BundleCollection.IgnoreList.FilterIgnoredFiles(context, enumerable);
                enumerable = this.Orderer.OrderFiles(context, enumerable);
                if (this.EnableFileExtensionReplacements)
                {
                    enumerable = context.BundleCollection.FileExtensionReplacementList.ReplaceFileExtensions(context, enumerable);
                }
 
                StringBuilder bundleContent = new StringBuilder();
                string text2 = ";" + Environment.NewLine;
                Microsoft.Ajax.Utilities.Minifier minifier = new Microsoft.Ajax.Utilities.Minifier();
                foreach (var bf in enumerable)
                {
                    if (bundles.Contains(bf.VirtualFile.VirtualPath))
                    {
                        bundleContent.Append(bf.ApplyTransforms());
                    }
                    else
                    {
                        bundleContent.Append(minifier.MinifyJavaScript(bf.ApplyTransforms(), new Microsoft.Ajax.Utilities.CodeSettings()
                        {
                            EvalTreatment = Microsoft.Ajax.Utilities.EvalTreatment.MakeImmediateSafe,
                            PreserveImportantComments = false
                        }));
                    }
 
                    bundleContent.Append(text2);
                }
 
                if (this.Transforms != null)
                {
                    this.Transforms.Clear();
                }
 
                return this.ApplyTransforms(context, bundleContent.ToString(), enumerable);
 
            }

 用法:

 var scriptBundles = new ScriptBundleFileIgnoreZip("~/JS");

 scriptBundles.Include("~/要壓縮的檔案0.js");
 scriptBundles.IgnoreMinInclude("~/忽略壓縮的檔案.js")
 .Include("~/要壓縮的檔案1.js", "~/要壓縮的檔案2.js");

 BundleTable.Bundles.Add(scriptBundles);

 

相關文章