.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.
Хуки
TL;DR
| Функція хука | Процес | Використання |
|---|---|---|
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 thepackage.jsoncontent.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>
Дозволяє змінювати вивід файлу блокування перед його серіалізацією.