Yarn Workspaces
Workspaces allow us to install dependencies from multiple package.json files at once.
- Yarn can also create symlinks between workspaces that depend on each other, ensuring directories are correct and consistent
- When there are repeated dependencies in the
node_modulesof sub-modules, yarn workspaces will pull up (hoist) the common dependencies to live in the root-levelnode_modules.- This is why we don't need to run
lerna bootstrap --hoistwhen using yarn + lerna — yarn already hoists.
- This is why we don't need to run
- if package1 depends on package2, then a symlink to package2 will also be created in the root
node_modules. This allows package2 torequirein the package, and take advantage of node's recursive resolver to find the package. - workspaces uses a single yarn.lock file at the root.
- any module (either your own code or the code of a dependency) that contains native code (ie. Swift, Java etc) must not be hoisted.
How to implement
- Add a
workspaceskey to the rootpackage.json:
@appholds all the modules
"workspaces": {
"packages": [
"@app/*"
]
}
- Change the name field of each module's
package.jsonto follow a pattern so that they can be referenced easily - Add module script shortcuts to root
package.json, using the module names as changed in the previous step:
"scripts": {
"db": "yarn workspace @tycholiz/db",
}
- Add
private: trueproperty to rootpackage.json. - Add script in root
package.jsonthat allows us to execute multiple commands at once (note: consider usingconcurrentlypackage to run servers in parallel)
"scripts": {
"dev": "concurrently --kill-others-on-fail \"yarn sanity dev\" \"yarn web dev\"",
}
Children