进一步了解:Yarn PnP
这些步骤完全是可选的!
¥These steps are completely optional!
虽然我们建议在新项目中使用 Yarn Plug'n'Play,但在现有项目上启用它可能需要时间投入。如果你愿意,可以跳过此部分,并在你有更多时间和/或从中获得具体好处时再回来。
¥While we recommend to use Yarn Plug'n'Play for new projects, enabling it on existing projects may require a time investment. Feel free to skip this part if you prefer, and to come back to it whenever you have more time and/or a concrete benefit to get from it.
调用医生
¥Calling the Doctor
Plug'n'Play 强制执行严格的 依赖规则。如果应用中的某些内容依赖于未列出的依赖,你将收到错误,这可能会导致你的应用变得不稳定。
¥Plug'n'Play enforces strict dependency rules. You'll get errors should something in your application rely on unlisted dependencies which could cause your application to become unstable.
为了快速检测哪些地方可能依赖不安全的模式,Yarn 提供了一个名为 Doctor 的工具。只需在你的项目中运行 yarn dlx @yarnpkg/doctor
,Doctor 就会开始查看你的源文件以检测任何潜在的问题模式。
¥To quickly detect which places may rely on unsafe patterns, Yarn provides a tool called the Doctor. Just run yarn dlx @yarnpkg/doctor
in your project and the Doctor will start looking at your source files to detect any potentially problematic pattern.
示例
¥Example
例如,这是 Doctor 过去对 webpack-dev-server
的说法:
¥For example, here's what the Doctor used to say about webpack-dev-server
:
➤ YN0000: Found 1 package(s) to process
➤ YN0000: For a grand total of 236 file(s) to validate
➤ YN0000: ┌ /webpack-dev-server/package.json
➤ YN0000: │ /webpack-dev-server/test/testSequencer.js:5:19: Undeclared dependency on @jest/test-sequencer
➤ YN0000: │ /webpack-dev-server/client-src/default/webpack.config.js:12:14: Webpack configs from non-private packages should avoid referencing loaders without require.resolve
➤ YN0000: │ /webpack-dev-server/test/server/contentBase-option.test.js:68:8: Strings should avoid referencing the node_modules directory (prefer require.resolve)
➤ YN0000: └ Completed in 5.12s
➤ YN0000: Failed with errors in 5.12s
我们可以看到 Doctor 发现了几个合法的问题:
¥We can see that the Doctor spotted a couple of legitimate issues:
-
testSequencer.js
依赖于@jest/test-sequencer
,但未将其列为适当的依赖 - 在 Yarn Plug'n'Play 下,这将在运行时报告为错误,因为没有什么可以保证@jest/test-sequencer
的版本与包测试过的版本相匹配。¥
testSequencer.js
depends on@jest/test-sequencer
without listing it as a proper dependency - which would be reported as an error at runtime under Yarn Plug'n'Play, as nothing guarantees that the version of@jest/test-sequencer
would match what the package has been tested with. -
webpack.config.js
引用加载器而不将其名称传递给require.resolve
- 这是不安全的,因为这意味着加载器将相对于webpack
包而不是webpack-dev-server
的依赖进行解析。¥
webpack.config.js
references a loader without passing its name throughrequire.resolve
- this is unsafe, as it means the loader will be resolved relative to thewebpack
package, rather thanwebpack-dev-server
's dependencies. -
contentBase-option.test.js
检查node_modules
文件夹的内容 - 在 Plug'n'Play 下将不再存在。¥
contentBase-option.test.js
checks the content of thenode_modules
folder - which wouldn't exist anymore under Plug'n'Play.
启用 Yarn PnP
¥Enabling Yarn PnP
-
查看你的
.yarnrc.yml
文件中的nodeLinker
设置。¥Look into your
.yarnrc.yml
file for thenodeLinker
setting. -
如果你找不到它,或者它被设置为
pnp
,那么一切都很好:你已经在使用 Yarn Plug'n'Play!¥If you don't find it, or if it's set to
pnp
, then it's all good: you're already using Yarn Plug'n'Play! -
否则,将其从配置文件中删除并运行
yarn install
。¥Otherwise, remove it from your configuration file and run
yarn install
. -
提交更改。
¥Commit the changes.
要查找什么
¥What to look for
现在你应该有一个可以正常工作的 Yarn Plug'n'Play 设置,但你的存储库可能仍需要一些额外的注意。需要记住的一些事项:
¥Now you should have a working Yarn Plug'n'Play setup, but your repository might still need some extra care. Some things to keep in mind:
-
没有
node_modules
文件夹。改用require.resolve
。¥There are no
node_modules
folders. Userequire.resolve
instead. -
没有
.bin
文件夹。如果你依赖它们,请改用yarn run bin
。¥There are no
.bin
folders. If you relied on them, useyarn run bin
instead. -
将不在
scripts
字段内的任何对node
的调用替换为yarn node
。¥Replace any calls to
node
that are not inside thescripts
field byyarn node
. -
现在需要手动调用自定义预钩子(例如
prestart
)(yarn prestart
)。¥Custom pre-hooks (e.g.
prestart
) need to be called manually now (yarn prestart
).
所有这些以及更多内容均记录在以下章节中。一般来说,我们建议你此时尝试运行你的应用并查看哪些地方出现故障,然后在此处查看有关如何更正安装的提示。
¥All of this and more is documented in the following sections. In general, we advise you at this point to try to run your application and see what breaks, then check here to find out tips on how to correct your install.
编辑器支持
¥Editor support
我们在这里只介绍 VSCode,但我们有一个专门的 文档页面 涵盖更多 IDE!
¥We only cover VSCode here, but we have a dedicated documentation page covering more IDEs!
确保 typescript
、eslint
、prettier
、...IDE 扩展通常使用的所有依赖都列在项目的顶层(而不是任意工作区中)。
¥Make sure that typescript
, eslint
, prettier
, ... all dependencies typically used by your IDE extensions are listed at the top level of the project (rather than in an arbitrary workspace).
-
安装 ZipFS VSCode 扩展。
¥Install the ZipFS VSCode extension.
-
运行
yarn dlx @yarnpkg/sdks vscode
并提交更改。¥Run
yarn dlx @yarnpkg/sdks vscode
and commit the changes. -
对于 TypeScript,不要忘记在 VSCode 中选择 使用工作区版本。
¥For TypeScript, don't forget to select Use Workspace Version in VSCode.
常规建议
¥General Advices