Skip to content

Commit 7fd4dc4

Browse files
arbrandesclaude
andcommitted
feat: support npm workspaces for local development
Decouple clean from build in the Makefile so that watch mode can rebuild without wiping dist/. Add nodemon.json and watch:build, watch:docs, watch:pack scripts to standardize file watching. Part of #184 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 094119f commit 7fd4dc4

7 files changed

Lines changed: 101 additions & 16 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cat_docs_command = cat ./docs/_API-header.md ./docs/_API-body.md > ./docs/API.md
1313
clean:
1414
rm -rf dist .tsbuildinfo.*
1515

16-
build: clean
16+
build:
1717
tsc --build ./tsconfig.build.json
1818
cp ./shell/app.scss ./dist/shell/app.scss
1919

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ This watches for changes in `frontend-base` and rebuilds the packaged tarball on
7878
```sh
7979
nvm use
8080
npm ci
81-
npm run pack:watch
81+
npm run watch:pack
8282
```
8383

8484
#### In the consuming application

docs/decisions/0010-typescript-compilation-and-local-dev-workflow.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ To develop a local dependency (e.g., ``@openedx/frontend-base``) against a
122122
consuming project:
123123

124124
1. In the dependency: ``npm run pack`` (or use a watcher like ``nodemon`` with
125-
``npm run pack:watch``)
125+
``npm run watch:pack``)
126126
2. In the consumer: install from the tarball and run the dev server (or use the
127127
`autoinstall tool`_ from the ``frontend-dev-utils`` package)
128128

docs/how_tos/migrate-frontend-app.md

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ With the exception of any custom scripts, replace the `scripts` section of your
134134
"i18n_extract": "openedx formatjs extract",
135135
"lint": "openedx lint .",
136136
"lint:fix": "openedx lint --fix .",
137-
"prepack": "npm run build",
137+
"prepack": "npm run clean && npm run build",
138138
"snapshot": "openedx test --updateSnapshot",
139139
"test": "openedx test --coverage --passWithNoTests"
140140
},
@@ -156,13 +156,15 @@ Also:
156156
> **Why change `fedx-scripts` to `openedx`?**
157157
> A few reasons. One, the Open edX project shouldn't be using the name of an internal community of practice at edX for its frontend tooling. Two, some dependencies of your MFE invariably still use frontend-build for their own build needs. This means that they already installed `fedx-scripts` into your `node_modules/.bin` folder. Only one version can be in there, so we need a new name. Seemed like a great time for a naming refresh. |
158158
159-
Last but not least, add `clean:` and `build:` targets to your `Makefile`. The build target compiles TypeScript to JavaScript, uses `tsc-alias` to rewrite `@src` path aliases to relative paths, and copies all SCSS files from `src/` into `dist/` preserving directory structure:
159+
Last but not least, add `clean:` and `build:` targets to your `Makefile`. The build target compiles TypeScript to JavaScript, uses `tsc-alias` to rewrite `@src` path aliases to relative paths, and copies all SCSS files from `src/` into `dist/` preserving directory structure.
160+
161+
Note that `build` intentionally does *not* depend on `clean`. This allows incremental rebuilds during development (especially in workspace mode, where a watcher triggers `build` on every change). The `prepack` script in `package.json` runs `clean && build` explicitly, so published packages always start fresh.
160162

161163
```makefile
162164
clean:
163165
rm -rf dist
164166

165-
build: clean
167+
build:
166168
tsc --project tsconfig.build.json
167169
tsc-alias -p tsconfig.build.json
168170
find src -type f -name '*.scss' -exec sh -c '\
@@ -248,6 +250,7 @@ node_modules
248250
npm-debug.log
249251
coverage
250252
dist/
253+
packages/
251254
/*.tgz
252255
253256
### i18n ###
@@ -952,3 +955,75 @@ Refactor slots
952955
First, rename `src/plugin-slots`, if it exists, to `src/slots`. Modify imports and documentation across the codebase accordingly.
953956

954957
Next, the frontend-base equivalent to `<PluginSlot />` is `<Slot />`, and has a different API. This includes a change in the slot ID, according to the [new slot naming ADR](../decisions/0009-slot-naming-and-lifecycle.rst) in this repository. Rename them accordingly. You can refer to the `src/shell/dev` in this repository for examples.
958+
959+
960+
Set up npm workspaces for local development
961+
===========================================
962+
963+
Frontend apps support `npm workspaces <https://docs.npmjs.com/cli/using-npm/workspaces>`_ so that developers can work on the app and its dependencies (such as ``frontend-base``) simultaneously, with changes reflected automatically.
964+
965+
Add the workspaces field to package.json
966+
-----------------------------------------
967+
968+
```diff
969+
+ "workspaces": [
970+
+ "packages/*"
971+
+ ],
972+
```
973+
974+
This tells npm to look in ``packages/`` for local overrides of published packages. The ``packages/`` directory is gitignored (see the `.gitignore` step above), since it contains development-only bind-mounted checkouts.
975+
976+
Add a nodemon.json file
977+
------------------------
978+
979+
Create a ``nodemon.json`` at the repository root. This configures the ``watch:build`` script to rebuild automatically when source files change:
980+
981+
```json
982+
{
983+
"watch": [
984+
"src"
985+
],
986+
"ext": "js,jsx,ts,tsx,scss"
987+
}
988+
```
989+
990+
Add workspace-aware scripts
991+
----------------------------
992+
993+
Install ``concurrently`` and ``nodemon`` as dev dependencies:
994+
995+
```sh
996+
npm install --save-dev concurrently nodemon
997+
```
998+
999+
Then add the following scripts to ``package.json``:
1000+
1001+
```json
1002+
"build:packages": "npm run build --workspaces --if-present",
1003+
"dev:packages": "npm run build:packages && concurrently 'npm run watch:build --workspaces --if-present' 'npm run dev'",
1004+
"watch:build": "nodemon --exec 'npm run build'",
1005+
```
1006+
1007+
- ``watch:build`` uses ``nodemon`` to watch for source changes (as configured in ``nodemon.json``) and re-runs ``npm run build`` on each change.
1008+
- ``build:packages`` runs ``build`` in every workspace package that has the script (i.e., ``frontend-base``), then in the app itself. This is a one-shot build with no watching.
1009+
- ``dev:packages`` does an initial ``build:packages``, then concurrently watches all workspace packages for changes and starts the dev server.
1010+
1011+
Using workspaces
1012+
-----------------
1013+
1014+
To develop against a local ``frontend-base``:
1015+
1016+
```sh
1017+
mkdir -p packages/frontend-base
1018+
sudo mount --bind /path/to/frontend-base packages/frontend-base
1019+
npm install
1020+
npm run dev:packages
1021+
```
1022+
1023+
Bind mounts are used instead of symlinks because Node.js resolves symlinks to real paths, which breaks hoisted dependency resolution. Docker volume mounts work equally well (and are what ``tutor dev`` uses).
1024+
1025+
When done, unmount with:
1026+
1027+
```sh
1028+
sudo umount packages/frontend-base
1029+
```

nodemon.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"watch": [
3+
"runtime",
4+
"shell",
5+
"tools",
6+
"index.ts",
7+
"types.ts"
8+
],
9+
"ext": "ts,tsx,js,jsx,json,scss,css",
10+
"ignore": [
11+
"node_modules/**",
12+
".git/**",
13+
"pack/**"
14+
],
15+
"delay": 250
16+
}

nodemon.pack.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
"clean": "make clean",
2626
"dev": "npm run build && node ./dist/tools/cli/openedx.js dev:shell",
2727
"docs": "jsdoc -c jsdoc.json",
28-
"docs:watch": "nodemon -w runtime -w docs/template -w README.md -e js,jsx,ts,tsx --exec npm run docs",
2928
"lint": "eslint .; npm run lint:tools; npm --prefix ./test-site run lint",
3029
"lint:tools": "cd ./tools && eslint . && cd ..",
3130
"pack": "mkdir -p pack && npm pack --silent --pack-destination pack >/dev/null && mv \"$(ls -t pack/*.tgz | head -n 1)\" pack/openedx-frontend-base.tgz",
32-
"pack:watch": "nodemon --config nodemon.pack.json",
3331
"prepack": "npm run build",
34-
"test": "jest"
32+
"test": "jest",
33+
"watch:build": "nodemon --exec 'npm run build'",
34+
"watch:docs": "nodemon --watch docs/template --watch README.md --exec npm run docs",
35+
"watch:pack": "nodemon --exec 'npm run pack'"
3536
},
3637
"repository": {
3738
"type": "git",

0 commit comments

Comments
 (0)