Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 2 additions & 0 deletions doc/changes/12464.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- melange support: don't emit empty JavaScript modules for generated module
aliases. (@anmonteiro)
Comment thread
anmonteiro marked this conversation as resolved.
Outdated
27 changes: 17 additions & 10 deletions src/dune_rules/melange/melange_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ let impl_only_modules_defined_in_this_lib ~sctx ~scope lib =
]
| true -> ()
in
( modules
, (Modules.With_vlib.split_by_lib modules).impl
|> List.filter ~f:(Module.has ~ml_kind:Impl) )
let impl_only =
Modules.With_vlib.fold_no_vlib_with_aliases
modules
~init:[]
~normal:(fun m acc -> if Module.has m ~ml_kind:Impl then m :: acc else acc)
~alias:(fun _m acc -> acc)
in
modules, impl_only
;;

let cmj_includes =
Expand Down Expand Up @@ -183,13 +188,15 @@ let compile_info ~scope (mel : Melange_stanzas.Emit.t) =
let js_targets_of_modules modules ~module_systems ~output =
List.map module_systems ~f:(fun (_, js_ext) ->
modules
|> Modules.With_vlib.drop_vlib
|> Modules.fold_user_available ~init:Path.Set.empty ~f:(fun m acc ->
if Module.has m ~ml_kind:Impl
then (
let target = Path.build @@ make_js_name ~js_ext ~output m in
Path.Set.add acc target)
else acc))
|> Modules.With_vlib.fold_no_vlib_with_aliases
~init:Path.Set.empty
~alias:(fun _m acc -> acc)
~normal:(fun m acc ->
if Module.has m ~ml_kind:Impl
then (
let target = Path.build @@ make_js_name ~js_ext ~output m in
Path.Set.add acc target)
else acc))
|> Path.Set.union_all
;;

Expand Down
1 change: 0 additions & 1 deletion test/blackbox-tests/test-cases/melange/emit-private.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Test dependency on a private library in the same package as melange.emit
> EOF

$ OCAMLPATH=$PWD/prefix/lib/:$OCAMLPATH dune build @dist --display=short 2>&1 | grep -v melange
melc b/dist/node_modules/pkg.__private__.a/a.js
melc b/dist/node_modules/pkg.__private__.a/foo.js
melc b/dist/b/bar.js

Expand Down
129 changes: 129 additions & 0 deletions test/blackbox-tests/test-cases/melange/empty-aliases-file.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

$ mkdir lib
$ cat > dune-project <<EOF
> (lang dune 3.20)
> (package (name foo))
> (using melange 1.0)
> EOF

$ cat > lib/dune <<EOF
> (library
> (name foo)
> (modes melange))
> EOF
$ cat > lib/bar.ml <<EOF
> let greeting = "hello"
> EOF

$ cat > dune <<EOF
> (melange.emit
> (target dist)
> (libraries foo)
> (emit_stdlib false)
> (modules))
> EOF
$ cat > lib/bar.ml <<EOF
> let greeting = "hello"
> EOF

$ dune build @melange

No `.js` file present for the library alias `foo.js`

$ find _build/default/dist/lib -type f | sort
_build/default/dist/lib/bar.js

Now write a foo.ml file

$ cat > lib/dune <<EOF
> (library
> (name foo)
> (modes melange))
> EOF
$ cat > lib/foo.ml <<EOF
> module Bar = Bar
> let x = "foo"
> EOF

$ dune build @melange

`foo.js` was manually written, therefore it's present

$ find _build/default/dist/ -type f | sort
_build/default/dist/lib/bar.js
_build/default/dist/lib/foo.js
$ cat _build/default/dist/lib/foo.js
// Generated by Melange
'use strict';


const x = "foo";

module.exports = {
x,
}
/* No side effect */

`(include_subdirs qualified)`

$ mkdir lib/sub
$ cat > lib/dune <<EOF
> (include_subdirs qualified)
> (library
> (name foo)
> (modes melange))
> EOF
$ cat > lib/bar.ml <<EOF
> let hello = Sub.Hello.hello
> EOF
$ cat > lib/sub/hello.ml <<EOF
> let hello = "hello from sub"
> EOF
$ cat > lib/foo.ml <<EOF
> module Bar = Bar
> let x = "foo"
> EOF

$ dune build @melange

`foo.js` was manually written, therefore it's present. `sub.js` is an alias
file, and it's not present

$ find _build/default/dist/lib -type f | sort
_build/default/dist/lib/bar.js
_build/default/dist/lib/foo.js
_build/default/dist/lib/sub/hello.js

$ cat > lib/dune <<EOF
> (include_subdirs qualified)
> (library
> (name foo)
> (modes melange))
> EOF
$ cat > lib/bar.ml <<EOF
> let hello = Sub.Hello.hello ^ Sub.world
> EOF
$ cat > lib/sub/sub.ml <<EOF
> module Hello = Hello
> let world = "world"
> EOF
$ cat > lib/sub/hello.ml <<EOF
> let hello = "hello from sub"
> EOF
$ cat > lib/foo.ml <<EOF
> module Bar = Bar
> let x = "foo"
> EOF

$ DUNE_SANDBOX=none dune build @melange

`foo.js` was manually written, therefore it's present. `sub.js` is an alias
file, and it's not present

$ find _build/default/dist/lib -type f | sort
_build/default/dist/lib/bar.js
_build/default/dist/lib/foo.js
_build/default/dist/lib/sub/hello.js
_build/default/dist/lib/sub/sub.js


Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ source:

$ find _build/default/$output -iname "*.js" | grep -v melange | sort
_build/default/inside/output/inside/app/b.js
_build/default/inside/output/inside/app/lib.js
_build/default/inside/output/inside/app/lib/a.js
_build/default/inside/output/inside/c.js
$ node _build/default/$output/inside/c.js
Expand Down
Loading