Los alias te permiten instalar paquetes con nombres personalizados.
Let's assume you uselodash
all over your project. There is a bug inlodash
that breaks your project. You have a fix butlodash
won't merge it. Normallyyou would either installlodash
from your fork directly (as a git-hosteddependency) or publish it with a different name. If you use the second solutionyou have to replace all the requires in your project with the new dependencyname (require('lodash')
=>require('awesome-lodash')
). Con los alias tienes una tercera opción.
Publish a new package calledawesome-lodash
and install it usinglodash
asits alias:
pnpm add lodash@npm:awesome-lodash
No son necesarios cambios en el código. All the requires oflodash
will now resolve toawesome-lodash
.
A veces querrás usar diferentes versiones de un paquete en tu proyecto. Fácil:
pnpm add lodash1@npm:lodash@1
pnpm add lodash2@npm:lodash@2
Now you can require the first version of lodash viarequire('lodash1')
and thesecond viarequire('lodash2')
.
Eso se vuelve más útil cuando es combinado con los hooks. Maybe you want to replacelodash
withawesome-lodash
in all the packages innode_modules
. You caneasily achieve that with the following.pnpmfile.cjs
:
functionreadPackage(pkg){
if(pkg.dependencies&& pkg.dependencies.lodash){
pkg.dependencies.lodash='npm:awesome-lodash@^1.0.0'
}
return pkg
}
module.exports={
hooks:{
readPackage
}
}