Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
模組化前端開發:從亂七八糟到組織有序
Search
高見龍
November 08, 2023
Programming
1.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
模組化前端開發:從亂七八糟到組織有序
高見龍
November 08, 2023
Transcript
96 slides
五倍學院 從亂七八糟到組織有序
五倍學院 愛玩、愛現,喜歡冷門的玩具 網站開發者、講師、作家 技術推廣、企業訓練、技術諮詢 部落格:kaochenlong.com 高見龍 @eddiekao
五倍學院
五倍學院 從亂七八糟到組織有序
五倍學院
五倍學院 前端?不就是寫網頁的嘛
五倍學院 模組化?
五倍學院 JavaScript = 玩具 1995 年
五倍學院 你知道在最初版本的 JS 是沒有陣列的嗎?
五倍學院 石器時代
五倍學院 模組化?
五倍學院 no module
五倍學院 <script type="text/javascript"> $().ready(function () { console.log("MWC 2023"); }); </
script> $
五倍學院 <script src="jquery.min.js"> < / script> <script type="text/javascript"> $().ready(function ()
{ console.log("MWC 2023"); }); </ script>
五倍學院 載入的順序很重要
五倍學院 <script type="text/javascript"> $().ready(function () { console.log("MWC 2023"); }); </
script> <script src="jquery.min.js"> < / script>
五倍學院 套件放在 <head> 裡, 自己寫的放 </body> 前
五倍學院 污染全域變數
五倍學院 使用 IIFE Immediately Invoked Function Expression
五倍學院 const MyApp = MyApp || {} MyApp.UserModule = (function
() { // 登入功能 function login(username, password) {} // 登出功能 function logout() {} return { login, logout } })() MyApp.UserModule.login("mwc", "2023") MyApp.UserModule.logout() 模組化!
五倍學院 官方不做,社群做!
五倍學院 戰國時代
五倍學院 群魔亂舞 / 群雄割據 CommonJS, AMD/CMD, UMD...etc
五倍學院 CommonJS
五倍學院 // 我是 mwc_mod.js const MWC = function () {
console.log("ModernWeb Conference") } module.exports.MWC = MWC
五倍學院 // 我是 main.js const mod = require("./mwc_mod.js") console.log(mod) //
{ MWC: [Function: MWC] } mod.MWC() // 印出 ModernWeb Conference
五倍學院 // 我是 mwc_mod.js const MWC = function () {
console.log("ModernWeb Conference") } module.exports.MWC = MWC 這是什麼?
五倍學院 console.log(module) // { // id: '.', // path: '/private/tmp/mwc-demo',
// exports: { MWC: [Function: MWC] }, // filename: '/private/tmp/mwc-demo/m.js', // loaded: false, // children: [], // paths: [ // '/private/tmp/mwc-demo/node_modules', // '/private/tmp/node_modules', // '/private/node_modules', // '/node_modules' // ] // } 在這裡!
五倍學院 console.log(module.exports === exports) // true
五倍學院 const MWC = function () { console.log("ModernWeb Conference") }
// module.exports.MWC = MWC exports.MWC = MWC 同樣效果
五倍學院 // 我是 mwc_mod.js const MWC = function () {
console.log("ModernWeb Conference") } exports.MWC = MWC exports = "謝謝你 9527" console.log(exports) // 印出 謝謝你 9527 console.log(module.exports) // { MWC: [Function: MWC] } 咦?
五倍學院 同步載入
五倍學院 AMD Asynchronous Module Definition
五倍學院 非同步
五倍學院 require.js
五倍學院 // 我是 hello.js define(function () { const hello =
function () { console.log("HELLO") } return { hello, } }) // 我是 world.js define(function () { const world = function () { console.log("WORLD") } return { world, } }) 模組 模組
五倍學院 <!DOCTYPE html> <html> <head> <script src="require.js" data-main="main"> </ script>
<title>MWC 2023 </ title> </ head> <body> </ body> </ html> 發動!
五倍學院 // 我是 main.js require(["hello", "world"], function (h, w) {
h.hello() w.world() }) 載入
五倍學院 UMD Universal Module Definition
五倍學院 (function (root, factory) { if (typeof define === "function"
&& define.amd) { // 是 AMD! define(["deps"], factory) } else if (typeof exports === "object") { // 是 CommonJS! module.exports = factory(require("deps")) } else { // 其它,來去全域變數上塞東 西 ! root.MyModule = factory(root) } })(this, function (deps) { return { MWC: function () { console.log("ModernWeb Conference") }, year: "2023", } }) 檢查 檢查 IIFE
五倍學院 jQuery
五倍學院 https: // 5xcamp.us/jquery-source
五倍學院 ESM? ES Module 2015 年
五倍學院 工業革命時代
五倍學院 Node = 蒸汽機
五倍學院 轉譯、打包、建置工具!
五倍學院 https: // webpack.js.org/
五倍學院 支援多種模組系統 CommonJS, AMD, ESM...etc
五倍學院 webpack.config.js 🤮
五倍學院 https: // parceljs.org/
五倍學院 esbuild
五倍學院 天下武功,唯快不破
五倍學院 https: // esbuild.github.io/
五倍學院 為什麼這麼快?
五倍學院 Go
五倍學院 https: // bun.sh/
五倍學院 Zig
五倍學院 大內捲時代
五倍學院 https: // rollupjs.org/
五倍學院 Tree-Shaking 除屑優化
五倍學院 // 我是 extra_fish.js const 工 具 人 = "🛠🔨🔧⛏"
function 多餘() { console.log("不是多了塊 魚 🐟,是多餘,你呀,你根本就是多餘呀你") } function MWC(year) { return `ModernWeb Conference ${year}` } console.log(MWC(2023)) :)
五倍學院
五倍學院 現代
五倍學院 瀏覽器原生支援 ESM! import vue from 'react' 🙌
五倍學院 https: // developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/import
五倍學院 <script type="module"> </ script>
五倍學院 <!DOCTYPE html> <html> <head> <title>MWC 2023 </ title> <script
type="module"> import has from "lodash-es/has.js" </ script> </ head> <body> </ body> </ html>
五倍學院
五倍學院 <!DOCTYPE html> <html> <head> <title>MWC 2023 </ title> <script
type="module"> import has from "./node_modules/lodash-es/has.js" </ script> </ head> <body> </ body> </ html> 👌
五倍學院 <!DOCTYPE html> <html> <head> <title>MWC 2023 < / title>
<script type="module"> import day from "./node_modules/dayjs/dayjs.min.js" </ script> < / head> <body> </ body> </ html>
五倍學院
五倍學院 Vite /vit/
五倍學院 Vite 不是 Vue 的專武!
五倍學院 Vite 不是打包工具
五倍學院 esbuild ↢ Vite ↣ Rollup pre- bundling production bundle
dev server
五倍學院 HMR
五倍學院 https: // 5xcamp.us/mwc-vite
五倍學院 https: // twitter.com/youyuxi/status/1709943106215530867
五倍學院 Rollup ↣ Rolldown
五倍學院 Rust
五倍學院 https: // 5xcamp.us/ithome-rust
五倍學院 importmap
五倍學院 <script type="module"> </ script>
五倍學院 <script type="importmap"> </ script>
五倍學院 <!DOCTYPE html> <html> <head> <title>MWC 2023 </ title> <script
type="importmap"> { "imports": { "dayjs": "https: // ga.jspm.io/npm:
[email protected]
/dayjs.min.js" } } < / script> </ head> <body> < / body> < / html>
五倍學院 這不就 CDN 嗎?
五倍學院 <!DOCTYPE html> <html> <head> <title>MWC 2023 </ title> <script
type="importmap"> { "imports": { "dayjs": "https: / / ga.jspm.io/npm:
[email protected]
/dayjs.min.js", "mwc-day": "https: // ga.jspm.io/npm:
[email protected]
/dayjs.min.js" } } </ script> <script type="module"> import day from "dayjs" import d from "mwc-day" </ script> < / head> <body> < / body> < / html>
五倍學院 no bundle
五倍學院 這樣效能好嗎?CDN 穩定嗎?
五倍學院 https: // 5xcamp.us/
五倍學院 擺脫 node_modules! 🙌
五倍學院 更多關於 importmap
五倍學院 12/16
五倍學院 工商服務 實體、線上課程、企業內訓 有時間 來實體 沒時間 買線上
五倍學院 工商服務 Node / Python / Django / Rust 課程
有時間 來實體 沒時間 買線上
五倍學院 愛玩、愛現,喜歡冷門的玩具 網站開發者、講師、作家 技術推廣、企業訓練、技術諮詢 部落格:kaochenlong.com 高見龍 @eddiekao
More Decks by 高見龍
See All by 高見龍
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
750
自己的售票系統自己做!
eddie
0
790
AI Agent 時代的開發者生存指南
eddie
4
3k
print("Hello, World")
eddie
2
780
為你自己學 Python - 冷知識篇
eddie
1
580
為你自己學 Python
eddie
0
890
Generative AI 年會小聚 - AI 教我寫程式
eddie
0
350
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
850
AI 時代的程式語言學習法
eddie
0
380
Other Decks in Programming
See All in Programming
Spec-driven Development with AI
simas
PRO
0
2.3k
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
3
580
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
140
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
110
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
380
Distance field of Julia set
kinakomoti321
0
330
Crash Auto Pipelines
veronikapj
1
430
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
430
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
260
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
720
LLM 101
dalinaum
0
510
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Into the Great Unknown - MozCon
thekraken
41
2.8k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7.1k
The Limits of Empathy - UXLibs8
cassininazir
2
790
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
How to make the Groovebox
asonas
2
2.6k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
4
1.3k
Color Theory Basics | Prateek | Gurzu
gurzu
1
530
Code Reviewing Like a Champion
maltzj
529
40k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Amusing Abliteration
ianozsvald
1
390