Skip to main content

目录

概述

¥Overview

目录为工作区提供集中的依赖版本管理。受 pnpm 的目录功能 的启发,它允许你在 .yarnrc.yml 中定义版本范围,并使用 catalog: 协议在多个 package.json 文件中引用这些版本范围。

¥Catalogs provide centralized dependency version management for workspaces. Inspired by pnpm's catalog functionality, they allow you to define version ranges in your .yarnrc.yml and reference them across multiple package.json files using the catalog: protocol.

这消除了版本重复,确保了包之间的一致性,并简化了依赖升级 - 只需更新一个地方,而不是多个文件。

¥This eliminates version duplication, ensures consistency across packages, and makes dependency upgrades much simpler — update one place instead of many files.

信息

从 Yarn 4.10.0 开始,目录插件默认包含在内。

¥The catalog plugin is included by default starting from Yarn 4.10.0.

基本用法

¥Basic usage

.yarnrc.yml 中定义目录:

¥Define a catalog in your .yarnrc.yml:

catalog:
react: ^18.3.1
lodash: ^4.17.21

package.json 中引用条目:

¥Reference entries in your package.json:

{
"dependencies": {
"react": "catalog:",
"lodash": "catalog:"
}
}

命名目录

¥Named catalogs

你可以使用 catalogs 字段定义多个命名目录,用于不同的用途:

¥You can define multiple named catalogs using the catalogs field for different purposes:

catalog:
lodash: ^4.17.21

catalogs:
react18:
react: ^18.3.1
react-dom: ^18.3.1
react17:
react: ^17.0.2
react-dom: ^17.0.2

通过指定名称引用命名目录:

¥Reference named catalogs by specifying the name:

{
"dependencies": {
"lodash": "catalog:",
"react": "catalog:react18"
}
}

发布

¥Publishing

发布包时,catalog: 协议会自动替换为实际的版本范围,从而确保与其他包管理器的兼容性:

¥When publishing packages, the catalog: protocol is automatically replaced with actual version ranges, ensuring compatibility with other package managers:

// Source package.json
{
"dependencies": {
"react": "catalog:react18"
}
}

// Published package.json
{
"dependencies": {
"react": "^18.3.1"
}
}

支持的字段

¥Supported fields

catalog: 协议适用于 dependenciesdevDependenciespeerDependencies

¥The catalog: protocol works in dependencies, devDependencies, peerDependencies.