bun patch
可讓您以可維護、對 git 友善的方式,持久地修補 node_modules。
有時,您需要對 node_modules/
中的套件進行小幅更改,以修正錯誤或新增功能。bun patch
讓您可以輕鬆完成此操作,而無需將整個套件複製出來,並在多個安裝、多個專案和多部機器上重複使用修補程式。
功能特色
- 產生
.patch
檔案,在安裝時套用至node_modules
中的相依性 .patch
檔案可以提交到您的儲存庫,在多個安裝、專案和機器之間重複使用package.json
中的"patchedDependencies"
追蹤已修補的套件bun patch
可讓您修補node_modules/
中的套件,同時保持 Bun 全域快取的完整性- 在提交變更之前,先使用
bun patch --commit <pkg>
在本機測試您的變更 - 為了節省磁碟空間並保持
bun install
的速度,修補後的套件會提交到全域快取,並在可能的情況下在專案之間共用
步驟 1. 準備要修補的套件
若要開始,請使用 bun patch <pkg>
準備要修補的套件
# you can supply the package name
bun patch react
# ...and a precise version in case multiple versions are installed
bun patch react@17.0.2
# or the path to the package
bun patch node_modules/react
注意 — 別忘了呼叫 bun patch <pkg>
!這可確保 node_modules/
中的套件資料夾包含套件的全新副本,且沒有指向 Bun 快取的符號連結/硬連結。
如果您忘記執行此操作,您最終可能會在快取中全域編輯套件!
步驟 2. 在本機測試您的變更
bun patch <pkg>
可以安全地直接編輯 node_modules/
中的 <pkg>
,同時保持 Bun 全域快取的完整性。其運作方式是在 node_modules/
中重新建立未連結的套件副本,並將其與全域快取中的原始套件進行差異比較。
步驟 3. 提交您的變更
對您的變更感到滿意後,請執行 bun patch --commit <path or pkg>
。
Bun 將在 patches/
中產生修補程式檔案,更新您的 package.json
和鎖定檔,Bun 將開始使用修補後的套件
# you can supply the path to the patched package
bun patch --commit node_modules/react
# ... or the package name and optionally the version
bun patch --commit react@17.0.2
# choose the directory to store the patch files
bun patch --commit react --patches-dir=mypatches
# `patch-commit` is available for compatibility with pnpm
bun patch-commit react