|
1 | 1 | # mlx |
2 | 2 |
|
3 | | -An OCaml syntax dialect which adds to JSX expressions to the language. |
| 3 | +An OCaml syntax dialect which adds JSX expressions to the language. |
4 | 4 |
|
5 | 5 | ``` |
6 | | -let header title = |
| 6 | +let header ~children () = |
7 | 7 | <header> |
8 | 8 | <h1>title</h1> |
9 | 9 | </header> |
10 | 10 |
|
11 | 11 | let page = |
12 | 12 | <html> |
13 | 13 | <body> |
14 | | - (header "Hello, world!") |
| 14 | + <header>"Hello, world!"</header> |
15 | 15 | <div> |
16 | | - "Some content gere" |
| 16 | + "Some content goes here" |
17 | 17 | </div> |
18 | 18 | </body> |
19 | 19 | </html> |
20 | 20 | ``` |
21 | 21 |
|
| 22 | +This code is transformed into the following OCaml code: |
| 23 | +``` |
| 24 | +let header ~children () = |
| 25 | + header () ~children:[ h1 () ~children:[ title ] [@JSX]; ] [@JSX] |
| 26 | +
|
| 27 | +let page = |
| 28 | + html () ~children:[ |
| 29 | + body () ~children:[ |
| 30 | + header () ~children:[ "Hello, world!" ] [@JSX]; |
| 31 | + div () ~children:[ "Some content goes here" ] [@JSX]; |
| 32 | + ] [@JSX]; |
| 33 | + ] [@JSX] |
| 34 | +``` |
| 35 | + |
| 36 | +It is expected to use `mlx-pp` preprocessor with either a runtime lib which |
| 37 | +provides the implementation of such functions or a ppx which which further |
| 38 | +transforms `[@JSX]` attributes into the desired output. |
| 39 | + |
22 | 40 | ## Installation & Usage |
23 | 41 |
|
24 | | -While mlx is not yet available on opam, you can use a custom opam repository to install it: |
| 42 | +Currently for editor integration an unreleased version of `ocaml-lsp-server` is |
| 43 | +needed (along with its depenedencies, `jsonrpc` and `lsp`). |
| 44 | + |
| 45 | +Use the following commands to install the necessary packages: |
25 | 46 | ```sh |
26 | | -opam repo add andreypopp https://github.com/andreypopp/opam-repository.git |
27 | | -opam update |
28 | | -opam pin add dune.dev --dev |
| 47 | +opam pin add jsonrpc.dev --dev |
| 48 | +opam pin add lsp.dev --dev |
29 | 49 | opam pin add ocaml-lsp-server.dev --dev |
30 | | -opam install mlx ocamlmerlin-mlx ocamlformat-mlx |
| 50 | +opam install mlx ocamlmerlin-mlx |
31 | 51 | ``` |
32 | 52 |
|
33 | | -Then add the following config to your `dune-project` file: |
| 53 | +To make dune consider `.mlx` files as OCaml files you need to configure an mlx |
| 54 | +dialect, put this in your `dune-project` file: |
34 | 55 | ``` |
35 | 56 | (dialect |
36 | 57 | (name mlx) |
37 | 58 | (implementation |
38 | 59 | (extension mlx) |
39 | 60 | (merlin_reader mlx) |
40 | | - (format |
41 | | - (run ocamlformat-mlx %{input-file})) |
42 | 61 | (preprocess |
43 | 62 | (run mlx-pp %{input-file})))) |
44 | 63 | ``` |
45 | | - |
46 | | -Now dune will treat `.mlx` files as `.ml` files. If you use `ocaml-lsp-server` |
47 | | -then autocompletion and other editor integrations should work out of the box. |
48 | | - |
49 | | -To format `.mlx` files you'd need to run `dune fmt` or configure |
50 | | -`ocamlformat-mlx` in your editor manually. |
|
0 commit comments