diff --git a/otherlibs/stdune/src/filename.ml b/otherlibs/stdune/src/filename.ml index ae9d80bfd85..4596d43567f 100644 --- a/otherlibs/stdune/src/filename.ml +++ b/otherlibs/stdune/src/filename.ml @@ -2,6 +2,10 @@ include Stdlib.Filename type t = string +module Extension = struct + type nonrec t = t +end + let split_extension fn = let ext = extension fn in (String.sub fn ~pos:0 ~len:(String.length fn - String.length ext), ext) diff --git a/otherlibs/stdune/src/filename.mli b/otherlibs/stdune/src/filename.mli index 38830a601eb..7d77037c60a 100644 --- a/otherlibs/stdune/src/filename.mli +++ b/otherlibs/stdune/src/filename.mli @@ -9,7 +9,11 @@ end (* TODO add invariants and make this abstract or private *) type t = string -val split_extension : t -> string * string +module Extension : sig + type nonrec t = t +end + +val split_extension : t -> string * Extension.t val split_extension_after_dot : t -> string * string diff --git a/src/dune_rules/melange/melange.ml b/src/dune_rules/melange/melange.ml index d6a034c891b..d28831ef881 100644 --- a/src/dune_rules/melange/melange.ml +++ b/src/dune_rules/melange/melange.ml @@ -5,6 +5,8 @@ module Module_system = struct | Es6 | CommonJs + let default = (CommonJs, ".js") + let to_string = function | Es6 -> "es6" | CommonJs -> "commonjs" diff --git a/src/dune_rules/melange/melange.mli b/src/dune_rules/melange/melange.mli index a8f5dfdb9de..b4d0d3e0e36 100644 --- a/src/dune_rules/melange/melange.mli +++ b/src/dune_rules/melange/melange.mli @@ -5,6 +5,8 @@ module Module_system : sig | Es6 | CommonJs + val default : t * Filename.Extension.t + val to_string : t -> string end diff --git a/src/dune_rules/melange/melange_rules.ml b/src/dune_rules/melange/melange_rules.ml index 09261a765d0..936f7612b23 100644 --- a/src/dune_rules/melange/melange_rules.ml +++ b/src/dune_rules/melange/melange_rules.ml @@ -106,60 +106,66 @@ let compile_info ~scope (mel : Melange_stanzas.Emit.t) = ~allow_overlaps:mel.allow_overlapping_dependencies ~forbidden_libraries:[] mel.libraries ~pps ~dune_version ~merlin_ident -let js_targets_of_modules modules ~js_ext ~output = - Modules.fold_no_vlib modules ~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) +let js_targets_of_modules modules ~module_systems ~output = + List.map module_systems ~f:(fun (_, js_ext) -> + Modules.fold_no_vlib modules ~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)) + |> Path.Set.union_all -let js_targets_of_libs sctx libs ~js_ext ~target_dir = - let of_lib lib = - let open Memo.O in - let+ modules = impl_only_modules_defined_in_this_lib sctx lib in - let output = output_of_lib ~target_dir lib in - List.rev_map modules ~f:(fun m -> - Path.build @@ make_js_name ~output ~js_ext m) - in - Resolve.Memo.List.concat_map libs ~f:(fun lib -> - let open Memo.O in - let* base = of_lib lib in - match Lib.implements lib with - | None -> Resolve.Memo.return base - | Some vlib -> - let open Resolve.Memo.O in - let* vlib = vlib in - let+ for_vlib = Resolve.Memo.lift_memo (of_lib vlib) in - List.rev_append for_vlib base) +let js_targets_of_libs sctx libs ~module_systems ~target_dir = + Resolve.Memo.List.concat_map module_systems ~f:(fun (_, js_ext) -> + let of_lib lib = + let open Memo.O in + let+ modules = impl_only_modules_defined_in_this_lib sctx lib in + let output = output_of_lib ~target_dir lib in + List.rev_map modules ~f:(fun m -> + Path.build @@ make_js_name ~output ~js_ext m) + in + Resolve.Memo.List.concat_map libs ~f:(fun lib -> + let open Memo.O in + let* base = of_lib lib in + match Lib.implements lib with + | None -> Resolve.Memo.return base + | Some vlib -> + let open Resolve.Memo.O in + let* vlib = vlib in + let+ for_vlib = Resolve.Memo.lift_memo (of_lib vlib) in + List.rev_append for_vlib base)) -let build_js ~loc ~dir ~pkg_name ~mode ~module_system ~output ~obj_dir ~sctx - ~includes ~js_ext m = +let build_js ~loc ~dir ~pkg_name ~mode ~module_systems ~output ~obj_dir ~sctx + ~includes m = let open Memo.O in let* compiler = Melange_binary.melc sctx ~loc:(Some loc) ~dir in - let src = Obj_dir.Module.cm_file_exn obj_dir m ~kind:(Melange Cmj) in - let output = make_js_name ~output ~js_ext m in - let obj_dir = [ Command.Args.A "-I"; Path (Obj_dir.melange_dir obj_dir) ] in - let melange_package_args = - let pkg_name_args = - match pkg_name with - | None -> [] - | Some pkg_name -> - [ "--bs-package-name"; Package.Name.to_string pkg_name ] - in - let js_modules_str = Melange.Module_system.to_string module_system in - "--bs-module-type" :: js_modules_str :: pkg_name_args - in - Super_context.add_rule sctx ~dir ~loc ~mode - (Command.run - ~dir:(Path.build (Super_context.context sctx).build_dir) - compiler - [ Command.Args.S obj_dir - ; Command.Args.as_any includes - ; As melange_package_args - ; A "-o" - ; Target output - ; Dep src - ]) + Memo.parallel_iter module_systems ~f:(fun (module_system, js_ext) -> + let src = Obj_dir.Module.cm_file_exn obj_dir m ~kind:(Melange Cmj) in + let output = make_js_name ~output ~js_ext m in + let obj_dir = + [ Command.Args.A "-I"; Path (Obj_dir.melange_dir obj_dir) ] + in + let melange_package_args = + let pkg_name_args = + match pkg_name with + | None -> [] + | Some pkg_name -> + [ "--bs-package-name"; Package.Name.to_string pkg_name ] + in + let js_modules_str = Melange.Module_system.to_string module_system in + "--bs-module-type" :: js_modules_str :: pkg_name_args + in + Super_context.add_rule sctx ~dir ~loc ~mode + (Command.run + ~dir:(Path.build (Super_context.context sctx).build_dir) + compiler + [ Command.Args.S obj_dir + ; Command.Args.as_any includes + ; As melange_package_args + ; A "-o" + ; Target output + ; Dep src + ])) let setup_emit_cmj_rules ~sctx ~dir ~scope ~expander ~dir_contents (mel : Melange_stanzas.Emit.t) = @@ -213,10 +219,10 @@ let setup_emit_cmj_rules ~sctx ~dir ~scope ~expander ~dir_contents match mel.alias with | None -> Memo.return () | Some alias_name -> - let js_ext = mel.javascript_extension in + let module_systems = mel.module_systems in let deps = js_targets_of_modules ~output:(`Private_library_or_emit target_dir) - ~js_ext modules + ~module_systems modules |> Action_builder.path_set in let alias = Alias.make alias_name ~dir in @@ -227,7 +233,7 @@ let setup_emit_cmj_rules ~sctx ~dir ~scope ~expander ~dir_contents @@ let open Resolve.Memo.O in Compilation_context.requires_link cctx - >>= js_targets_of_libs sctx ~js_ext ~target_dir + >>= js_targets_of_libs sctx ~module_systems ~target_dir in Action_builder.paths deps) |> Rules.Produce.Alias.add_deps alias @@ -266,7 +272,7 @@ let setup_entries_js ~sctx ~dir ~dir_contents ~scope ~compile_info ~target_dir let requires_link = Lib.Compile.requires_link compile_info in let pkg_name = Option.map mel.package ~f:Package.name in let loc = mel.loc in - let js_ext = mel.javascript_extension in + let module_systems = mel.module_systems in let* requires_link = Memo.Lazy.force requires_link in let includes = cmj_includes ~requires_link ~scope in let modules_for_js = @@ -276,12 +282,12 @@ let setup_entries_js ~sctx ~dir ~dir_contents ~scope ~compile_info ~target_dir let output = `Private_library_or_emit target_dir in let obj_dir = Obj_dir.of_local obj_dir in Memo.parallel_iter modules_for_js ~f:(fun m -> - build_js ~dir ~loc ~pkg_name ~mode ~module_system:mel.module_system - ~output ~obj_dir ~sctx ~includes ~js_ext m) + build_js ~dir ~loc ~pkg_name ~mode ~module_systems ~output ~obj_dir ~sctx + ~includes m) let setup_js_rules_libraries ~dir ~scope ~target_dir ~sctx ~requires_link ~mode (mel : Melange_stanzas.Emit.t) = - let build_js = build_js ~sctx ~mode ~js_ext:mel.javascript_extension in + let build_js = build_js ~sctx ~mode ~module_systems:mel.module_systems in Memo.parallel_iter requires_link ~f:(fun lib -> let open Memo.O in let lib_name = Lib.name lib in @@ -294,7 +300,7 @@ let setup_js_rules_libraries ~dir ~scope ~target_dir ~sctx ~requires_link ~mode let build_js = let obj_dir = Lib_info.obj_dir info in let pkg_name = Lib_info.package info in - build_js ~loc ~pkg_name ~module_system:mel.module_system ~obj_dir + build_js ~loc ~pkg_name ~obj_dir in let* includes = let+ requires_link = diff --git a/src/dune_rules/melange/melange_stanzas.ml b/src/dune_rules/melange/melange_stanzas.ml index cdfd93df423..6c7b151bb71 100644 --- a/src/dune_rules/melange/melange_stanzas.ml +++ b/src/dune_rules/melange/melange_stanzas.ml @@ -6,7 +6,7 @@ module Emit = struct { loc : Loc.t ; target : string ; alias : Alias.Name.t option - ; module_system : Melange.Module_system.t + ; module_systems : (Melange.Module_system.t * Filename.Extension.t) list ; modules : Stanza_common.Modules_settings.t ; libraries : Lib_dep.t list ; package : Package.t option @@ -15,7 +15,6 @@ module Emit = struct ; promote : Rule.Promote.t option ; compile_flags : Ordered_set_lang.Unexpanded.t ; allow_overlapping_dependencies : bool - ; javascript_extension : string } type Stanza.t += T of t @@ -51,14 +50,57 @@ module Emit = struct t let decode = - let extension_field name = - let+ loc, extension = - field name ~default:(Loc.none, "js") (located string) - in + let extension_field = + let+ loc, extension = located string in if String.is_prefix ~prefix:"." extension then User_error.raise ~loc [ Pp.textf "extension must not start with '.'" ]; "." ^ extension in + let module_systems = + let module_system = + enum [ ("es6", Melange.Module_system.Es6); ("commonjs", CommonJs) ] + in + let+ module_systems = + repeat + (pair module_system (located extension_field) + <|> let+ loc, module_system = located module_system in + let _, ext = Melange.Module_system.default in + (module_system, (loc, ext))) + in + + let module_systems = + match + String.Map.of_list_map module_systems ~f:(fun (ms, (loc, ext)) -> + (ext, (loc, ms))) + with + | Ok m -> String.Map.to_list_map m ~f:(fun ext (_loc, ms) -> (ms, ext)) + | Error (ext, (_, (loc1, _)), (_, (loc2, _))) -> + let main_message = + Pp.textf "JavaScript extension %s appears more than once:" ext + in + let annots = + let main = User_message.make ~loc:loc2 [ main_message ] in + let related = + [ User_message.make ~loc:loc1 [ Pp.text "Already defined here" ] ] + in + User_message.Annots.singleton Compound_user_error.annot + [ Compound_user_error.make ~main ~related ] + in + User_error.raise ~annots ~loc:loc2 + [ main_message + ; Pp.textf "- %s" (Loc.to_file_colon_line loc1) + ; Pp.textf "- %s" (Loc.to_file_colon_line loc2) + ; Pp.textf "Extensions must be unique per melange.emit stanza" + ] + ~hints: + [ Pp.textf + "specify different extensions with (module_systems \ + ( ) ( ))" + ] + in + + module_systems + in fields (let+ loc = loc and+ target = @@ -81,16 +123,15 @@ module Emit = struct in field "target" (plain_string (fun ~loc s -> of_string ~loc s)) and+ alias = field_o "alias" Alias.Name.decode - and+ module_system = - field "module_system" - (enum [ ("es6", Melange.Module_system.Es6); ("commonjs", CommonJs) ]) + and+ module_systems = + field "module_systems" module_systems + ~default:[ Melange.Module_system.default ] and+ libraries = field "libraries" decode_lib ~default:[] and+ package = field_o "package" Stanza_common.Pkg.decode and+ preprocess, preprocessor_deps = Stanza_common.preprocess_fields and+ promote = field_o "promote" Rule_mode_decoder.Promote.decode and+ loc_instrumentation, instrumentation = Stanza_common.instrumentation and+ compile_flags = Ordered_set_lang.Unexpanded.field "compile_flags" - and+ javascript_extension = extension_field "javascript_extension" and+ allow_overlapping_dependencies = field_b "allow_overlapping_dependencies" and+ modules = @@ -109,7 +150,7 @@ module Emit = struct { loc ; target ; alias - ; module_system + ; module_systems ; modules ; libraries ; package @@ -117,7 +158,6 @@ module Emit = struct ; preprocessor_deps ; promote ; compile_flags - ; javascript_extension ; allow_overlapping_dependencies }) end diff --git a/src/dune_rules/melange/melange_stanzas.mli b/src/dune_rules/melange/melange_stanzas.mli index d53d3280afa..e81e04b7aaa 100644 --- a/src/dune_rules/melange/melange_stanzas.mli +++ b/src/dune_rules/melange/melange_stanzas.mli @@ -6,7 +6,7 @@ module Emit : sig { loc : Loc.t ; target : string ; alias : Alias.Name.t option - ; module_system : Melange.Module_system.t + ; module_systems : (Melange.Module_system.t * string) list ; modules : Stanza_common.Modules_settings.t ; libraries : Lib_dep.t list ; package : Package.t option @@ -15,7 +15,6 @@ module Emit : sig ; promote : Rule.Promote.t option ; compile_flags : Ordered_set_lang.Unexpanded.t ; allow_overlapping_dependencies : bool - ; javascript_extension : string } type Stanza.t += T of t diff --git a/test/blackbox-tests/test-cases/melange/add-rules-under-melange-emit-target.t b/test/blackbox-tests/test-cases/melange/add-rules-under-melange-emit-target.t index 1fd16363525..1cb36c76978 100644 --- a/test/blackbox-tests/test-cases/melange/add-rules-under-melange-emit-target.t +++ b/test/blackbox-tests/test-cases/melange/add-rules-under-melange-emit-target.t @@ -8,8 +8,7 @@ Test that the target directory exists $ cat > dune < (melange.emit > (alias melange) - > (target output) - > (module_system commonjs)) + > (target output)) > EOF Create the target dir @@ -36,8 +35,7 @@ Target promotion works > (melange.emit > (alias melange) > (target output) - > (promote (until-clean)) - > (module_system commonjs)) + > (promote (until-clean))) > EOF $ dune build @melange diff --git a/test/blackbox-tests/test-cases/melange/aliases.t b/test/blackbox-tests/test-cases/melange/aliases.t index 9128aea287b..57e5c4802ad 100644 --- a/test/blackbox-tests/test-cases/melange/aliases.t +++ b/test/blackbox-tests/test-cases/melange/aliases.t @@ -8,8 +8,7 @@ Test (preprocess) field on melange.emit stanza $ cat > dune < (melange.emit > (target output) - > (alias app) - > (module_system commonjs)) + > (alias app)) > EOF $ cat > main.ml < src/dune < (melange.emit > (target app) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > > (subdir > app @@ -42,6 +41,7 @@ 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 @@ -51,3 +51,4 @@ Now add include_subdirs unqualified to show issue $ node $src hello from file + diff --git a/test/blackbox-tests/test-cases/melange/copy-files-into-emit-target-folder.t b/test/blackbox-tests/test-cases/melange/copy-files-into-emit-target-folder.t index 576a350f8df..6a85a6ce027 100644 --- a/test/blackbox-tests/test-cases/melange/copy-files-into-emit-target-folder.t +++ b/test/blackbox-tests/test-cases/melange/copy-files-into-emit-target-folder.t @@ -11,8 +11,7 @@ into the melange.emit target folder $ cat > src/dune < (melange.emit > (target output) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF $ touch public/img.png @@ -26,8 +25,7 @@ Now try copying a file $ cat > src/dune < (melange.emit > (target output) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > > (subdir > output diff --git a/test/blackbox-tests/test-cases/melange/copy-files-simple.t b/test/blackbox-tests/test-cases/melange/copy-files-simple.t index 3f7b87d2cd6..3161335fe12 100644 --- a/test/blackbox-tests/test-cases/melange/copy-files-simple.t +++ b/test/blackbox-tests/test-cases/melange/copy-files-simple.t @@ -8,8 +8,7 @@ Test simple interactions between melange.emit and copy_files $ cat > dune < (melange.emit > (target output) - > (alias mel) - > (module_system commonjs)) + > (alias mel)) > > (copy_files > (alias mel) @@ -32,13 +31,13 @@ Test simple interactions between melange.emit and copy_files $ node _build/default/output/main.js hello from file + Copy the file into the output folder, so we can use same path as in-source $ cat > dune < (melange.emit > (target output) - > (alias mel) - > (module_system commonjs)) + > (alias mel)) > > (subdir output > (subdir assets @@ -56,3 +55,4 @@ 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 + diff --git a/test/blackbox-tests/test-cases/melange/depend-on-installed.t b/test/blackbox-tests/test-cases/melange/depend-on-installed.t index 87a0ab697e7..33ca61aa919 100644 --- a/test/blackbox-tests/test-cases/melange/depend-on-installed.t +++ b/test/blackbox-tests/test-cases/melange/depend-on-installed.t @@ -88,7 +88,6 @@ Test dependency on installed package $ cat > app/dune < (melange.emit > (target dist) - > (module_system commonjs) > (alias melange-dist) > (libraries b)) > EOF diff --git a/test/blackbox-tests/test-cases/melange/dune-js-file-unmangling.t/dune b/test/blackbox-tests/test-cases/melange/dune-js-file-unmangling.t/dune index 8cb64c1441c..2474ffe1d3c 100644 --- a/test/blackbox-tests/test-cases/melange/dune-js-file-unmangling.t/dune +++ b/test/blackbox-tests/test-cases/melange/dune-js-file-unmangling.t/dune @@ -1,6 +1,5 @@ (melange.emit (target dist) (alias melange) - (module_system commonjs) (entries entry_module) (libraries parent)) diff --git a/test/blackbox-tests/test-cases/melange/dune-package-source-path.t b/test/blackbox-tests/test-cases/melange/dune-package-source-path.t index 5a28eeaa5b5..6de663abac9 100644 --- a/test/blackbox-tests/test-cases/melange/dune-package-source-path.t +++ b/test/blackbox-tests/test-cases/melange/dune-package-source-path.t @@ -47,8 +47,7 @@ form `foo.bar.baz` > (melange.emit > (target dist) > (alias dist) - > (libraries a.sub) - > (module_system commonjs)) + > (libraries a.sub)) > EOF $ cat > app/bar.ml < (melange.emit > (target dist) > (alias dist) - > (libraries a) - > (module_system commonjs)) + > (libraries a)) > EOF $ cat > b/bar.ml < (melange.emit > (target dist) > (alias dist) - > (libraries a) - > (module_system commonjs)) + > (libraries a)) > EOF $ cat > c/dune < (melange.emit > (target dist) > (alias dist) - > (libraries a) - > (module_system commonjs)) + > (libraries a)) > EOF $ cat > b/bar.ml < (target dist) > (alias dist) > (libraries a) - > (package a) - > (module_system commonjs)) + > (package a)) > EOF $ cat > b/bar.ml < (melange.emit > (target dist) > (alias dist) - > (libraries aa_fe.melange) - > (module_system commonjs)) + > (libraries aa_fe.melange)) > EOF $ cat > bar.ml < dune < (melange.emit > (alias melange-dist) - > (target dist) - > (module_system commonjs)) + > (target dist)) > EOF Create the dist folder diff --git a/test/blackbox-tests/test-cases/melange/empty-entries.t b/test/blackbox-tests/test-cases/melange/empty-entries.t index 592f79a8561..a2f686b4f17 100644 --- a/test/blackbox-tests/test-cases/melange/empty-entries.t +++ b/test/blackbox-tests/test-cases/melange/empty-entries.t @@ -8,8 +8,7 @@ Test (entries) field can be left empty $ cat > dune < (melange.emit > (target output) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF $ cat > hello.ml < (melange.emit > (target output) > (entries main) - > (module_system commonjs) > (flags -w -14-26)) > EOF $ dune build @melange - File "dune", line 5, characters 2-7: - 5 | (flags -w -14-26)) + File "dune", line 4, characters 2-7: + 4 | (flags -w -14-26)) ^^^^^ Error: Unknown field flags [1] @@ -33,8 +32,7 @@ Adds a module that contains unused var (warning 26) and illegal backlash (warnin > (melange.emit > (target output) > (entries main) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF Trying to build triggers both warnings @@ -57,7 +55,6 @@ Let's ignore them using compile_flags > (target output) > (entries main) > (alias melange) - > (module_system commonjs) > (compile_flags -w -14-26)) > EOF @@ -71,8 +68,7 @@ Can also pass flags from the env stanza. Let's go back to failing state: > (melange.emit > (target output) > (entries main) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF $ dune build @melange @@ -95,8 +91,7 @@ Adding env stanza with both warnings silenced allows the build to pass successfu > (melange.emit > (alias melange) > (target output) - > (entries main) - > (module_system commonjs)) + > (entries main)) > EOF $ dune build @melange @@ -113,8 +108,7 @@ Warning 102 (Melange only) is available if explicitly set > (melange.emit > (target output) > (entries main) - > (compile_flags -w +a-70) - > (module_system commonjs)) + > (compile_flags -w +a-70)) > EOF $ dune build output/main.js @@ -128,8 +122,7 @@ But it is disabled by default $ cat > dune < (melange.emit > (target output) - > (entries main) - > (module_system commonjs)) + > (entries main)) > EOF $ dune build output/main.js diff --git a/test/blackbox-tests/test-cases/melange/gh7020.t b/test/blackbox-tests/test-cases/melange/gh7020.t index e1403803687..228c2e89847 100644 --- a/test/blackbox-tests/test-cases/melange/gh7020.t +++ b/test/blackbox-tests/test-cases/melange/gh7020.t @@ -44,7 +44,7 @@ Reproduce github #7020 > (target es6) > (alias melange) > (libraries dummyfoo) - > (module_system es6)) + > (module_systems es6)) > EOF $ dune build @melange diff --git a/test/blackbox-tests/test-cases/melange/include_subdirs.t/inside/dune b/test/blackbox-tests/test-cases/melange/include_subdirs.t/inside/dune index 699777c88ff..b68dd9b5ad9 100644 --- a/test/blackbox-tests/test-cases/melange/include_subdirs.t/inside/dune +++ b/test/blackbox-tests/test-cases/melange/include_subdirs.t/inside/dune @@ -2,5 +2,4 @@ (target output) (alias melange) (entries c) - (libraries lib) - (module_system commonjs)) + (libraries lib)) diff --git a/test/blackbox-tests/test-cases/melange/intfonly-entries.t b/test/blackbox-tests/test-cases/melange/intfonly-entries.t index ecff83d58b8..55365136623 100644 --- a/test/blackbox-tests/test-cases/melange/intfonly-entries.t +++ b/test/blackbox-tests/test-cases/melange/intfonly-entries.t @@ -8,7 +8,6 @@ Entry points should not allow mli only modules as entry points. $ cat >dune < (melange.emit > (target output) - > (module_system commonjs) > (modules_without_implementation foo) > (alias melange)) > EOF diff --git a/test/blackbox-tests/test-cases/melange/intfonly.t/dune b/test/blackbox-tests/test-cases/melange/intfonly.t/dune index d50f462f744..9b813e62889 100644 --- a/test/blackbox-tests/test-cases/melange/intfonly.t/dune +++ b/test/blackbox-tests/test-cases/melange/intfonly.t/dune @@ -1,5 +1,4 @@ (melange.emit (target output) (alias melange) - (libraries lib) - (module_system commonjs)) + (libraries lib)) diff --git a/test/blackbox-tests/test-cases/melange/javascript-extension.t b/test/blackbox-tests/test-cases/melange/javascript-extension.t deleted file mode 100644 index 815d6c563a7..00000000000 --- a/test/blackbox-tests/test-cases/melange/javascript-extension.t +++ /dev/null @@ -1,42 +0,0 @@ -Test (javascript_extension) field on melange.emit stanzas - - $ cat > dune-project < (lang dune 3.7) - > (using melange 0.1) - > EOF - - $ cat > hello.ml < let () = - > print_endline "hello" - > EOF - -Can use extension with dots - - $ cat > dune < (melange.emit - > (target output) - > (alias melange) - > (module_system commonjs) - > (javascript_extension bs.js)) - > EOF - - $ dune build @melange - $ node _build/default/output/hello.bs.js - hello - -Errors out if extension starts with dot - - $ cat > dune < (melange.emit - > (target output) - > (alias melange) - > (module_system commonjs) - > (javascript_extension .bs.js)) - > EOF - - $ dune build @melange - File "dune", line 5, characters 23-29: - 5 | (javascript_extension .bs.js)) - ^^^^^^ - Error: extension must not start with '.' - [1] diff --git a/test/blackbox-tests/test-cases/melange/merlin.t b/test/blackbox-tests/test-cases/melange/merlin.t index f8ac030c475..ca3439e5162 100644 --- a/test/blackbox-tests/test-cases/melange/merlin.t +++ b/test/blackbox-tests/test-cases/melange/merlin.t @@ -42,8 +42,7 @@ All 3 entries (Foo, Foo__ and Bar) contain a ppx directive $ cat >dune < (melange.emit > (target "$target") - > (entries main) - > (module_system commonjs)) + > (entries main)) > EOF $ touch main.ml @@ -67,6 +66,7 @@ Dump-dot-merlin includes the melange flags # FLG -w @1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence -strict-formats -short-paths -keep-locs + Check for flag directives ordering when another preprocessor is defined $ cat >fooppx.ml < (melange.emit > (target output) > (entries main_melange) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF $ (unset INSIDE_DUNE; PATH=_path dune build --always-show-command-line --root . @melange) - File "dune", line 1, characters 0-98: + File "dune", line 1, characters 0-72: 1 | (melange.emit 2 | (target output) 3 | (entries main_melange) - 4 | (alias melange) - 5 | (module_system commonjs)) + 4 | (alias melange)) Error: Program melc not found in the tree or in PATH (context: default) Hint: opam install melange @@ -81,8 +79,7 @@ If melange.emit stanza is found, but no rules are executed, build does not fail > (melange.emit > (target output) > (entries main_melange) - > (libraries lib1) - > (module_system commonjs)) + > (libraries lib1)) > EOF $ (unset INSIDE_DUNE; PATH=_path dune build --always-show-command-line --root . main_native.bc) diff --git a/test/blackbox-tests/test-cases/melange/mli.t/dune b/test/blackbox-tests/test-cases/melange/mli.t/dune index 6d629fafb3a..608af6a4f46 100644 --- a/test/blackbox-tests/test-cases/melange/mli.t/dune +++ b/test/blackbox-tests/test-cases/melange/mli.t/dune @@ -2,5 +2,4 @@ (target mli) (entries x) (libraries lib) - (alias melange) - (module_system commonjs)) + (alias melange)) diff --git a/test/blackbox-tests/test-cases/melange/module-systems.t b/test/blackbox-tests/test-cases/melange/module-systems.t new file mode 100644 index 00000000000..332dc665764 --- /dev/null +++ b/test/blackbox-tests/test-cases/melange/module-systems.t @@ -0,0 +1,140 @@ +Parses the full form ( ) + + $ cat > dune-project < (lang dune 3.7) + > (using melange 0.1) + > EOF + + $ cat > dune < (melange.emit + > (alias mel) + > (target output) + > (module_systems + > (commonjs bs.js) + > (es6 mjs))) + > EOF + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ dune build @mel --display=short + melc .output.mobjs/melange/melange__Main.{cmi,cmj,cmt} + melc output/main.bs.js + melc output/main.mjs + +Outputs both module systems + + $ ls _build/default/output + main.bs.js + main.mjs + $ node _build/default/output/main.bs.js + hello + $ node _build/default/output/main.mjs + hello + + +Parses the simplified form and defaults extension to `.js` + + $ dune clean + $ cat > dune < (melange.emit + > (alias mel) + > (target output) + > (module_systems + > commonjs + > (es6 mjs))) + > EOF + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ dune build @mel --display=short + melc .output.mobjs/melange/melange__Main.{cmi,cmj,cmt} + melc output/main.js + melc output/main.mjs + + $ ls _build/default/output + main.js + main.mjs + +Defaults to commonjs / `.js` if no config present at all + + $ dune clean + $ cat > dune < (melange.emit + > (alias mel) + > (target output)) + > EOF + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ dune build @mel --display=short + melc .output.mobjs/melange/melange__Main.{cmi,cmj,cmt} + melc output/main.js + + $ ls _build/default/output + main.js + +Errors out if extension starts with dot + + $ cat > dune < (melange.emit + > (target output) + > (alias melange) + > (module_systems (commonjs .bs.js))) + > EOF + + $ dune build @mel --display=short + File "dune", line 4, characters 27-33: + 4 | (module_systems (commonjs .bs.js))) + ^^^^^^ + Error: extension must not start with '.' + [1] + +Errors if the same extension is present multiple times + + $ dune clean + $ cat > dune < (melange.emit + > (alias mel) + > (target output) + > (module_systems commonjs es6)) + > EOF + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ dune build @mel --display=short + File "dune", line 4, characters 26-29: + 4 | (module_systems commonjs es6)) + ^^^ + Error: JavaScript extension .js appears more than once: + - dune:4 + - dune:4 + Extensions must be unique per melange.emit stanza + Hint: specify different extensions with (module_systems ( + ) ( )) + [1] + + $ cat > dune < (melange.emit + > (alias mel) + > (target output) + > (module_systems (commonjs bs.js) commonjs (es6 bs.js))) + > EOF + $ cat > main.ml < let () = Js.log "hello" + > EOF + + $ dune build @mel --display=short + File "dune", line 4, characters 48-53: + 4 | (module_systems (commonjs bs.js) commonjs (es6 bs.js))) + ^^^^^ + Error: JavaScript extension .bs.js appears more than once: + - dune:4 + - dune:4 + Extensions must be unique per melange.emit stanza + Hint: specify different extensions with (module_systems ( + ) ( )) + [1] diff --git a/test/blackbox-tests/test-cases/melange/multilib.t/dune b/test/blackbox-tests/test-cases/melange/multilib.t/dune index 5f88711783a..74e22505522 100644 --- a/test/blackbox-tests/test-cases/melange/multilib.t/dune +++ b/test/blackbox-tests/test-cases/melange/multilib.t/dune @@ -2,5 +2,4 @@ (target multilib) (entries c) (libraries x) - (alias melange) - (module_system commonjs)) + (alias melange)) diff --git a/test/blackbox-tests/test-cases/melange/nested-melange-emit-stanzas.t b/test/blackbox-tests/test-cases/melange/nested-melange-emit-stanzas.t index f56f7adbaf9..d5f4e59f25f 100644 --- a/test/blackbox-tests/test-cases/melange/nested-melange-emit-stanzas.t +++ b/test/blackbox-tests/test-cases/melange/nested-melange-emit-stanzas.t @@ -9,13 +9,13 @@ Make sure an error is returned if trying to nest `melange.emit` stanzas > (melange.emit > (target output) > (alias mel) - > (module_system commonjs)) + > (module_systems commonjs)) > EOF $ cat > a/b/c/dune < (melange.emit > (target output) > (alias mel) - > (module_system commonjs)) + > (module_systems commonjs)) > EOF $ dune build @mel @@ -23,7 +23,7 @@ Make sure an error is returned if trying to nest `melange.emit` stanzas 1 | (melange.emit 2 | (target output) 3 | (alias mel) - 4 | (module_system commonjs)) + 4 | (module_systems commonjs)) Error: melange.emit stanzas cannot be nested - a/dune:1 - a/b/c/dune:1 diff --git a/test/blackbox-tests/test-cases/melange/ocaml-flags.t b/test/blackbox-tests/test-cases/melange/ocaml-flags.t index 28b968a0a63..d30420ad8bd 100644 --- a/test/blackbox-tests/test-cases/melange/ocaml-flags.t +++ b/test/blackbox-tests/test-cases/melange/ocaml-flags.t @@ -12,7 +12,6 @@ Create dune file that uses melange.compile_flags > (target output) > (alias melange) > (entries main) - > (module_system commonjs) > (compile_flags -w -14-26)) > EOF @@ -35,15 +34,14 @@ Update dune file to use ocamlc_flags > (melange.emit > (target output) > (entries main) - > (module_system commonjs) > (ocamlc_flags -w -14-26)) > EOF Building should fail as ocamlc flags are not supported in melange emit stanzas $ dune build output/main.js - File "dune", line 5, characters 2-14: - 5 | (ocamlc_flags -w -14-26)) + File "dune", line 4, characters 2-14: + 4 | (ocamlc_flags -w -14-26)) ^^^^^^^^^^^^ Error: Unknown field ocamlc_flags [1] @@ -54,15 +52,14 @@ Update dune file to use ocamlopt_flags > (melange.emit > (target output) > (entries main) - > (module_system commonjs) > (ocamlopt_flags -w -14-26)) > EOF Building should fail as ocamlopt flags are not supported in melange emit stanzas $ dune build output/main.js - File "dune", line 5, characters 2-16: - 5 | (ocamlopt_flags -w -14-26)) + File "dune", line 4, characters 2-16: + 4 | (ocamlopt_flags -w -14-26)) ^^^^^^^^^^^^^^ Error: Unknown field ocamlopt_flags [1] diff --git a/test/blackbox-tests/test-cases/melange/preprocess.t b/test/blackbox-tests/test-cases/melange/preprocess.t index 926516a15a8..b9af4c4612f 100644 --- a/test/blackbox-tests/test-cases/melange/preprocess.t +++ b/test/blackbox-tests/test-cases/melange/preprocess.t @@ -10,7 +10,6 @@ Test (preprocess) field on melange.emit stanza > (target output) > (entries main) > (alias melange) - > (module_system commonjs) > (preprocess > (action > (run cat %{input-file})))) diff --git a/test/blackbox-tests/test-cases/melange/private-module.t/dune b/test/blackbox-tests/test-cases/melange/private-module.t/dune index f1a30f92518..594fb808e8a 100644 --- a/test/blackbox-tests/test-cases/melange/private-module.t/dune +++ b/test/blackbox-tests/test-cases/melange/private-module.t/dune @@ -1,5 +1,4 @@ (melange.emit (target private-module) (entries foo) - (libraries lib) - (module_system commonjs)) + (libraries lib)) diff --git a/test/blackbox-tests/test-cases/melange/private.t/inside/dune b/test/blackbox-tests/test-cases/melange/private.t/inside/dune index 3ea6bd9824e..be4f9101128 100644 --- a/test/blackbox-tests/test-cases/melange/private.t/inside/dune +++ b/test/blackbox-tests/test-cases/melange/private.t/inside/dune @@ -3,5 +3,5 @@ (entries c) (libraries app) (alias melange) - (module_system es6) - (javascript_extension mjs)) + (module_systems + (es6 mjs))) diff --git a/test/blackbox-tests/test-cases/melange/promote-with-lib.t b/test/blackbox-tests/test-cases/melange/promote-with-lib.t index a5d3afb2edd..2bd62eebd53 100644 --- a/test/blackbox-tests/test-cases/melange/promote-with-lib.t +++ b/test/blackbox-tests/test-cases/melange/promote-with-lib.t @@ -22,8 +22,7 @@ Test melange.emit promotion > (entries hello) > (promote (until-clean)) > (target dist) - > (libraries mylib) - > (module_system commonjs)) + > (libraries mylib)) > EOF $ cat > hello.ml < (alias dist) > (entries hello) > (promote (until-clean)) - > (target dist) - > (module_system commonjs)) + > (target dist)) > EOF $ cat > hello.ml < (melange.emit > (alias dist) > (entries hello) - > (target dist) - > (module_system commonjs)) + > (target dist)) > EOF $ cat > hello.res < (name lib) > (modes melange)) > (melange.emit - > (target output) - > (module_system commonjs)) + > (target output)) > EOF $ cat > main.ml < (alias melange) > (target output) > (libraries lib1) - > (root_module root) - > (module_system commonjs)) + > (root_module root)) > EOF $ cat > lib1.ml < let greeting = () diff --git a/test/blackbox-tests/test-cases/melange/simple.t/lib/dune b/test/blackbox-tests/test-cases/melange/simple.t/lib/dune index 081c4f27389..e2468a06891 100644 --- a/test/blackbox-tests/test-cases/melange/simple.t/lib/dune +++ b/test/blackbox-tests/test-cases/melange/simple.t/lib/dune @@ -7,5 +7,4 @@ (target simple) (alias melange) (entries x) - (libraries lib) - (module_system commonjs)) + (libraries lib)) diff --git a/test/blackbox-tests/test-cases/melange/unmangling.t b/test/blackbox-tests/test-cases/melange/unmangling.t index 6e49f85664f..2615733c239 100644 --- a/test/blackbox-tests/test-cases/melange/unmangling.t +++ b/test/blackbox-tests/test-cases/melange/unmangling.t @@ -8,8 +8,7 @@ Test unmangling of js files $ cat > dune < (melange.emit > (target output) - > (alias melange) - > (module_system commonjs)) + > (alias melange)) > EOF Using uppercase produces uppercase artifacts diff --git a/test/blackbox-tests/test-cases/melange/virtual_lib.t/dune b/test/blackbox-tests/test-cases/melange/virtual_lib.t/dune index ebb9dbf943a..b28c910c6db 100644 --- a/test/blackbox-tests/test-cases/melange/virtual_lib.t/dune +++ b/test/blackbox-tests/test-cases/melange/virtual_lib.t/dune @@ -2,5 +2,4 @@ (target output) (alias melange) (entries mel) - (libraries impl_melange) - (module_system commonjs)) + (libraries impl_melange)) diff --git a/test/blackbox-tests/test-cases/melange/virtual_lib_compilation.t/dune b/test/blackbox-tests/test-cases/melange/virtual_lib_compilation.t/dune index e18f48bc534..c95ed3105f1 100644 --- a/test/blackbox-tests/test-cases/melange/virtual_lib_compilation.t/dune +++ b/test/blackbox-tests/test-cases/melange/virtual_lib_compilation.t/dune @@ -7,5 +7,4 @@ (target output) (alias melange) (entries mel) - (libraries vlib impl_melange) - (module_system commonjs)) + (libraries vlib impl_melange))