diff --git a/CHANGELOG.md b/CHANGELOG.md index 8949a6c567..73a2faf061 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ - Fix trailing undefined for optional parameters not omitted with `@send` and `@new`. https://github.com/rescript-lang/rescript-compiler/pull/6716 - Fix JSX4 adding the incorrect type annotation for the prop `ref` in React.forwardRef component https://github.com/rescript-lang/rescript-compiler/pull/6718 +#### :nail_care: Polish + +- Module spec `es6` and `es6-global` is deprecated in favor of `esmodule`. https://github.com/rescript-lang/rescript-compiler/pull/6709 + # 11.1.0-rc.7 #### :bug: Bug Fix diff --git a/docs/docson/build-schema.json b/docs/docson/build-schema.json index 32702d9cd5..c0139a7e9f 100644 --- a/docs/docson/build-schema.json +++ b/docs/docson/build-schema.json @@ -2,8 +2,8 @@ "$schema": "http://json-schema.org/draft-04/schema#", "definitions": { "module-format": { - "enum": ["commonjs", "es6", "es6-global"], - "description": "es6-global generate relative `require` paths instead of relying on NodeJS' module resolution. Default: commonjs." + "enum": ["esmodule", "commonjs", "es6", "es6-global"], + "description": "es6 and es6-global are deprecated. Default: commonjs." }, "suffix-spec": { "type": "string", diff --git a/jscomp/bsb/bsb_config.ml b/jscomp/bsb/bsb_config.ml index a115a9510a..3c3d7d510e 100644 --- a/jscomp/bsb/bsb_config.ml +++ b/jscomp/bsb/bsb_config.ml @@ -41,16 +41,18 @@ let rev_lib_bs = ".." // ".." (* access the js directory from "lib/bs", it would be '../js' + + TODO: should be renamed, js -> cjs, es6 -> mjs in v12 *) let lib_bs_prefix_of_format (x : Ext_module_system.t) = ".." - // match x with NodeJS -> "js" | Es6 -> "es6" | Es6_global -> "es6_global" + // match x with Commonjs -> "js" | Esmodule -> "es6" | Es6_global -> "es6_global" (* lib/js, lib/es6, lib/es6_global *) let top_prefix_of_format (x : Ext_module_system.t) = match x with - | NodeJS -> lib_js - | Es6 -> lib_es6 + | Commonjs -> lib_js + | Esmodule -> lib_es6 | Es6_global -> lib_es6_global let rev_lib_bs_prefix p = rev_lib_bs // p diff --git a/jscomp/bsb/bsb_config.mli b/jscomp/bsb/bsb_config.mli index ab3efadd80..c5df914df1 100644 --- a/jscomp/bsb/bsb_config.mli +++ b/jscomp/bsb/bsb_config.mli @@ -33,8 +33,10 @@ val lib_js : string val lib_bs : string val lib_es6 : string +[@@ocaml.deprecated "will be removed in v12"] val lib_es6_global : string +[@@ocaml.deprecated "will be removed in v12"] val lib_ocaml : string diff --git a/jscomp/bsb/bsb_package_specs.ml b/jscomp/bsb/bsb_package_specs.ml index 69b43accf8..bcb88c90ac 100644 --- a/jscomp/bsb/bsb_package_specs.ml +++ b/jscomp/bsb/bsb_package_specs.ml @@ -40,19 +40,29 @@ let ( .?() ) = Map_string.find_opt let bad_module_format_message_exn ~loc format = Bsb_exception.errorf ~loc "package-specs: `%s` isn't a valid output module format. It has to be one \ - of: %s, %s or %s" - format Literals.commonjs Literals.es6 Literals.es6_global + of: %s or %s" + format Literals.esmodule Literals.commonjs let supported_format (x : string) loc : Ext_module_system.t = - if x = Literals.commonjs then NodeJS - else if x = Literals.es6 then Es6 + let _ = + if x = Literals.es6 || x = Literals.es6_global then + let loc_end = + {loc with Lexing.pos_cnum = loc.Lexing.pos_cnum + String.length x} + in + let loc = {Warnings.loc_start = loc; loc_end; loc_ghost = false} in + Location.deprecated loc + (Printf.sprintf "Option \"%s\" is deprecated. Use \"%s\" instead." x + Literals.esmodule) + in + if x = Literals.es6 || x = Literals.esmodule then Esmodule + else if x = Literals.commonjs then Commonjs else if x = Literals.es6_global then Es6_global else bad_module_format_message_exn ~loc x let string_of_format (x : Ext_module_system.t) = match x with - | NodeJS -> Literals.commonjs - | Es6 -> Literals.es6 + | Commonjs -> Literals.commonjs + | Esmodule -> Literals.esmodule | Es6_global -> Literals.es6_global let js_suffix_regexp = Str.regexp "[A-Za-z0-9-_.]*\\.[cm]?js" @@ -158,7 +168,8 @@ let package_flag_of_package_specs (package_specs : t) ~(dirname : string) : | Some x -> Ext_string.inter3 res "-runtime" x let default_package_specs suffix = - Spec_set.singleton { format = NodeJS; in_source = false; suffix } + (* TODO: swap default to Esmodule in v12 *) + Spec_set.singleton { format = Commonjs; in_source = false; suffix } (** [get_list_of_output_js specs "src/hi/hello"] diff --git a/jscomp/bsb/bsb_spec_set.ml b/jscomp/bsb/bsb_spec_set.ml index 6934bc1913..a53862fbc8 100644 --- a/jscomp/bsb/bsb_spec_set.ml +++ b/jscomp/bsb/bsb_spec_set.ml @@ -25,7 +25,7 @@ [@@@warning "+9"] (* TODO: sync up with {!Js_packages_info.module_system} *) -type format = Ext_module_system.t = NodeJS | Es6 | Es6_global +type format = Ext_module_system.t type spec = { format : format; in_source : bool; suffix : string } diff --git a/jscomp/build_tests/cycle1/rescript.json b/jscomp/build_tests/cycle1/rescript.json index 0c0d776b4b..fdbf975812 100644 --- a/jscomp/build_tests/cycle1/rescript.json +++ b/jscomp/build_tests/cycle1/rescript.json @@ -7,7 +7,7 @@ "subdirs": true }, "package-specs": { - "module": "es6", + "module": "esmodule", "in-source": true }, "suffix": ".bs.js" diff --git a/jscomp/build_tests/deprecated-package-specs/input.js b/jscomp/build_tests/deprecated-package-specs/input.js new file mode 100644 index 0000000000..ed66ae02ec --- /dev/null +++ b/jscomp/build_tests/deprecated-package-specs/input.js @@ -0,0 +1,9 @@ +const child_process = require("child_process"); +const assert = require("assert"); +const rescript_exe = require("../../../scripts/bin_path").rescript_exe; + +const out = child_process.spawnSync(rescript_exe, { encoding: "utf8" }); +assert.match( + out.stderr, + /deprecated: Option "es6-global" is deprecated\. Use "esmodule" instead\./ +); diff --git a/jscomp/build_tests/deprecated-package-specs/rescript.json b/jscomp/build_tests/deprecated-package-specs/rescript.json new file mode 100644 index 0000000000..ada61dab5b --- /dev/null +++ b/jscomp/build_tests/deprecated-package-specs/rescript.json @@ -0,0 +1,8 @@ +{ + "name": "deprecated-package-specs", + "version": "0.1.0", + "sources": "src", + "package-specs": { + "module": "es6-global" + } +} diff --git a/jscomp/build_tests/deprecated-package-specs/src/Index.res b/jscomp/build_tests/deprecated-package-specs/src/Index.res new file mode 100644 index 0000000000..8d0b19151f --- /dev/null +++ b/jscomp/build_tests/deprecated-package-specs/src/Index.res @@ -0,0 +1 @@ +let () = Js.log("Hello, ReScript") diff --git a/jscomp/build_tests/in_source/rescript.json b/jscomp/build_tests/in_source/rescript.json index eb9f5b9ac5..fc69fdc991 100644 --- a/jscomp/build_tests/in_source/rescript.json +++ b/jscomp/build_tests/in_source/rescript.json @@ -7,7 +7,7 @@ "in-source": true }, { - "module": "es6", + "module": "esmodule", "in-source": true } ] diff --git a/jscomp/core/dune b/jscomp/core/dune index 0d4e56c441..c62f4cfee8 100644 --- a/jscomp/core/dune +++ b/jscomp/core/dune @@ -6,4 +6,4 @@ (run %{bin:cppo} %{env:CPPO_FLAGS=} %{input-file}))) (flags (:standard -w +a-4-9-27-30-40-41-42-48-70)) - (libraries depends frontend gentype js_parser)) + (libraries depends ext frontend gentype js_parser)) diff --git a/jscomp/core/js_dump_program.ml b/jscomp/core/js_dump_program.ml index 3342988047..ce80e210c5 100644 --- a/jscomp/core/js_dump_program.ml +++ b/jscomp/core/js_dump_program.ml @@ -79,7 +79,7 @@ let node_program ~output_dir f (x : J.deps_program) = | true -> None | false -> Some ( x.id, - Js_name_of_module_id.string_of_module_id x ~output_dir NodeJS, + Js_name_of_module_id.string_of_module_id x ~output_dir Commonjs, is_default x.kind ))) in program f cxt x.program @@ -129,8 +129,8 @@ let pp_deps_program ~(output_prefix : string) let output_dir = Filename.dirname output_prefix in ignore (match kind with - | Es6 | Es6_global -> es6_program ~output_dir kind f program - | NodeJS -> node_program ~output_dir f program); + | Esmodule | Es6_global -> es6_program ~output_dir kind f program + | Commonjs -> node_program ~output_dir f program); P.newline f; P.string f (match program.side_effect with diff --git a/jscomp/core/js_name_of_module_id.ml b/jscomp/core/js_name_of_module_id.ml index d1cee4333a..4cb6433325 100644 --- a/jscomp/core/js_name_of_module_id.ml +++ b/jscomp/core/js_name_of_module_id.ml @@ -77,7 +77,7 @@ let get_runtime_module_path *) else match module_system with - | NodeJS | Es6 -> + | Commonjs | Esmodule -> Js_packages_info.runtime_package_path module_system js_file (* Note we did a post-processing when working on Windows *) | Es6_global @@ -164,7 +164,7 @@ let string_of_module_id get_runtime_module_path dep_module_id current_package_info module_system else begin match module_system with - | NodeJS | Es6 -> + | Commonjs | Esmodule -> dep_pkg.pkg_rel_path // js_file (* Note we did a post-processing when working on Windows *) | Es6_global @@ -200,4 +200,4 @@ let string_of_module_id | None -> Bs_exception.error (Js_not_found js_file)) -#endif \ No newline at end of file +#endif diff --git a/jscomp/core/js_packages_info.ml b/jscomp/core/js_packages_info.ml index 1f189a9e5c..64a01e8249 100644 --- a/jscomp/core/js_packages_info.ml +++ b/jscomp/core/js_packages_info.ml @@ -24,15 +24,15 @@ [@@@warning "+9"] -type module_system = NodeJS | Es6 | Es6_global +type module_system = Ext_module_system.t (* ignore node_modules, just calcluating relative path *) (* ocamlopt could not optimize such simple case..*) let compatible (dep : module_system) (query : module_system) = match query with - | NodeJS -> dep = NodeJS - | Es6 -> dep = Es6 - | Es6_global -> dep = Es6_global || dep = Es6 + | Commonjs -> dep = Commonjs + | Esmodule -> dep = Esmodule + | Es6_global -> dep = Es6_global || dep = Esmodule (* As a dependency Leaf Node, it is the same either [global] or [not] *) type package_info = { @@ -47,7 +47,7 @@ let ( // ) = Filename.concat (* in runtime lib, [es6] and [es6] are treated the same wway *) let runtime_dir_of_module_system (ms : module_system) = - match ms with NodeJS -> "js" | Es6 | Es6_global -> "es6" + match ms with Commonjs -> "js" | Esmodule | Es6_global -> "es6" let runtime_package_path (ms : module_system) js_file = !Bs_version.package_name // "lib" @@ -61,8 +61,8 @@ let runtime_package_specs : t = name = Pkg_runtime; module_systems = [ - { module_system = Es6; path = "lib/es6"; suffix = Literals.suffix_js }; - { module_system = NodeJS; path = "lib/js"; suffix = Literals.suffix_js }; + { module_system = Esmodule; path = "lib/es6"; suffix = Literals.suffix_js }; + { module_system = Commonjs; path = "lib/js"; suffix = Literals.suffix_js }; ]; } @@ -107,12 +107,12 @@ let from_name (name : string) : t = let is_empty (x : t) = x.name = Pkg_empty let string_of_module_system (ms : module_system) = - match ms with NodeJS -> "NodeJS" | Es6 -> "Es6" | Es6_global -> "Es6_global" + match ms with Commonjs -> "CommonJS" | Esmodule -> "ESModule" | Es6_global -> "Es6_global" let module_system_of_string package_name : module_system option = match package_name with - | "commonjs" -> Some NodeJS - | "es6" -> Some Es6 + | "commonjs" -> Some Commonjs + | "esmodule" | "es6" -> Some Esmodule | "es6-global" -> Some Es6_global | _ -> None @@ -201,7 +201,7 @@ let add_npm_package_path (packages_info : t) (s : string) : t = in let m = match Ext_string.split ~keep_empty:true s ':' with - | [ path ] -> { module_system = NodeJS; path; suffix = Literals.suffix_js } + | [ path ] -> { module_system = Esmodule; path; suffix = Literals.suffix_js } | [ module_system; path ] -> { module_system = handle_module_system module_system; diff --git a/jscomp/core/js_packages_info.mli b/jscomp/core/js_packages_info.mli index bf099f5d6c..93beccd1ee 100644 --- a/jscomp/core/js_packages_info.mli +++ b/jscomp/core/js_packages_info.mli @@ -22,7 +22,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -type module_system = NodeJS | Es6 | Es6_global +type module_system = Ext_module_system.t val runtime_dir_of_module_system : module_system -> string diff --git a/jscomp/core/lam_compile_main.ml b/jscomp/core/lam_compile_main.ml index e70c6dc260..2cc59ca73b 100644 --- a/jscomp/core/lam_compile_main.ml +++ b/jscomp/core/lam_compile_main.ml @@ -292,7 +292,7 @@ let lambda_as_module : unit = let package_info = Js_packages_state.get_packages_info () in if Js_packages_info.is_empty package_info && !Js_config.js_stdout then begin - Js_dump_program.dump_deps_program ~output_prefix NodeJS (lambda_output) stdout + Js_dump_program.dump_deps_program ~output_prefix Commonjs (lambda_output) stdout end else Js_packages_info.iter package_info (fun {module_system; path; suffix} -> let output_chan chan = diff --git a/jscomp/core/lam_compile_primitive.ml b/jscomp/core/lam_compile_primitive.ml index 33e7db0e6d..f78c5deaeb 100644 --- a/jscomp/core/lam_compile_primitive.ml +++ b/jscomp/core/lam_compile_primitive.ml @@ -44,12 +44,12 @@ let get_module_system () = let package_info = Js_packages_state.get_packages_info () in let module_system = if Js_packages_info.is_empty package_info && !Js_config.js_stdout then - [Js_packages_info.NodeJS] + [Ext_module_system.Commonjs] else Js_packages_info.map package_info (fun {module_system} -> module_system) in match module_system with | [module_system] -> module_system - | _ -> NodeJS + | _ -> Commonjs let import_of_path path = E.call diff --git a/jscomp/ext/ext_module_system.ml b/jscomp/ext/ext_module_system.ml index b3178b0c9d..9b06848f3f 100644 --- a/jscomp/ext/ext_module_system.ml +++ b/jscomp/ext/ext_module_system.ml @@ -1 +1 @@ -type t = NodeJS | Es6 | Es6_global +type t = Commonjs | Esmodule | Es6_global diff --git a/jscomp/ext/literals.ml b/jscomp/ext/literals.ml index b8345e3c5f..b1aa1c2bc5 100644 --- a/jscomp/ext/literals.ml +++ b/jscomp/ext/literals.ml @@ -125,11 +125,15 @@ let suffix_gen_js = ".gen.js" let suffix_gen_tsx = ".gen.tsx" +let esmodule = "esmodule" + let commonjs = "commonjs" let es6 = "es6" +[@@ocaml.deprecated "Will be removed in v12"] let es6_global = "es6-global" +[@@ocaml.deprecated "Will be removed in v12"] let unused_attribute = "Unused attribute " diff --git a/jscomp/gentype/EmitType.ml b/jscomp/gentype/EmitType.ml index faf600f530..9a55a77520 100644 --- a/jscomp/gentype/EmitType.ml +++ b/jscomp/gentype/EmitType.ml @@ -382,7 +382,7 @@ let emitRequire ~importedValueOrComponent ~early ~emitters ~(config : Config.t) let importPathString = ImportPath.emit importPath in let output = match config.module_ with - | ES6 when not importedValueOrComponent -> + | ESModule when not importedValueOrComponent -> "import * as " ^ moduleNameString ^ " from '" ^ importPathString ^ "';" | _ -> "const " ^ moduleNameString ^ " = require('" ^ importPathString ^ "');" diff --git a/jscomp/gentype/GenTypeConfig.ml b/jscomp/gentype/GenTypeConfig.ml index 299e52f945..9e5ec193c8 100644 --- a/jscomp/gentype/GenTypeConfig.ml +++ b/jscomp/gentype/GenTypeConfig.ml @@ -1,6 +1,6 @@ module ModuleNameMap = Map.Make (ModuleName) -type module_ = CommonJS | ES6 +type module_ = CommonJS | ESModule (** Compatibility for `compilerOptions.moduleResolution` in TypeScript projects. *) type moduleResolution = @@ -41,7 +41,7 @@ let default = everything = false; exportInterfaces = false; generatedFileExtension = None; - module_ = ES6; + module_ = ESModule; moduleResolution = Node; namespace = None; platformLib = ""; @@ -53,7 +53,7 @@ let default = let bsPlatformLib ~config = match config.module_ with - | ES6 -> config.platformLib ^ "/lib/es6" + | ESModule -> config.platformLib ^ "/lib/es6" | CommonJS -> config.platformLib ^ "/lib/js" let getBsCurryPath ~config = Filename.concat (bsPlatformLib ~config) "curry.js" @@ -154,9 +154,9 @@ let readConfig ~getConfigFile ~namespace = (* Give priority to gentypeconfig, followed by package-specs *) match (moduleString, packageSpecsModuleString) with | Some "commonjs", _ -> CommonJS - | Some "es6", _ -> ES6 + | Some ("esmodule" | "es6"), _ -> ESModule | None, Some "commonjs" -> CommonJS - | None, Some ("es6" | "es6-global") -> ES6 + | None, Some ("esmodule" | "es6" | "es6-global") -> ESModule | _ -> default.module_ in let moduleResolution = diff --git a/jscomp/gentype_tests/typescript-react-example/rescript.json b/jscomp/gentype_tests/typescript-react-example/rescript.json index 1e12553c13..1fbab8ac5a 100644 --- a/jscomp/gentype_tests/typescript-react-example/rescript.json +++ b/jscomp/gentype_tests/typescript-react-example/rescript.json @@ -1,7 +1,7 @@ { "gentypeconfig": { "language": "typescript", - "module": "es6", + "module": "esmodule", "importPath": "relative", "shims": { "Js": "Js", @@ -26,7 +26,7 @@ ], "uncurried": false, "package-specs": { - "module": "es6", + "module": "esmodule", "in-source": true }, "suffix": ".res.js" diff --git a/jscomp/jsoo/jsoo_playground_main.ml b/jscomp/jsoo/jsoo_playground_main.ml index f15e6e70ca..0bc73daf12 100644 --- a/jscomp/jsoo/jsoo_playground_main.ml +++ b/jscomp/jsoo/jsoo_playground_main.ml @@ -74,7 +74,7 @@ end module BundleConfig = struct type t = { - mutable module_system: Js_packages_info.module_system; + mutable module_system: Ext_module_system.t; mutable filename: string option; mutable warn_flags: string; mutable open_modules: string list; @@ -86,7 +86,7 @@ module BundleConfig = struct } let make () = { - module_system=Js_packages_info.NodeJS; + module_system=Ext_module_system.Commonjs; filename=None; warn_flags=Bsc_warnings.defaults_w; open_modules=[]; @@ -97,8 +97,8 @@ module BundleConfig = struct let default_filename (lang: Lang.t) = "playground." ^ (Lang.toString lang) let string_of_module_system m = (match m with - | Js_packages_info.NodeJS -> "nodejs" - | Es6 -> "es6" + | Ext_module_system.Commonjs -> "nodejs" + | Esmodule -> "es6" | Es6_global -> "es6_global") end @@ -608,10 +608,10 @@ module Export = struct let config = BundleConfig.make () in let set_module_system value = match value with - | "es6" -> - config.module_system <- Js_packages_info.Es6; true - | "nodejs" -> - config.module_system <- NodeJS; true + | "esmodule" | "es6" -> + config.module_system <- Ext_module_system.Esmodule; true + | "commonjs" | "nodejs" -> + config.module_system <- Commonjs; true | _ -> false in let set_filename value = config.filename <- Some value; true diff --git a/packages/playground-bundling/bsconfig.json b/packages/playground-bundling/rescript.json similarity index 90% rename from packages/playground-bundling/bsconfig.json rename to packages/playground-bundling/rescript.json index a9765361c3..bb00f330c9 100644 --- a/packages/playground-bundling/bsconfig.json +++ b/packages/playground-bundling/rescript.json @@ -3,7 +3,7 @@ "version": "0.1.0", "bs-dependencies": ["@rescript/react", "@rescript/core"], "package-specs": { - "module": "es6", + "module": "esmodule", "in-source": false }, "sources": { diff --git a/packages/playground-bundling/scripts/generate_cmijs.js b/packages/playground-bundling/scripts/generate_cmijs.js index a994811e15..2dd3dd1799 100644 --- a/packages/playground-bundling/scripts/generate_cmijs.js +++ b/packages/playground-bundling/scripts/generate_cmijs.js @@ -6,7 +6,7 @@ * rescript version as the compiler bundle. * * This script extracts all cmi / cmj files of the rescript/lib/ocaml and all - * dependencies listed in the project root's bsconfig.json, creates cmij.js + * dependencies listed in the project root's rescript.json, creates cmij.js * files for each library and puts them in the compiler playground directory. * * The cmij files are representing the marshaled dependencies that can be used with the ReScript @@ -17,7 +17,7 @@ const child_process = require("child_process"); const fs = require("fs"); const path = require("path"); -const bsconfig = require("../bsconfig.json"); +const resConfig = require("../rescript.json"); const RESCRIPT_COMPILER_ROOT_DIR = path.join(__dirname, "..", "..", ".."); const PLAYGROUND_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "playground"); @@ -51,7 +51,7 @@ e(`npm link ${RESCRIPT_COMPILER_ROOT_DIR}`); e(`npx rescript clean`); e(`npx rescript`); -const packages = bsconfig["bs-dependencies"]; +const packages = resConfig["bs-dependencies"]; // We need to build the compiler's builtin modules as a separate cmij. // Otherwise we can't use them for compilation within the playground. diff --git a/packages/test/bsconfig.json b/packages/test/rescript.json similarity index 87% rename from packages/test/bsconfig.json rename to packages/test/rescript.json index 3ae42bf353..5923440e94 100644 --- a/packages/test/bsconfig.json +++ b/packages/test/rescript.json @@ -8,7 +8,7 @@ ], "package-specs": [ { - "module": "es6", + "module": "esmodule", "in-source": true } ],