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
# 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
246
258
```
247
259
248
260
_Troubleshooting: if ninja build step failed with `Error: cannot find file '+runtime.js'`, make sure `ocamlfind` is installed with `opam install ocamlfind`._
249
261
250
262
After a successful compilation, you will find following files in your project:
251
263
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.
253
265
-`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`)
254
267
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`:
256
269
257
270
```
258
271
$ node
259
-
> require("./exports.js");
272
+
> require("./compiler.js");
260
273
> let compiler = rescript_compiler.make()
261
274
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
262
275
> eval(result.js_code);
263
276
4.06.2+BS
264
277
```
265
278
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
+
266
281
### Playground JS bundle API
267
282
268
283
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:
269
284
270
285
```
271
286
$ node
272
-
require('./exports.js')
287
+
require('./compiler.js')
273
288
274
289
> let compiler = rescript_compiler.make()
275
290
> console.log(compiler)
@@ -294,7 +309,7 @@ In this repo, these files usually sit right next to each compiled `.ml` / `.res`
294
309
295
310
`.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?
296
311
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.
298
313
299
314
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).
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.
0 commit comments