$ opam install js_of_ocaml-compiler
Compiling to JS¶
Dune has full support building Js_of_ocaml libraries and executables transparently. There’s no need to customize or enable anything to compile OCaml libraries/executables to JS.
To build a JS executable, just define an executable as you would normally. Consider this example:
echo 'print_endline "hello from js"' > foo.ml
With the following
(executable (name foo) (modes js))
And then request the
$ dune build ./foo.bc.js $ node _build/default/foo.bc.js hello from js
Similar targets are created for libraries, but we recommend sticking to the executable targets.
If you’re using the Js_of_ocaml syntax extension, you must remember to add the
appropriate PPX in the
(executable (name foo) (modes js) (preprocess (pps js_of_ocaml-ppx)))
Dune supports two modes of compilation:
The separate compilation mode will be selected when the build profile
dev, which is the default. It can also be explicitly specified
env stanza. See env for more information.