Skip to content

Commit 1fb4eca

Browse files
authored
doc(melange): rehaul melange docs for (using melange 1.0) (#12631)
Signed-off-by: Antonio Nuno Monteiro <[email protected]>
1 parent 03f80ca commit 1fb4eca

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

doc/melange.rst

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ be installed with opam:
1515
1616
$ opam install melange
1717
18-
Dune can build projects using Melange, and it allows the user to produce
19-
JavaScript files by defining a :ref:`melange-emit` stanza. Dune libraries can be
20-
used with Melange by adding ``melange`` to ``(modes ...)`` in the
21-
:doc:`/reference/dune/library` stanza.
18+
Dune can build Melange projects, and produces JavaScript files by defining a
19+
:ref:`melange-emit` stanza. Dune libraries may also define Melange libraries by
20+
adding ``melange`` to ``(modes ...)`` in the :doc:`/reference/dune/library`
21+
stanza.
2222

23-
Melange support is still experimental in Dune and needs to be enabled in the
24-
:doc:`/reference/dune-project/index` file:
23+
Melange support must be enabled in the :doc:`/reference/dune-project/index`
24+
file:
2525

2626
.. code:: dune
2727
28-
(using melange 0.1)
28+
(using melange 1.0)
2929
3030
Once that's in place, you can use the Melange mode in
31-
:doc:`/reference/dune/library` stanzas ``melange.emit`` stanzas.
31+
:doc:`/reference/dune/library` and ``melange.emit`` stanzas.
3232

3333
Simple Project
3434
==============
@@ -37,13 +37,13 @@ Let's start by looking at a simple project with Melange and Dune. Subsequent
3737
sections explain the different concepts used here in further detail.
3838

3939
First, make sure that the :doc:`/reference/dune-project/index` file
40-
specifies at least version 3.8 of the Dune language, and the Melange extension
40+
specifies at least version 3.20 of the Dune language, and the Melange extension
4141
is enabled:
4242

4343
.. code:: dune
4444
4545
(lang dune {{latest}})
46-
(using melange 0.1)
46+
(using melange 1.0)
4747
4848
Next, write a :doc:`/reference/dune/index` file with a
4949
:ref:`melange-emit` stanza:
@@ -100,23 +100,29 @@ melange.emit
100100

101101
.. versionadded:: 3.8
102102

103-
The ``melange.emit`` stanza allows the user to produce JavaScript files
104-
from Melange libraries and entry-point modules. It's similar to the OCaml
105-
:doc:`/reference/dune/executable` stanza, with the exception that there
106-
is no linking step.
103+
The ``melange.emit`` stanza produces JavaScript files from Melange libraries or
104+
entry-point modules. It's similar to the OCaml
105+
:doc:`/reference/dune/executable` stanza, with the exception that there is no
106+
linking step.
107107

108108
.. code:: dune
109109
110110
(melange.emit
111111
(target <target>)
112112
<optional-fields>)
113113
114-
``<target>`` is the name of the folder where resulting JavaScript artifacts will
115-
be placed. In particular, the folder will be placed under
116-
``_build/default/$path-to-directory-of-melange-emit-stanza``.
114+
.. _target:
117115

118-
The result of building a ``melange.emit`` stanza will match the file structure
119-
of the source tree. For example, given the following source tree:
116+
- ``<target>`` is the name of the folder inside the build directory where Dune
117+
will compile the resulting JavaScript. In particular, the folder will be
118+
placed under ``_build/default/$path-to-directory-of-melange-emit-stanza``.
119+
120+
**Note:** when using `promotion`_, Dune will additionally copy the
121+
resulting JavaScript back to the source tree, next to the original source
122+
files.
123+
124+
``$path-to-directory-of-melange-emit-stanza`` matches the file structure of the
125+
source tree. For example, given the following source tree:
120126

121127
.. code::
122128
@@ -209,12 +215,15 @@ The resulting layout in ``_build/default/output`` will be as follows:
209215
default is ``true``. If this option is ``false``, the Melange standard library
210216
and runtime JavaScript files won't be produced in the target directory.
211217

218+
.. _melange_promote:
219+
212220
- ``(promote <options>)`` promotes the generated ``.js`` files to the
213221
source tree. The options are the same as for the
214222
:ref:`rule promote mode <promote>`.
215223
Adding ``(promote (until-clean))`` to a ``melange.emit`` stanza will cause
216224
Dune to copy the ``.js`` files to the source tree and ``dune clean`` to
217225
delete them.
226+
Check `Promotion`_ for more details.
218227

219228
- ``(preprocess <preprocess-spec>)`` specifies how to preprocess files when
220229
needed. The default is ``no_preprocessing``. Additional options are described
@@ -298,19 +307,40 @@ well if you need to override the build rules in one of the packages.
298307
(vendored_dirs reason-react)
299308
(dirs reason-react))
300309
310+
Promotion
311+
=====================
312+
313+
Compiling and promoting Melange output in Dune is slightly different than
314+
compiling OCaml:
315+
316+
- Limitations in Dune `rule production
317+
<https://github.com/ocaml/dune/blob/main/doc/dev/rule-streaming.md>`_ require
318+
a :ref:`target directory <target>` in :ref:`melange-emit`.
319+
320+
- The target directory is :ref:`total <total>`: it can be exported as is from
321+
the Dune build directory
322+
- Many popular tools and frameworks in the JavaScript ecosystem today rely on
323+
convention over configuration, especially as it relates to folder structure.
324+
When using :ref:`promotion <melange_promote>`
325+
326+
301327

302328
Design choices
303329
=====================
304330

305331
Melange support in Dune follows the following design choices:
306332

333+
.. _total:
334+
307335
- :ref:`melange-emit` produces a "total" directory: the artifacts in the
308-
``target`` dir contain all the JavaScript and ``runtime_deps`` assets
336+
``target`` directory contain all the JavaScript and ``runtime_deps`` assets
309337
necessary to run the application either through a JS framework, a bundler, or
310338
otherwise a deployment (excluding external dependencies installed via a JS
311339
package manager). The structure is designed such that relative paths and
312340
dependencies work out of the box relative to their paths in the source tree,
313341
before compilation.
314342
- public libraries are compiled to ``%{target}/node_modules/%{lib_name}`` such
315-
that the `resolution algorithm <https://nodejs.org/api/modules.html#all-together>`_
316-
works to resolve Melange libraries from compiled JS code.
343+
that the `resolution algorithm
344+
<https://nodejs.org/api/modules.html#all-together>`_ works to resolve Melange
345+
libraries from compiled JS code.
346+
- JavaScript output is promoted to the source tree

0 commit comments

Comments
 (0)