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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
高見龍
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
740
自己的售票系統自己做!
eddie
0
790
AI Agent 時代的開發者生存指南
eddie
4
3k
print("Hello, World")
eddie
2
770
為你自己學 Python - 冷知識篇
eddie
1
570
為你自己學 Python
eddie
0
890
Generative AI 年會小聚 - AI 教我寫程式
eddie
0
340
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
840
AI 時代的程式語言學習法
eddie
0
380
Other Decks in Programming
See All in Programming
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
330
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
2
410
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
130
Performance Optimization With LLMs
xleitix
0
260
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.9k
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
670
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.5k
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
2.1k
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
260
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
370
GraphQL の onError と Nullability のこれから
quramy
0
220
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
3
12k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k
Thoughts on Productivity
jonyablonski
76
5.5k
First, design no harm
axbom
PRO
2
1.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
300
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
300
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
330
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k