工作区
pnpm 内置了对单一存储库(也称为多包存储库、多项目存储库或整体存储库)的支持。你可以创建一个工作区来将多个项目联合到一个存储库中。
¥pnpm has built-in support for monorepositories (AKA multi-package repositories, multi-project repositories, or monolithic repositories). You can create a workspace to unite multiple projects inside a single repository.
工作区的根目录中必须有一个 pnpm-workspace.yaml
文件。工作区的根目录中也可能有 .npmrc
。
¥A workspace must have a pnpm-workspace.yaml
file in its
root. A workspace also may have an .npmrc
in its root.
如果你正在研究 monorepo 管理,你可能 还想研究 Bit。Bit 在底层使用 pnpm,但会自动执行当前在 pnpm/npm/Yarn 管理的传统工作区中手动完成的许多事情。有一篇关于 bit install
的文章谈到了它:使用 Bit 进行无痛 Monorepo 依赖管理。
¥If you are looking into monorepo management, you might also want to look into Bit.
Bit uses pnpm under the hood but automates a lot of the things that are currently done manually in a traditional workspace managed by pnpm/npm/Yarn. There's an article about bit install
that talks about it: Painless Monorepo Dependency Management with Bit.
工作区协议(工作区:)
¥Workspace protocol (workspace:)
如果 link-workspace-packages 设置为 true
,如果可用包与声明的范围匹配,pnpm 将从工作区链接包。例如,如果 bar
在其依赖中有 "foo": "^1.0.0"
并且 foo@1.0.0
在工作区中,则 foo@1.0.0
链接到 bar
。但是,如果 bar
在依赖中有 "foo": "2.0.0"
,并且 foo@2.0.0
不在工作区中,则将从注册表安装 foo@2.0.0
。这种行为带来了一些不确定性。
¥If link-workspace-packages is set to true
, pnpm will link packages from the workspace if the available packages
match the declared ranges. For instance, foo@1.0.0
is linked into bar
if
bar
has "foo": "^1.0.0"
in its dependencies and foo@1.0.0
is in the workspace. However, if bar
has
"foo": "2.0.0"
in dependencies and foo@2.0.0
is not in the workspace,
foo@2.0.0
will be installed from the registry. This behavior introduces some
uncertainty.
幸运的是,pnpm 支持 workspace:
协议。使用此协议时,pnpm 将拒绝解析本地工作区包以外的任何内容。因此,如果你设置了 "foo": "workspace:2.0.0"
,这次安装将失败,因为工作区中不存在 "foo@2.0.0"
。
¥Luckily, pnpm supports the workspace:
protocol. When
this protocol is used, pnpm will refuse to resolve to anything other than a
local workspace package. So, if you set "foo": "workspace:2.0.0"
, this time
installation will fail because "foo@2.0.0"
isn't present in the workspace.
当 link-workspace-packages 选项设置为 false
时,此协议特别有用。在这种情况下,如果使用 workspace:
协议,pnpm 将仅链接工作区中的包。
¥This protocol is especially useful when the link-workspace-packages option is
set to false
. In that case, pnpm will only link packages from the workspace if
the workspace:
protocol is used.
通过别名引用工作区包
¥Referencing workspace packages through aliases
假设工作区中有一个名为 foo
的包。通常,你会将其引用为 "foo": "workspace:*"
。
¥Let's say you have a package in the workspace named foo
. Usually, you would
reference it as "foo": "workspace:*"
.
如果你想使用不同的别名,以下语法也可以使用:"bar": "workspace:foo@*"
。
¥If you want to use a different alias, the following syntax will work too:
"bar": "workspace:foo@*"
.
在发布之前,别名将转换为常规别名依赖。上面的例子将变成:"bar": "npm:foo@1.0.0"
。
¥Before publish, aliases are converted to regular aliased dependencies. The above
example will become: "bar": "npm:foo@1.0.0"
.
通过相对路径引用工作区包
¥Referencing workspace packages through their relative path
在有 2 个包的工作区中:
¥In a workspace with 2 packages:
+ packages
+ foo
+ bar
bar
的依赖中可能有 foo
声明为 "foo": "workspace:../foo"
。在发布之前,这些规范将转换为所有包管理器支持的常规版本规范。
¥bar
may have foo
in its dependencies declared as
"foo": "workspace:../foo"
. Before publishing, these specs are converted to
regular version specs supported by all package managers.
发布工作区包
¥Publishing workspace packages
当工作区包打包到存档中时(无论是通过 pnpm pack
还是像 pnpm publish
这样的发布命令之一),我们通过以下方式动态替换任何 workspace:
依赖 :
¥When a workspace package is packed into an archive (whether it's through
pnpm pack
or one of the publish commands like pnpm publish
), we dynamically
replace any workspace:
dependency by:
-
目标工作区中的相应版本(如果你使用
workspace:*
、workspace:~
或workspace:^
)¥The corresponding version in the target workspace (if you use
workspace:*
,workspace:~
, orworkspace:^
) -
关联的 semver 范围(对于任何其他范围类型)
¥The associated semver range (for any other range type)
例如,如果工作区中有 foo
、bar
、qar
、zoo
,它们的版本均为 1.5.0
,则如下:
¥So for example, if we have foo
, bar
, qar
, zoo
in the workspace and they all are at version 1.5.0
, the following:
{
"dependencies": {
"foo": "workspace:*",
"bar": "workspace:~",
"qar": "workspace:^",
"zoo": "workspace:^1.5.0"
}
}
将转化为:
¥Will be transformed into:
{
"dependencies": {
"foo": "1.5.0",
"bar": "~1.5.0",
"qar": "^1.5.0",
"zoo": "^1.5.0"
}
}
此功能允许你依赖本地工作区包,同时仍然能够将生成的包发布到远程注册表,而无需中间发布步骤 - 你的消费者将能够像使用任何其他包一样使用你发布的工作区,同时仍然受益于 semver 提供的保证。
¥This feature allows you to depend on your local workspace packages while still being able to publish the resulting packages to the remote registry without needing intermediary publish steps - your consumers will be able to use your published workspaces as any other package, still benefitting from the guarantees semver offers.
发布工作流程
¥Release workflow
对工作区内的包进行版本控制是一项复杂的任务,pnpm 目前没有为此提供内置解决方案。然而,有 2 个经过充分测试的工具可以处理版本控制并支持 pnpm:
¥Versioning packages inside a workspace is a complex task and pnpm currently does not provide a built-in solution for it. However, there are 2 well tested tools that handle versioning and support pnpm:
有关如何使用 Rush 设置存储库,请阅读 此页。
¥For how to set up a repository using Rush, read this page.
要通过 pnpm 使用变更集,请阅读 本指南。
¥For using Changesets with pnpm, read this guide.
故障排除
¥Troubleshooting
如果工作区依赖之间存在循环,pnpm 无法保证脚本将按拓扑顺序运行。如果 pnpm 在安装过程中检测到循环依赖,它将产生警告。如果 pnpm 能够找出哪些依赖导致了循环,它也会显示它们。
¥pnpm cannot guarantee that scripts will be run in topological order if there are cycles between workspace dependencies. If pnpm detects cyclic dependencies during installation, it will produce a warning. If pnpm is able to find out which dependencies are causing the cycles, it will display them too.
如果你看到消息 There are cyclic workspace dependencies
,请检查 dependencies
、optionalDependencies
和 devDependencies
中声明的工作区依赖。
¥If you see the message There are cyclic workspace dependencies
, please inspect workspace dependencies declared in dependencies
, optionalDependencies
and devDependencies
.
使用示例
¥Usage examples
以下是一些使用 pnpm 工作区功能的最流行的开源项目:
¥Here are a few of the most popular open source projects that use the workspace feature of pnpm:
项目 | 星星 | 迁移日期 | 迁移提交 |
---|---|---|---|
Next.js |
| 2022-05-29 | f7b81316aea4fc9962e5e54981a6d559004231aa
| | Material UI |
| 2024-01-03 | a1263e3e5ef8d840252b4857f85b33caa99f471d
| | Vite |
| 2021-09-26 | 3e1cce01d01493d33e50966d0d0fd39a86d229f9
| | Nuxt |
| 2022-10-17 | 74a90c566c936164018c086030c7de65b26a5cb6
| | Vue 3.0 |
| 2021-10-09 | 61c5fbd3e35152f5f32e95bf04d3ee083414cecb
| | Astro |
| 2022-03-08 | 240d88aefe66c7d73b9c713c5da42ae789c011ce
| | n8n |
| 2022-11-09 | 736777385c54d5b20174c9c1fda38bb31fbf14b4
| | Prisma |
| 2021-09-21 | c4c83e788aa16d61bae7a6d00adc8a58b3789a06
| | Novu |
| 2021-12-23 | f2ea61f7d7ac7e12db4c9e70767082841ed98b2b
| | Slidev |
| 2021-04-12 | d6783323eb1ab1fc612577eb63579c8f7bc99c3a
| | Turborepo |
| 2022-03-02 | fd171519ec02a69c9afafc1bc5d9d1b481fba721
| | Quasar 框架 |
¥| 2022-03-02 | fd171519ec02a69c9afafc1bc5d9d1b481fba721
|
| Quasar Framework |
| 2024-03-13 | 7f8e550bb7b6ab639ce423d02008e7f5e61cbf55
| | Element Plus |
| 2021-09-23 | f9e192535ff74d1443f1d9e0c5394fad10428629
| | NextAuth.js |
| 2022-05-03 | 4f29d39521451e859dbdb83179756b372e3dd7aa
| | Ember.js |
| 2023-10-18 | b6b05da662497183434136fb0148e1dec544db04
| | Qwik |
| 2022-11-14 | 021b12f58cca657e0a008119bc711405513e1ee9
| | VueUse |
| 2021-09-25 | 826351ba1d9c514e34426c85f3d69fb9875c7dd9
| | SvelteKit |
| 2021-09-26 | b164420ab26fa04fd0fbe0ac05431f36a89ef193
| | Verdaccio |
| 2021-09-21 | 9dbf73e955fcb70b0a623c5ab89649b95146c744
| | Vercel |
| 2023-01-12 | 9c768b98b71cfc72e8638bf5172be88c39e8fa69
| | Vitest |
| 2021-12-13 | d6ff0ccb819716713f5eab5c046861f4d8e4f988
| | Cycle.js |
| 2021-09-21 | f2187ab6688368edb904b649bd371a658f6a8637
| | Milkdown |
| 2021-09-26 | 4b2e1dd6125bc2198fd1b851c4f00eda70e9b913
| | Nhost |
| 2022-02-07 | 10a1799a1fef2f558f737de3bb6cadda2b50e58f
| | Logto |
| 2021-07-29 | 0b002e07850c8e6d09b35d22fab56d3e99d77043
| | Rollup 插件 |
¥| 2021-07-29 | 0b002e07850c8e6d09b35d22fab56d3e99d77043
|
| Rollup plugins |
| 2021-09-21 | 53fb18c0c2852598200c547a0b1d745d15b5b487
| | icestark |
| 2021-12-16 | 4862326a8de53d02f617e7b1986774fd7540fccd
| | ByteMD |
| 2021-02-18 | 36ef25f1ea1cd0b08752df5f8c832302017bb7fb
| | 刺激组件 |
¥| 2021-02-18 | 36ef25f1ea1cd0b08752df5f8c832302017bb7fb
|
| Stimulus Components |
| 2024-10-26 | 8e100d5b2c02ad5bf0b965822880a60f543f5ec3
|