Перейти к основному содержимому
Версия: Будущая

.pnpmfile.cjs

pnpm позволяет подключаться непосредственно к процессу установки с помощью специальных функций (хуков). Хуки могут быть объявлены в файле .pnpmfile.cjs.

По умолчанию .pnpmfile.cjs должен быть расположен в том же каталоге, что и лок-файл. Например, в рабочем пространстве (воркспейсе) с общим лок-файлом, .pnpmfile.cjs должен находиться в корне монорепо.

Хуки

TL;DR

Хук-функцияProcessUses
hooks.readPackage(pkg, context): pkgCalled after pnpm parses the dependency's package manifestAllows you to mutate a dependency's package.json
hooks.afterAllResolved(lockfile, context): lockfileCalled after the dependencies have been resolved.Allows you to mutate the 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 neverBuiltDependencies field.

hooks.updateConfig(config): config | Promise<config>

Added in: v10.8.0

Allows you to modify the configuration settings used by pnpm. This hook is most useful when paired with configDependencies, allowing you to share and reuse settings across different Git repositories.

For example, @pnpm/better-defaults uses the updateConfig hook to apply a curated set of recommended settings.

Пример использования

.pnpmfile.cjs
module.exports = {
hooks: {
updateConfig (config) {
return Object.assign(config, {
enablePrePostScripts: false,
optimisticRepeatInstall: true,
resolutionMode: 'lowest-direct',
verifyDepsBeforeRun: 'install',
})
}
}
}

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

Известные ограничения

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.

hooks.preResolution(options): Promise<void>

This hook is executed after reading and parsing the lockfiles of the project, but before resolving dependencies. It allows modifications to the lockfile objects.

Аргументы

  • options.existsCurrentLockfile - A boolean that is true if the lockfile at node_modules/.pnpm/lock.yaml exists.
  • options.currentLockfile - The lockfile object from node_modules/.pnpm/lock.yaml.
  • options.existsNonEmptyWantedLockfile - A boolean that is true if the lockfile at pnpm-lock.yaml exists.
  • options.wantedLockfile - The lockfile object from pnpm-lock.yaml.
  • options.lockfileDir - The directory where the wanted lockfile is found.
  • options.storeDir - The location of the store directory.
  • options.registries - A map of scopes to registry URLs.

hooks.importPackage(destinationDir, options): Promise<string | undefined>

This hook allows to change how packages are written to node_modules. The return value is optional and states what method was used for importing the dependency, e.g.: clone, hardlink.

Аргументы

  • destinationDir - The destination directory where the package should be written.
  • options.disableRelinkLocalDirDeps
  • options.filesMap
  • options.force
  • options.resolvedFrom
  • options.keepModulesDir

hooks.fetchers

This hook allows to override the fetchers that are used for different types of dependencies. It is an object that may have the following fields:

  • localTarball
  • remoteTarball
  • gitHostedTarball
  • directory
  • git

Опции конфигурации

ignore-pnpmfile

  • По умолчанию: false
  • Тип: Boolean

.pnpmfile.cjs будет проигнорирован. Полезно вместе с --ignore-scripts, когда вы когда вы хотите убедиться, что ни один скрипт не будет выполнен во время установки.

pnpmfile

  • По умолчанию: .pnpmfile.cjs
  • Тип: путь
  • Пример: .pnpm/.pnpmfile.cjs

Расположение локального pnpmfile.

global-pnpmfile

  • По умолчанию: null
  • Тип: путь
  • Пример: ~/.pnpm/global_pnpmfile.cjs

Расположение глобального pnpmfile. Глобальный pnpm-файл используется всеми проектами во время установки.

заметка

Рекомендуется использовать локальные pnpm-файлы. Используйте глобальный pnpm-файл, только если вы используете pnpm в проектах, которые не используют pnpm в качестве основного менеджера пакетов.