Skip to content

Commit 35365bc

Browse files
ryyppycristianoc
authored andcommitted
Streamline building all build artifacts / cmij files required by the online playground
1 parent 23d5167 commit 35365bc

File tree

11 files changed

+417
-11
lines changed

11 files changed

+417
-11
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,10 @@ jscomp/ml/lexer.ml
148148

149149
# opam local switch is stored in `_opam` folder
150150
_opam
151+
152+
.env
153+
playground/packages/
154+
playground/stdlib/
155+
playground/*.cmj
156+
playground/*.cmi
157+
playground/compiler.js

CONTRIBUTING.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,50 @@ mkdir playground && mkdir playground/stdlib
241241
# We build the ReScript source code and also the bytecode for the JSOO entrypoint
242242
node scripts/ninja.js config && node scripts/ninja.js build
243243
244-
# Now we run the repl.js script pointing to our playground directory (note how it needs to be relative to the repl.js file)
245-
PLAYGROUND=../playground node scripts/repl.js
244+
# Now we run the repl.js script which will create all the required artifacts in the `./playground` directory
245+
node scripts/repl.js
246+
```
247+
248+
In case you want to build the project with our default third party packages (like `@rescript/react`), prepare the `playground-bundling` project and then run `repl.js` with `BUILD_THIRD_PARTY` enabled:
249+
250+
```
251+
# Prepare the `playground-bundling` project to allow building of the third party cmij packages
252+
npm link
253+
cd packages/playground-bundling
254+
npm install
255+
npm link rescript
256+
257+
BUILD_THIRD_PARTY=true node scripts/repl.js
246258
```
247259

248260
_Troubleshooting: if ninja build step failed with `Error: cannot find file '+runtime.js'`, make sure `ocamlfind` is installed with `opam install ocamlfind`._
249261

250262
After a successful compilation, you will find following files in your project:
251263

252-
- `playground/exports.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object.
264+
- `playground/compiler.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object.
253265
- `playground/stdlib/*.js` -> All the ReScript runtime files.
266+
- `playground/packages` -> Contains third party deps with cmij.js files (as defined in `packages/playground-bundling/bsconfig.json`)
254267

255-
You can now use the `exports.js` file either directly by using a `<script src="/path/to/exports.js"/>` inside a html file, use a browser bundler infrastructure to optimize it, or you can even use it with `nodejs`:
268+
You can now use the `compiler.js` file either directly by using a `<script src="/path/to/compiler.js"/>` inside a html file, use a browser bundler infrastructure to optimize it, or you can even use it with `nodejs`:
256269

257270
```
258271
$ node
259-
> require("./exports.js");
272+
> require("./compiler.js");
260273
> let compiler = rescript_compiler.make()
261274
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
262275
> eval(result.js_code);
263276
4.06.2+BS
264277
```
265278

279+
You can also run `node playground/playground_test.js` for a quick sanity check to see if all the build artifacts are working together correctly.
280+
266281
### Playground JS bundle API
267282

268283
As soon as the bundle is loaded, you will get access to the functions exposed in [`jsoo_playground_main.ml`](jscomp/main/jsoo_playground_main.ml). Best way to check out the API is by inspecting a compiler instance it either in node, or in the browser:
269284

270285
```
271286
$ node
272-
require('./exports.js')
287+
require('./compiler.js')
273288
274289
> let compiler = rescript_compiler.make()
275290
> console.log(compiler)
@@ -294,7 +309,7 @@ In this repo, these files usually sit right next to each compiled `.ml` / `.res`
294309

295310
`.cmj` files are required for making ReScript compile modules (this includes modules like ReasonReact). ReScript includes a subset of modules by default, which can be found in `jscomp/stdlib-406` and `jscomp/others`. You can also find those modules listed in the `jsoo` call in `scripts/repl.js`. As you probably noticed, the generated `playground` files are all plain `.js`, so how are the `cmj` / `cmi` files embedded?
296311

297-
`repl.js` calls an executable called `cmjbrowser.exe` on every build, which is a compile artifact from `jscomp/main/jscmj_main.ml`. It is used to serialize `cmj` / `cmi` artifacts into two files called `jscomp/core/js_cmj_datasets.ml`. These files are only linked for the browser target, where ReScript doesn't have access to the filesystem. When working on BS, you'll see diffs on those files whenever there are changes on core modules, e.g. stdlib modules or when the ocaml version was changed. We usually check in these files to keep it in sync with the most recent compiler implementation. JSOO will pick up those files to encode them into the `exports.js` bundle.
312+
`repl.js` calls an executable called `cmjbrowser.exe` on every build, which is a compile artifact from `jscomp/main/jscmj_main.ml`. It is used to serialize `cmj` / `cmi` artifacts into two files called `jscomp/core/js_cmj_datasets.ml`. These files are only linked for the browser target, where ReScript doesn't have access to the filesystem. When working on BS, you'll see diffs on those files whenever there are changes on core modules, e.g. stdlib modules or when the ocaml version was changed. We usually check in these files to keep it in sync with the most recent compiler implementation. JSOO will pick up those files to encode them into the `compiler.js` bundle.
298313

299314
For any other dependency needed in the playground, such as `ReasonReact`, you will be required to serialize your `.cmi` / `.cmt` files accordingly from binary to hex encoded strings so that BS Playground's `ocaml.load` function can load the data. Right now we don't provide any instructions inside here yet, but [here's how the official ReasonML playground did it](https://github.com/reasonml/reasonml.github.io/blob/source/website/setupSomeArtifacts.js#L65).
300315

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
*.exe
2+
*.obj
3+
*.out
4+
*.compile
5+
*.native
6+
*.byte
7+
*.cmo
8+
*.annot
9+
*.cmi
10+
*.cmx
11+
*.cmt
12+
*.cmti
13+
*.cma
14+
*.a
15+
*.cmxa
16+
*.obj
17+
*~
18+
*.annot
19+
*.cmj
20+
*.bak
21+
lib/bs
22+
*.mlast
23+
*.mliast
24+
.vscode
25+
.merlin
26+
.bsb.lock
27+
/node_modules/
28+
lib/
29+
packages/
30+
.DS_Store
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# rescript-compiler-js
2+
3+
This repo is useful to generate a bunch of cmij.js files for a list of dependencies ready to be used in the ReScript playground.
4+
5+
## Setup
6+
7+
Check the `bsconfig.json` and `package.json` files for the respective versions used for the compiler & packages.
8+
9+
It's useful to `npm link` the compiler locally to prevent mismatching compiler versions and therefore "stale artifact" warnings.
10+
11+
```
12+
# Go to the root of the rescript-compiler project
13+
cd ../..
14+
15+
npm link
16+
17+
# Link the local build in this project as well
18+
cd packages/playground-bundling
19+
npm link rescript
20+
```
21+
22+
## Building
23+
24+
Run the following commands:
25+
26+
```
27+
npm run build
28+
node scripts/generate_cmijs.js
29+
```
30+
31+
All the cmij files will now be available in the `packages/` directory with a structure like this:
32+
33+
```
34+
tree packages
35+
packages
36+
└── @rescript
37+
└── react
38+
├── React.js
39+
├── ReactDOM.js
40+
├── ReactDOMRe.js
41+
├── ReactDOMServer.js
42+
├── ReactDOMStyle.js
43+
├── ReactEvent.js
44+
├── ReactTestUtils.js
45+
├── ReasonReact.js
46+
├── RescriptReactErrorBoundary.js
47+
├── RescriptReactRouter.js
48+
└── cmij.js
49+
50+
2 directories, 11 files
51+
```
52+
53+
## Using cmij artifacts with the playground bundle
54+
55+
Let's assume our `compiler.js` file represents our playground bundle, you'd first load the compiler, and then load any cmij file:
56+
57+
```
58+
require("playground/compiler.js")
59+
require("packages/@rescript/react/cmij.js")
60+
61+
let comp = rescript_compiler.make()
62+
comp.rescript.compile("let a = <div/>")
63+
```
64+
65+
The script above will be able to successfully compile this React code, since all the `React` module functionality required by JSX was injected in the compiler's state.
66+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "playground",
3+
"version": "0.1.0",
4+
"bs-dependencies": ["@rescript/react"],
5+
"package-specs": {
6+
"module": "es6",
7+
"in-source": false
8+
},
9+
"sources": {
10+
"dir" : "src",
11+
"subdirs" : true
12+
}
13+
}

packages/playground-bundling/package-lock.json

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "proj",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "rescript clean && rescript build && node ./scripts/generate_cmijs.js",
8+
"clean": "rescript clean"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"@rescript/react": "^0.10.2"
15+
}
16+
}

0 commit comments

Comments
 (0)