ANN: Melange 1.0 – compile OCaml / ReasonML to JavaScript

The Melange team and I are thrilled to announce the release of Melange 1.0 today, marking a major milestone in the life of the project. This release represents the culmination of many months of hard work and incredible collaboration.

Melange, which started as a fork of BuckleScript, was created with the vision of maintaining compatibility with OCaml and providing the best OCaml experience within the modern JavaScript ecosystem. Today, we are proud to present Melange 1.0, a mature and reliable tool for compiling OCaml to efficient and readable JavaScript that teams rely on to deliver complex OCaml / ReasonML applications.

Get it now:

$ opam install melange.1.0.0

Highlights

Melange 1.0 radically improves user experience. This release focuses on robustness, OCaml compatibility and developer experience: Melange is fully embracing the OCaml Platform to make it easy and reliable for OCaml users to target JavaScript.

Dune Integration

Integrating with Dune was our biggest priority. Dune 3.8, released very recently, adds Melange support by understanding the following types of stanzas:

(library
  (modes melange) ;; <- new Melange mode
)

;; emit JS to `js-output` folder in this 
;; directory
(melange.emit
 (target js-output))

In Melange 1.0, the Dune integration is the officially supported workflow to build Melange projects. It provides robust rule generation, static assets support (your HTML / CSS / SVG / images), seamless editor integration (e.g. with OCaml LSP or Merlin).

Documentation

With Melange 1.0, we’re also launching a new documentation effort, melange.re. This website should currently be considered a work in progress, and we’re looking for feedback on how to best explain the Melange workflow and its available features. Feel free to get in touch in the OSS repository.

Additionally, the Dune documentation includes reference materials specific to using Melange with Dune.

Everything else

Wider OCaml version support

Melange was previously only available on OCaml 4.14. In this release, we’ve widened that range to versions of OCaml starting from version 4.13. This includes the OCaml 5 release line and allows Melange projects to share the same OCaml compiler switch as e.g. server-side projects.

Editor integration is the only caveat: it only works on OCaml 4.14, since Melange emits .cmt artifacts (used by e.g. LSP) targeting the OCaml 4.14 binary format.

Multiple syntaxes

Dune supports ReasonML out of the box via dialects, keeping ReasonML support in Melange unchanged from a user perspective. Internally, however, Melange 1.0 has dropped any knowledge of ReasonML, relying on the existing, battle-tested Dune support for dialects instead.

A rescript-syntax package is part of the Melange release too. It enables support for ReScript syntax in Melange, which Dune also supports. Keep in mind, however, that newer ReScript features are unlikely to be supported by this best-effort compatibility package.

Separate PPX

A big benefit of deep integration with the OCaml platform is having the freedom to assume that a native toolchain is present. That made it possible to unbundle the Melange distribution into a few separate components.

Melange now ships with a melange.ppx preprocessor based on Ppxlib that can be added to (preprocess (pps melange.ppx)), as per Dune’s preprocessing specification.

The React JS PPX (for Reason + JSX) has also been extracted and is now distributed separately as reactjs-jsx-ppx.

Enabling modern JS workflows

The Melange design in Dune was designed from day one with the goal of embracing the JavaScript platform:

  • The Dune integration generates JavaScript files in a predictable way
  • To exercise modern workflows, Melange has been tested in a Next.js application using React Server Components, and the available constructs were deemed sufficient to enable similar use cases.

Full list of changes:

The full list of changes can be consulted here.

Support & Sponsorship

The effort that went into this release would not have been possible without the support of many.

We’d like to thank everyone who made it possible:

We’d also like to thank the following notable contributors to this release:

  • Rudi Grinberg, for his indispensable guidance and direction on the design and implementation of the Dune integration.
  • Javier Chávarri, for migrating a huge production codebase at Ahrefs to Melange, working on the Dune integration, the Melange documentation effort and providing vital feedback to the project.
  • David Sancho, for trying out our most bleeding edge ideas and providing early feedback on how to move forward with ways that encompass the whole ecosystem.

Looking Forward

We are enthusiastic about the progress we have made and the positive feedback we have received from the community. We remain committed to continuously improving Melange, ensuring it remains a robust and efficient tool for OCaml developers targeting the JavaScript platform.

Our Q2 2023 roadmap includes most of the goals that we set out to achieve over the past few months, and some of what we’re thinking about working in the months ahead. Melange 1.0 only marks the beginning of our journey towards the best OCaml experience on the JS platform.

Finally, we would like to extend our deepest thanks to everyone who has supported the project, whether through code contributions, testing, or providing feedback. This is your achievement as much as it is ours, and we look forward to continuing this journey together.

Thank you,

Antonio & The Melange Team

58 Likes
  1. Congrats!

  2. Two quick questions:

  3. Does Melange support Incrr_Dom/Bonsai ? If not, is the main gui to use React ? Is there any “functional reactive programming” lib that works with Melange ?

  4. Is the output of Melange closer to non-readable-bytecode-transliteration (like js_of_ocaml) or faily-readable (like Rescript) ?

Thanks!

Melange does not support Incr_dom / Bonsai today. Looking at the dependency list, I believe that nothing prevents to adapt these libraries to make them compatible with Melange though.

ReactJS (through reason-react) has been the most commonly used framework to build UIs with BuckleScript and now Melange. There was also another project called bucklescript-tea that should be easily portable to Melange.

The output is much closer to that of ReScript, due to the shared BuckleScript inheritance between both ReScript and Melange.

1 Like

If you have time, can you expand more on this? I’m not disagreeing with you; I am trying to gain intuition:

  1. what does incr_dom / bonsai rely on that jsoo provides but melange lacks ?
  2. how difficult would an adaptation be ?

I’m curious about this as when I was testing out rescript, I found rescript runtime errors to be much easier to debug than jsoo runtime errors.

You’re right. I’ve started to add support for Melange to a fork of bucklescript-tea in there. The build succeeds, now I’m trying to understand how to bundle the accompanying sample code, so that I can actually test it…

incr_dom and bonsai depend on a bunch of dependencies both https://github.com/janestreet/bonsai/blob/master/bonsai.opam#L14-L55 and https://github.com/janestreet/incr_dom/blob/master/incr_dom.opam#L13-L24. This is technically doable but challenging if some dependency binds to C code, but if you really want to do it, the possibility is there.

With all said, using reason-react works out of the box and it’s a 2 minutes install so I highly recommend using it instead.

1 Like

I work on a project which currently uses js_of_ocaml. Is there a good overview of what the advantages/disadvantages of that package are compared to Melange, and perhaps an example showing the results of using both?

1 Like

Congratulations on hitting 1.0. Superb leadership and contributors, really happy to see Melange getting better and better.

Re: JSX, there’s an entry in the linked Q2 2023 roadmap

  • Fix React JSX PPX to include better AST locations
    • Results in better editor integration

When I tried the latest melange-esy-template I see this hover type hints issue in ReasonReact component bodies. Is there an issue with my config, or did this one not make the cut?

1 Like

Hey @osener, your editor is configured correctly and it’s just us that need to work on two issues, which those are:

We have been comfortable for way too long on broken tooling and we have chances to fix those issues now. Keep reporting what you find!

4 Likes

Hello, sorry for the dumb question - does melange still require to use rescript syntax in order to use react? Do OCaml → React bindings exist? Are there 100% OCaml syntax projects using melange? Thanks.

Melange recommends using ReasonML syntax with ReasonReact JSX for React apps.

1 Like