跳至主要內容
版本:下一個

.pnpmfile.cjs

pnpm 讓您可以透過特殊函式 (hooks) 對安裝過程進行作用。 hooks 可以被定義於稱為 .pnpmfile.cjs 的檔案中。

預設情況下,.pnpmfile.cjs 應與 lockfile 置於同樣的目錄中。 例如,在一個共用 lockfile 的 工作區 中,.pnpmfile.cjs 應置於此 monorepo 的根目錄。

Hooks

摘要

Hook 函式ProcessUses
hooks.readPackage(pkg, context): pkg在 pnpm 剖析依附套件的資訊清單 (manifest) 後呼叫此函式讓您可以變更依附套件的 package.json
hooks.afterAllResolved(lockfile, context): lockfile在解析依附關係完成後呼叫此函式讓您可以變更 lockfile

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

Allows you to mutate a dependency's package.json after parsing and prior to resolution. These mutations are not saved to the filesystem, however, they will affect what gets resolved in the lockfile and therefore what gets installed.

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>

Allows you to mutate the lockfile output before it is serialized.

引數

  • 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
}
}

已知的限制

There are none - anything that can be done with the lockfile can be modified via this function, and you can even extend the lockfile's functionality.

相關設定

ignore-pnpmfile

  • 預設值:false
  • 型別: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
  • 類型:path
  • Example: .pnpm/.pnpmfile.cjs

指定 pnpmfile 的位置。

global-pnpmfile

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

指定一份全域 pnpmfile 的位置。 安裝期間的所有專案都會使用此檔案。

注意事項

It is recommended to use local pnpmfiles. Only use a global pnpmfile if you use pnpm on projects that don't use pnpm as the primary package manager.