Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题

由于捆绑压缩会对所有包含的文件进行压缩,无法设置忽略对某个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);

 

转载于:https://www.cnblogs.com/HeiBai-blog/p/9318216.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值