Skip to content

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

Merged
merged 37 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
fe63894
Add a getting-started page
davesnx Sep 3, 2023
5081bd7
Improve installation
davesnx Sep 3, 2023
f67528f
Push jsoo into it's own page
davesnx Sep 3, 2023
092e5f8
Setup compiling to js melange page
davesnx Sep 3, 2023
035f7e4
Improve what and why
davesnx Sep 3, 2023
6a59a1e
Setup sidebar
davesnx Sep 3, 2023
b085af2
Merge branch 'source' of github.com:/reasonml/reasonml.github.io into…
davesnx Sep 3, 2023
8ef94bc
Fix getting started
davesnx Sep 11, 2023
b1c2b1d
Annotate compile to js *with melange*
davesnx Sep 11, 2023
9716a91
Fix localized-strings
davesnx Oct 17, 2023
ef31d26
Add installation stuff
davesnx Oct 17, 2023
f36461d
Merge pull request #1 from davesnx/Improve-documentation-and-add-melange
davesnx Oct 17, 2023
32993f0
Fix localized-strings safety
davesnx Oct 17, 2023
c14dec8
Installation + what + started
davesnx Oct 19, 2023
3055bc0
Rename Reason
davesnx Oct 23, 2023
a1f5a93
Develop better installation
davesnx Oct 23, 2023
abcf79d
Recommend the melange template to install
davesnx Oct 23, 2023
83e2014
Simplify started
davesnx Oct 23, 2023
7eb0d06
Explain a small environtments intro
davesnx Oct 23, 2023
b07ce5d
Remove system req
davesnx Oct 23, 2023
7c7aa7d
Setup melange page
davesnx Oct 24, 2023
842e85d
Minimse jsoo
davesnx Oct 24, 2023
3cfb9f4
Make editor clear relation with ocaml platform
davesnx Oct 24, 2023
b10dd13
Specify switch
davesnx Oct 24, 2023
7b0f53b
Better compile to js w melange
davesnx Oct 24, 2023
7b228ed
Bring stuff from the past back
davesnx Oct 24, 2023
94588e1
Add note from reason-react
davesnx Oct 24, 2023
3bca4d9
Mention Melange on the homepage
davesnx Oct 24, 2023
486752b
Remove esy cache
davesnx Oct 24, 2023
faed3bc
Explain JSX better
davesnx Oct 24, 2023
4b4d81d
Add refmt and rtop
davesnx Oct 24, 2023
feaa16b
Polish usage
davesnx Oct 24, 2023
d1fe924
Use bash everywhere
davesnx Oct 24, 2023
c8e1896
Add title
davesnx Oct 25, 2023
3032546
Fix pattern matching snippets
davesnx Nov 3, 2023
b260631
Update docs/compiling-to-js-with-melange.md
davesnx Nov 3, 2023
03cada3
Update docs/compiling-to-js-with-melange.md
davesnx Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Setup melange page
  • Loading branch information
davesnx committed Oct 24, 2023
commit 7c7aa7d174fbce7578d8f0db9298bb020af0df58
89 changes: 62 additions & 27 deletions docs/compiling-to-js-with-melange.md
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).

## 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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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:

```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.

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 -->
4 changes: 2 additions & 2 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"title": "Community"
},
"compiling-to-js-with-jsoo": {
"title": "Compiling to JavaScript with Js_of_ocaml"
"title": "Js_of_ocaml"
},
"compiling-to-js-with-melange": {
"title": "Compiling to JavaScript with Melange"
"title": "Melange"
},
"converting-from-js": {
"title": "Converting from JS"
Expand Down
1 change: 0 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
],
"JavaScript": [
"compiling-to-js-with-melange",
"compiling-to-js-with-jsoo",
"interop",
"syntax-cheatsheet",
"pipe-first",
Expand Down