pnpm patch <pkg>
给软件包添加补丁(灵感来自于 Yarn 中一个类似的命令)。
This command extracts the package to node_modules/.pnpm_patches/<pkg>, where <pkg> is the argument as given, such as foo or foo@1.0.0 (or the --edit-dir), a directory you can edit at will.
Once you're done with your changes, run pnpm patch-commit <path> (with <path> being the directory you received) to generate a patchfile and register it into your top-level manifest via the patchedDependencies field.
使用方法:
pnpm patch <pkg name>@<version>
Run pnpm install first. Without installed dependencies, the command fails with ERR_PNPM_PATCH_NO_LOCKFILE.
If several versions of the package are installed, pnpm asks which version to patch, then whether to apply the patch to all versions. If you apply it to all versions, the patchedDependencies key is the package name without a version.
注意
To change a package's dependencies, don't use pnpm patch to modify its package.json! Use overrides or a package hook instead.
配置项#
--edit-dir, -d <dir>#
需要修补的软件包将被解压到此目录。 If the directory exists and is not empty, the command fails with ERR_PNPM_PATCH_EDIT_DIR_EXISTS. Without this option, a non-empty default directory fails with ERR_PNPM_EDIT_DIR_NOT_EMPTY.
--ignore-existing#
修补时忽略现有的修补文件。
配置#
patchedDependencies#
此字段会在你运行 pnpm patch-commit 时自动添加/更新。它使用字典定义依赖的补丁:
- 键:具有精确版本、版本范围或仅仅是名称的包名称。
- 值:补丁文件的相对路径。
示例:
patchedDependencies:
express@4.18.1: patches/express@4.18.1.patch
依赖项可以按版本范围进行修补。优先顺序为:
- 精确版本(最高优先级)
- 版本范围
- 只有名称的补丁(应用于所有版本,除非覆盖)
示例:
patchedDependencies:
foo: patches/foo-1.patch
foo@^2.0.0: patches/foo-2.patch
foo@2.1.0: patches/foo-3.patch
patches/foo-3.patch应用于foo@2.1.0。patches/foo-2.patch适用于所有与^2.0.0匹配的 foo 版本,除了2.1.0。patches/foo-1.patch适用于所有其他 foo 版本。
避免版本范围重叠。如果需要专用化某个子范围,请将其从更广泛的范围中明确排除。
示例:
patchedDependencies:
# 特殊子范围
"foo@2.2.0-2.8.0": patches/foo.2.2.0-2.8.0.patch
# 通用补丁,不包括上述子范围
"foo@>=2.0.0 <2.2.0 || >2.8.0": patches/foo.gte2.patch
在大多数情况下,定义一个精确的版本就足以覆盖更广泛的范围。
allowUnusedPatches#
- 默认值: false
- 类型:Boolean
当设置为 true 时,如果 patchedDependencies 字段中的某些补丁未被应用,安装不会失败。
patchedDependencies:
express@4.18.1: patches/express@4.18.1.patch
allowUnusedPatches: true
A patch that fails to apply fails the install. When several patches in a group are applied, a failure in one does not prevent the rest from being attempted, and all patch errors are reported together at the end.