工作区
什么是工作区?
¥What are workspaces?
工作区是属于同一项目的各个包的名称,Yarn 将安装并链接在一起以简化交叉引用。
¥Workspaces are the name of individual packages that are part of the same project and that Yarn will install and link together to simplify cross-references.
当与存储库结合使用时,此模式通常称为 monorepo。工作区最初由 Lerna 等项目推广,但 Yarn 是第一个为其提供原生支持的包管理器 - 随着我们围绕它们构建更多功能,多年来支持从未停止改进。
¥This pattern is often called monorepo when used in conjunction with a repository. Workspaces were initially popularized by projects like Lerna, but Yarn was the first package manager to provide native support for them - support which never stopped improving over years as we build more features around them.
我们始终尝试对我们提供的功能进行测试,工作区就是一个很好的例子:Yarn 由几十个包组成,每个包都可以根据需要独立部署!
¥We always try to dogfood the features we offer, and workspaces are a prime example: Yarn is composed of a couple of dozens of packages that can each be deployed independently if needed!
我什么时候应该使用工作区?
¥When should I use workspaces?
工作区在许多情况下都很出色,主要情况是:
¥Workspaces shine in many situations, with the main ones being:
-
当项目的核心包周围有各种附加组件时;例如,Babel 就是这种情况。
¥When a project has a core package surrounded with various add-ons; this is for example the case for Babel.
-
当你需要发布多个包并希望避免你的贡献者在想要进行更改时不得不在许多单独的存储库上打开 PR 时。例如,这是 Jest 的情况。
¥When you need to publish multiple packages and want to avoid your contributors having to open PRs on many separate repositories whenever they want to make a change. This is for example the case for Jest.
-
或者当项目希望在其代码中保持严格的界限并避免成为纠缠不清的巨石时。例如,对于 Yarn 本身或许多企业代码库来说就是这种情况。
¥Or when projects want to keep strict boundaries within their code and avoid becoming an entangled monolith. This is for example the case for Yarn itself, or many enterprise codebases.
关于 monorepos 是好是坏已经有很多讨论,双方都有合理的参数。我们的团队多年来一直使用 monorepos,并且使用 Yarn 提供的工具,其价值始终远远超过其缺点。如果你需要为你的项目创建另一个包,请考虑将它们分组在一起是否有意义。
¥There's been a significant amount of discussions about whether monorepos are good or bad, with decent arguments on both side. Our team worked with monorepos for years at this point, and with the tooling Yarn provides, the value has always outweighed the cons by a large margin. If you need to create another package for your project, consider whether grouping them together makes sense.
你无需将代码拆分到多个工作区中才能使用它们。例如,Clipanion 存储库 仅使用两个工作区,一个用于库,一个用于其网站。此模式避免了混合依赖,同时也使编写影响代码和文档的 PR 变得容易。
¥You don't need to split your code in many workspaces for them to become useful. For instance, the Clipanion repository uses only two workspaces, one for the library and one for its website. This patterns avoids mixing dependencies while also making it easy to author PRs that affect both the code and the documentation.
如何声明工作区?
¥How are workspaces declared?
要声明工作区,你所要做的就是将 workspaces
数组添加到根 package.json
文件,并列出指向工作区文件夹的相对 glob 模式。在以下示例中,packages
文件夹中的所有子目录都将成为工作区。
¥To declare a workspace, all you have to do is add a