Bundle 小镇中由 EasyUI 引发的“血案”

简介:

由于默认的 ASP.NET MVC 模板使用了 Bundle 技术,大家开始接受并喜欢上这种技术。Bundle 技术通过 Micorosoft.AspNet.Web.Optimization 包实现,如果在 ASP.NET WebForm 项目中引入这个包及其依赖包,在 ASP.NET WebForm 项目中使用 Bundle 技术也非常容易。


关于在 WebForm 中使用 Bundle 技术的简短说明

通过 NuGet 很容易在 WebForm 项目中引入Microsoft.AspNet.Web.Optimization 包及其依赖包。不过在 MVC 项目的 Razor 页面中可以使用类似下面的语句引入资源

1
@Scripts.Render( "..." )

而在 *.aspx 页面中则需要通过 <%= %> 来引入了:

1
2
3
<%@ Import Namespace= "System.Web.Optimization"  %>
// ...
<%= Scripts.Render( "..." ) %>

备注 有些资料中是使用的 <%: %>,我实在没有发现它和 <%= %> 有啥区别,但至少我在《ASP.NET Reference》《Code Render Blocks》一节找到了 <%= %>,却暂时没在官方文档里找到 <%: %>



然后,我在一个使用了 EasyUI 的项目中使用了 Bundle 技术。才开始一切正常,至到第一个 Release 版本测试的那一天,“血案”发生了——


由于一个脚本错误,EasyUI 没有生效。最终原因是 Bunlde 在 Release 版中将 EasyUI 的脚本压缩了——当然,定位到这个原因还是经历了一翻周折,这就不细说了。


[方案一] 禁用代码压缩

这个解决方案理论上只需要在配置里加一句话就行:

1
BundleTable.EnableOptimizations =  false ;

但问题在于,这样一来,为了一个 EasyUI,就放弃了所有脚本的压缩,而仅仅只是合并,效果折半,只能当作万不得已的备选


[方案二] 分段引入并阻止压缩 EasyUI 的 Bundle

先看看原本的 Bundle 配置(已简化)

1
2
3
4
5
6
7
8
9
public  static  void  Register(BundleCollection bundles)
{
     bundles.Add( new  ScriptBundle( "~/libs" )
         .Include( "~/scripts/jquery-{version}.js" )
         .Include( "~/scripts/jquery.eaysui-{versoin}.js" )
         .Include( "~/scripts/locale/easyui-lang-zh_CN.js" )
         .IncludeDirectory( "~/scripts/app" "*.js" true )
     );
}


这段配置先引入了 jquery,再引入了 easyui,最后引入了一些为当前项目写的公共脚本。为了实现解决方案二,必须要改成分三个 Bundle 引入,同时还得想办法阻止压缩其中一个 Bundle。


要分段,简单

1
2
3
4
5
6
7
8
9
10
11
12
13
public  static  void  Register(BundleCollection bundles)
{
     bundles.Add( new  ScriptBundle( "~/jquery" )
         .Include( "~/scripts/jquery-{version}.js" )
     );
     bundles.Add( new  ScriptBundle( "~/easyui" )
         .Include( "~/scripts/jquery.eaysui-{versoin}.js" )
         .Include( "~/scripts/locale/easyui-lang-zh_CN.js" )
     );
     bundles.Add( new  ScriptBundle( "~/libs" )
         .IncludeDirectory( "~/scripts/app" "*.js" true )
     );
}


但为了阻止压缩,查了文档,也搜索了不少资料都没找到解决办法,所以只好看源码分析了,请出 JetBrains dotPeek。分析代码之后得出结论,只需要去掉默认的 Transform 就行

1
2
3
4
5
6
7
8
9
10
// bundles.Add(new ScriptBundle("~/easyui")
//     .Include("~/scripts/jquery.eaysui-{versoin}.js")
//     .Include("~/scripts/locale/easyui-lang-zh_CN.js")
// );
Bundle easyuiBundle =  new  ScriptBundle( "~/easyui" )
     .Include( "~/scripts/jquery.eaysui-{versoin}.js" )
     .Include( "~/scripts/locale/easyui-lang-zh_CN.js" )
);
easyuiBundle.Transforms.Clear();
bundles.Add(easyuiBundle);



关键代码的分析说明


首先从 ScriptBunlde 入手

1
2
3
4
5
6
7
8
9
10
11
public  class  ScriptBundle: Bundle {
     public  ScriptBundle( string  virtualPath)
         this (virtualPath, ( string null ) {}
 
     public  ScriptBundle( string  virtualPath,  string  cdnPath)
         base (virtualPath, cdnPath,
             (IBundleTransform)  new  JsMinify()
         ) {
         this .ConcatenationToken =  ";"  + Environment.NewLine;
     }
}


可以看出,ScriptBunlde 的构建最终是通过其基类 Bunlde 中带 IBunldeTransform 参数的那一个来构造的。再看 Bunlde 的关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public  class  Bunlde 
 
     public  IList<IBundleTransform> Transforms {
         get  return  this ._transforms; }
     }
 
     public  Bundle(
         string  virtualPath,
         string  cdnPath,
         params  IBundleTransform[] transforms
     ) {
 
         // ...
 
         foreach (IBundleTransform bundleTransform  in  transforms) {
             this ._transforms.Add(bundleTransform);
         }
     }
}


容易理解,ScriptBunlde 构建的时候往 Transforms 中添加了一默认的 Transform——JsMinify,从名字就可以看出来,这是用来压缩脚本的。而 IBundleTransform 只有一个接口方法

1
2
3
public  interface  IBundleTransform {
     void  Process(BundleContext context, BundleResponse response);
}


看样子它是在处理 BundleResponse。而 BundleResponse 中定义有文本类型的 Content 和 ContentType 属性,以及一个 IEnumerable<BundleFile> Files


为什么是 Files 而不是 File 呢,我猜 Content 中包含的是一个 Bundle 中所有文件的内容,而不是某一个文件的内容。要验证也很容易,自己实现个 IBundleTransform 试下就行了

1
2
3
4
5
6
Bundle b =  new  ScriptBundle( "~/test" )
     .Include(...)
     .Include(...);
b.Transforms.Clear();b.Transforms.Add( new  MyTransform())
 
// MyTransform 可以自由发挥,我其实啥都没写,只是在 Process 里打了个断点,检查了 response 的属性值而已


实验证明在 BundleResponse 传入 Transforms 之前,其 Content 就已经有所有引入文件的内容了。



方案二解决了方案一不能解决的问题,但同时也带来了新问题。原来只需要一句话就能引入所有脚本

1
@Scripts.Render( "~/libs" )

而现在需要 3 句话

1
2
3
@Scripts.Render( "~/jquery" )
@Scripts.Render( "~/easyui" )
@Scripts.Render( "~/libs" )


[方案三] Bundle 的 Bundle

鉴于方案二带来的新问题,试想,如果有一个东西,能把 3 个 Bundle 对象组合起来,变成一个 Bundle 对象,岂不是就解决了?


于是,我发明了 Bundle 的 Bundle,不妨就叫 BundleBundle 吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public  class  BundleBundle : Bundle{
     readonly  List<Bundle> bundles =  new  List<Bundle>();
  
     public  BundleBundle( string  virtualPath)
         base (virtualPath)
     {
     }
  
     public  BundleBundle Include(Bundle bundle)
     {
         bundles.Add(bundle);
         return  this ;
     }
  
     // 在引入 Bundle 对象时申明清空 Transforms,这几乎就是为 EasyUI 准备的
     public  BundleBundle Include(Bundle bundle,  bool  isClearTransform)
     {
         if  (isClearTransform)
         {
             bundle.Transforms.Clear();
         }
         bundles.Add(bundle);
         return  this ;
     }
  
     public  override  BundleResponse GenerateBundleResponse(BundleContext context)
     {
         List<BundleFile> allFiles =  new  List<BundleFile>();
         StringBuilder content =  new  StringBuilder();
         string  contentType =  null ;
  
         foreach  (Bundle b  in  bundles)
         {
             var  r = b.GenerateBundleResponse(context);
             content.Append(r.Content);
 
             // 考虑到 BundleBundle 可能用于 CSS,所以这里进行一次判断,
             // 只在 ScriptBundle 后面加分号(兼容 ASI 风格脚本)
             // 这里可能会出现在已有分号的代码后面加分号的情况,
             // 考虑到只会浪费 1 个字节,忍了
             if  (b  is  ScriptBundle)
             {
                 content.Append( ';' );
             }
             content.AppendLine();
  
             allFiles.AddRange(r.Files);
             if  (contentType ==  null )
             {
                 contentType = r.ContentType;
             }
         }
  
         var  response =  new  BundleResponse(content.ToString(), allFiles);
         response.ContentType = contentType;
         return  response;
     }
}


使用 BundleBundle 也简单,就像这样

1
2
3
4
5
6
7
8
9
10
11
12
13
bundles.Add( new  BundleBundle( "~/libs" )
     .Include( new  ScriptBundle( "~/bundle/jquery" )
         .Include( "~/scripts/jquery-{version}.js" )
     )
     .Include(
         new  ScriptBundle( "~/bundle/easyui" )
             .Include( "~/scripts/jquery.easyui-{version}.js" )
             .Include( "~/scripts/locale/easyui-lang-zh_CN.js" )
     )
     .Include( new  ScriptBundle( "~/bundle/app" )
         .IncludeDirectory( "~/scripts/app" "*.js" true )
     )
);

然后

1
@Scripts.Render( "~/libs" )


注意,每个子 Bundle 都有名字,但这些名字不能直接给 @Scripts.Render() 使用,因为它们并没有直接加入 BundleTable.Bundles 中。但名字是必须的,而且不能是 null,不信就试试。



本文转自边城__ 51CTO博客,原文链接:http://blog.51cto.com/jamesfancy/1605467,如需转载请自行联系原作者

相关文章
|
Python
Visual Studio更改编码格式为“UTF-8”
原文:Visual Studio更改编码格式为“UTF-8” 用VS2015新建了个Python文件,在VS2015打开时中文显示正常, 用Visual Studio Code文本编辑器打开时,发现中文乱码 解决方案:在VS2015中修改该文件的编码格式为“UTF-8” 操作方法如下所...
6330 0
|
1天前
|
云安全 人工智能 安全
|
10天前
|
数据采集 自然语言处理 搜索推荐
基于Qwen3的Embedding和Rerank模型系列,开源!
近年来,随着大规模预训练语言模型(LLM)的飞速发展,文本嵌入(Embedding)和重排序(Reranking)技术在搜索引擎、问答系统、推荐系统等多个领域的重要性愈发凸显。
1046 89
|
3天前
|
自然语言处理 安全 开发者
|
10天前
|
安全 JavaScript 前端开发
Typora免费下载,不需要激活,Typora早期版本,Markdown编辑器
Typora是一款支持实时预览的Markdown编辑器,跨平台兼容Windows、macOS和Linux,适合写作、笔记和技术文档。本文提供合法安全的Typora早期免费版安装方案及常见问题解决方法,助你快速上手。同时详细介绍了Markdown基础用法,包括标题、加粗、斜体、列表、链接、图片、引用、代码块和表格等常用语法,帮助用户高效编写结构化内容。
1004 21
|
9天前
|
人工智能 前端开发 Devops
通义灵码带你玩转开发者常用的MCP(合辑,持续更新中)
今天我们精选了与开发者息息相关和比较热门的MCP 服务,总结了这些技术服务在实际开发的最佳实践,涵盖了从前端开发、后端开发、DevOps、测试、运维等关键环节,及非研发领域中的热度较高的MCP服务。我们通过直播、图文等形式带你了解和学习!
807 4
|
11天前
|
存储 前端开发 JavaScript
2025 最新前端 100 道经典面试题及详细答案汇总整理
本文整理了100道前端常见面试题及其详细答案,涵盖HTML、CSS、JavaScript等多个领域,助你系统复习前端知识。内容包括HTML5新特性、CSS盒模型、Flex与Grid布局区别、选择器优先级等核心知识点,以及伪类和伪元素的区别等细节。适合准备面试或巩固基础的开发者学习。资源可从文末链接下载。
599 2
|
14天前
|
人工智能 Java 决策智能
Spring AI Alibaba Graph:多智能体框架实践
Spring AI Alibaba 是一个面向 Java 开发者的开源人工智能框架,旨在简化 AI 应用开发。本文重点介绍其 Graph 组件,用于解决工作流与多智能体协作问题。Graph 组件通过声明式编程接口,提供统一的上下文管理、消息记忆、人工确认节点等功能,支持复杂 AI 应用的构建。
|
21天前
|
人工智能 API 开发者
一文带你 "看见" MCP 的过程,彻底理解 MCP 的概念
本文介绍了模型上下文协议(MCP)的基本概念、工作原理及其应用过程。MCP 是一种连接 AI 助手与数据系统的开放标准,旨在帮助大模型生成更高质量的响应。文章从 RAG 和 Function Calling 的理论基础出发,详细解析了 MCP 的核心组件(主机、客户端、服务器)及优势,并通过 ModelScope 和 Cherry Studio 实例展示其操作流程。同时,文中指出了 MCP 存在的手动配置复杂、工具稳定性不足等问题,但也强调其作为过渡协议的重要性,为未来智能体间的协同和工具使用提供了方向。
1811 57
一文带你 "看见" MCP 的过程,彻底理解 MCP 的概念