Skip to content

Deprecated es6 and es6-global in favor of esmodule #6709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/docson/build-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions jscomp/bsb/bsb_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions jscomp/bsb/bsb_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 18 additions & 7 deletions jscomp/bsb/bsb_package_specs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion jscomp/bsb/bsb_spec_set.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion jscomp/build_tests/cycle1/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"subdirs": true
},
"package-specs": {
"module": "es6",
"module": "esmodule",
"in-source": true
},
"suffix": ".bs.js"
Expand Down
9 changes: 9 additions & 0 deletions jscomp/build_tests/deprecated-package-specs/input.js
Original file line number Diff line number Diff line change
@@ -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\./
);
8 changes: 8 additions & 0 deletions jscomp/build_tests/deprecated-package-specs/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "deprecated-package-specs",
"version": "0.1.0",
"sources": "src",
"package-specs": {
"module": "es6-global"
}
}
1 change: 1 addition & 0 deletions jscomp/build_tests/deprecated-package-specs/src/Index.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = Js.log("Hello, ReScript")
2 changes: 1 addition & 1 deletion jscomp/build_tests/in_source/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"in-source": true
},
{
"module": "es6",
"module": "esmodule",
"in-source": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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))
6 changes: 3 additions & 3 deletions jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/js_name_of_module_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -200,4 +200,4 @@ let string_of_module_id
| None ->
Bs_exception.error (Js_not_found js_file))

#endif
#endif
22 changes: 11 additions & 11 deletions jscomp/core/js_packages_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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"
Expand All @@ -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 };
];
}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_packages_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_compile_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jscomp/ext/ext_module_system.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type t = NodeJS | Es6 | Es6_global
type t = Commonjs | Esmodule | Es6_global
4 changes: 4 additions & 0 deletions jscomp/ext/literals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "

Expand Down
2 changes: 1 addition & 1 deletion jscomp/gentype/EmitType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ^ "');"
Expand Down
10 changes: 5 additions & 5 deletions jscomp/gentype/GenTypeConfig.ml
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -41,7 +41,7 @@ let default =
everything = false;
exportInterfaces = false;
generatedFileExtension = None;
module_ = ES6;
module_ = ESModule;
moduleResolution = Node;
namespace = None;
platformLib = "";
Expand All @@ -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"
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions jscomp/gentype_tests/typescript-react-example/rescript.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"gentypeconfig": {
"language": "typescript",
"module": "es6",
"module": "esmodule",
"importPath": "relative",
"shims": {
"Js": "Js",
Expand All @@ -26,7 +26,7 @@
],
"uncurried": false,
"package-specs": {
"module": "es6",
"module": "esmodule",
"in-source": true
},
"suffix": ".res.js"
Expand Down
Loading
Loading