跳到主内容
版本:9.x

.pnpmfile.cjs

pnpm 允许您通过特殊功能(钩子)直接挂钩到安装过程。 Hooks can be declared in a file called .pnpmfile.cjs.

By default, .pnpmfile.cjs should be located in the same directory as the lockfile. For instance, in a workspace with a shared lockfile, .pnpmfile.cjs should be in the root of the monorepo.

钩子

摘要:

钩子函数过程用途
hooks.readPackage(pkg, context): pkg在 pnpm 解析依赖项的包清单后调用Allows you to mutate a dependency's package.json
hooks.afterAllResolved(lockfile, context): lockfile在解析完依赖关系后调用。允许您更改锁文件。

hooks.readPackage(pkg, context): pkg | Promise<pkg>

Allows you to mutate a dependency's package.json after parsing and prior to resolution. 这些突变不会保存到文件系统,但是,它们将影响被解析至锁文件的内容,从而影响哪些包将被安装。

Note that you will need to delete the pnpm-lock.yaml if you have already resolved the dependency you want to modify.

提示

If you need changes to package.json saved to the filesystem, you need to use the pnpm patch command and patch the package.json file. This might be useful if you want to remove the bin field of a dependency for instance.

参数

  • pkg - The manifest of the package. Either the response from the registry or the package.json content.
  • context - Context object for the step. Method #log(msg) allows you to use a debug log for the step.

使用方法

Example .pnpmfile.cjs (changes the dependencies of a dependency):

function readPackage(pkg, context) {
// Override the manifest of foo@1.x after downloading it from the registry
if (pkg.name === 'foo' && pkg.version.startsWith('1.')) {
// Replace bar@x.x.x with bar@2.0.0
pkg.dependencies = {
...pkg.dependencies,
bar: '^2.0.0'
}
context.log('bar@1 => bar@2 in dependencies of foo')
}

// This will change any packages using baz@x.x.x to use baz@1.2.3
if (pkg.dependencies.baz) {
pkg.dependencies.baz = '1.2.3';
}

return pkg
}

module.exports = {
hooks: {
readPackage
}
}

已知限制

Removing the scripts field from a dependency's manifest via readPackage will not prevent pnpm from building the dependency. When building a dependency, pnpm reads the package.json of the package from the package's archive, which is not affected by the hook. In order to ignore a package's build, use the pnpm.neverBuiltDependencies field.

hooks.afterAllResolved(lockfile, context): lockfile | Promise<lockfile>

允许您在序列化之前更改锁文件的输出。

参数

  • lockfile - The lockfile resolutions object that is serialized to pnpm-lock.yaml.
  • context - Context object for the step. Method #log(msg) allows you to use a debug log for the step.

用法示例

.pnpmfile.cjs
function afterAllResolved(lockfile, context) {
// ...
return lockfile
}

module.exports = {
hooks: {
afterAllResolved
}
}

已知限制

没有 - 任何可以通过修改锁文件达到的功能都可以通过这个函数完成,甚至可以扩展锁文件的功能。

相关配置

ignore-pnpmfile

  • Default: false
  • Type: Boolean

.pnpmfile.cjs will be ignored. Useful together with --ignore-scripts when you want to make sure that no script gets executed during install.

pnpmfile

  • Default: .pnpmfile.cjs
  • Type: path
  • Example: .pnpm/.pnpmfile.cjs

本地 pnpmfile 的位置。

global-pnpmfile

  • Default: null
  • Type: path
  • Example: ~/.pnpm/global_pnpmfile.cjs

全局 pnpmfile 的位置。 在安装期间,所有项目都会使用全局 pnpmfile 。

注意

建议使用本地 pnpmfiles。 仅当在未使用 pnpm 作为主包管理器的项目上使用 pnpm 时,才使用全局 pnpmfile 。