|
| 1 | +--- |
| 2 | +title: "Monorepo Setup" |
| 3 | +metaTitle: "Monorepo Setup" |
| 4 | +description: "Configuring ReScript in a monorepo like setup" |
| 5 | +canonical: "/docs/manual/latest/build-monorepos" |
| 6 | +--- |
| 7 | + |
| 8 | +# Monorepo Setup |
| 9 | + |
| 10 | +**Since 8.4** |
| 11 | + |
| 12 | +We usually recommend using ReScript in a single-codebase style, where there is only one `bsconfig.json` file for the whole codebase. Many JavaScript libraries maintain multiple packages in one single codebase though, and use `yarn workspaces` to cross-link packages locally for development. |
| 13 | + |
| 14 | +This workflow is not straightforward for ReScript usage, so we need to explain some additional concepts to make this work. There are three different kinds of packages: |
| 15 | + |
| 16 | +- Toplevel (this is usually the final app you are building, which has dependencies to other packages) |
| 17 | +- Pinned dependencies (these are your local packages that should always rebuild when you build your toplevel, those should be listed in `bs-dependencies` and `pinned-dependencies`) |
| 18 | +- Normal dependencies (these are packages that are consumed from npm and listed via `bs-dependencies`) |
| 19 | + |
| 20 | +Whenever a package is being built (`bsb -make-world`), the build system will build the toplevel package with its pinned-dependencies. So any changes made in a pinned dependency will automatically be reflected in the final app. |
| 21 | + |
| 22 | +## Build System Package Rules |
| 23 | + |
| 24 | +The build system respects the following rules for each package type: |
| 25 | + |
| 26 | +**Toplevel** |
| 27 | +- Warnings reported |
| 28 | +- Warn-error respected |
| 29 | +- Builds dev dependencies |
| 30 | +- Builds pinned dependencies |
| 31 | +- Runs custom rules |
| 32 | +- Package-specs like ES6/CommonJS overrides all its dependencies |
| 33 | + |
| 34 | +**Pinned dependencies** |
| 35 | +- Warnings reported |
| 36 | +- Warn-error respected |
| 37 | +- Ignores pinned dependencies |
| 38 | +- Builds dev dependencies |
| 39 | +- Runs custom rules |
| 40 | + |
| 41 | +**Normal dependencies** |
| 42 | +- Warnings, warn-error ignored |
| 43 | +- Ignores dev directories |
| 44 | +- Ignores pinned dependencies |
| 45 | +- Ignores custom generator rules |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +### Yarn workspaces |
| 50 | + |
| 51 | +Let's assume we have a codebase like this: |
| 52 | + |
| 53 | +``` |
| 54 | +myproject/ |
| 55 | + app/ |
| 56 | + - src/App.res |
| 57 | + - bsconfig.json |
| 58 | + common/ |
| 59 | + - src/Header.res |
| 60 | + - bsconfig.json |
| 61 | + myplugin/ |
| 62 | + - src/MyPlugin.res |
| 63 | + - bsconfig.json |
| 64 | + package.json |
| 65 | +``` |
| 66 | + |
| 67 | +Our `package.json` file within our codebase root would look like this: |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "name": "myproject", |
| 72 | + "private": true, |
| 73 | + "workspaces": { |
| 74 | + "packages": [ |
| 75 | + "app", |
| 76 | + "common", |
| 77 | + "legacy" |
| 78 | + ] |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +Our `app` folder would be our toplevel package, consuming our `common` and `myplugin` packages as `pinned-dependencies`. The configuration for `app/bsconfig.json` looks like this: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "name": "app", |
| 89 | + "version": "1.0.0", |
| 90 | + "sources": { |
| 91 | + "dir" : "src", |
| 92 | + "subdirs" : true |
| 93 | + }, |
| 94 | + /* ... */ |
| 95 | + "bs-dependencies": [ |
| 96 | + "common", |
| 97 | + "myplugin" |
| 98 | + ], |
| 99 | + "pinned-dependencies": ["common", "myplugin"], |
| 100 | + /* ... */ |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Now, whenever we are running `npx bsb -make-world` within our `app` package, the compiler would always rebuild any changes within its pinned dependencies as well. |
| 105 | + |
| 106 | +**Important:** ReScript will not rebuild any `pinned-dependencies` in watch mode! This is due to the complexity of file watching, so you'd need to set up your own file-watcher process that runs `bsb -make-world` on specific file changes. E.g. you could use [`watchman-make`](https://facebook.github.io/watchman/docs/watchman-make.html) to automatically run the build task when a file in `common` or `myplugin` has been changed. |
| 107 | + |
0 commit comments