Hi everyone,
I’ve been working on a tool called ocaml-emcc, and it’s now in a state where other people could try it: GitHub - rboudrouss/ocaml-emcc: Ocaml-emcc compiles ocaml projects and their C dependecies to javascript/wasm using Emscripten · GitHub
The idea is pretty simple. Instead of compiling OCaml to JS or wasm, I compile ocamlrun with emscripten, compile all the C stubs of the program and its dependencies with emcc, and link the whole thing into one module. Nothing has to be rewritten in JavaScript. zarith, sqlite3, camlzip, cryptokit, mirage-crypto, ctypes bindings and so on “just” work, and so does your own C.
The obvious downside is that it’s bytecode running in an interpreter, so it’s a lot slower than js_of_ocaml or wasm_of_ocaml. If your code is pure OCaml or its C stubs already have JavaScript implementations, use those instead. This is for the cases where the FFI part is the problem.
Give it a shot!
opam pin add ocaml-emcc git+https://github.com/rboudrouss/ocaml-emcc.git
source /path/to/emsdk/emsdk_env.sh # emcc in PATH (you need to install emscripten)
ocaml-emcc build main.js # in your dune project
node main.js # or load main.js in a web page
The extension of the output decides what you get (see the README and emscripten’s documentation for more flags):
main.js: node and browsersmain.html: the same, plus a minimal pagemain.wasm: a standalone module, which still imports a few emscripten functionsmain.wasm --wasi: imports only WASI functions, so it runs on wasmtime or node’s WASI
It’s still a prototype, so there are rough edges, if you try it on something and it breaks, I’d really like to hear about it. Packages that don’t build are exactly what I’m looking for right now. Feel free to propose/ask for new recipes too.
This started as an attempt to compile MOPSA, a static analyzer written in OCaml, to wasm. It turned out to be really hard because of how much FFI it relies on (apron, gmp, clang’s frontend…). I eventually got it working by patching both OCaml and MOPSA’s sources, and gave a talk about it at the OCaml Workshop 2026. Since then I’ve been turning that work into a general tool, and it turns out to generalize quite well, so here it is.
I’m curious to hear what people would use it for, especially if you have an OCaml project that never made it to the browser because of its C dependencies.