|
| 1 | +--- |
| 2 | +title: "Setting up a monorepo" |
| 3 | +metaTitle: "Setting up a monorepo" |
| 4 | +description: "Setting up a monorepo" |
| 5 | +canonical: "/docs/manual/v12.0.0/build-monorepo-setup" |
| 6 | +--- |
| 7 | + |
| 8 | +# Setting up a monorepo with ReScript |
| 9 | + |
| 10 | +**Since 12.0** |
| 11 | + |
| 12 | +> A monorepo is a single repository containing multiple distinct projects, with well-defined relationships. |
| 13 | +
|
| 14 | +ReScript 12.0 introduces native monorepo support via the new "Rewatch" build system. This guide shows you how to set it up. |
| 15 | + |
| 16 | +**Note:** This feature requires the new build system and is **not compatible** with `rescript legacy`. |
| 17 | + |
| 18 | +## Project Structure |
| 19 | + |
| 20 | +A ReScript monorepo requires a `rescript.json` file at the repository root, plus a `rescript.json` file in each sub-project directory. |
| 21 | + |
| 22 | +A typical structure looks like this: |
| 23 | + |
| 24 | +``` |
| 25 | +my-monorepo/ |
| 26 | +├── rescript.json |
| 27 | +├── packages/ |
| 28 | +│ ├── package-1/ |
| 29 | +│ │ ├── rescript.json |
| 30 | +│ │ ├── src/ |
| 31 | +│ ├── package-2/ |
| 32 | +│ │ ├── rescript.json |
| 33 | +│ │ ├── src/ |
| 34 | +│ ├── ... |
| 35 | +``` |
| 36 | + |
| 37 | +## Root `rescript.json` Configuration |
| 38 | + |
| 39 | +The root `rescript.json` orchestrates the monorepo by listing its constituent packages. |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "name": "my-monorepo", |
| 44 | + "dependencies": [ |
| 45 | + "package-1", |
| 46 | + "package-2" |
| 47 | + ], |
| 48 | + "package-specs": { |
| 49 | + "module": "esmodule", |
| 50 | + "in-source": true |
| 51 | + }, |
| 52 | + "suffix": ".res.mjs", |
| 53 | + "bsc-flags": [] |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The "dependencies" array specifies the names of your packages, which must correspond to the "name" fields in their respective sub-rescript.json files. |
| 58 | + |
| 59 | +Inheritance: By default, all settings defined in the root rescript.json are inherited by the individual packages. |
| 60 | + |
| 61 | +## Package `rescript.json` Configuration |
| 62 | + |
| 63 | +Each nested rescript.json configures a specific package. |
| 64 | + |
| 65 | +`packages/package-1/rescript.json`: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "name": "package-1", |
| 70 | + "sources": ["src"], |
| 71 | + "dependencies": [] |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +`packages/package-2/rescript.json`: |
| 76 | + |
| 77 | +```json |
| 78 | +{ |
| 79 | + "name": "package-2", |
| 80 | + "sources": ["src"], |
| 81 | + "dependencies": ["package-1"], |
| 82 | + "warnings": { |
| 83 | + "number":"-27" |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +In `package-2`, we demonstrate overriding a root setting by specifically disabling warning 27 (unused variable) for this package only. |
| 89 | + |
| 90 | +Note the dependencies array here, which allows one package to depend on another within the monorepo. |
| 91 | + |
| 92 | +## Building the monorepo |
| 93 | + |
| 94 | +From the root directory, build all packages with: |
| 95 | + |
| 96 | +```bash |
| 97 | +rescript build |
| 98 | +``` |
| 99 | + |
| 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. |
0 commit comments