I am trying to build JavaScript sources from my ML module collab_vis.ml which I plan to use from JavaScript. Unfortunately, I can’t get jbuilder (1.0+beta16) to launch js_of_ocaml (3.0.1)
It just won’t build anything. The only thing it generates is collab_vis.requires.sexp. I tried converting it into an executable which generates both the .exe as well as .bc, but I haven’t managed to get .bc.js out of it.
There is no error or something, it just doesn’t create these files. As if it didn’t know it should.
I had the same problem, after spending a few hours with absolutely no recognition from jbuilder (no error messages, no .js output) I ended up reverting to ocamlbuild.
This yields Don't know how to build collab_vis.bc.js or if I call it with the full source path src/collab/collab_vis.bc.js then Don't know how to build src/collab/collab_vis.bc.js.
I looked at some other projects which use js_of_ocaml and jbuilder and found JsOfCairo, which when I build it with jbuilder build does not generate any .bc nor .bc.js files at all.
(I am using OCaml 4.04.2, so I doubt this is a problem with an unsupported OCaml version, and js_of_ocaml 3.0.1 fortunately supports up to 4.06 anyway)
Ah, I ran the rules trick (it is a bit annoying it depends on odoc but that’s easy to install) and saw a lot of executable from my project having a .bc.js rule. So I transformed the target into
And invoking jbuilder build src/collab/collab_vis.bc.js did in fact generate a .bc.js! Thanks!
Now my follow up question: how to make jbuilder build build this target by default? I’d rather avoid to call this very specific incantation, it would be preferable to have that expressed somehow in the jbuild. Something like (modes (:standard js_of_ocaml)).
This is a bug that will be fixed in the next version of jbuilder.
To answer your other question, it helps to recall what it means to build “by default” in jbuilder. When you call $ jbuilder build, you really are doing $ jbuilder build @install i.e. requesting to build the install alias. So to make something build “by default”, you could simply add your target as a dependency of this alias. But IMO this is a bad idea because @install already has a semantic meaning - the set of installable targets. However, you can of course create your own aliases like this:
(alias
((name js)
(deps (collab_vs.bc.js))))
and then simply do $ jbuilder build @install @js to build both installable and js targets.
There has been some discussion on improving the set of aliases to make development a little more convenient but we don’t really have anything conclusive yet. If you have some better ideas, you can add something here option: jbuilder build -a/--all · Issue #204 · ocaml/dune · GitHub