Skip to content

Commit

Permalink
melange: start parsing "esm" and "esm-global" for module system too (#…
Browse files Browse the repository at this point in the history
…1086)

* melange: start parsing "esm" and "esm-global" for module system too

* chore: add changelog entry
  • Loading branch information
anmonteiro authored Mar 15, 2024
1 parent d9468a2 commit 68a4440
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 38 deletions.
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down
8 changes: 4 additions & 4 deletions bin/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions bin/melc_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -259,22 +259,24 @@ 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
| Some module_system -> Ok module_system
| 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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 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 jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions jscomp/core/js_name_of_module_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions jscomp/melstd/module_system.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 1 addition & 2 deletions jscomp/melstd/module_system.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion playground/mel_playground.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 68a4440

Please sign in to comment.