별칭
Aliases는 커스텀 네임으로 패키지를 설치할 수 있게 해줍니다.
Let's assume you use lodash
all over your project. There is a bug in lodash
that breaks your project. You have a fix but lodash
won't merge it. Normally
you would either install lodash
from your fork directly (as a git-hosted
dependency) or publish it with a different name. If you use the second solution
you have to replace all the requires in your project with the new dependency
name (require('lodash')
=> require('awesome-lodash')
). aliases를 사용하게 되면 세번째 옵션이 생깁니다.
Publish a new package called awesome-lodash
and install it using lodash
as
its alias:
pnpm add lodash@npm:awesome-lodash
코드의 변경은 필요하지 않고, All the requires of lodash
will now resolve to
awesome-lodash
.
서로 다른 두가지 버전의 패키지를 사용하게 된다면 다음과 같이 사용합니다:
pnpm add lodash1@npm:lodash@1
pnpm add lodash2@npm:lodash@2
Now you can require the first version of lodash via require('lodash1')
and the
second via require('lodash2')
.
이 기능은 훅과 함께 사용되면 더욱 유용합니다. Maybe you want to replace
lodash
with awesome-lodash
in all the packages in node_modules
. You can
easily achieve that with the following .pnpmfile.cjs
:
function readPackage(pkg) {
if (pkg.dependencies && pkg.dependencies.lodash) {
pkg.dependencies.lodash = 'npm:awesome-lodash@^1.0.0'
}
return pkg
}
module.exports = {
hooks: {
readPackage
}
}