You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
6
+
7
+
Reason compiles to JavaScript thanks to our partner project, [Melange](https://melange.re).
8
+
9
+
## What's Melange?
10
+
11
+
[Melange](https://melange.re) is a backend for the OCaml compiler that emits readable, modular and highly performant JavaScript code.
12
+
13
+
> Melange strives to provide the best integration with both the OCaml and JavaScript ecosystems
14
+
15
+
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).
16
+
17
+
## Getting started
18
+
19
+
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).
20
+
21
+
Since [Melange v1](https://buttondown.email/anmonteiro/archive/melange-hits-v10/), Melange integrates with dune. This means that you can use dune to compile your Reason code to JavaScript and also to bytecode or native.
22
+
23
+
## Template
24
+
25
+
To get started with Reason and Melange, there's an official template:
make watch # Watch for reason file changes and recompile
37
+
make serve # Run the development server
38
+
```
39
+
40
+
## Manual
41
+
42
+
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.
43
+
44
+
#### Install melange
45
+
46
+
```bash
47
+
opam install melange
48
+
```
49
+
50
+
#### Create dune-project
51
+
52
+
If you don't have a `dune-project` file, create one with the following content:
53
+
54
+
```lisp
55
+
; dune-project
56
+
(dune lang 3.8)
57
+
(use melange 0.1) ; Here we enable melange to work with dune
58
+
```
59
+
60
+
#### Emit JavaScript
61
+
62
+
In your `dune` file, add a `melange.emit` stanza to emit JavaScript.
63
+
64
+
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).
65
+
66
+
```lisp
67
+
; dune
68
+
(melange.emit
69
+
(target app))
70
+
```
71
+
72
+
#### Libraries
73
+
74
+
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
75
+
76
+
To compile a library with melange, add the `(modes melange)` stanza to the library dune file.
77
+
78
+
```diff
79
+
(library
80
+
(name ui_library)
81
+
+ (modes melange)
82
+
)
83
+
```
84
+
85
+
Once you have a melange library, you can add it to the `melange.emit` stanza:
86
+
```lisp
87
+
; dune
88
+
(melange.emit
89
+
(target app)
90
+
(libraries ui_library))
91
+
```
92
+
93
+
#### Compile
94
+
95
+
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`).
96
+
97
+
To compile the project, run:
98
+
99
+
```bash
100
+
dune build # Compile the entire project
101
+
# or compile the melange alias (only melange.emit stanzas)
Copy file name to clipboardExpand all lines: docs/converting-from-js.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -2,4 +2,6 @@
2
2
title: Converting from JS
3
3
---
4
4
5
-
This section has been moved to the new ReScript project: https://rescript-lang.org/docs/manual/v8.0.0/converting-from-js. More info on the transition [here](https://rescript-lang.org/blog/bucklescript-is-rebranding).
5
+
This section has been moved to ReScript project: https://rescript-lang.org/docs/manual/v8.0.0/converting-from-js. More info on the transition [here](https://rescript-lang.org/blog/bucklescript-is-rebranding).
6
+
7
+
Some parts aren't relevant anymore, but the general idea is still the same. The process of introducing a new language to your project applies for Reason also.
Copy file name to clipboardExpand all lines: docs/editor-plugins.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ And other features.
15
15
16
16
## Editor Plugins & Language Server
17
17
18
-
Since Reason is just an alternative syntax for OCaml, we integrate seamlessly into the official OCaml editor toolchain as well.
18
+
The OCaml Platform provides support for Reason and integrates with editors such as VSCode, Vim, or Emacs.
19
19
20
20
- For VSCode, we recommend using the [vscode-ocaml-platform](https://github.com/ocamllabs/vscode-ocaml-platform) plugin, which offers OCaml & Reason support out of the box.
21
21
- For other editors, we recommend using a language server client plugin of your choice, and pairing it with the [`ocaml-lsp`](https://github.com/ocaml/ocaml-lsp). Check out the respective README's to get started.
@@ -24,3 +24,5 @@ Since Reason is just an alternative syntax for OCaml, we integrate seamlessly in
24
24
25
25
-[Vim/Neovim](https://github.com/reasonml-editor/vim-reason-plus): Make sure to use [ocaml-language-server](https://www.npmjs.com/package/ocaml-language-server) for native development as suggested. (Mac, Linux): `npm install -g ocaml-language-server`
26
26
-[vim-reasonml](https://github.com/jordwalke/vim-reasonml): For use with [esy](https://esy.sh/), Reason, and Merlin (not LSP based). (Mac, Linux, Windows).
You can do all the things you'd usually do with OCaml! OCaml is an incredible useful language for systems programming, while still being able to compile to pretty type safe JS with the aid of the `js_of_ocaml` compiler.
7
+
You can do all the things you'd usually do with JavaScript or OCaml! OCaml is an incredible useful language for general programming, while still being able to compile to pretty type safe JS with the aid of the [`Melange`](https://melange.re/v2.0.0/) compiler.
8
8
9
-
Natively compiled CLI's are also known to be really fast (like... C-like fast), and since the language is garbage collected, you will find yourself in a very nice spot of not having to worry about borrow-checking like in Rust and you don't have to deal with verbose non-ML languages like Go.
9
+
Natively compiled CLI's are also known to be really fast (like... C-like fast), and since the language is garbage collected, you will find yourself in a very nice spot of not having to worry about borrow-checking like in Rust and you don't have to deal with verbose non-ML languages like Go.
10
10
11
11
Reason also gives access to the declarative UI framework [revery-ui](https://github.com/revery-ui/revery) to build native applications with a ReactJS like paradigm (+ JSX).
12
12
13
13
### What is BuckleScript and ReScript, and why is it mentioned in so many Reason related resources?
14
14
15
-
Reason was originally bundled with BuckleScript (JS compiler) to provide a single toolchain for JS / ReactJS development.
15
+
Reason was originally integrated with BuckleScript to provide a single toolchain for JavaScript and React.js development.
16
16
17
17
In July 2020, BuckleScript released its own syntax and rebranded to ReScript to be its own language. More infos can be found in their [official rebranding announcement](https://rescript-lang.org/blog/bucklescript-is-rebranding).
18
+
19
+
### Where do all these `print_endline`, `string_of_int` functions come from?
20
+
They're from the standard library, pre-`open`ed during the compilation of your file. This is why you see them in scope.
21
+
22
+
You can read more about the Pervasives library in the api documentation:
23
+
24
+
https://reasonml.github.io/api/Pervasives.html
25
+
26
+
### Why is there a + for adding ints and +. for adding floats, etc.?
27
+
See [here](integer-and-float.md#design-decisions).
28
+
29
+
### What's the `.merlin` file at the root of my project?
30
+
That's the metadata file for [editor support](editor-plugins.md). This is usually generated for you; You don't need to check that into your version control and don't have to manually modify it.
31
+
32
+
### I don't see any `import` or `require` in my file; how does module resolution work?
33
+
Reason/OCaml doesn't require you to write any import; modules being referred to in the file are automatically searched in the project. Specifically, a module `Hello` asks the compiler to look for the file `hello.re` or `hello.ml` (and their corresponding [interface file](module.md#signatures), `hello.rei` or `hello.mli`, if available).
34
+
35
+
### Is `Some | None`, `contents`, `Array`, `List` and all of these special? Where do they come from?
36
+
They're ordinary variants/records/module definitions that come with the [standard library](/api/index.html), `open`ed by default during compilation out of convenience.
37
+
38
+
### What's this `MyModule.t` I keep seeing?
39
+
Assuming `MyModule` is a module's name, `t` is a community convention that indicates "the type that represents that module, whatever that means". For example, for the [`Js.String`](http://bucklescript.github.io/bucklescript/api/Js.String.html) module, [`String.t`](http://bucklescript.github.io/bucklescript/api/Js.String.html#TYPEt) is the type carried around and representing "a string".
40
+
41
+
### What does an argument with a prepended underscore (e.g. `_` or `_foo`) mean?
42
+
Say you have `List.map(item => 1, myList);`. The argument `item` isn't used and will generate a compiler warning. Using `_ => 1` instead indicates that you're intentionally receiving and ignoring the argument, therefore bypassing the warning. Alternatively, `_item => 1` has the same effect, but indicates more descriptively what you're ignoring.
This page is a detailed explanation on how to install Reason with [esy](https://esy.sh/) Easy package management for native Reason, OCaml and more, both manually and using a template.
6
+
7
+
Reason can be installed with a package manager like [opam](https://opam.ocaml.org/) also, check the [installation page](installation.md) for more details.
8
+
9
+
### Requirements
10
+
11
+
System Requirements:
12
+
13
+
- Node.js 16.14 or later.
14
+
- macOS, Windows (including WSL), and Linux are supported.
15
+
16
+
Esy is distributed via npm, install it using the following command:
17
+
18
+
```
19
+
npm install -g esy
20
+
```
21
+
> It's recommended to install esy globally
22
+
23
+
### Automatic
24
+
Using the [`hello-reason`](https://github.com/esy-ocaml/hello-reason) template
25
+
26
+
### Manual installation
27
+
28
+
To add packages that happen to be hosted on npm, run `esy add npm-package-name`.
29
+
30
+
To add packages from opam's registry, prefix the package name with `@opam`. `esy` treats the npm scope `@opam` specially. In this case, `esy` will install any package name with the `@opam` scope directly from [opam](https://opam.ocaml.org/packages/).
31
+
32
+
```bash
33
+
esy add @opam/bos
34
+
```
35
+
36
+
<!-- Add a warning about being crazy slow the first time -->
37
+
38
+
We install reason from opam using the following command:
39
+
40
+
```
41
+
esy add @opam/reason
42
+
```
43
+
44
+
Link to getting started with esy https://esy.sh/docs/en/getting-started.html
45
+
See the [configuration](https://esy.sh/docs/en/configuration.html) section from the complete `esy` docs.
46
+
47
+
To create your first Reason native CLI program, run the following commands:
# Install all dependencies (might take a while in the first run)
54
+
esy
55
+
56
+
# Compile and run Hello.exe
57
+
esy x Hello
58
+
```
59
+
60
+
## What's Next?
61
+
62
+
After you have successfully compiled your first example, it's time to [set up your editor](editor-plugins.md) to get access to all the nice features such as auto-completion. Later on you can check out the [language basics](overview.md) to get a basic understanding of all the Reason language constructs.
0 commit comments