diff --git a/doc/changes/11252.md b/doc/changes/11252.md new file mode 100644 index 00000000000..4badca61595 --- /dev/null +++ b/doc/changes/11252.md @@ -0,0 +1,3 @@ +- Pass `melange.emit`'s `compile_flags` to the JS emission phase. (@amonteiro, + #11252) + diff --git a/src/dune_rules/melange/melange_rules.ml b/src/dune_rules/melange/melange_rules.ml index dff2d385c02..c52908f2dd9 100644 --- a/src/dune_rules/melange/melange_rules.ml +++ b/src/dune_rules/melange/melange_rules.ml @@ -194,6 +194,7 @@ let build_js ~obj_dir ~sctx ~includes + ~(compile_flags : Ocaml_flags.t) ~local_modules_and_obj_dir m = @@ -218,6 +219,7 @@ let build_js compiler [ Command.Args.S obj_dir ; Command.Args.as_any includes + ; Command.Args.dyn (Ocaml_flags.get compile_flags Melange) ; As melange_package_args ; A "-o" ; Target output @@ -259,6 +261,13 @@ let add_deps_to_aliases ?(alias = Melange_stanzas.Emit.implicit_alias) ~dir deps Memo.parallel_iter ~f:attach [ alias; dune_default_alias ] ;; +let melange_compile_flags ~sctx ~dir (mel : Melange_stanzas.Emit.t) = + let specific = Lib_mode.Map.make_all mel.compile_flags in + Ocaml_flags.Spec.make ~common:Ordered_set_lang.Unexpanded.standard ~specific + |> Ocaml_flags_db.ocaml_flags sctx ~dir + >>| Ocaml_flags.allow_only_melange +;; + let setup_emit_cmj_rules ~sctx ~dir @@ -297,12 +306,7 @@ let setup_emit_cmj_rules Modules.With_vlib.modules modules, pp in let requires_link = Lib.Compile.requires_link compile_info in - let* flags = - let specific = Lib_mode.Map.make_all mel.compile_flags in - Ocaml_flags.Spec.make ~common:Ordered_set_lang.Unexpanded.standard ~specific - |> Ocaml_flags_db.ocaml_flags sctx ~dir - >>| Ocaml_flags.allow_only_melange - in + let* flags = melange_compile_flags ~sctx ~dir mel in let* cctx = let direct_requires = Lib.Compile.direct_requires compile_info in Compilation_context.create @@ -474,7 +478,7 @@ let setup_entries_js ocaml.lib_config in cmj_includes ~requires_link ~scope lib_config - in + and* compile_flags = melange_compile_flags ~sctx ~dir mel in let output = `Private_library_or_emit target_dir in let obj_dir = Obj_dir.of_local local_obj_dir in let* () = @@ -494,6 +498,7 @@ let setup_entries_js ~obj_dir ~sctx ~includes + ~compile_flags ~local_modules_and_obj_dir m) ;; @@ -557,7 +562,7 @@ let setup_js_rules_libraries = |> Resolve.Memo.map ~f:(with_vlib_implementations lib) in cmj_includes ~requires_link ~scope lib_config - in + and* compile_flags = melange_compile_flags ~sctx ~dir mel in let+ () = setup_runtime_assets_rules sctx @@ -602,13 +607,13 @@ let setup_js_rules_libraries = ~sctx ~scope vlib - ~f:(build_js ~dir ~output ~includes) + ~f:(build_js ~dir ~output ~includes ~compile_flags) and+ () = parallel_build_source_modules ~sctx ~scope lib - ~f:(build_js ~dir ~output ~includes) + ~f:(build_js ~dir ~output ~includes ~compile_flags) in ()) ;; diff --git a/test/blackbox-tests/test-cases/melange/flags-for-emission.t b/test/blackbox-tests/test-cases/melange/flags-for-emission.t new file mode 100644 index 00000000000..cfb40a084d7 --- /dev/null +++ b/test/blackbox-tests/test-cases/melange/flags-for-emission.t @@ -0,0 +1,31 @@ +Test compile_flags are passed to JS file emission + + $ cat > dune-project < (lang dune 3.13) + > (using melange 0.1) + > EOF + + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ cat > dune < (melange.emit + > (target output) + > (emit_stdlib false) + > (modules main) + > (compile_flags :standard --mel-no-version-header)) + > EOF + + $ dune build @melange + +File is generated with the "Generated by Melange" header even though we +specified the opposite in flags + + $ cat _build/default/output/main.js + 'use strict'; + + + console.log("hello"); + + /* Not a pure module */