A demo create-react-app ("CRA") app that consumes libraries from an Nx monorepo.
Treat this like a normal CRA project, there are no changes in the workflow.
If you need to make changes to one or more underlying libs in the Nx repo, you'll need to link to those libraries locally, instead of using the version downloaded from npmjs.org.
This will allow you to see and test the changes before publishing the libraries for external usage.
- Convert this app to a pnpm workspace and reinstall the packages.
- Increment the semver for the library to be worked on.
- Make code changes in the library and this app.
- Publish the updated library.
- Convert the app back to non-workspace.
- Upgrade to the new library version.
-
Navigate to the app project root.
-
Create the workspace file.
cp pnpm-workspace.yaml.example pnpm-workspace.yaml
-
Adjust the path to the monorepo libs if needed. By default it's assumed that the monorepo and the app are peers in the directory structure, e.g.
src/monorepo
andsrc/my-app
. -
Install dependencies, linking the monorepo libs locally.
npx pnpm i --no-lockfile
The
--no-lockfile
switch ensures that we don't overwritepnpm-lock.yaml
. -
Verify that the linking worked.
$ ls -l node_modules/@gsong/ lrwxr-xr-x 30 george 1 Jun 7:01 ui -> ../../../monorepo/dist/libs/ui
You should see that the library is linked to a local directory.
-
Navigate to the library directory.
cd $(MONOREPO)/libs/ui
-
Upgrade the version number
npm version [type]
Where
[type]
ismajor
,minor
,patch
, etc.
-
Set up the real-time development build system.
-
In the monorepo project root:
npm run build:libs:watch
-
In the app project root:
npm start
-
-
Make the code changes in the library.
-
Navigate to the monorepo project root.
-
Build the library and its dependencies.
nx build ui --with-deps
-
Navigate to the library dist directory.
cd $(MONOREPO)/dist/libs/ui
-
Publish.
npm publish --access public
-
Quit the real-time monorepo build watchers and the CRA dev server.
-
Navigate to the app project root.
-
Remove the workspace file.
rm pnpm-workspace.yaml
-
Undo the local package link and upgrade.
npx pnpm i @gsong/ui@latest
-
Verify that the unlinking worked.
$ ls -l node_modules/@gsong/ lrwxr-xr-x 69 george 1 Jun 8:05 design-system -> ../.pnpm/@gsong/design-system@0.0.1/node_modules/@gsong/design-system lrwxr-xr-x 47 george 1 Jun 8:05 ui -> ../.pnpm/@gsong/ui@0.0.3/node_modules/@gsong/ui
You should see that the library (and its dependency) is linked to the pnpm registry.
-
Verify that everything is working properly.
npm run build && npx serve -s build