可扩展性
概述
¥Overview
许多 Yarn 用户有许多不同的用例,虽然我们试图为最常见的问题找到令人满意的解决方案,但我们的团队可能没有足够的带宽来研究和维护一些最奇特的问题。为了避免在你遇到 Yarn 尚未开箱即用支持的新情况时出现阻塞,我们提供了一个非常强大的 API,你可以在自己的自定义插件中利用它。
¥Many Yarn users have many different use cases, and while we try to find satisfying solutions to most common problems, our team may not have the bandwidth to research and maintain some of the most exotic ones. To avoid blocking you should you face a novel situation that Yarn doesn't support out of the box yet, we provide a very powerful API that you can leverage in your own custom plugins.
插件本质上是配置中列出的小脚本,Yarn 将在启动时动态需要这些脚本。
¥Plugins are, in essence, small scripts listed in your configuration that Yarn will dynamically require at startup.
插件能做什么?
¥What can plugins do?
-
插件可以添加新的解析器。解析器是负责将依赖范围(例如
^1.2.0
)转换为完全限定的包引用(例如npm:1.2.0
)的组件。通过实现解析器,你可以告诉 Yarn 哪些版本是特定范围的有效候选者。¥Plugins can add new resolvers. Resolvers are the components tasked from converting dependency ranges (for example
^1.2.0
) into fully-qualified package references (for examplenpm:1.2.0
). By implementing a resolver, you can tell Yarn which versions are valid candidates to a specific range. -
插件可以添加新的获取器。获取器是获取我们在上一步中提到的完全合格的包引用(例如
npm:1.2.0
)并知道如何获取它们引用的实际包数据的组件。获取器可以使用远程源(例如 npm 注册表),但也可以直接从磁盘上的位置(或任何其他数据源)找到包。¥Plugins can add new fetchers. Fetchers are the components that take the fully-qualified package references we mentioned in the previous step (for example
npm:1.2.0
) and know how to obtain the actual package data they reference. Fetchers can work with remote sources (for example the npm registry), but can also find the packages directly from their location on the disk (or any other data source). -
插件可以添加新的链接器。一旦找到所有软件包并准备好安装,Yarn 将调用链接器来生成安装目标正常工作所需的文件。例如,PnP 链接器生成一个 Node.js 加载器文件,node-modules 链接器生成一个
node_modules
文件夹,而假设的 Python 链接器将生成一个虚拟环境。¥Plugins can add new linkers. Once all the packages have been located and are ready for installation, Yarn will call the linkers to generate the files needed for the install targets to work properly. As an example, the PnP linker generates a Node.js loader file, the node-modules linker generates a
node_modules
folder, and an hypothetical Python linker would generate a virtualenv. -
插件可以添加新命令。每个插件都可以发送任意数量的命令,这些命令将被注入我们的 CLI(也可通过
yarn --help
获得)。由于 Yarn 插件与正在运行的 Yarn 进程动态链接,因此它们可以访问完整的 Yarn API,就像任何其他官方命令一样。如果 Yarn 缺少你需要的东西,这可以让你尝试自己的自定义逻辑。¥Plugins can add new commands. Each plugin can ship as many commands as they see fit, which will be injected into our CLI (also making them available through
yarn --help
). Because the Yarn plugins are dynamically linked with the running Yarn process, they have access to the full Yarn API, just like any other official command. This lets you experiment with your own custom logic, should Yarn be missing something you need. -
插件可以注册某些事件。Yarn 有一个称为 "hooks" 的概念,其中事件在包管理器的生命周期中定期触发。插件可以注册这些钩子,以便根据核心允许的内容添加自己的逻辑。例如,每次执行安装时都会调用
afterAllInstalled
钩子。¥Plugins can register to some events. Yarn has a concept known as "hooks", where events are periodically triggered during the lifecycle of the package manager. Plugins can register to those hooks in order to add their own logic depending on what the core allows. For example, the
afterAllInstalled
hook will be called each time an install is performed.
如何编写插件?
¥How to write a plugin?
我们有一个教程!前往 插件教程。
¥We have a tutorial for this! Head over to Plugin Tutorial.
你将希望从钩子和命令中使用 Yarn API - 学习它可能看起来很可怕,但你可以访问最好的示例:Yarn 本身!
¥You'll want to use the Yarn API from your hooks and commands - learning it may look scary, but you have access to the best examples there is: Yarn itself!
例如,yarn workspaces focus
的实现仅与 一百行代码 有关,如果你希望根据自己的逻辑实现部分安装,这是一个很好的起点。
¥For example, the implementation of yarn workspaces focus
is only about a hundred lines of code, making it a good starting point if you wish to implement partial installs according to your own logic.