You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> 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.
13
13
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.
15
15
16
-
**Note:** This feature requires the new build system and is **not compatible** with `rescriptlegacy`.
16
+
**Note:** This feature requires the new build system and is **not compatible** with `rescript-legacy`.
17
17
18
18
## Project Structure
19
19
20
20
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.
21
24
22
25
A typical structure looks like this:
23
26
24
27
```
25
28
my-monorepo/
26
29
├── rescript.json
30
+
├── package.json
31
+
├── node_modules/
32
+
│ ├── package-1/ # symlinked
33
+
│ ├── package-2/ # symlinked
27
34
├── packages/
28
35
│ ├── package-1/
29
36
│ │ ├── rescript.json
37
+
│ │ ├── package.json
30
38
│ │ ├── src/
31
39
│ ├── package-2/
32
40
│ │ ├── rescript.json
41
+
│ │ ├── package.json
33
42
│ │ ├── src/
34
43
│ ├── ...
35
44
```
36
45
37
46
## Root `rescript.json` Configuration
38
47
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.
40
49
41
50
```json
42
51
{
43
52
"name": "my-monorepo",
44
-
"dependencies": [
45
-
"package-1",
46
-
"package-2"
47
-
],
53
+
"dependencies": ["package-1", "package-2"],
48
54
"package-specs": {
49
55
"module": "esmodule",
50
56
"in-source": true
@@ -54,21 +60,42 @@ The root `rescript.json` orchestrates the monorepo by listing its constituent pa
54
60
}
55
61
```
56
62
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:
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.
58
83
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.
60
86
61
87
## Package `rescript.json` Configuration
62
88
63
-
Each nested rescript.json configures a specific package.
89
+
Each nested rescript.json sets up a specific package.
64
90
65
91
`packages/package-1/rescript.json`:
66
92
67
93
```json
68
94
{
69
95
"name": "package-1",
70
96
"sources": ["src"],
71
-
"dependencies": []
97
+
"dependencies": [],
98
+
"compiler-flags": ["-open Foobar"]
72
99
}
73
100
```
74
101
@@ -79,24 +106,109 @@ Each nested rescript.json configures a specific package.
79
106
"name": "package-2",
80
107
"sources": ["src"],
81
108
"dependencies": ["package-1"],
82
-
"warnings": {
83
-
"number":"-27"
109
+
"warnings": {
110
+
"number":"-27"
84
111
}
85
112
}
86
113
```
87
114
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.
89
120
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.
91
122
92
123
## Building the monorepo
93
124
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.
95
141
96
142
```bash
143
+
# Build from the package directory
144
+
cd packages/package-3
97
145
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
+
98
161
```
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`.
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
99
213
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