[ANN] New release of js_of_ocaml 3.6.0

I’m pleased to announce the release Js_of_ocaml 3.6.0.

Js_of_ocaml is a compiler from OCaml bytecode to JavaScript. It makes it possible to run pure OCaml programs in JavaScript environment like browsers and Node.js.

Try it online.

Notable changes:

  • The js_of_ocaml compiler now accepts sub-commands (link, build-runtime, build-fs, …). The plan for future versions is to remove other binary (e.g. jsoo_link) and consolidate everything inside the js_of_ocaml binary itself.
  • The standard JavaScript runtime is now embedded in the compiler (findlib is no longer needed to locate it)
  • Add support for the Str library (Regular expressions and high-level string processing) shipped with the OCaml compiler
  • Change memory representation of Int64.t (you might need to update your JavaScript stubs)
  • Many bug fixes (thanks to many more tests)
16 Likes

Awesome job!

  1. Does the project have roadmap?
  2. Is the project generally exists only for Ocsigen needs?
  3. Will it be adopted for modern front-end development (commonjs/esmodules compatibility for working with existing building tools ex. webpack, etc).
  4. Does the project competing with bucklescript?
  5. Why not to do ocaml to js compiler tools (based on js_of_ocaml and bucklescript experience) that combine possibility of using native ocaml and js libraries across back-end and front-end like implemented in Scala.js/Fable F#?
1 Like
  1. Does the project have roadmap?

There is no official roadmap, the project evolves based on issues, requests and contributions.
You can take a look at some of the Github issues

  1. Is the project generally exists only for Ocsigen needs?

js_of_ocaml is used by various projects, not only Ocsigen. See Bonsai, sketch-sh or jscoq for instance.

  1. Will it be adopted for modern front-end development (commonjs/esmodules compatibility for working with existing building tools ex. webpack, etc).

Being more friendly with the JavaScript ecosystem as been discussed here and there in the past but little has been done, possibly by lack of interest or use cases.

  1. Does the project competing with bucklescript?

I don’t think so. The two projects have different goals and different audience. One of Js_of_ocaml main goal is to stay as close as possible to the official OCaml semantic, allowing to leverage existing OCaml libraries without any modification.

  1. Why not to do ocaml to js compiler tools (based on js_of_ocaml and bucklescript experience) that combine possibility of using native ocaml and js libraries across back-end and front-end like implemented in Scala.js/Fable F#?

I don’t understand this question. I would expect both js_of_ocaml and bucklescript to be like Scala.js/Fable F# in their own way.

3 Likes

Thank you for answers.

I don’t understand this question. I would expect both js_of_ocaml and bucklescript to be like Scala.js/Fable F# in their own way.

I mean what Scala.js/Fable F# allows to use the most native libraries (not all) and JS ones (from npm registry or from custom JS module) in one project (ex. front-end). But in case of js_of_ocaml we limited to native OCaml libs and “HTML scripts” (not JS compatible modules). For bucklescript case we have whole JS ecosystem but have no access to useful native libs from opam registry.

In Js_of_OCaml, you can deal with JavaScript’s module (and npm/yarn), using for example:

(* val require : string -> 'a *)
let require module_name =
  let open Js.Unsafe in
  fun_call
    (js_expr "require")
    [|inject (Js.string module_name)|]
5 Likes

Good. Is there any real working example? Maybe “hello world” github repo project.

I have some “old experiments” here: https://github.com/xvw/ocamlectron (1) (an enternal-unfinished project). But if you have your bundle.bc.js (the compilation target of your JSOO’s program) in the same directory of your node_module you can have arbitrary require (but you have to type manually or using gen_js_api the output of your usage of require).

1.) https://github.com/xvw/ocamlectron/blob/develop/lib/electron_main/Electron.ml#L3

1 Like

Here is a version of Todo web app created using js_of_ocaml, react (not react.js but opam react), reactiveData. Uses just good old ocaml stdlib and libs that come with js_of_ocaml installation.

I have avoided using Lwt to keep it simple (in terms of concepts one needs to be familiar with to understand code). :slight_smile:

1 Like

Seems like you’ve changed interface so my code doesn’t compile anymore. I thought js_of_ocaml follows semver, didn’t it?

Thanks. In case of nodejs environment you solution is good.
But in browser the only solution I found is Js.Unsafe.js_expr {|require("react")|} hack.
Here is my experimental ReactJS bindings with webpack integration (not for production but for fun).
Even lazy components (external only) via dynamic imports works https://github.com/Voronar/jsoo-webpack/blob/master/main.ml#L5

Yes, the solution I suggest was “only for Node”. I think that “require” is not the standard approach in the front-end. When I have to deal with module in browser context I use a mix between Webpack and Jsoo. (But it can be a pain).