跳到主内容
版本:Next

pnpm patch <pkg>

给软件包添加补丁(灵感来自于 Yarn 中一个类似的命令)。

该命令会将指定的软件包提取到一个可以随意编辑的临时目录中。

Once you're done with your changes, run pnpm patch-commit <path> (with <path> being the temporary directory you received) to generate a patchfile and register it into your top-level manifest via the patchedDependencies field.

使用方法:

pnpm patch <pkg name>@<version>
注意

如果你想更改包的依赖项,请不要使用修补来修改包的 package.json 文件。 要覆盖依赖项,请使用 overrides软件包钩子

配置项

--edit-dir <dir>

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

--ignore-existing

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

配置

patchedDependencies

This field is added/updated automatically when you run pnpm patch-commit. 它使用字典定义依赖的补丁:

  • Keys: Package names with an exact version, a version range, or just the name.
  • Values: Relative paths to patch files.

示例:

patchedDependencies:
express@4.18.1: patches/express@4.18.1.patch

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

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

A special case: the version range * behaves like a name-only patch but does not ignore patch failures.

示例:

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 is applied to foo@2.1.0.
  • patches/foo-2.patch applies to all foo versions matching ^2.0.0, except 2.1.0.
  • patches/foo-1.patch applies to all other foo versions.

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

示例:

patchedDependencies:
# Specialized sub-range
"foo@2.2.0-2.8.0": patches/foo.2.2.0-2.8.0.patch
# General patch, excluding the sub-range above
"foo@>=2.0.0 <2.2.0 || >2.8.0": patches/foo.gte2.patch

在大多数情况下,定义一个精确的版本就足以覆盖更广泛的范围。

allowUnusedPatches

Added in: v10.7.0 (Previously named allowNonAppliedPatches)

  • 默认值: false
  • 类型:Boolean

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

patchedDependencies:
express@4.18.1: patches/express@4.18.1.patch
allowUnusedPatches: true

ignorePatchFailures

添加于: v10.7.0

  • 默认值:undefined
  • Type: Boolean, undefined

控制如何处理补丁失败。

行为:

  • undefined (default):
    • 当具有精确版本或版本范围的补丁失败时会出现错误。
    • 忽略仅有名称的补丁的失败。
  • false: Errors out for any patch failure.
  • true: Prints a warning instead of failing when any patch cannot be applied.