Quantcast
Channel: 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY
Viewing all articles
Browse latest Browse all 325

減少 node_modules 大小來加速部署 Node.js 專案

$
0
0
yarn-kitten-full 相信 Node.js 開發者現在大部分都在使用 Yarn 了吧?如果還不知道或無法體會 Yarn 帶來的好處可以參考之前寫的一篇『用 Yarn 取代 Npm 管理 JavaScript 套件版本』,帶你體會 yarn install vs npm install 的速度差異。本篇最主要會介紹在部署 Node.js 專案都需要把 node_modules 壓縮一起丟到遠端伺服器 (假設你不是用 Docker 部署),這時候來聊聊怎麼減少 node_modules 大小。

傳統 npm 作法

Yarn 尚未出來時,可以透過 npm prune --production 的方式來將 devDependencies 內的套件全部清除
$ npm install
$ .... 處理其他事情
$ npm prune --production
$ .... 最後將 node_modeuls 打包

yarn 作法

原本我是把 yarn 搭配 npm prune --production,在早期的 yarn 版本似乎不會有問題,但是發現最新版本 npm prune 會把非 devDependencies 內的套件也一併清除,雖然 yarn 沒有提供 prune 指令,但是有個 flag 可以使用,效果跟 npm prune 一樣,就是加上 --production 這樣就可以降低不少大小,所以部署流程會變成底下
$ yarn install
先是安裝全部套件 (像是 babel-cli),接著透過 babel 轉換程式碼,最後在下底下指令
$ yarn install --production
這時候就會把 babel 相關套件全部移除,最後將 node_modules 打包就可以了。

結論

除了上述過程外,在 CI/CD 流程內,務必設定 yarn 快取目錄在專案內。
$ yarn config set cache-folder .yarn-cache
跑完部屬流程後,可以把 .yarn-cachenode_modules 同時打包,等到下次跑 CI/CD 時會加速不少喔。

Viewing all articles
Browse latest Browse all 325

Trending Articles


Vimeo 10.7.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE


KASAMBAHAY BILL IN THE HOUSE


Girasoles para colorear


Presence Quotes – Positive Quotes


EASY COME, EASY GO


Love with Heart Breaking Quotes


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo 10.7.0 by Vimeo.com, Inc.


FORECLOSURE OF REAL ESTATE MORTGAGE


FORTUITOUS EVENT


Pokemon para colorear


Sapos para colorear


Smile Quotes


Letting Go Quotes


Love Song lyrics that marks your Heart


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Long Distance Relationship Tagalog Love Quotes