-
Notifications
You must be signed in to change notification settings - Fork 397
Documentation update (mentions Melange and fixes a few things) #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fe63894
5081bd7
f67528f
092e5f8
035f7e4
6a59a1e
b085af2
8ef94bc
b1c2b1d
9716a91
ef31d26
f36461d
32993f0
c14dec8
3055bc0
a1f5a93
abcf79d
83e2014
7eb0d06
b07ce5d
7c7aa7d
842e85d
3cfb9f4
b10dd13
7b0f53b
7b228ed
94588e1
3bca4d9
486752b
faed3bc
4b4d81d
feaa16b
d1fe924
c8e1896
3032546
b260631
03cada3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,93 @@ | ||
--- | ||
title: Compiling to JavaScript with Melange | ||
title: Melange | ||
--- | ||
|
||
After the installation page, we have a CLI application written in Reason, running a native executable, but one of the best usages of Reason is to compile it to JavaScript an run it in the browser or any JavaScript platform such as Node, Deno Cloudflare Workers, | ||
One of the best usages of Reason is to compile it to JavaScript an run it in the browser or any JavaScript platform such as [Node.js](https://nodejs.org), [Deno](https://deno.com), [Cloudflare Workers](https://workers.cloudflare.com). | ||
|
||
To do so, we need to introduce [Melange](https://melange.re). | ||
Reason compiles to JavaScript thanks to our partner project, [Melange](https://melange.re). | ||
davesnx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Melange | ||
## What's Melange? | ||
|
||
[Melange](https://melange.re) is a backend for the OCaml compiler that emits readable, modular and highly performant JavaScript code. It's not the only option, there are other alternatives such as [Js_of_ocaml](compiling-to-js-with-jsoo.md). | ||
[Melange](https://melange.re) is a backend for the OCaml compiler that emits readable, modular and highly performant JavaScript code. | ||
|
||
If you have experience with JavaScript and it's ecosystem we recommend Melange. | ||
> Melange strives to provide the best integration with both the OCaml and JavaScript ecosystems | ||
|
||
<!-- Turn it into a quote --> | ||
From their docs | ||
> Melange strives to provide the best integration with both the OCaml and JavaScript ecosystems. To learn all about melange go to their documentation. | ||
If you have experience with JavaScript and the ecosystem we recommend Melange, over other alternatives such as [Js_of_ocaml](compiling-to-js-with-jsoo.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks broken: https://reasonml-github-io-reasonml.vercel.app/docs/en/compiling-to-js-with-jsoo is it intended to be outside of the docs list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should add in on the sidebar (which will fix this issue) but since it's a little rough page I'm unsure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the link, when this page is ready can be pointed out in the sidebar and in the content |
||
|
||
## Getting started | ||
|
||
The install guide on the Melange website shares a lot of common steps with [our installation](installation.md), check it out: [melange.re/v2.0.0/getting-started](https://melange.re/v2.0.0/getting-started). | ||
|
||
Since [Melange v1](https://buttondown.email/anmonteiro/archive/melange-hits-v10/), it integrates with dune, the OCaml build system. This means that you can use dune to compile | ||
|
||
## Template | ||
<!-- To get started with Reason and Melange, there's an official template | ||
|
||
``` | ||
git clone https://github.com/melange-re/melange-esy-template | ||
cd melange-esy-template | ||
esy | ||
To get started with Reason and Melange, there's an official template: | ||
|
||
```bash | ||
git clone https://github.com/melange-re/melange-opam-template | ||
cd melange-opam-template | ||
|
||
# Create opam switch and install dependencies | ||
make init | ||
|
||
npm install | ||
npm run webpack | ||
# In separate terminals: | ||
|
||
make watch # Watch for reason file changes and recompile | ||
make serve # Run the development server | ||
``` | ||
--> | ||
|
||
## Manual | ||
|
||
If you prefer to do all the steps manually, here is a step by step. | ||
If you prefer to do all the steps manually, here is a step by step. Assuming you have an opam switch with OCaml 5.1.0. If not, check the [installation](installation.md#setup-a-new-environment-manually) guide. | ||
|
||
#### Install melange | ||
```sh | ||
esy add @opam/melange | ||
opam install melange | ||
``` | ||
|
||
#### Create dune-project | ||
```clojure | ||
|
||
If you don't have a dune-project file, create one with the following content: | ||
davesnx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```lisp | ||
; dune-project | ||
(dune lang 3.8) | ||
(use melange 0.1) | ||
(use melange 0.1) ; Here we enable melange to work with dune | ||
``` | ||
|
||
#### Emit JavaScript | ||
- Create a dune file | ||
- Add melange.emit stanza | ||
```clojure | ||
|
||
In your dune file, add `melange.emit` stanza to emit JavaScript. | ||
davesnx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `melange.emit` stanza tells dune to generate JavaScript files from a set of libraries and modules. In-depth documentation about this stanza can be found in the [build-system on the Melange documentation](https://melange.re/v2.0.0/build-system/#entry-points-with-melangeemit). | ||
|
||
```lisp | ||
; dune | ||
(melange.emit | ||
(target ???) | ||
() | ||
(target app)) | ||
``` | ||
|
||
#### Compile | ||
|
||
Each time you compile the project, dune will emit JavaScript files under `_build/default/app/` (`app` comes from the `(target app)` defined under `melange.emit`). | ||
|
||
To compile the project, run: | ||
|
||
```bash | ||
dune build # Compile the entire project | ||
# or compile the melange alias (only melange.emit stanzas) | ||
dune build @melange | ||
``` | ||
|
||
#### Libraries | ||
|
||
dune allows to define libraries by creating a dune file inside a folder and adding a library stanza: https://dune.readthedocs.io/en/stable/concepts/package-spec.html#libraries | ||
|
||
To compile a library with melange, add the `(modes melange)` stanza to the library dune file. | ||
|
||
```diff | ||
(library | ||
+ (modes melange) | ||
) | ||
``` | ||
<!-- Link to all options in Melange --> |
Uh oh!
There was an error while loading. Please reload this page.