Skip to content

Commit b9ccd5c

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

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

lang/en/docs/workspaces.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
id: docs_workspaces
3+
guide: docs_workspaces
4+
layout: guide
5+
---
6+
7+
{% include vars.html %}
8+
9+
Workspace 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 a workspace can depend on another and always use the most up-to-date version of it, without having to run `yarn install` again like with the `file:` protocol, and with better safety mechanisms than with `yarn link`.
14+
15+
- All your project dependencies will be installed together, which gives Yarn more latitude to better optimize them.
16+
17+
- Yarn will use a single lockfile rather than a different one for each project.
18+
19+
### How to use it?
20+
21+
Add the following in a `package.json` file:
22+
23+
```json
24+
{
25+
"private": true,
26+
"workspaces": [
27+
"workspace-a",
28+
"workspace-b"
29+
]
30+
}
31+
```
32+
33+
Then create two sub-folders named `workspace-a` and `workspace-b`. In each of them, create another `package.json` file with the following content:
34+
35+
**workspace-a/package.json:**
36+
37+
```json
38+
{
39+
"name": "workspace-a",
40+
"version": "1.0.0",
41+
"dependencies": {
42+
"cross-env": "5.0.5"
43+
}
44+
}
45+
```
46+
47+
**workspace-b/package.json:**
48+
49+
```json
50+
{
51+
"name": "workspace-b",
52+
"version": "1.0.0",
53+
"dependencies": {
54+
"cross-env": "5.0.5",
55+
"workspace-a": "1.0.0"
56+
}
57+
}
58+
```
59+
60+
Finally, run `yarn install` somewhere, ideally inside the folder that contains the `workspaces` property (we call this folder the workspace root). If everything works well, you should now have a similar file hierarchy:
61+
62+
```
63+
/package.json
64+
/yarn.lock
65+
66+
/node_modules
67+
/node_modules/cross-env
68+
/node_modules/workspace-a -> /workspace-a
69+
70+
/workspace-a/package.json
71+
/workspace-b/package.json
72+
```
73+
74+
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`.
75+
76+
### How does it compare to Lerna?
77+
78+
Yarn's workspaces are the low-level primitives that tools like Lerna can use. In fact, [they actually do](https://github.com/lerna/lerna/pull/899)! Workspaces will never try to support the high-level feature that Lerna offers, but by implementing the core part of the resolution and linking part of the workspaces inside Yarn itself we hope to enable new usages and better performances.
79+
80+
### Tips & Tricks
81+
82+
- 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!
83+
84+
### Limitations & Caveheats
85+
86+
- 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 dangerous (because the hoisting process is not standardized), so theoretically nothing new here.
87+
88+
- In the example above, if `workspace-b` depends on `workspace-1@0.0.1` instead of the version referenced in `workspace-a`'s package.json, the dependency will be installed from Github. This is because some packages actually need to use the previous versions in order to build the new ones (for example Babel is one of them).
89+
90+
- 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.
91+
92+
- Nested workspaces are not supported at the moment.

0 commit comments

Comments
 (0)