Introduction to Reason Compilation
[CG]Nick
13K views
7 - Compile to JavaScript
Would you like some sweet, sweet interop with the JS ecosystem?
We're gonna use BuckleScript, an OCaml-to-JS compiler that emits really, really clean output.
The other option is js_of_ocaml, a mature and battle-tested compiler.
We're gonna use real third-party dependencies this time around. Check the new package.json's dependencies
field (a reason binding to JS functions) and devDependencies
field (BuckleScript compiler). npm install
in the current directory to get them.
Making this work is surprisingly simple. See the updated run.sh
. We've also made modifications to .gitignore
and .merlin
.
Make sure you read the compiled output. Yes, read it. Trust us on this one!
"Run"
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Suggested playgrounds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
print_endline MyDep.secret;
print_endline MyDep2.secret;
/* values that can potentially be null are type checked correctly! */
Random.self_init ();
let msg =
Random.int 2 === 0 ? Js.null : Js.Null.return "This message might not display.";
switch (Js.Null.to_opt msg) {
| None => ()
| Some msg => print_endline msg
};
/* let's try some BuckleScript-JavaScript interop */
ReasonJs.setTimeout (fun () => print_endline "Here's a message after a 1 second timeout.") 1000;
1
let secret = "hello";
1
let secret = MyDep.secret ^ " world";
1
cd step7