diff --git a/Changes.md b/Changes.md index efb53d5f6..91656b12a 100644 --- a/Changes.md +++ b/Changes.md @@ -35,6 +35,10 @@ Unreleased - This warning had previously been disabled entirely in the typechecker version that Melange uses. It becomes more important with TRMC support added in Melange 2.1.0. +- core: accept `esm{,-global}` in addition to `es6{,-global}` for + `--mel-module-type`; accept `--mel-module-system` in addition to + `--mel-module-type` too + ([#1086](https://github.com/melange-re/melange/pull/1086)). 3.0.0 2024-01-28 --------------- diff --git a/bin/melc.ml b/bin/melc.ml index 245afb25d..6cf966b07 100644 --- a/bin/melc.ml +++ b/bin/melc.ml @@ -213,7 +213,7 @@ let main: Melc_cli.t -> _ Cmdliner.Term.ret ppx; open_modules; bs_package_output; - bs_module_type; + mel_module_system; bs_syntax_only; bs_g; bs_package_name; @@ -295,9 +295,9 @@ let main: Melc_cli.t -> _ Cmdliner.Term.ret if bs_g then Js_config.debug := bs_g; Option.iter Js_packages_state.set_package_name bs_package_name; - begin match bs_module_type, bs_package_output with + begin match mel_module_system, bs_package_output with | None, [] -> () - | Some bs_module_type, [] -> + | Some mel_module_system, [] -> let suffix = match output_name with | Some output_name -> (match Filename.get_all_extensions_maybe output_name with @@ -307,7 +307,7 @@ let main: Melc_cli.t -> _ Cmdliner.Term.ret | None -> raise (Arg.Bad "`-o FILENAME` is required when passing `-bs-module-type`") in - Js_packages_state.set_output_info ~suffix bs_module_type + Js_packages_state.set_output_info ~suffix mel_module_system | None, bs_package_output -> List.iter ~f:(Js_packages_state.update_npm_package_path ?module_name:bs_module_name) diff --git a/bin/melc_cli.ml b/bin/melc_cli.ml index 36950684c..6f662f5f5 100644 --- a/bin/melc_cli.ml +++ b/bin/melc_cli.ml @@ -33,7 +33,7 @@ type t = { ppx : string list; open_modules : string list; bs_package_output : string list; - bs_module_type : Module_system.t option; + mel_module_system : Module_system.t option; bs_syntax_only : bool; bs_g : bool; bs_package_name : string option; @@ -259,7 +259,7 @@ module Internal = struct value & opt_all string [] & info [ "bs-package-output"; "mel-package-output" ] ~doc) - let bs_module_type = + let mel_module_system = let module_system_conv = let parse m = match Module_system.of_string m with @@ -267,14 +267,16 @@ module Internal = struct | None -> Error (`Msg (Format.asprintf "Invalid module system %s" m)) in let print fmt ms = Format.fprintf fmt "%s" (Module_system.to_string ms) in - Arg.conv ~docv:"method" (parse, print) + Arg.conv ~docv:"module system" (parse, print) in let doc = "Specify the module type for JS imports" in let docv = "module-type" in Arg.( value & opt (some module_system_conv) None - & info [ "bs-module-type"; "mel-module-type" ] ~doc ~docv) + & info + [ "bs-module-type"; "mel-module-type"; "mel-module-system" ] + ~doc ~docv) let as_ppx = let doc = "*internal* As ppx for editor integration" in @@ -459,7 +461,7 @@ module Compat = struct end let parse help include_dirs alerts warnings output_name ppx open_modules - bs_package_output bs_module_type bs_syntax_only bs_g bs_package_name + bs_package_output mel_module_system bs_syntax_only bs_g bs_package_name bs_module_name as_ppx as_pp no_alias_deps bs_gentype unboxed_types bs_unsafe_empty_array nostdlib color bs_eval bs_cmi_only bs_no_version_header bs_cross_module_opt bs_diagnose where verbose keep_locs @@ -477,7 +479,7 @@ let parse help include_dirs alerts warnings output_name ppx open_modules ppx; open_modules; bs_package_output; - bs_module_type; + mel_module_system; bs_syntax_only; bs_g; bs_package_name; @@ -536,7 +538,7 @@ let parse help include_dirs alerts warnings output_name ppx open_modules let cmd = Term.( const parse $ help $ include_dirs $ alerts $ warnings $ output_name $ ppx - $ open_modules $ Internal.bs_package_output $ Internal.bs_module_type + $ open_modules $ Internal.bs_package_output $ Internal.mel_module_system $ bs_syntax_only $ bs_g $ bs_package_name $ bs_module_name $ Internal.as_ppx $ Internal.as_pp $ Internal.no_alias_deps $ Internal.bs_gentype $ unboxed_types $ Internal.bs_unsafe_empty_array $ Internal.nostdlib $ color diff --git a/flake.lock b/flake.lock index 8417e744f..f8d9fe03e 100644 --- a/flake.lock +++ b/flake.lock @@ -64,11 +64,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1710451106, - "narHash": "sha256-ldO1qLTiTPTfAvMiQQEkxqlboQYH9KNMcqQGL9xKV9E=", + "lastModified": 1710460369, + "narHash": "sha256-f0LgQ/jid2iHR6T4NxiKJASmlGq7hb+4n/twNjDrF8E=", "owner": "nix-ocaml", "repo": "nix-overlays", - "rev": "59d1bb03ddfc5fe6653cdbef4e20315570d9a138", + "rev": "19b927746c677bee12113a611f3bb8bcd62d27be", "type": "github" }, "original": { diff --git a/jscomp/core/js_dump_program.ml b/jscomp/core/js_dump_program.ml index 77018fc77..4c733a83a 100644 --- a/jscomp/core/js_dump_program.ml +++ b/jscomp/core/js_dump_program.ml @@ -132,9 +132,9 @@ let pp_deps_program = let output_dir = Filename.dirname output_prefix in ignore (match output_info.module_system with - | Es6 | Es6_global -> + | ESM | ESM_global -> es6_program ~package_info ~output_dir ~output_info f program - | NodeJS -> + | CommonJS -> node_program ~package_info ~output_info ~output_dir f program); P.newline f; P.string f diff --git a/jscomp/core/js_name_of_module_id.ml b/jscomp/core/js_name_of_module_id.ml index cf6a46832..fccc572ec 100644 --- a/jscomp/core/js_name_of_module_id.ml +++ b/jscomp/core/js_name_of_module_id.ml @@ -62,13 +62,16 @@ let get_runtime_module_path ~package_info ~output_info | Package_script -> Module_system.runtime_package_path js_file | Package_found _path_info -> ( match module_system with - | NodeJS | Es6 -> Module_system.runtime_package_path js_file + | CommonJS | ESM -> Module_system.runtime_package_path js_file (* Note we did a post-processing when working on Windows *) - | Es6_global -> + | ESM_global -> (* lib/ocaml/xx.cmj -- HACKING: FIXME maybe we can caching relative package path calculation or employ package map *) - let dep_path = "lib" // Module_system.runtime_dir module_system in + let dep_path = + "lib" + (* // Module_system.runtime_dir module_system *) + in (* TODO(anmonteiro): This doesn't work yet *) Path.rel_normalized_absolute_path ~from: @@ -132,9 +135,9 @@ let string_of_module_id ~package_info ~output_info ~to_:dep_info.rel_path js_file | false -> ( match module_system with - | NodeJS | Es6 -> dep_info.pkg_rel_path // js_file + | CommonJS | ESM -> dep_info.pkg_rel_path // js_file (* Note we did a post-processing when working on Windows *) - | Es6_global -> + | ESM_global -> Path.rel_normalized_absolute_path ~from: (Js_packages_info.get_output_dir diff --git a/jscomp/melstd/module_system.ml b/jscomp/melstd/module_system.ml index f32dd5730..af38c1142 100644 --- a/jscomp/melstd/module_system.ml +++ b/jscomp/melstd/module_system.ml @@ -22,35 +22,32 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -type t = NodeJS | Es6 | Es6_global +type t = CommonJS | ESM | ESM_global -let default = NodeJS +let default = CommonJS (* ocamlopt could not optimize such simple case..*) let compatible ~dep t = match t with - | NodeJS -> dep = NodeJS - | Es6 -> dep = Es6 - | Es6_global -> dep = Es6_global || dep = Es6 + | CommonJS -> dep = CommonJS + | ESM -> dep = ESM + | ESM_global -> dep = ESM_global || dep = ESM (* As a dependency Leaf Node, it is the same either [global] or [not] *) -(* in runtime lib, [es6] and [es6] are treated the same way *) -let runtime_dir = function NodeJS -> "js" | Es6 | Es6_global -> "es6" - let runtime_package_path = let ( // ) = Path.( // ) in let melange_js = "melange.js" in fun js_file -> melange_js // js_file let to_string = function - | NodeJS -> "commonjs" - | Es6 -> "es6" - | Es6_global -> "es6-global" + | CommonJS -> "commonjs" + | ESM -> "esm" + | ESM_global -> "esm-global" let of_string_exn = function - | "commonjs" -> NodeJS - | "es6" -> Es6 - | "es6-global" -> Es6_global + | "commonjs" -> CommonJS + | "esm" | "es6" -> ESM + | "esm-global" | "es6-global" -> ESM_global | s -> raise (Arg.Bad ("invalid module system " ^ s)) let of_string s = diff --git a/jscomp/melstd/module_system.mli b/jscomp/melstd/module_system.mli index 85b6b9c30..fb6754ad7 100644 --- a/jscomp/melstd/module_system.mli +++ b/jscomp/melstd/module_system.mli @@ -22,11 +22,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -type t = NodeJS | Es6 | Es6_global +type t = CommonJS | ESM | ESM_global val default : t val compatible : dep:t -> t -> bool -val runtime_dir : t -> string val runtime_package_path : string -> string val to_string : t -> string val of_string_exn : string -> t diff --git a/playground/mel_playground.ml b/playground/mel_playground.ml index e82f54302..96daf2f94 100644 --- a/playground/mel_playground.ml +++ b/playground/mel_playground.ml @@ -208,7 +208,7 @@ let compile = ~package_info:Js_packages_info.empty ~output_info: { - Js_packages_info.module_system = Es6; + Js_packages_info.module_system = ESM; suffix = Js_suffix.default; } (Js_pp.from_buffer buffer)