Skip to content

Commit 9daaa72

Browse files
committed
Add monorepo support doc
1 parent d853033 commit 9daaa72

File tree

3 files changed

+111
-24
lines changed

3 files changed

+111
-24
lines changed

_blogposts/2020-12-07-release-8-4.mdx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,9 @@ dependent's command line flags for building binary artifacts. Such strategy bene
5555

5656
### Introducing `pinned-dependencies`
5757

58-
To make `bsb -make-world` more practical, we also introduce a new concept called: `pinned-dependencies`.
59-
In general, packages are classified as three categories:
60-
61-
- toplevel
62-
- warnings reported
63-
- warn-error respected
64-
- build dev dependency
65-
- run custom rules
66-
- package-specs like ES6/CommonJS overrides all its dependencies
67-
- pinned dependencies
68-
- warnings reported
69-
- warn-error respected
70-
- build dev dependency
71-
- run custom rules
72-
- normal dependencies
73-
- warnings, warn-error ignored
74-
- ignore dev directories
75-
- ignore custom generator rules
76-
77-
Previously, we only had `toplevel` and `normal dependencies`, by introducing `pinned-dependencies`,
78-
such package will be built mostly like `toplevel` packages.
79-
80-
The usage is quite simple, add `pinned-dependencies` in the toplevel package's `bsconfig.json`.
81-
Note such field only make sense in toplevel's configuration.
58+
To make `bsb -make-world` more practical for e.g. monorepo setups (`lerna`, `yarn workspaces`, etc.), we introduced a new concept called `pinned-dependencies`. A pinned dependency allows us to automatically rebuild packages that are pinned by a toplevel package, like your final webapp.
59+
60+
Please refer to our [Monorepo Setup](/docs/manual/latest/build-monorepos) docs for more information on how to use `pinned-dependencies` with different monorepo setups.
8261

8362
### More robust handling of removal of staled output
8463

data/sidebar_manual_latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"build-overview",
5454
"build-configuration",
5555
"build-configuration-schema",
56+
"build-monorepos",
5657
"interop-with-js-build-systems",
5758
"build-performance"
5859
],
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)