Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5299ff9

Browse files
committedMay 31, 2022
Add all relevant changes from previous 2020 bundle API
For reference on the previous work in 2020, refer to #4518
1 parent bf5a154 commit 5299ff9

File tree

4 files changed

+789
-117
lines changed

4 files changed

+789
-117
lines changed
 

‎CONTRIBUTING.md

+30-22
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,17 @@ This is usually the file you want to create to test certain compile behavior wit
197197
- Verify the output, check in the `jscomp/test/my_file_test.ml` and `jscomp/test/my_file_test.js` to version control. The checked in `.js` file is essential for verifying regressions later on.
198198
- Eventually check in other relevant files changed during the rebuild (depends on your compiler changes).
199199

200-
## Contribute to the BS Playground Bundle
200+
## Contribute to the ReScript Playground Bundle
201201

202202
> Note: These instructions are designed for building the 4.06 based version of ReScript (ReScript v6).
203203
204204
The "Playground bundle" is the BS compiler compiled to JavaScript, including all necessary dependency files (stdlib / belt etc). It is useful for building tools where you want to compile and execute arbitrary Reason / OCaml in the browser.
205205

206-
The ReScript 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 bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem. Before we can compile anything, we need to install the required tools (requires [`opam`](https://opam.ocaml.org/doc/Install.html) to be installed):
206+
The ReScript source code is compiled with a tool called [JSOO (js_of_ocaml)](https://ocsigen.org/js_of_ocaml/3.6.0/manual/overview), which uses OCaml bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem.
207+
208+
We actually ship a bytecode version of `js_of_ocaml`, so you actually don't need to install any third party dependencies. This is usually for basic usage if you don't care about the compilation speed or a fast feedback loop (e.g. CI).
209+
210+
For faster compilation times, we recommend to install a proper `jsoo` executable:
207211

208212
```sh
209213
# Create the right switch, if not created yet (first install)
@@ -213,18 +217,25 @@ opam switch create 4.06.1
213217
opam switch 4.06.1
214218
eval `opam config env`
215219

216-
opam install js_of_ocaml.3.5.1
220+
opam install js_of_ocaml.3.6.0
221+
```
222+
223+
Additionally, make sure to adapt the `scripts/repl.js` file to use the `js_of_ocaml` binary instead (otherwise you'll continue using the slower bytecode version):
224+
225+
```diff
226+
- e(`${OCAMLRUN} ${JSOO} compile jsc.byte ${jsooFlag}-o exports.js`);
227+
+ e(`js_of_ocaml compile jsc.byte ${jsooFlag}-o exports.js`);
217228
```
218229

219-
### Build the Bundle
230+
### Building the Bundle
220231

221-
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`. A full clean build can be done like this:
232+
The entry point of the JSOO bundle is located in `jscomp/main/jsoo_refmt_main.ml` and the script for running JSOO can be found in `scripts/repl.js`. A full clean build can be done like this:
222233

223234
```
224235
# We create a target directory for storing the bundle / stdlib files
225236
mkdir playground && mkdir playground/stdlib
226237
227-
# We build the ReScript source code and also the bytecode for jsoo_main.ml
238+
# We build the ReScript source code and also the bytecode for jsoo_refmt_main.ml
228239
node scripts/ninja.js config && node scripts/ninja.js build
229240
230241
# Now we run the repl.js script pointing to our playground directory (note how it needs to be relative to the repl.js file)
@@ -233,7 +244,7 @@ BS_PLAYGROUND=../playground node scripts/repl.js
233244

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

236-
**You should now find following files:**
247+
After a successful compilation, you will find following files in your project:
237248

238249
- `playground/exports.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object.
239250
- `playground/stdlib/*.js` -> All the ReScript runtime files.
@@ -243,31 +254,28 @@ You can now use the `exports.js` file either directly by using a `<script src="/
243254
```
244255
$ node
245256
> require("./exports.js");
246-
undefined
247-
> let compile_result = ocaml.compile(`Js.log Sys.ocaml_version`); // You can change the code here
248-
undefined
249-
> eval(compile_result);
257+
> let compiler = rescript_compiler.make()
258+
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
259+
> eval(result);
250260
4.06.2+BS
251-
undefined
252261
```
253262

254263
### Playground JS bundle API
255264

256-
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)):
265+
As soon as the bundle is loaded, you will get access to the functions exposed in [`jsoo_refmt_main.ml`](jscomp/refmt/jsoo_refmt_main.ml). Best way to check out the API is by inspecting a compiler instance it either in node, or in the browser:
257266

258-
- `window.ocaml`:
259-
- `compile(code: string)`: Compiles given code
260-
- `shake_compile(code: string)`: Compiles given code with tree-shaking
261-
- `compile_super_errors(code: string)`: Compiles given code and outputs `super_errors` related messages on `console.error`
262-
- `compile_super_errors_ppx_v2(code: string)`: Compiles given code with the React v2 syntax
263-
- `compile_super_errors_ppx_v3(code: string)`: Compiles given code with the React v3 syntax
264-
- `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)
267+
```
268+
$ node
269+
require('./exports.js')
270+
271+
> let compiler = rescript_compiler.make()
272+
> console.log(compiler)
273+
```
265274

266-
For each compile every successful operation will return `{js_code: string}`. On compile errors, the returned object will be `{js_error_msg: string}`.
267275

268276
### Working on the Playground JS API
269277

270-
Whenever you are modifying any files in the ReScript compiler, or in the `jsoo_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.
278+
Whenever you are modifying any files in the ReScript compiler, or in the `jsoo_refmt_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.
271279

272280
```sh
273281
node scripts/ninja.js config && node scripts/ninja.js build

‎jscomp/refmt/jsoo_refmt_main.ml

+754-94
Large diffs are not rendered by default.

‎jscomp/refmt/jsoo_refmt_main.mli

-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
val make_compiler : name:string -> (Lexing.lexbuf -> Parsetree.structure) -> unit

‎jscomp/super_errors/super_location.mli

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
(* Needed for the online playground experience *)
3+
val super_warning_printer :
4+
Warnings.loc ->
5+
Format.formatter ->
6+
Warnings.t -> unit
27

38
val error_of_printer :
49
Location.t ->

0 commit comments

Comments
 (0)
Please sign in to comment.