SlideShare une entreprise Scribd logo
1  sur  17
前端工程師不可不知的 Source Maps 應用技巧
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
新手入門篇
錄影重播
http://bit.ly/sourcemaps4all
認識 Source Maps
Introduction to Source Maps
3
很久很久以前 …
• 小明為客戶設計了一個 HTML 網頁
– https://github.com/doggy8088/sourcemapdemo
• 當客戶下載並執行網頁之後
– git clone https://github.com/doggy8088/sourcemapdemo.git
• 抱怨網頁載入速度太慢了
– 因為 main.js 有 620KB 這麼大!
• 然後 ……
小明決定改善網頁效能
• 他找到了全新的 UglifyJS 3 套件可以有效壓縮 JS 檔案
– https://github.com/mishoo/UglifyJS2
• 透過 npm 安裝了 uglify-js 套件
– https://www.npmjs.com/package/uglify-js
– npm i -g uglify-js
• 產生了壓縮過的 JS 檔案 ( 620K  141KB )
– uglifyjs -o main.min.js main.js
– 修改 index.html 並將 main.js 改成載入 main.min.js 檔案
• 然後 ……
如何對最佳化過的 JS 進行偵錯
• 用肉眼分析壓縮過的程式碼
• 用 PrettyPrint 擴充套件(Chrome)自動格式化原始碼
• 用 F12 開發者工具的 Sources 的自動格式化功能
6
使用 Source Map 技術
• 產生 Source map 檔案
– uglifyjs --source-map -o main.min.js main.js
• 啟用 Source map 功能
– 先確定瀏覽器有啟用 Source maps
• IE11 / Edge (來源對應)
• Chrome (Source map)
• Firefox (Source map)
– 再確認你的 *.min.js 檔案有連結到 *.min.js.map
1. 手動加入 source map 檔案 ( 僅 Chrome 支援 )
2. //# sourceMappingURL=/path/to/file.js.map
3. X-SourceMap: /path/to/file.js.map 7
Chrome 瀏覽器的 Source maps 設定
• 預設啟用 JavaScript 與 CSS 的 Source maps
8
IE11/Edge 瀏覽器的 Source maps 設定
• 預設啟用 JavaScript 的 Source maps
9
關於 JS 最佳化 (合併、壓縮)
• 合併
– 將多個 JS 檔案合併成一個
– 減少 HTTP 要求次數以提升瀏覽效率
• 壓縮 (醜化)
– 將 JS 中多餘的空白字元刪除
– 將 JS 中過長的區域變數變短
– 縮小 JS 檔案大小,以加快檔案下載速度
• 常見工具
– uglify-js、webpack module bundler、TypeScript
以 jQuery 原始碼為例
Source Maps Example: jQuery
11
下載 jQuery 原始碼
• Downloading jQuery
http://jquery.com/download/
12
完整的 jQuery 原始碼
• 官網的檔案連結
– https://code.jquery.com/jquery-3.2.1.js
– https://code.jquery.com/jquery-3.2.1.min.js
– https://code.jquery.com/jquery-3.2.1.min.map
• 透過 npm 安裝
– npm i jquery
13
以 Angular 4 原始碼為例
Source Maps Example: Angular 4
14
查詢 Source maps 對應的原始碼
15
相關連結
• Introduction to JavaScript Source Maps
• MDN / Firefox
– Use a source map - Firefox Developer Tools
• MSDN / IE11 / Edge
– 使用偵錯工具檢查執行中的 JavaScript - 來源對應
– Inspect running JavaScript with the Debugger
• Chrome
– Enabling Source Maps in Chrome
聯絡資訊
• 多奇教育訓練最新課程
– https://coolrare.accupass.com
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

Contenu connexe

Plus de Will Huang

Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Will Huang
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點Will Huang
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門Will Huang
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)Will Huang
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)Will Huang
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Will Huang
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)Will Huang
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Will Huang
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Will Huang
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)Will Huang
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)Will Huang
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)Will Huang
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)Will Huang
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)Will Huang
 
以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談Will Huang
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018Will Huang
 
迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:KubernetesWill Huang
 
Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017Will Huang
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWill Huang
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Will Huang
 

Plus de Will Huang (20)

Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
 
以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
 
迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes
 
Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, Kubernetes
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸
 

前端工程師不可不知的 Source Maps 應用技巧

  • 1. 前端工程師不可不知的 Source Maps 應用技巧 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/ 新手入門篇
  • 4. 很久很久以前 … • 小明為客戶設計了一個 HTML 網頁 – https://github.com/doggy8088/sourcemapdemo • 當客戶下載並執行網頁之後 – git clone https://github.com/doggy8088/sourcemapdemo.git • 抱怨網頁載入速度太慢了 – 因為 main.js 有 620KB 這麼大! • 然後 ……
  • 5. 小明決定改善網頁效能 • 他找到了全新的 UglifyJS 3 套件可以有效壓縮 JS 檔案 – https://github.com/mishoo/UglifyJS2 • 透過 npm 安裝了 uglify-js 套件 – https://www.npmjs.com/package/uglify-js – npm i -g uglify-js • 產生了壓縮過的 JS 檔案 ( 620K  141KB ) – uglifyjs -o main.min.js main.js – 修改 index.html 並將 main.js 改成載入 main.min.js 檔案 • 然後 ……
  • 6. 如何對最佳化過的 JS 進行偵錯 • 用肉眼分析壓縮過的程式碼 • 用 PrettyPrint 擴充套件(Chrome)自動格式化原始碼 • 用 F12 開發者工具的 Sources 的自動格式化功能 6
  • 7. 使用 Source Map 技術 • 產生 Source map 檔案 – uglifyjs --source-map -o main.min.js main.js • 啟用 Source map 功能 – 先確定瀏覽器有啟用 Source maps • IE11 / Edge (來源對應) • Chrome (Source map) • Firefox (Source map) – 再確認你的 *.min.js 檔案有連結到 *.min.js.map 1. 手動加入 source map 檔案 ( 僅 Chrome 支援 ) 2. //# sourceMappingURL=/path/to/file.js.map 3. X-SourceMap: /path/to/file.js.map 7
  • 8. Chrome 瀏覽器的 Source maps 設定 • 預設啟用 JavaScript 與 CSS 的 Source maps 8
  • 9. IE11/Edge 瀏覽器的 Source maps 設定 • 預設啟用 JavaScript 的 Source maps 9
  • 10. 關於 JS 最佳化 (合併、壓縮) • 合併 – 將多個 JS 檔案合併成一個 – 減少 HTTP 要求次數以提升瀏覽效率 • 壓縮 (醜化) – 將 JS 中多餘的空白字元刪除 – 將 JS 中過長的區域變數變短 – 縮小 JS 檔案大小,以加快檔案下載速度 • 常見工具 – uglify-js、webpack module bundler、TypeScript
  • 11. 以 jQuery 原始碼為例 Source Maps Example: jQuery 11
  • 12. 下載 jQuery 原始碼 • Downloading jQuery http://jquery.com/download/ 12
  • 13. 完整的 jQuery 原始碼 • 官網的檔案連結 – https://code.jquery.com/jquery-3.2.1.js – https://code.jquery.com/jquery-3.2.1.min.js – https://code.jquery.com/jquery-3.2.1.min.map • 透過 npm 安裝 – npm i jquery 13
  • 14. 以 Angular 4 原始碼為例 Source Maps Example: Angular 4 14
  • 15. 查詢 Source maps 對應的原始碼 15
  • 16. 相關連結 • Introduction to JavaScript Source Maps • MDN / Firefox – Use a source map - Firefox Developer Tools • MSDN / IE11 / Edge – 使用偵錯工具檢查執行中的 JavaScript - 來源對應 – Inspect running JavaScript with the Debugger • Chrome – Enabling Source Maps in Chrome
  • 17. 聯絡資訊 • 多奇教育訓練最新課程 – https://coolrare.accupass.com • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang