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
- Verify the output, check in the `jscomp/test/my_file_test.ml` and `jscomp/test/my_file_test.js` to
189
189
version control. The checked in `.js` file is essential for verifying
190
190
regressions later on.
191
191
- Eventually check in other relevant files changed during the rebuild (depends on
192
192
your compiler changes)
193
193
194
+
## Contributing to the BS Playground Bundle
195
+
196
+
> Note: These instructions are designed for building the 4.06 based version of BuckleScript (BS v6)
197
+
198
+
The "BuckleScript Playground bundle" is the BS compiler compiled to JavaScript, including all necessary dependency files (stdlib / belt etc).
199
+
It is useful for building tools where you want to compile and execute arbitrary Reason / OCaml in the browser.
200
+
201
+
The BuckleScript source code is compiled with a tool called [JSOO (js_of_ocaml)](https://ocsigen.org/js_of_ocaml/3.5.1/manual/overview), which uses OCaml
202
+
bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem. Before we can compile anything, we need to install the required
203
+
tools (requires [`opam`](https://opam.ocaml.org/doc/Install.html) to be installed):
204
+
205
+
```
206
+
# Create the right switch, if not created yet (first install)
207
+
opam switch create 4.06.1
208
+
209
+
# Makes sure to be on the right switch
210
+
opam switch 4.06.1
211
+
eval `opam config env`
212
+
213
+
opam install js_of_ocaml.3.5.1
214
+
```
215
+
216
+
### Building the bundle
217
+
218
+
The entry point of the JSOO bundle is located in `jscomp/main/jsoo_main.ml` and the script for running JSOO can be found in `scripts/repl.js`.
219
+
A full clean build can be done like this:
220
+
221
+
```
222
+
# We create a target directory for storing the bundle / stdlib files
223
+
mkdir playground && mkdir playground/stdlib
224
+
225
+
# We build the BuckleScript source code and also the bytecode for jsoo_main.ml
# Now we run the repl.js script pointing to our playground directory (note how it needs to be relative to the repl.js file)
229
+
BS_PLAYGROUND=../playground node scripts/repl.js
230
+
```
231
+
232
+
**You should now find following files:**
233
+
234
+
-`playground/exports.js` -> This is the BuckleScript compiler, which binds the BuckleScript API to the `window` object
235
+
-`playground/stdlib/*.js` -> All the BuckleScript runtime files
236
+
237
+
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`:
238
+
239
+
```
240
+
$ node
241
+
> require("./exports.js");
242
+
undefined
243
+
> let compile_result = ocaml.compile(`Js.log Sys.ocaml_version`); // You can change the code here
244
+
undefined
245
+
> eval(compile_result);
246
+
4.06.2+BS
247
+
undefined
248
+
```
249
+
250
+
### Playground JS bundle API
251
+
252
+
As soon as the bundle is loaded, you will get access to following functions (as seen in [`jsoo_main.ml`](jscomp/main/jsoo_main.ml)):
253
+
254
+
-`window.ocaml`:
255
+
-`compile(code: string)`: Compiles given code
256
+
-`shake_compile(code: string)`: Compiles given code with tree-shaking
257
+
-`compile_super_errors(code: string)`: Compiles given code and outputs `super_errors` related messages on `console.error`
258
+
-`compile_super_errors_ppx_v2(code: string)`: Compiles given code with the React v2 syntax
259
+
-`compile_super_errors_ppx_v3(code: string)`: Compiles given code with the React v3 syntax
260
+
-`load_module(cmi_path: string, cmi_content: string, cmj_name: string, cmj_content: string)`: Loads a module into the compiler (see notes on `cmj` / `cmi` below)
261
+
262
+
For each compile every successful operation will return `{js_code: string}`.
263
+
On compile errors, the returned object will be `{js_error_msg: string}`.
264
+
265
+
### Working on the Playground JS API
266
+
267
+
Whenever you are modifying any files in the BuckleScript compiler, or in the `jsoo_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.
0 commit comments