Skip to content

Commit 1c0bbf1

Browse files
authored
Omit dynamic imported module from require, import statement (#6232)
1 parent cc3cbcf commit 1c0bbf1

26 files changed

+121
-65
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
- `-bs-super-errors` flag has been removed along with Super_errors. https://github.com/rescript-lang/rescript-compiler/pull/6199
1818

19+
#### :bug: Bug Fix
20+
21+
- Remove unnecessary require and import statements when using dynamic imports. https://github.com/rescript-lang/rescript-compiler/pull/6232
22+
1923
#### :nail_care: Polish
2024

2125
- In uncurried mode, outcome printer swaps curried and uncurries function printing compared to legacy.

jscomp/core/j.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ and ident = Ident.t
5151
currently we always use quote
5252
*)
5353

54-
and module_id = { id : ident; kind : Js_op.kind }
54+
and module_id = { id : ident; kind : Js_op.kind ; dynamic_import : bool }
5555

5656
and required_modules = module_id list
5757
and vident = Id of ident | Qualified of module_id * string option

jscomp/core/js_dump_program.ml

+12-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ let node_program ~output_dir f (x : J.deps_program) =
7373
P.newline f;
7474
let cxt =
7575
Js_dump_import_export.requires L.require Ext_pp_scope.empty f
76-
(Ext_list.map x.modules (fun x ->
77-
( x.id,
76+
(* Not be emitted in require statements *)
77+
(Ext_list.filter_map x.modules (fun x ->
78+
match x.dynamic_import with
79+
| true -> None
80+
| false ->
81+
Some ( x.id,
7882
Js_name_of_module_id.string_of_module_id x ~output_dir NodeJS,
7983
is_default x.kind )))
8084
in
@@ -83,8 +87,12 @@ let node_program ~output_dir f (x : J.deps_program) =
8387
let es6_program ~output_dir fmt f (x : J.deps_program) =
8488
let cxt =
8589
Js_dump_import_export.imports Ext_pp_scope.empty f
86-
(Ext_list.map x.modules (fun x ->
87-
( x.id,
90+
(* Not be emitted in import statements *)
91+
(Ext_list.filter_map x.modules (fun x ->
92+
match x.dynamic_import with
93+
| true -> None
94+
| false ->
95+
Some ( x.id,
8896
Js_name_of_module_id.string_of_module_id x ~output_dir fmt,
8997
is_default x.kind )))
9098
in

jscomp/core/js_exp_make.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ let runtime_var_dot ?comment (x : string) (e1 : string) : J.expression =
7676
{
7777
expression_desc =
7878
Var
79-
(Qualified ({ id = Ident.create_persistent x; kind = Runtime }, Some e1));
79+
(Qualified ({ id = Ident.create_persistent x; kind = Runtime; dynamic_import = false }, Some e1));
8080
comment;
8181
}
8282

83-
let ml_var_dot ?comment (id : Ident.t) e : J.expression =
84-
{ expression_desc = Var (Qualified ({ id; kind = Ml }, Some e)); comment }
83+
let ml_var_dot ?comment ?(dynamic_import = false) (id : Ident.t) e : J.expression =
84+
{ expression_desc = Var (Qualified ({ id; kind = Ml; dynamic_import }, Some e)); comment }
8585

8686
(**
8787
module as a value
@@ -93,7 +93,7 @@ let external_var_field ?comment ~external_name:name (id : Ident.t) ~field
9393
~default : t =
9494
{
9595
expression_desc =
96-
Var (Qualified ({ id; kind = External { name; default } }, Some field));
96+
Var (Qualified ({ id; kind = External { name; default }; dynamic_import = false }, Some field));
9797
comment;
9898
}
9999

@@ -102,13 +102,13 @@ let external_var ?comment ~external_name (id : Ident.t) : t =
102102
expression_desc =
103103
Var
104104
(Qualified
105-
( { id; kind = External { name = external_name; default = false } },
105+
( { id; kind = External { name = external_name; default = false }; dynamic_import = false },
106106
None ));
107107
comment;
108108
}
109109

110-
let ml_module_as_var ?comment (id : Ident.t) : t =
111-
{ expression_desc = Var (Qualified ({ id; kind = Ml }, None)); comment }
110+
let ml_module_as_var ?comment ?(dynamic_import = false) (id : Ident.t) : t =
111+
{ expression_desc = Var (Qualified ({ id; kind = Ml; dynamic_import }, None)); comment }
112112

113113
(* Static_index .....................**)
114114
let runtime_call module_name fn_name args =

jscomp/core/js_exp_make.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ val runtime_var_dot : ?comment:string -> string -> string -> t
4949

5050
(* val runtime_var_vid : string -> string -> J.vident *)
5151

52-
val ml_var_dot : ?comment:string -> Ident.t -> string -> t
52+
val ml_var_dot : ?comment:string -> ?dynamic_import:bool -> Ident.t -> string -> t
5353
(** [ml_var_dot ocaml_module name]
5454
*)
5555

@@ -66,7 +66,7 @@ val external_var_field :
6666

6767
val external_var : ?comment:string -> external_name:string -> Ident.t -> t
6868

69-
val ml_module_as_var : ?comment:string -> Ident.t -> t
69+
val ml_module_as_var : ?comment:string -> ?dynamic_import:bool -> Ident.t -> t
7070

7171
val runtime_call :
7272
string -> (* module_name *)

jscomp/core/js_record_map.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ let label : label fn = unknown
5656
let ident : ident fn = unknown
5757

5858
let module_id : module_id fn =
59-
fun _self { id = _x0; kind = _x1 } ->
59+
fun _self { id = _x0; kind = _x1; dynamic_import = _x2 } ->
6060
let _x0 = _self.ident _self _x0 in
61-
{ id = _x0; kind = _x1 }
61+
{ id = _x0; kind = _x1; dynamic_import = _x2 }
6262

6363
let required_modules : required_modules fn =
6464
fun _self arg -> list _self.module_id _self arg

jscomp/core/lam.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module Types = struct
8989

9090
and t =
9191
| Lvar of ident
92-
| Lglobal_module of ident
92+
| Lglobal_module of ident * bool
9393
| Lconst of Lam_constant.t
9494
| Lapply of apply
9595
| Lfunction of lfunction
@@ -136,7 +136,7 @@ module X = struct
136136

137137
and t = Types.t =
138138
| Lvar of ident
139-
| Lglobal_module of ident
139+
| Lglobal_module of ident * bool
140140
| Lconst of Lam_constant.t
141141
| Lapply of apply
142142
| Lfunction of lfunction
@@ -344,8 +344,8 @@ let rec apply fn args (ap_info : ap_info) : t =
344344

345345
let rec eq_approx (l1 : t) (l2 : t) =
346346
match l1 with
347-
| Lglobal_module i1 -> (
348-
match l2 with Lglobal_module i2 -> Ident.same i1 i2 | _ -> false)
347+
| Lglobal_module (i1, b1) -> (
348+
match l2 with Lglobal_module (i2, b2) -> Ident.same i1 i2 && b1 = b2 | _ -> false)
349349
| Lvar i1 -> ( match l2 with Lvar i2 -> Ident.same i1 i2 | _ -> false)
350350
| Lconst c1 -> (
351351
match l2 with Lconst c2 -> Lam_constant.eq_approx c1 c2 | _ -> false)
@@ -434,7 +434,7 @@ let rec seq (a : t) b : t =
434434
| _ -> Lsequence (a, b)
435435

436436
let var id : t = Lvar id
437-
let global_module id = Lglobal_module id
437+
let global_module ?(dynamic_import = false) id = Lglobal_module (id, dynamic_import)
438438
let const ct : t = Lconst ct
439439

440440
let function_ ~attr ~arity ~params ~body : t =
@@ -574,7 +574,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
574574
Lprim
575575
{
576576
primitive = Pfield (pos, Fld_module { name = f1 });
577-
args = [ (Lglobal_module v1 | Lvar v1) ];
577+
args = [ (Lglobal_module (v1, _) | Lvar v1) ];
578578
}
579579
:: args ) ->
580580
pos = i && f = f1 && Ident.same var v1
@@ -586,7 +586,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
586586
Lprim
587587
{
588588
primitive = Pfield (pos, Fld_module { name = f1 });
589-
args = [ ((Lglobal_module v1 | Lvar v1) as lam) ];
589+
args = [ ((Lglobal_module (v1, _) | Lvar v1) as lam) ];
590590
}
591591
:: args1 ) ->
592592
if pos = 0 && field1 = f1 && aux rest args1 v1 1 then lam

jscomp/core/lam.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ and prim_info = private {
5858

5959
and t = private
6060
| Lvar of ident
61-
| Lglobal_module of ident
61+
| Lglobal_module of ident * bool
6262
| Lconst of Lam_constant.t
6363
| Lapply of apply
6464
| Lfunction of lfunction
@@ -98,7 +98,7 @@ val handle_bs_non_obj_ffi :
9898
val var : ident -> t
9999
(** Smart constructors *)
100100

101-
val global_module : ident -> t
101+
val global_module : ?dynamic_import:bool -> ident -> t
102102

103103
val const : Lam_constant.t -> t
104104

jscomp/core/lam_arity_analysis.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
4343
| Lprim
4444
{
4545
primitive = Pfield (_, Fld_module { name });
46-
args = [ Lglobal_module id ];
46+
args = [ Lglobal_module (id, dynamic_import) ];
4747
_;
4848
} -> (
49-
match (Lam_compile_env.query_external_id_info id name).arity with
49+
match (Lam_compile_env.query_external_id_info ~dynamic_import id name).arity with
5050
| Single x -> x
5151
| Submodule _ -> Lam_arity.na)
5252
| Lprim
@@ -57,12 +57,12 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
5757
Lprim
5858
{
5959
primitive = Pfield (_, Fld_module { name });
60-
args = [ Lglobal_module id ];
60+
args = [ Lglobal_module (id, dynamic_import) ];
6161
};
6262
];
6363
_;
6464
} -> (
65-
match (Lam_compile_env.query_external_id_info id name).arity with
65+
match (Lam_compile_env.query_external_id_info ~dynamic_import id name).arity with
6666
| Submodule subs -> subs.(m) (* TODO: shall we store it as array?*)
6767
| Single _ -> Lam_arity.na)
6868
(* TODO: all information except Pccall is complete, we could

jscomp/core/lam_compile.ml

+13-13
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ type initialization = J.block
228228
let compile output_prefix =
229229

230230
let rec compile_external_field (* Like [List.empty]*)
231-
(lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t =
232-
match Lam_compile_env.query_external_id_info id name with
231+
?(dynamic_import = false) (lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t =
232+
match Lam_compile_env.query_external_id_info ~dynamic_import id name with
233233
| { persistent_closed_lambda = Some lam } when Lam_util.not_function lam ->
234234
compile_lambda lamba_cxt lam
235235
| _ ->
236236
Js_output.output_of_expression lamba_cxt.continuation
237-
~no_effects:no_effects_const (E.ml_var_dot id name)
237+
~no_effects:no_effects_const (E.ml_var_dot ~dynamic_import id name)
238238
(* TODO: how nested module call would behave,
239239
In the future, we should keep in track of if
240240
it is fully applied from [Lapply]
@@ -263,10 +263,10 @@ let rec compile_external_field (* Like [List.empty]*)
263263
for the function, generative module or functor can be a function,
264264
however it can not be global -- global can only module
265265
*)
266-
and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
266+
and compile_external_field_apply ?(dynamic_import = false) (appinfo : Lam.apply) (module_id : Ident.t)
267267
(field_name : string) (lambda_cxt : Lam_compile_context.t) : Js_output.t =
268268
let ident_info =
269-
Lam_compile_env.query_external_id_info module_id field_name
269+
Lam_compile_env.query_external_id_info ~dynamic_import module_id field_name
270270
in
271271
let ap_args = appinfo.ap_args in
272272
match ident_info.persistent_closed_lambda with
@@ -292,7 +292,7 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
292292
| _ -> assert false)
293293
in
294294

295-
let fn = E.ml_var_dot module_id ident_info.name in
295+
let fn = E.ml_var_dot ~dynamic_import module_id ident_info.name in
296296
let expression =
297297
match appinfo.ap_info.ap_status with
298298
| (App_infer_full | App_uncurry) as ap_status ->
@@ -1410,11 +1410,11 @@ and compile_apply (appinfo : Lam.apply) (lambda_cxt : Lam_compile_context.t) =
14101410
(* External function call: it can not be tailcall in this case*)
14111411
| {
14121412
ap_func =
1413-
Lprim { primitive = Pfield (_, fld_info); args = [ Lglobal_module id ]; _ };
1413+
Lprim { primitive = Pfield (_, fld_info); args = [ Lglobal_module (id, dynamic_import) ]; _ };
14141414
} -> (
14151415
match fld_info with
14161416
| Fld_module { name } ->
1417-
compile_external_field_apply appinfo id name lambda_cxt
1417+
compile_external_field_apply ~dynamic_import appinfo id name lambda_cxt
14181418
| _ -> assert false)
14191419
| _ -> (
14201420
(* TODO: ---
@@ -1491,11 +1491,11 @@ and compile_apply (appinfo : Lam.apply) (lambda_cxt : Lam_compile_context.t) =
14911491
and compile_prim (prim_info : Lam.prim_info)
14921492
(lambda_cxt : Lam_compile_context.t) =
14931493
match prim_info with
1494-
| { primitive = Pfield (_, fld_info); args = [ Lglobal_module id ]; _ } -> (
1494+
| { primitive = Pfield (_, fld_info); args = [ Lglobal_module (id, dynamic_import) ]; _ } -> (
14951495
(* should be before Lglobal_global *)
14961496
match fld_info with
14971497
| Fld_module { name = field } ->
1498-
compile_external_field lambda_cxt id field
1498+
compile_external_field ~dynamic_import lambda_cxt id field
14991499
| _ -> assert false)
15001500
| { primitive = Praise; args = [ e ]; _ } -> (
15011501
match
@@ -1715,13 +1715,13 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
17151715
Js_output.output_of_expression lambda_cxt.continuation
17161716
~no_effects:no_effects_const
17171717
(Lam_compile_const.translate c)
1718-
| Lglobal_module i ->
1718+
| Lglobal_module (i, dynamic_import) ->
17191719
(* introduced by
17201720
1. {[ include Array --> let include = Array ]}
17211721
2. inline functor application
17221722
*)
17231723
Js_output.output_of_block_and_expression lambda_cxt.continuation []
1724-
(E.ml_module_as_var i)
1724+
(E.ml_module_as_var ~dynamic_import i)
17251725
| Lprim prim_info -> compile_prim prim_info lambda_cxt
17261726
| Lsequence (l1, l2) ->
17271727
let output_l1 =
@@ -1759,4 +1759,4 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
17591759
in compile_recursive_lets, compile_lambda
17601760
17611761
let compile_recursive_lets ~output_prefix = fst (compile output_prefix)
1762-
let compile_lambda ~output_prefix = snd (compile output_prefix)
1762+
let compile_lambda ~output_prefix = snd (compile output_prefix)

jscomp/core/lam_compile_env.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ let add_js_module (hint_name : External_ffi_types.module_bind_name)
7171
| Phint_nothing -> Ext_modulename.js_id_name_of_hint_name module_name)
7272
in
7373
let lam_module_ident : J.module_id =
74-
{ id; kind = External { name = module_name; default } }
74+
{ id; kind = External { name = module_name; default }; dynamic_import = false }
7575
in
7676
match Lam_module_ident.Hash.find_key_opt cached_tbl lam_module_ident with
7777
| None ->
7878
lam_module_ident +> External;
7979
id
8080
| Some old_key -> old_key.id
8181

82-
let query_external_id_info (module_id : Ident.t) (name : string) : ident_info =
83-
let oid = Lam_module_ident.of_ml module_id in
82+
let query_external_id_info ?(dynamic_import = false) (module_id : Ident.t) (name : string) : ident_info =
83+
let oid = Lam_module_ident.of_ml ~dynamic_import module_id in
8484
let cmj_table =
8585
match Lam_module_ident.Hash.find_opt cached_tbl oid with
8686
| None ->

jscomp/core/lam_compile_env.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ val add_js_module :
5959
pay attention to for those modules are actually used or not
6060
*)
6161

62-
val query_external_id_info : Ident.t -> string -> Js_cmj_format.keyed_cmj_value
62+
val query_external_id_info : ?dynamic_import:bool -> Ident.t -> string -> Js_cmj_format.keyed_cmj_value
6363
(**
6464
[query_external_id_info id pos env found]
6565
will raise if not found

jscomp/core/lam_compile_primitive.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
130130

131131
let path =
132132
let module_system = get_module_system () in
133-
Js_name_of_module_id.string_of_module_id module_id ~output_dir module_system
133+
Js_name_of_module_id.string_of_module_id {module_id with dynamic_import = true} ~output_dir module_system
134134
in
135135

136136
match module_value with

jscomp/core/lam_convert.ml

+6-5
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,10 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
505505
primitive %s"
506506
s
507507
in
508-
let args = Ext_list.map args convert_aux in
508+
let dynamic_import = primitive = Pimport in
509+
let args = Ext_list.map args (convert_aux ~dynamic_import) in
509510
prim ~primitive ~args loc
510-
and convert_aux (lam : Lambda.lambda) : Lam.t =
511+
and convert_aux ?(dynamic_import = false) (lam : Lambda.lambda) : Lam.t =
511512
match lam with
512513
| Lvar x -> Lam.var (Hash_ident.find_default alias_tbl x x)
513514
| Lconst x -> Lam.const (Lam_constant_convert.convert_constant x)
@@ -547,9 +548,9 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
547548
if Ident.is_predef_exn id then
548549
Lam.const (Const_string { s = id.name; unicode = false })
549550
else (
550-
may_depend may_depends (Lam_module_ident.of_ml id);
551+
may_depend may_depends (Lam_module_ident.of_ml ~dynamic_import id);
551552
assert (args = []);
552-
Lam.global_module id)
553+
Lam.global_module ~dynamic_import id)
553554
| Lprim
554555
( Puncurried_apply,
555556
[ Lapply { ap_func = Lprim (Popaque, [ ap_func ], _); ap_args } ],
@@ -561,7 +562,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
561562
for cases like `(fun [@bs] a b -> a + b ) 1 2 [@bs]` *)
562563
| Lprim (Puncurried_apply, _, _) -> assert false
563564
| Lprim (primitive, args, loc) ->
564-
let args = Ext_list.map args convert_aux in
565+
let args = Ext_list.map args (convert_aux ~dynamic_import) in
565566
lam_prim ~primitive ~args loc
566567
| Lswitch (e, s, _loc) -> convert_switch e s
567568
| Lstringswitch (e, cases, default, _) ->

0 commit comments

Comments
 (0)