Does anyone have experience with using dune and js_of_ocaml to generate from a big OCaml codebase multiple JS libraries that can be dynamically load and bundled separately?
Does anyone have experience with that?
I’ve looked at the dune documentation on its integration with js_of_ocaml but it does not say much.
I’m also open to other systems such as melange, as long as I don’t have to switch away from dune
for the build system of my application.
but the generated code for it is currently huge
What’s the current size ? Are you compiling with whole program compilation ?
Js_of_ocaml has some support for dynlink but it’s not free. In particular, dead code elimination will be limited
If you are comfortable with Dune … I had a variation of the problem you have and solved it with a generated Dune include file. My problem was how to statically combine N “components” into a small number of executables, where N has to be discovered at build time. It doesn’t involve JS, but perhaps the idea translates to your problem. Which … if I were to reframe your problem … is how to statically generate N libraries, where N has to be discovered at build time.
Recreating my steps:
- My generated Dune include file is at https://github.com/diskuv/dkml-installer-ocaml/blob/main/installer/bin/dune.inc; I purposely promote it to the source code directory, but you can let Dune keep it in the build directory if you’d like by adjusting
(mode promote) in step 2.
- The Dune include file is both generated and included in https://github.com/diskuv/dkml-installer-ocaml/blob/main/installer/bin/dune. The core structure is
(rule (target dune.inc) ...) (include dune.inc)
- The rule invokes executables I hand-crafted that discovers the N components (*) and prints out a
dune.inc file. The central code is https://github.com/diskuv/dkml-install-api/blob/main/installer/dune-generator/main.ml. You would structure your
dune.inc so that there is one
(library ...) statement per semgrep language.
Not sure if that is what you were looking for.
(*) In my scenario … which is to generate an installer of components … I used
ocamlfind to discover components at build time; in semgrep I think you have a JSON file you can parse.