Skip to content

Commit

Permalink
Merge branch 'main' into ps/branch/test__rename_excluded_by_modules_f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
Alizter authored Apr 24, 2023
2 parents db08650 + 880f846 commit d066a04
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 115 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ ppx_inline_test \
ppxlib \
ctypes \
"utop>=2.6.0" \
"melange>=0.3.1" \
"mel>=0.3.1"

"melange" \
"rescript-syntax"
# Dependencies recommended for developing dune locally,
# but not wanted in CI
DEV_DEPS := \
Expand Down Expand Up @@ -74,8 +73,9 @@ dev-depext:

.PHONY: melange
melange:
opam pin add -n melange-compiler-libs https://github.com/melange-re/melange-compiler-libs.git#7263bea2285499f5da857f2bb374345a5178791e
opam pin add -n melange https://github.com/melange-re/melange.git#68c6eff82ed056feed809d6cc82558e8697b965b
opam pin add -n melange-compiler-libs.dev https://github.com/melange-re/melange-compiler-libs.git#575ac4c24b296ea897821f9aaee1146ff258c704
opam pin add -n melange.dev https://github.com/melange-re/melange.git#60eabf03d60b1e71f8d5aae72d24d7a1dfcf5bf6
opam pin add -n rescript-syntax.dev https://github.com/melange-re/melange.git#60eabf03d60b1e71f8d5aae72d24d7a1dfcf5bf6

.PHONY: dev-deps
dev-deps: melange
Expand Down
105 changes: 54 additions & 51 deletions doc/melange.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ JavaScript files by defining a :ref:`melange-emit` stanza. Dune libraries can be
used with Melange by adding ``melange`` to ``(modes ...)`` in the
:ref:`library` stanza.

Melange support is still experimental in Dune and needs to be enabled
Melange support is still experimental in Dune and needs to be enabled
in the :ref:`dune-project` file:

.. code:: dune
Expand All @@ -37,6 +37,55 @@ in the :ref:`dune-project` file:
Once that's in place, you can use the Melange mode in :ref:`library` stanzas
``melange.emit`` stanzas.

Simple Project
==============

Let's start by looking at a simple project with Melange and Dune. Subsequent
sections explain the different concepts used here in further detail.

First, make sure that the :ref:`dune-project` file specifies at least
version 3.8 of the dune language and the Melange extension is enabled:

.. code:: dune
(lang dune 3.8)
(using melange 0.1)
Next, write a :ref:`dune<dune-files>` file with a :ref:`melange-emit` stanza:

.. code:: dune
(melange.emit
(target output))
Finally, add a source file to build:

.. code:: bash
echo 'Js.log "hello from melange"' > hello.ml
After running ``dune build output/hello.js``, Dune produces the following
file structure:

.. code::
.
├── _build
│ └── default
│ └── output
│ └── hello.js
├── dune
├── dune-project
└── hello.ml
The resulting JavaScript can now be run:

.. code:: bash
$ node _build/default/output/hello.js
hello from melange
Libraries
=========

Expand Down Expand Up @@ -98,7 +147,7 @@ The resulting layout in ``_build/default/output`` will be as follows:
modules, the targets for the ``.js`` files of any library that the stanza
depends on, and any copy rules for runtime dependencies (see
``runtime_deps`` field below).

- By default, all stanzas will have their targets attached to an alias
``melange``. The behavior of this default alias is exclusive: if an alias
is explicitly defined in the stanza, the targets from this stanza will
Expand Down Expand Up @@ -210,8 +259,8 @@ are used across them. As an example:
(melange.emit
(target app2)
(libraries foo))
The JavaScript artifacts for library ``foo`` will be emitted twice in the
The JavaScript artifacts for library ``foo`` will be emitted twice in the
``_build`` folder. They will be present under ``_build/default/app1``
and ``_build/default/app2``.

Expand All @@ -230,7 +279,7 @@ unnecessary folders in ``node_modules``, it is recommended to explicitly
include only the folders that are relevant for Melange builds.

This can be accomplished by combining :ref:`subdir` and :ref:`dune-subdirs`
stanzas in a ``dune`` file next to the ``node_modules`` folder. The
stanzas in a ``dune`` file next to the ``node_modules`` folder. The
:ref:`dune-vendored_dirs` stanza can be used to avoid warnings in Melange
libraries during the application build. The :ref:`dune-data_only_dirs` stanza
can be useful as well if you need to override the build rules in one of the
Expand All @@ -243,49 +292,3 @@ packages.
(vendored_dirs reason-react)
(dirs reason-react))
Simple Project
==============

Let's look at building a simple project with Melange and Dune.

First, make sure that the :ref:`dune-project` file specifies at least
version 3.8 of the dune language and the Melange extension is enabled:

.. code:: dune
(lang dune 3.8)
(using melange 0.1)
Next, write a :ref:`dune<dune-files>` file with a :ref:`melange-emit` stanza:

.. code:: dune
(melange.emit
(target output))
Finally, add a source file to build:

.. code:: bash
echo 'Js.log "hello from melange"' > hello.ml
After running ``dune build output/hello.js``, Dune produces the following
file structure:

.. code::
.
├── _build
│ └── default
│ └── output
│ └── hello.js
├── dune
├── dune-project
└── hello.ml
The resulting JavaScript can now be run:

.. code:: bash
$ node _build/default/output/hello.js
hello from melange
68 changes: 43 additions & 25 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
slim-melange = mkSlim {
extraBuildInputs = [
pkgs.ocamlPackages.melange
pkgs.ocamlPackages.mel
pkgs.ocamlPackages.rescript-syntax
];
meta.description = ''
Provides a minimal shell environment built purely from nixpkgs
Expand Down Expand Up @@ -214,7 +214,7 @@
++ [
ocamllsp.outputs.packages.${system}.ocaml-lsp-server
pkgs.ocamlPackages.melange
pkgs.ocamlPackages.mel
pkgs.ocamlPackages.rescript-syntax
] ++ nixpkgs.lib.attrsets.attrVals (builtins.attrNames devPackages) scope;
inputsFrom = [ self.packages.${system}.dune ];
meta.description = ''
Expand Down
2 changes: 1 addition & 1 deletion src/dune_engine/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module Prog = struct
match program with
| "refmt" -> Some (Option.value ~default:"opam install reason" hint)
| "rescript_syntax" ->
Some (Option.value ~default:"opam install mel" hint)
Some (Option.value ~default:"opam install rescript-syntax" hint)
| _ -> hint
in
Utils.program_not_found_message ?hint ~loc ~context program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Example using melange.emit, copy_files and include_subdirs
$ cat > src/dune <<EOF
> (melange.emit
> (target app)
> (preprocess (pps melange.ppx))
> (emit_stdlib false)
> (alias mel))
>
> (subdir
Expand All @@ -41,7 +43,6 @@ Example using melange.emit, copy_files and include_subdirs
$ node $src
hello from file


Now add include_subdirs unqualified to show issue

$ echo "(include_subdirs unqualified)" >> src/dune
Expand All @@ -51,4 +52,3 @@ Now add include_subdirs unqualified to show issue
$ node $src
hello from file


6 changes: 5 additions & 1 deletion test/blackbox-tests/test-cases/melange/copy-files-simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Test simple interactions between melange.emit and copy_files
$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (preprocess (pps melange.ppx))
> (alias mel))
>
> (copy_files
Expand All @@ -32,11 +34,14 @@ Test simple interactions between melange.emit and copy_files
hello from file



Copy the file into the output folder, so we can use same path as in-source

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (preprocess (pps melange.ppx))
> (alias mel))
>
> (subdir output
Expand All @@ -55,4 +60,3 @@ Copy the file into the output folder, so we can use same path as in-source
$ node _build/default/output/main.js
hello from file


Loading

0 comments on commit d066a04

Please sign in to comment.