Using yarn workspaces with typescript is more straightforward than it looks, I made this example as a reference.
yarn
cd docs
yarn dev
yarn clean
cd <root folder>
yarn test
You might have heard of Typescript's new Project References. Seriously, don't use this. Yarn workspaces will take care of the linking.
There should only be 1 lockfile and it should be in the root folder. If you have others they aren't needed.
How yarn workspaces work: you should only have to run yarn
or yarn install
in the root directory, and this will install node_modules
for both the root and inside each workspace.
- root directory
-- package.json
-- components
---- package.json
-- docs
---- package.json
- root directory
-- package.json
-- node_modules (shared)
-- components
---- package.json
---- node_modules (for components and anything added to nohoist)
-- docs
---- package.json
---- node_modules (for docs and anything added to nohoist)
Each package.json in the workspace should have a name with the owner's @scope. In this example I named it @fakescope
since this is a fake project.
@fakescope
should also be the name in the root.
Example
- root -> @fakescope
-- components -> @fakescope/components
-- docs -> @fakescope/docs
However for the workspace names in the root package.json
-- those should be the folder names.
"workspaces": [
"components",
"docs"
]
To use the components
in docs
, the package name of components should be used in docs:
"dependencies": {
other dependencies...
"@fakescope/components": "*"
},
Then, you can treat imports as if they are an actual npm package, whether you have published the package or not.
import { Button } from "@fakescope/components";
<Button />
For this to work however, you'll need to make sure you've created a build (dist) in your components
package.
This is because in components/package.json
we have declared a main file:
"main": "dist/index.js",
One way to do this (easiest) is to have a postinstall hook in docs
since docs is depending on a build of components
.