`
JerryWang_SAP
  • 浏览: 967490 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

UI5 Source code map机制的细节介绍

阅读更多

在我的博客A debugging issue caused by source code mapping里我介绍了在我做SAP C4C开发时遇到的一个曾经困扰我很久的问题,最后结论是这个问题由于JavaScript的source code map机制在Chrome开发者工具里起作用,其实是working as designed的一种行为。但是当时因为时间限制,没有去深入学习JavaScript source code map的更多细节。

在这篇文章里我用一个简单的UI5应用来研究该机制。这个应用的UI仅仅包含一个Button,点击之后弹出一个Message Toast。

下面是我XML view和Controller的实现。

 
 

打开Chrome开发者工具里的source code map开关:

 

然后浏览器里访问这个UI5应用,我们就能在Chrome开发者工具里看到这些UI5库文件的调试版本(.dbg.js)。但是在Chrome开发者工具的Network标签里,我们观察不到这些调试版本文件的加载。那么问题来了:这些.dbg.js文件从哪里来的?

 
 

当关闭Chrome开发者工具的source code map功能之后,我们在Chrome开发者工具里再也观察不到这些.dbg.js文件了。将下图和source code map打开时的截图做比较:

 

如何在本地找到sap-ui-core.js.map文件

单击sap-ui-core.js,在其最后一行1875行,看到该行内容:


//# sourceMappingURL=sap-ui-core.js.map

这个文件的后缀.map给了我们提示:其作用就是维护位置映射关系,将sap-ui-core.js(压缩之后的文件)里的代码位置映射到压缩之前的代码位置(来自压缩之前的文件名,代码行数,代码列数,涉及到的压缩之前的JavaScript变量名)。

 

但是,同样的,我在开发者工具的Network标签里也观察不到这个.map文件被加载。

 

在Chrome里输入url: "chrome://net-internals/#events", 结果显示确实有一个url请求去访问sap-ui-core.js.map, 只是因为本地磁盘缓存能响应该请求, 因此没有产生真正的网络请求:

 

在Chrome里输入"chrome://cache"能看到Chrome本地的所有缓存,从这里我成功找到了文件sap-ui-core.js.map的本地缓存。

 

单击该超链接能看到这条缓存的抬头信息。但是缓存的具体文件内容显示格式为HEX,没法直接分析。

 

因此我使用了工具Cache viewer for Google Chrome Web browser, 将该缓存导出成本地文件。

 

sap-ui-core.js.map文件内容一览

这篇博客Introduction to JavaScript Source Maps介绍了JavaScript source code map的基本知识。

文件sap-ui-core.js.map的内容:

 
  • version: 3

.map文件的各组成部分的作用和含义定义在一个叫做Source Map Revision Proposal的协议文档里,在我的例子sap-ui-core.js.map里使用了该协议的第三版。

  • sources:

这是一个数组,包含了所有用于生成压缩之后的js文件的原始文件的名称。

  • names:

这是一个数组,包含了原始js文件里出现的JavaScript变量和属性名称。

下面是一个例子,体现了原始文件之一Device-dbg.js里的变量名称和其在sap-ui-core.js.map文件里的names数组里的对应记录,方便您理解。

 
  • mappings:

.map文件最重要的部分,定义了原始文件内的位置和生成压缩版本文件内位置的对应关系。对应关系记录的粒度是基于压缩之后文件的每一行,用分号隔开。这样做的好处是无需再分配而外的位来维护压缩文件位置的行号信息。

 

回到我的例子,压缩文件sap-ui-core.js一共包含1874行,因此sap-ui-core.js.map一共出现了1874次分号,每个分号内又是一个很长的字符串,由一系列逗号隔开,这些由逗号隔开的字符串片段称为Segment。每个Segment维护了一个位置的映射关系。

 

如何生成.map文件

有很多开源的组件用于生成.map文件,其中之一是Google Closure compiler。假设我想基于我的测试应用里的controller实现文件App.controller.js生成一个压缩版本的文件:

Google网站下载compile.jar, 然后生成一个名为script-min.js的压缩文件和script-min.js.map:


java -jar compile.jar --js App.controller.js --create_source_map ./script-min.js.map --source_map_format=V3 --js_output_file script-min.js

生成的压缩文件script-min.js只有1行内容:

 

生成的script-min.js.map内容:

 

可以使用vlq.js将这些segment解码:

 

浏览器打开该html,产生如下输出:每个segment由4或5个字符组成。

 

每一位的对应含义:

  • 第一位,表示这个位置在转换后的压缩文件的第几列。

  • 第二位,sources数组中的索引,表示这个位置来自哪一个原始文件。

  • 第三位,表示这个位置属于原始文件的第几行。

  • 第四位,表示这个位置属于原始文件的第几列。

  • 第五位,names数组中的索引,表示这个位置属于源文件中的哪一个变量。

关于VLQ编码的更多细节,可以阅读这篇博客Source Maps under the hood – VLQ, Base64 and Yoda

要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

 
 
0
0
分享到:
评论

相关推荐

    DevExpress Components 18.2.3 Source Code

    DevExpress Components 18.2.3 Source Code DevExpress Components 18.2.3 源代码 内含: BuildUWP DevExpress.AspNetCore.Bootstrap DevExpress.AspNetCore.Common DevExpress.BonusSkins DevExpress.Code...

    前端UI框架Svelte.zip

    const { code, map } = svelte.compile( source, { // the target module format – defaults to 'es' (ES2015 modules), can // also be 'amd', 'cjs', 'umd' or 'iife' format: 'umd', // the filename of ...

    source.zip

    视频名称 源码 ----------------------...复杂UI界面设计、GPS与GoogleMap、自定义View、Ubuntu Linux下使用C语言面向底层开发、通过JNI进行底层组件调用、图形与OpenGl ES、界面特效、下载修改及编绎Android框架代码。

    CodeSMART.Integration.with.The.Visual.Studio.2010.IDE

    7,Source Code Perfectly Formatted and Documented with The CodeSMART Code Maintenance Tools Code Formatter Sort Procedures XML Comments XML Comments Preview Regular Comments Code Commenting ...

    Pro Radar Builder (Source Included) 3.0.1

    Since this is the source included version you have access to all the source code you need.. For 2D Radars please see: 2D Radar Builder For 3D Radars please see: 3D Radar Builder Simple AI (just ...

    Programming Windows Store Apps with C#

    You’ll learn how to develop the code, incorporate third-party open source products, and package your app for the Windows Store. Build a UI with XAML and the Model/View/View-Model pattern Understand...

    完全开源的Html5游戏引擎:青瓷引擎 QICI Engine.zip

    青瓷引擎是一套开源免费的JavaScript游戏引擎类库,其基于开源免费的Phaser游戏引擎... A wide range of source code examples for you to explore. Download all in one zip file.  标签:游戏引擎

    Shadero Sprite - 2D Shader Editor 1.9.3

    - Source code included - Load/Save project - Optimised shader - Fog Support - HDR Support - Visual selection node preview - Light Support - Blend Mode support - Normalmap with light Support - Preview ...

    Bloodshed Dev-C++

    * Added support for macros in the "default source code" (Tools/Editor Options/Code) * Separated layout info from project file. It is now kept in a different file (the same filename as the project...

    leetcode中国-favorite:给我收藏过的github项目做tag

    code your UI, Draw it !" 前端 Tentative to a simple JavaScript AST modification library 后端接口和文档自动化,前端(客户端) 定制返回JSON的数据和结构! Headless Chrome Node API http知识点总结 HTTP, ...

    Gamebryo.3.0 LightSpeed

    附件为链接文件 Gamebryo.3.0 LightSpeed 详情请自行搜索,该资源 来源于互联网。 安装后目录,大小为10G左右 ├─Build │ ├─CoreRuntime │ │ └─Win32 │ │ ├─VC80 │ │ │ └─Property Sheets ...

    stdafx.h代码

    // This source code is only intended as a supplement to the // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for ...

    Bochs - The cross platform IA-32 (x86) emulator

    [3001637] CMOS MAP register meaning error [2994370] Cannot build with 3DNow support - these S.F. feature requests were closed/implemented [1510142] Native Windows XP x64 Edition binary [1062553] ...

    外文翻译 stus MVC

    You would map some input event to a pointer to a function. If you where slick, you would place the configuration information into a file and load the file at run time. Function pointer arrays were ...

    EurekaLog_7.5.0.0_Enterprise

    5)....Fixed: Wrong settings for MAP files in C++ Builder 6)....Fixed: Wrong code page was used to decode ANSI bug reports 7)....Fixed: Attaching .PAS files instead of .OBJ in C++ Builder 2006+ Pro/...

    计算机网络第六版答案

    5. Wifi (802.11): home and enterprise: 6. 3G and 4G: wide-area wireless. HFC bandwidth is shared among the users. On the downstream channel, all packets emanate from a single source, namely, the ...

    MCloud - OAuth2 认证中心

    - **mapstruct** 主要用于DTO 与 Entity 之间的转换 - **flywaydb** 以版本化的方式管理数据库脚本 - **thymeleaf** 模板框架,用于实现后台管理界面 - **Redis** 主要用作缓存实现 (暂未实现) - **lombok** ## ...

    Graviton-App::rocket:现代外观的代码编辑器

    Graviton是一个开源,跨平台,简约的代码编辑... :nail_polish: 现代UI :artist_palette: 主题 :package: 外挂程式 :world_map: 内置图像预览器 :memo: 多行编辑 :megaphone: 社区 :loudspeaker: :speech_balloo

    python3.6.5参考手册 chm

    Porting Python code Porting C code Building C extensions Command Line Switch Changes What’s New In Python 3.2 PEP 384: Defining a Stable ABI PEP 389: Argparse Command Line Parsing Module PEP ...

Global site tag (gtag.js) - Google Analytics