|
1 | 1 | # yarn-plugin-catalogs |
2 | 2 |
|
3 | | -A Yarn plugin that enables "Catalogs" - a workspace feature for defining dependency version ranges as reusable constants across your project. |
| 3 | +**Extended options plugin for Yarn's Catalogs feature.** |
4 | 4 |
|
5 | | -Highly inspired by [pnpm Catalogs](https://pnpm.io/catalogs). |
| 5 | +Starting from Yarn 4.10.0, Catalogs are natively supported by Yarn. This plugin provides **additional options** on top of Yarn's implementation, including default alias groups, workspace ignoring, and validation settings. |
6 | 6 |
|
7 | | -## Why use Catalogs? |
| 7 | +For basic Catalogs functionality, refer to [Yarn's official documentation](https://yarnpkg.com/features/catalogs). |
8 | 8 |
|
9 | | -In larger projects, especially monorepos, it's common to have the same dependencies used across multiple packages. Catalogs help you: |
| 9 | +## Requirements |
10 | 10 |
|
11 | | -- **Maintain consistent versions** across your workspace |
12 | | -- **Simplify upgrades** by editing versions in just one place |
13 | | -- **Reduce merge conflicts** in package.json files |
14 | | -- **Standardize dependencies** across teams and repositories |
| 11 | +- **Yarn 4.10.0 or higher** (for Catalogs support) |
15 | 12 |
|
16 | | -## Installation |
17 | | - |
18 | | -```bash |
19 | | -yarn plugin import https://raw.githubusercontent.com/toss/yarn-plugin-catalogs/main/bundles/%40yarnpkg/plugin-catalogs.js |
20 | | -``` |
21 | | - |
22 | | -## Usage |
23 | | - |
24 | | -### 1. Add `catalogs` to `.yarnrc.yml` |
| 13 | +## Why use this plugin? |
25 | 14 |
|
26 | | -```yaml |
27 | | -# in .yarnrc.yml |
| 15 | +While Yarn 4.10+ provides native catalog support, this plugin extends that functionality with: |
28 | 16 |
|
29 | | -catalogs: |
30 | | - list: |
31 | | - # Root catalogs (can be referenced with just "catalog:") |
32 | | - react: 18.0.0 |
33 | | - react-dom: 18.0.0 |
34 | | - typescript: 5.1.6 |
35 | | - |
36 | | - # Named catalogs (must be referenced with "catalog:name") |
37 | | - beta: |
38 | | - react: 19.0.0 |
39 | | - react-dom: 19.0.0 |
40 | | - |
41 | | - legacy: |
42 | | - react: 17.0.2 |
43 | | - react-dom: 17.0.2 |
44 | | -``` |
| 17 | +- **Default alias groups** - Automatically apply catalog protocols when adding dependencies |
| 18 | +- **Workspace ignoring** - Disable catalogs for specific workspaces using glob patterns |
| 19 | +- **Validation levels** - Enforce catalog usage with configurable warning or error levels |
| 20 | +- **Group-specific validation** - Different validation rules for different catalog groups |
45 | 21 |
|
46 | | -### 2. Reference catalog versions in your package.json |
47 | | -
|
48 | | -```json |
49 | | -{ |
50 | | - "dependencies": { |
51 | | - "react": "catalog:", // Uses version from root catalog |
52 | | - "react-dom": "catalog:", // Uses version from root catalog |
53 | | - "typescript": "catalog:", // Uses version from root catalog |
| 22 | +## Installation |
54 | 23 |
|
55 | | - "next": "catalog:beta", // Uses version from beta catalog |
56 | | - "styled-components": "catalog:legacy" // Uses version from legacy catalog |
57 | | - } |
58 | | -} |
| 24 | +```bash |
| 25 | +yarn plugin import https://raw.githubusercontent.com/toss/yarn-plugin-catalogs/main/bundles/%40yarnpkg/plugin-catalogs.js |
59 | 26 | ``` |
60 | 27 |
|
61 | | -### Advanced Usage |
| 28 | +## Features |
62 | 29 |
|
63 | | -#### Protocol Support |
| 30 | +This plugin adds the `catalogsOptions` configuration to `.yarnrc.yml`: |
64 | 31 |
|
65 | | -The plugin automatically adds the `npm:` protocol if none is specified in the catalog: |
| 32 | +### Default Alias Groups |
66 | 33 |
|
67 | | -```yaml |
68 | | -# In .yarnrc.yml |
69 | | -catalogs: |
70 | | - list: |
71 | | - react: 18.0.0 // Will be transformed to "npm:18.0.0" |
72 | | - next: "npm:13.4.9" // Protocol explicitly specified |
73 | | - lodash: "patch:[email protected]#./.patches/lodash.patch" // Custom protocol |
74 | | -``` |
75 | | -
|
76 | | -#### Scoped Packages |
77 | | -
|
78 | | -Scoped packages work as expected: |
| 34 | +Automatically applies catalog protocols when adding dependencies. |
79 | 35 |
|
80 | 36 | ```yaml |
81 | | -# In .yarnrc.yml |
82 | | -catalogs: |
83 | | - list: |
84 | | - "@emotion/react": 11.11.1 |
85 | | - "@types/react": 18.2.15 |
86 | | - |
87 | | - beta: |
88 | | - "@tanstack/react-query": 5.0.0 |
89 | | -``` |
90 | | -
|
91 | | -```json |
92 | | -{ |
93 | | - "dependencies": { |
94 | | - "@emotion/react": "catalog:", |
95 | | - "@types/react": "catalog:", |
96 | | - "@tanstack/react-query": "catalog:beta" |
97 | | - } |
98 | | -} |
99 | | -``` |
100 | | - |
101 | | -#### Default Catalogs |
102 | | - |
103 | | -The `default` option automatically selects a catalog for `yarn add` when no catalog name is specified. If multiple catalogs are listed, priority is determined by their order. |
| 37 | +# in .yarnrc.yml |
| 38 | +catalogsOptions: |
| 39 | + default: [beta, legacy] # Priority order |
104 | 40 |
|
105 | | -```yaml |
106 | | -# In .yarnrc.yml |
107 | 41 | catalogs: |
108 | | - options: |
109 | | - default: [beta, legacy] |
110 | | - list: |
111 | | - beta: |
112 | | - react: 19.0.0 |
113 | | - legacy: |
114 | | - react: 17.0.2 |
115 | | - typescript: 4.8.3 |
| 42 | + beta: |
| 43 | + react: npm:19.0.0 |
| 44 | + legacy: |
| 45 | + react: npm:17.0.2 |
| 46 | + typescript: npm:4.8.3 |
116 | 47 | ``` |
117 | 48 |
|
118 | 49 | ```sh |
119 | | -yarn add react # Same as `yarn add react@catalog:beta` |
120 | | -yarn add typescript # Same as `yarn add typescript@catalog:legacy` |
| 50 | +yarn add react # Automatically becomes: yarn add react@catalog:beta |
| 51 | +yarn add typescript # Automatically becomes: yarn add typescript@catalog:legacy |
121 | 52 | ``` |
122 | 53 |
|
123 | | -To use the root catalogs as the default, just set the `default` option to `root`: |
| 54 | +#### Using `root` as Default |
124 | 55 |
|
125 | 56 | ```yaml |
126 | | -# In .yarnrc.yml |
127 | | -catalogs: |
128 | | - options: |
129 | | - default: [root] |
130 | | - list: |
131 | | - react: 19.0.0 |
132 | | - react-dom: 19.0.0 |
| 57 | +catalogsOptions: |
| 58 | + default: [root] # Use root catalog as default |
| 59 | + |
| 60 | +catalog: |
| 61 | + react: npm:19.0.0 |
| 62 | + react-dom: npm:19.0.0 |
133 | 63 | ``` |
134 | 64 |
|
135 | 65 | ```sh |
136 | | -yarn add react # Same as `yarn add react@catalog:` |
| 66 | +yarn add react # Same as: yarn add react@catalog: |
137 | 67 | ``` |
138 | 68 |
|
139 | | -The `default` option can also be set to a non-list value that defines a selection rule instead of specifying a catalog name. For example, `max` selects the most frequently used catalog in package.json as the default. |
| 69 | +#### Using `max` Strategy |
| 70 | + |
| 71 | +The `max` option selects the most frequently used catalog in your package.json. |
140 | 72 |
|
141 | 73 | ```yaml |
142 | | -# In .yarnrc.yml |
| 74 | +catalogsOptions: |
| 75 | + default: max |
| 76 | + |
143 | 77 | catalogs: |
144 | | - options: |
145 | | - default: max |
146 | | - list: |
147 | | - beta: |
148 | | - react: 19.0.0 |
149 | | - react-dom: 19.0.0 |
150 | | - typescript: 5.1.6 |
151 | | - next: 15.3.0 |
152 | | - legacy: |
153 | | - react: 17.0.2 |
154 | | - react-dom: 17.0.2 |
155 | | - typescript: 4.8.3 |
156 | | - next: 13.4.9 |
| 78 | + beta: |
| 79 | + react: npm:19.0.0 |
| 80 | + typescript: npm:5.1.6 |
| 81 | + legacy: |
| 82 | + react: npm:17.0.2 |
| 83 | + typescript: npm:4.8.3 |
157 | 84 | ``` |
158 | 85 |
|
159 | 86 | ```json |
160 | 87 | { |
161 | 88 | "dependencies": { |
162 | 89 | "react": "catalog:beta", |
163 | 90 | "react-dom": "catalog:beta", |
164 | | - "typescript": "catalog:legacy", |
| 91 | + "typescript": "catalog:legacy" |
165 | 92 | } |
166 | 93 | } |
167 | 94 | ``` |
168 | 95 |
|
169 | 96 | ```sh |
170 | | -yarn add next # Same as `yarn add next@catalog:beta` |
171 | | - # because beta is the most frequently used catalog in package.json |
| 97 | +yarn add next # Will use "catalog:beta" (most frequent) |
172 | 98 | ``` |
173 | 99 |
|
174 | | -Currently, only the `max` option is available, but additional options may be added in the future. |
175 | | - |
176 | | -#### Disabling Catalogs |
| 100 | +### Ignoring Workspaces |
177 | 101 |
|
178 | | -You can disable catalogs for certain workspaces by listing their names in the `ignoredWorkspaces` option. You can also use glob patterns here. |
| 102 | +Disable catalog features for specific workspaces using glob patterns. |
179 | 103 |
|
180 | 104 | ```yaml |
181 | | -# In .yarnrc.yml |
182 | | -catalogs: |
183 | | - options: |
184 | | - ignoredWorkspaces: [package, test-*] |
185 | | - list: |
186 | | - react: 19.0.0 |
187 | | - react-dom: 19.0.0 |
188 | | -``` |
| 105 | +catalogsOptions: |
| 106 | + ignoredWorkspaces: [package, test-*] |
189 | 107 |
|
190 | | -The ignored workspaces cannot use the catalog protocol, and the default alias group is also disabled for them. |
| 108 | +catalog: |
| 109 | + react: npm:19.0.0 |
| 110 | +``` |
191 | 111 |
|
192 | | -#### Validation |
| 112 | +Ignored workspaces: |
| 113 | +- Cannot use the `catalog:` protocol |
| 114 | +- Default alias groups are disabled |
| 115 | +- Validation is skipped |
193 | 116 |
|
194 | | -The `validation` option allows you to configure whether to enforce catalog usage when dependencies listed in the catalog are used with actual versions instead of the catalog protocol. |
| 117 | +### Validation |
195 | 118 |
|
196 | | -- `warn` (default): Shows warning when catalog dependencies don't use the catalog protocol. |
197 | | -- `strict`: Throws error and prevents installation when catalog dependencies don't use catalog protocol. |
198 | | -- `off`: Disables validation. |
| 119 | +Enforce catalog usage when dependencies listed in catalogs are added with actual versions. |
199 | 120 |
|
200 | 121 | ```yaml |
201 | | -# In .yarnrc.yml |
202 | | -catalogs: |
203 | | - options: |
204 | | - validation: warn # "warn" | "strict" | "off" |
205 | | - list: |
206 | | - react: 19.0.0 |
207 | | - react-dom: 19.0.0 |
208 | | -``` |
209 | | - |
210 | | -The validation level can be configured for individual catalog groups. |
| 122 | +catalogsOptions: |
| 123 | + validation: warn # "warn" | "strict" | "off" |
211 | 124 |
|
212 | | -```yaml |
213 | | -# In .yarnrc.yml |
214 | | -catalogs: |
215 | | - options: |
216 | | - validation: |
217 | | - beta: warn |
218 | | - stable: strict |
219 | | - legacy: off |
220 | | - list: |
221 | | - beta: |
222 | | - react: 18.0.0 |
223 | | - stable: |
224 | | - next: 14.0.0 |
225 | | - legacy: |
226 | | - jquery: 3.6.0 |
| 125 | +catalog: |
| 126 | + react: npm:19.0.0 |
| 127 | + lodash: npm:4.17.21 |
227 | 128 | ``` |
228 | 129 |
|
229 | | -When a package exists in multiple groups, the strictest validation level applies (`strict` > `warn` > `off`). Validation settings follow inheritance chains - if a child group doesn't have an explicit setting, it uses the default validation level. |
| 130 | +- **`warn`** (default): Shows warnings when catalog dependencies don't use the catalog protocol |
| 131 | +- **`strict`**: Throws errors and prevents installation |
| 132 | +- **`off`**: Disables validation |
230 | 133 |
|
231 | | -#### Catalog Group Inheritance |
| 134 | +#### Group-Specific Validation |
232 | 135 |
|
233 | | -Catalog groups can inherit from parent groups using the `/` delimiter. Child groups inherit all dependencies from their parent groups and can override specific versions. |
| 136 | +Configure different validation levels for different catalog groups: |
234 | 137 |
|
235 | 138 | ```yaml |
236 | | -# In .yarnrc.yml |
| 139 | +catalogsOptions: |
| 140 | + validation: |
| 141 | + beta: warn |
| 142 | + stable: strict |
| 143 | + legacy: off |
| 144 | +
|
237 | 145 | catalogs: |
238 | | - list: |
239 | | - stable: |
240 | | - react: 18.0.0 |
241 | | - lodash: 4.17.21 |
242 | | - typescript: 5.1.6 |
243 | | - stable/canary: |
244 | | - react: 18.2.0 # Overrides parent version |
245 | | - typescript: 5.2.0 # Overrides parent version |
246 | | - # lodash: 4.17.21 # Inherited from stable |
247 | | - stable/canary/next: |
248 | | - react: 18.3.1 # Overrides parent version |
249 | | - # typescript: 5.2.0 # Inherited from stable/canary |
250 | | - # lodash: 4.17.21 # Inherited from stable |
| 146 | + beta: |
| 147 | + react: npm:18.0.0 |
| 148 | + stable: |
| 149 | + next: npm:14.0.0 |
| 150 | + legacy: |
| 151 | + jquery: npm:3.6.0 |
251 | 152 | ``` |
252 | 153 |
|
253 | | -Dependencies are resolved by searching from the most specific group upward until a match is found. An error will be thrown if any parent group in the inheritance chain doesn't exist. |
| 154 | +When a package exists in multiple groups, the strictest validation level applies (`strict` > `warn` > `off`). |
254 | 155 |
|
255 | 156 | ## Contributing |
256 | 157 |
|
|
0 commit comments