bun patch 允许你以可维护、对 git 友好的方式持久地修补 node_modules。
有时,你需要对 node_modules/ 中的包进行少量更改以修复错误或添加功能。bun patch 使这变得容易,无需导入整个包,并可以在多次安装、多个项目和多台机器上重用该补丁。
功能
- 生成在安装时应用于
node_modules中依赖项的.patch文件 .patch文件可以提交到你的仓库,并在多次安装、项目和机器上重复使用package.json中的"patchedDependencies"跟踪已修补的包bun patch允许你在修补node_modules/中的包时,同时保持 Bun 全局缓存 的完整性- 使用
bun patch --commit <pkg>在提交更改之前在本地测试你的更改 - 为了节省磁盘空间并保持
bun install快速,已修补的包会提交到全局缓存,并在可能的情况下跨项目共享
步骤 1. 准备要修补的包
首先,使用 bun patch <pkg> 准备要修补的包
# you can supply the package namebun patch react
# ...and a precise version in case multiple versions are installedbun patch react@17.0.2
# or the path to the packagebun 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 和 lockfile,Bun 将开始使用已修补的包
# you can supply the path to the patched packagebun patch --commit node_modules/react
# ... or the package name and optionally the versionbun patch --commit react@17.0.2
# choose the directory to store the patch filesbun patch --commit react --patches-dir=mypatches
# `patch-commit` is available for compatibility with pnpmbun patch-commit react