Skip to content

Commit 7d3c6bf

Browse files
author
Maël Nison
committed
Adds the workspace documentation
1 parent 34a266a commit 7d3c6bf

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

_data/guides.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@
204204
- id: docs_prune_offline_mirror
205205
path: /docs/prune-offline-mirror
206206

207+
- id: docs_workspaces
208+
title: docs_workspaces_title
209+
description: docs_workspaces_description
210+
pages:
211+
- id: docs_workspaces
212+
path: /docs/workspaces
213+
207214
- id: yarn_organization
208215
title: yarn_organization_title
209216
description: yarn_organization_description

_data/i18n/en.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ docs_offline_mirror_description: |
200200
201201
docs_prune_offline_mirror: Pruning an Offline Mirror
202202

203+
docs_workspaces: Workspaces
204+
docs_workspaces_title: Workspaces
205+
docs_workspaces_description: |
206+
Link together your projects for easier maintenance.
207+
203208
yarn_organization_title: Yarn Organization
204209
yarn_organization_description: |
205210
The Yarn organization is a collaboration of many companies and
@@ -349,7 +354,7 @@ script:
349354
display_all: Display all
350355
hide: Hide
351356

352-
not_found:
357+
not_found:
353358
whoa: Whoa, {package_name} does not exist yet
354359
yours: But that means it is now yours!
355360
make: Make your package

lang/en/docs/workspaces.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
id: docs_workspaces
3+
guide: docs_workspaces
4+
layout: guide
5+
---
6+
7+
{% include vars.html %}
8+
9+
Workspaces are a new way to setup your package architecture that's available by default starting from Yarn 1.0. It allows you to setup multiple packages in such a way that you only need to run `yarn install` once to install all of them in a single pass.
10+
11+
### Why would you want to do this?
12+
13+
- Your dependencies can be linked together, which means that your workspaces can depend on one another while always using the most up-to-date code available. This is also a better mechanism than `yarn link` since it only affects your workspace tree rather than your whole system.
14+
15+
- All your project dependencies will be installed together, giving Yarn more latitude to better optimize them.
16+
17+
- Yarn will use a single lockfile rather than a different one for each project, which means less conflicts and easier reviews.
18+
19+
### How to use it?
20+
21+
Add the following in a `package.json` file. Starting from now on, we'll call this directory the "workspace root":
22+
23+
**package.json**
24+
25+
```json
26+
{
27+
"private": true,
28+
"workspaces": [
29+
"workspace-a",
30+
"workspace-b"
31+
]
32+
}
33+
```
34+
35+
Note that the `private: true` is required! Workspaces are not meant to be published, so we've added this safety measure to make sure that nothing can accidentaly expose them.
36+
37+
After this file has been created, create two new subfolders named `workspace-a` and `workspace-b`. In each of them, create another `package.json` file with the following content:
38+
39+
**workspace-a/package.json:**
40+
41+
```json
42+
{
43+
"name": "workspace-a",
44+
"version": "1.0.0",
45+
46+
"dependencies": {
47+
"cross-env": "5.0.5"
48+
}
49+
}
50+
```
51+
52+
**workspace-b/package.json:**
53+
54+
```json
55+
{
56+
"name": "workspace-b",
57+
"version": "1.0.0",
58+
59+
"dependencies": {
60+
"cross-env": "5.0.5",
61+
"workspace-a": "1.0.0"
62+
}
63+
}
64+
```
65+
66+
Finally, run `yarn install` somewhere, ideally inside the workspace root. If everything works well, you should now have a similar file hierarchy:
67+
68+
```
69+
/package.json
70+
/yarn.lock
71+
72+
/node_modules
73+
/node_modules/cross-env
74+
/node_modules/workspace-a -> /workspace-a
75+
76+
/workspace-a/package.json
77+
/workspace-b/package.json
78+
```
79+
80+
And that's it! Requiring `workspace-a` from a file located in `workspace-b` will now use the exact code currently located inside your project rather than what is published on Github, and the `cross-env` package has been correctly deduped and put at the root of your project to be used by both `workspace-a` and `workspace-b`.
81+
82+
### How does it compare to Lerna?
83+
84+
Yarn's workspaces are the low-level primitives that tools like Lerna can (and [do](https://github.com/lerna/lerna/pull/899)!) use. They will never try to support the high-level feature that Lerna offers, but by implementing the core logic of the resolution and linking steps inside Yarn itself we hope to enable new usages and improve performance.
85+
86+
### Tips & Tricks
87+
88+
- The `workspaces` field is an array containing the paths to each workspace. Since it might be tedious to keep track of each of them, this field also accepts glob patterns! For example, Babel reference all of their packages through a single `packages/*` directive.
89+
90+
- Workspaces are stable enough to be used in large-scale applications and shouldn't change anything to the way the regular installs work, but if you think they're breaking something, you can disable them by adding the following line into your Yarnrc file:
91+
92+
```
93+
workspaces-experimental false
94+
```
95+
96+
### Limitations & Caveats
97+
98+
- The package layout will be different between your workspace and what your users will get (the workspace dependencies will be hoisted higher into the filesystem hierarchy). Making assumptions about this layout was already hazardous since the hoisting process is not standardized, so theoretically nothing new here.
99+
100+
- In the example above, if `workspace-b` depends on a different version than the one referenced in `workspace-a`'s package.json, the dependency will be installed from Github rather than linked from your local filesystem. This is because some packages actually need to use the previous versions in order to build the new ones (Babel is one of them).
101+
102+
- Workspaces must be children of the workspace root in term of folder hierarchy. You cannot and must not reference a workspace that is located outside of this filesystem hierarchy.
103+
104+
- Nested workspaces are not supported at this time.

0 commit comments

Comments
 (0)