Skip to content

Commit 5bb2f82

Browse files
committed
Correct rewatch guide
1 parent cf121b7 commit 5bb2f82

File tree

1 file changed

+132
-20
lines changed

1 file changed

+132
-20
lines changed

pages/docs/manual/v12.0.0/build-monorepo-setup.mdx

Lines changed: 132 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,48 @@ canonical: "/docs/manual/v12.0.0/build-monorepo-setup"
99

1010
**Since 12.0**
1111

12-
> A monorepo is a single repository containing multiple distinct projects, with well-defined relationships.
12+
> A monorepo is a single repository containing multiple separate projects, with clear relationships between them.
1313
14-
ReScript 12.0 introduces native monorepo support via the new "Rewatch" build system. This guide shows you how to set it up.
14+
ReScript 12.0 introduces improved support for native monorepos through the new "Rewatch" build system. This guide walks you through the setup process.
1515

16-
**Note:** This feature requires the new build system and is **not compatible** with `rescript legacy`.
16+
**Note:** This feature requires the new build system and is **not compatible** with `rescript-legacy`.
1717

1818
## Project Structure
1919

2020
A ReScript monorepo requires a `rescript.json` file at the repository root, plus a `rescript.json` file in each sub-project directory.
21+
Basically, the monorepo contains a root package that manages all local dependencies. Building the root package will build all its dependencies.
22+
23+
**Important:** You also need a node_modules monorepo setup with symlinks. In practice, if you want a ReScript monorepo, you will also need an npm/yarn/pnpm/bun monorepo.
2124

2225
A typical structure looks like this:
2326

2427
```
2528
my-monorepo/
2629
├── rescript.json
30+
├── package.json
31+
├── node_modules/
32+
│ ├── package-1/ # symlinked
33+
│ ├── package-2/ # symlinked
2734
├── packages/
2835
│ ├── package-1/
2936
│ │ ├── rescript.json
37+
│ │ ├── package.json
3038
│ │ ├── src/
3139
│ ├── package-2/
3240
│ │ ├── rescript.json
41+
│ │ ├── package.json
3342
│ │ ├── src/
3443
│ ├── ...
3544
```
3645

3746
## Root `rescript.json` Configuration
3847

39-
The root `rescript.json` orchestrates the monorepo by listing its constituent packages.
48+
The root `rescript.json` manages the monorepo by listing its packages.
4049

4150
```json
4251
{
4352
"name": "my-monorepo",
44-
"dependencies": [
45-
"package-1",
46-
"package-2"
47-
],
53+
"dependencies": ["package-1", "package-2"],
4854
"package-specs": {
4955
"module": "esmodule",
5056
"in-source": true
@@ -54,21 +60,42 @@ The root `rescript.json` orchestrates the monorepo by listing its constituent pa
5460
}
5561
```
5662

57-
The "dependencies" array specifies the names of your packages, which must correspond to the "name" fields in their respective sub-rescript.json files.
63+
The `"dependencies"` array lists the names of your packages, which must match the `"name"` fields in their respective sub-rescript.json files.
64+
When you build a package in ReScript, it will use the `"package-specs"` and `"suffix"` settings from the root package.
65+
Therefore, it is recommended to place these settings in the root `rescript.json` file and avoid specifying them in local package `rescript.json` files.
66+
67+
**Settings from different config files:** When Rewatch builds a package within a monorepo setup, it uses these settings from the root rescript.json:
68+
69+
- `"jsx"` (jsx_args, jsx_module_args, jsx_mode_args, jsx_preserve_args)
70+
- `"experimental"` (experimental_features_args)
71+
- `"package-specs"` (used for implementation_args)
72+
- `"suffix"` (used for package output)
73+
74+
These settings come from the package's own rescript.json:
75+
76+
- `"sources"` (determines which files to compile)
77+
- `"dependencies"` (package dependencies)
78+
- `"warnings"` (warning_args)
79+
- `"compiler-flags"` (bsc_flags)
80+
81+
When the root package is built, Rewatch will look for the dependencies inside the `my-monorepo/node_modules` folder.
82+
It is expected that `package-1` and `package-2` are available there via a symlink system provided by your node_modules package manager.
5883

59-
Inheritance: By default, all settings defined in the root rescript.json are inherited by the individual packages.
84+
Note that your root rescript.json is allowed to have a `"sources"` setting.
85+
These files will be compiled as expected.
6086

6187
## Package `rescript.json` Configuration
6288

63-
Each nested rescript.json configures a specific package.
89+
Each nested rescript.json sets up a specific package.
6490

6591
`packages/package-1/rescript.json`:
6692

6793
```json
6894
{
6995
"name": "package-1",
7096
"sources": ["src"],
71-
"dependencies": []
97+
"dependencies": [],
98+
"compiler-flags": ["-open Foobar"]
7299
}
73100
```
74101

@@ -79,24 +106,109 @@ Each nested rescript.json configures a specific package.
79106
"name": "package-2",
80107
"sources": ["src"],
81108
"dependencies": ["package-1"],
82-
"warnings": {
83-
"number":"-27"
109+
"warnings": {
110+
"number": "-27"
84111
}
85112
}
86113
```
87114

88-
In `package-2`, we demonstrate overriding a root setting by specifically disabling warning 27 (unused variable) for this package only.
115+
In `package-1`, we show how to use special compiler flags.
116+
In `package-2`, we show how to disable warning 27 (unused variable).
117+
In both cases, the settings only apply to the package where they are specified.
118+
Defining these in the root rescript.json will not affect the packages.
119+
There is no inheritance system.
89120

90-
Note the dependencies array here, which allows one package to depend on another within the monorepo.
121+
Also note the dependencies array in `package-2`, which allows that package to depend on `package-1` within the monorepo.
91122

92123
## Building the monorepo
93124

94-
From the root directory, build all packages with:
125+
From the root directory, you can run all ReScript commands:
126+
127+
```bash
128+
# Build all packages
129+
rescript build
130+
131+
# Clean all packages
132+
rescript clean
133+
134+
# Format all packages
135+
rescript format
136+
```
137+
138+
### Building individual packages
139+
140+
You can also run ReScript commands on individual packages instead of the entire monorepo. This is useful when you only want to work on one package.
95141

96142
```bash
143+
# Build from the package directory
144+
cd packages/package-3
97145
rescript build
146+
rescript clean
147+
rescript format
148+
149+
# Or run from the root directory
150+
rescript build packages/package-3
151+
rescript clean packages/package-3
152+
rescript format packages/package-3
153+
```
154+
155+
When building a single package, ReScript will use the settings from the root rescript.json as explained in the [Root rescript.json Configuration](#root-rescriptjson-configuration) section above.
156+
157+
### Building without a root rescript.json
158+
159+
If your node_modules monorepo is set up with symlinks, you can build packages even without a root rescript.json:
160+
98161
```
162+
my-monorepo/
163+
├──node_modules/
164+
│ ├── package-1/ # symlinked
165+
│ ├── package-2/ # symlinked
166+
├── package.json
167+
├── packages/
168+
│ ├── package-1/
169+
│ │ ├── rescript.json
170+
│ │ ├── package.json
171+
│ │ ├── src/
172+
│ ├── package-2/
173+
│ │ ├── rescript.json
174+
│ │ ├── package.json
175+
│ │ ├── src/
176+
│ ├── ...
177+
```
178+
179+
Building `package-2` (which depends on `package-1`) will search up the folder structure to find `package-1`.
180+
181+
Example:
182+
183+
```bash
184+
rescript build ./packages/package-2
185+
```
186+
187+
Internally, Rewatch will look for:
188+
189+
- 🔴 `my-monorepo/packages/package-2/node_modules/package-1`
190+
- 🔴 `my-monorepo/packages/node_modules/package-1`
191+
-`my-monorepo/node_modules/package-1`
192+
193+
This only happens as a last resort if `package-1` is not listed as a (dev-)dependency in a parent `rescript.json`.
194+
195+
## Troubleshooting
196+
197+
If you're having issues with your monorepo setup, you can use the `-v` flag during build to see what Rewatch detected as the project context:
198+
199+
```bash
200+
rescript build -v
201+
```
202+
203+
This will show you detailed information about how Rewatch is interpreting your project structure and which configuration files it's using.
204+
205+
## Recommendation
206+
207+
**The ReScript team strongly recommends using a root `rescript.json` file when setting up monorepos.** While it's technically possible to build packages without one (as shown in the section above), having a root configuration file provides several benefits:
208+
209+
- **Consistent settings** across all packages (jsx, experimental features, package-specs, suffix)
210+
- **Simplified dependency management** through the root dependencies array
211+
- **Better developer experience** with unified build commands from the root
212+
- **Easier maintenance** and configuration updates across the entire monorepo
99213

100-
Note on package.json: ReScript's build system manages the compilation of your ReScript code.
101-
It does not directly interact with your `package.json` setup or `node_modules`.
102-
You might need a separate monorepo tool (like Yarn Workspaces, pnpm, or npm Workspaces) to manage your JavaScript/Node.js dependencies across packages if applicable.
214+
The root `rescript.json` approach is the intended and supported way to work with ReScript monorepos.

0 commit comments

Comments
 (0)