将 pnpm 与 Lerna 一起使用
¥Using pnpm with Lerna
Lerna 可在 pnpm 工作区 中使用,以获得 pnpm 和 Lerna 的全部优点。
¥Lerna can be used in a pnpm workspace to get the full benefits of both pnpm and Lerna.
当在 pnpm 工作区中使用时,Lerna 将:
¥When used in a pnpm workspace, Lerna will:
-
使用
pnpm-workspace.yaml(https://pnpm.nodejs.cn/workspaces) 解析包的位置¥resolve package locations with
pnpm-workspace.yaml(https://pnpm.nodejs.cn/workspaces) -
忽略
package.json中的"workspaces"¥ignore
"workspaces"inpackage.json -
阻止使用
bootstrap、link和add命令。相反,你应该直接使用pnpm命令来管理依赖 (https://pnpm.nodejs.cn/cli/install)。¥block usage of
bootstrap,link, andaddcommands. Instead, you should usepnpmcommands directly to manage dependencies (https://pnpm.nodejs.cn/cli/install). -
尊重 工作区协议 的包依赖。
¥respect the workspace protocol for package dependencies.
-
在
lerna version期间,依赖将照常更新,但将保留workspace:前缀(如果存在)。¥During
lerna version, dependencies will be updated as normal, but will preserve theworkspace:prefix if it exists. -
如果使用 工作区别名,则
lerna version将不会更改依赖的版本,因为别名不指定要更改的版本号。¥If a workspace alias is used, then
lerna versionwill not bump the version of the dependency, since aliases don't specify a version number to bump.
-
入门
¥Getting Started
要使用 Lerna 设置 pnpm:
¥To set up pnpm with Lerna:
-
如果尚未安装,请安装
pnpm:https://pnpm.nodejs.cn/installation。¥If not installed already, install
pnpm: https://pnpm.nodejs.cn/installation. -
删除根目录中的
node_modules/文件夹(如果存在)。如果尚未使用工作区,请运行lerna clean以删除所有包中的node_modules/文件夹。¥Remove the
node_modules/folder in the root, if it exists. If not already using workspaces, runlerna cleanto remove thenode_modules/folder in all packages. -
将
"npmClient": "pnpm"设置为lerna.json。¥Set
"npmClient": "pnpm"inlerna.json. -
在项目的根目录中创建一个
pnpm-workspace.yaml文件。如果你已经在使用 npm 或 yarn 工作区,请将 "workspaces" 属性从package.json移动到pnpm-workspace.yaml。如果你尚未使用工作区,请将 "packages" 属性从lerna.json移至pnpm-workspace.yaml。例如:¥Create a
pnpm-workspace.yamlfile in the root of your project. If you are already using npm or yarn workspaces, move the "workspaces" property frompackage.jsontopnpm-workspace.yaml. If you were not already using workspaces, move the "packages" property fromlerna.jsontopnpm-workspace.yaml. For example:package.json{
"workspaces": ["packages/*"]
}和
¥and
lerna.json{
"packages": ["packages/*"]
}变得:
¥become:
pnpm-workspace.yamlpackages:
- "packages/*" -
(可选)运行
pnpm import以从现有锁定文件生成pnpm-lock.yaml文件。请参阅 https://pnpm.nodejs.cn/cli/import 了解受支持的锁定文件源。¥(optional) Run
pnpm importto generate apnpm-lock.yamlfile from an existing lockfile. See https://pnpm.nodejs.cn/cli/import for supported lockfile sources. -
运行
pnpm install。¥Run
pnpm install.