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>

注意

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 <dir>#

需要修补的软件包将被解压到此目录。

--ignore-existing#

修补时忽略现有的修补文件。

配置#

patchedDependencies#

此字段会在你运行 pnpm patch-commit 时自动添加/更新。它使用字典定义依赖的补丁:

  • 键:具有精确版本、版本范围或仅仅是名称的包名称。
  • 值:补丁文件的相对路径。

示例:

pnpm-workspace.yaml
patchedDependencies:
  express@4.18.1: patches/express@4.18.1.patch

依赖项可以按版本范围进行修补。优先顺序为:

  1. 精确版本(最高优先级)
  2. 版本范围
  3. 只有名称的补丁(应用于所有版本,除非覆盖)

示例:

pnpm-workspace.yaml
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 版本。

避免版本范围重叠。如果需要专用化某个子范围,请将其从更广泛的范围中明确排除。

示例:

pnpm-workspace.yaml
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#

添加于: v10.7.0 (原命名为 allowNonAppliedPatches)

  • 默认值: false
  • 类型:Boolean

当设置为 true 时,如果 patchedDependencies 字段中的某些补丁未被应用,安装不会失败。

pnpm-workspace.yaml
patchedDependencies:
  express@4.18.1: patches/express@4.18.1.patch
allowUnusedPatches: true

注意

In v11, patch application failures always throw an error. The ignorePatchFailures setting has been removed. When multiple patches in a group are applied, a failure in one does not prevent the rest from being attempted; all patch errors are reported together at the end.