Skip to content

Commit a9610c2

Browse files
committed
then callback function for dynamic import
1 parent 3feef88 commit a9610c2

File tree

5 files changed

+143
-45
lines changed

5 files changed

+143
-45
lines changed

jscomp/core/lam_compile_primitive.ml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,25 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
8282
match args with
8383
| [ e ] -> (
8484
match e.expression_desc with
85-
(* TODO: check if value or unpack *)
8685
| _ -> (
8786
match output_prefix with
88-
| Some output_prefix ->
87+
| Some output_prefix -> (
8988
let output_dir = Filename.dirname output_prefix in
9089

9190
(* TODO: pull this function out to top-level *)
92-
let rec module_id_of_expression = function
93-
| J.Var (J.Qualified (module_id, _)) -> [ module_id ]
91+
let rec module_of_expression = function
92+
| J.Var (J.Qualified (module_id, value)) ->
93+
[ (module_id, value) ]
9494
| J.Caml_block (exprs, _, _, _) ->
9595
exprs
9696
|> List.map (fun (e : J.expression) ->
97-
module_id_of_expression e.expression_desc)
97+
module_of_expression e.expression_desc)
9898
|> List.concat
9999
| _ -> []
100100
in
101101

102-
let module_id =
103-
match module_id_of_expression e.expression_desc with
102+
let module_id, value =
103+
match module_of_expression e.expression_desc with
104104
| [ module_name ] -> module_name
105105
| _ -> assert false
106106
(* TODO: graceful error message here *)
@@ -112,10 +112,34 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
112112
(* TODO: where is Js_package_info.module_system ? *)
113113
Js_packages_info.NodeJS
114114
in
115-
E.call
116-
~info:{ arity = Full; call_info = Call_na }
117-
(E.js_global "import")
118-
[ E.str path ]
115+
let arg_of_callback_fn = Ident.create "m" in
116+
match value with
117+
| Some value ->
118+
E.call
119+
~info:{ arity = Full; call_info = Call_na }
120+
(E.dot
121+
(E.call
122+
~info:{ arity = Full; call_info = Call_na }
123+
(E.js_global "import")
124+
[ E.str path ])
125+
"then")
126+
[
127+
E.ocaml_fun ~return_unit:false ~async:false
128+
[ arg_of_callback_fn ]
129+
[
130+
{
131+
statement_desc =
132+
J.Return
133+
(E.dot (E.var arg_of_callback_fn) value);
134+
comment = None;
135+
};
136+
];
137+
]
138+
| None ->
139+
E.call
140+
~info:{ arity = Full; call_info = Call_na }
141+
(E.js_global "import")
142+
[ E.str path ])
119143
| None -> assert false))
120144
| _ -> assert false)
121145
| Pjs_function_length -> E.function_length (Ext_list.singleton_exn args)

jscomp/test/Import.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
var Curry = require("../../lib/js/curry.js");
44

5-
var each = import("../../lib/js/belt_List.js");
5+
var each = import("../../lib/js/belt_List.js").then(function (m) {
6+
return m.forEach;
7+
});
68

79
function eachInt(list, f) {
810
var arg1 = function (each) {

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96694,25 +96694,25 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9669496694
match args with
9669596695
| [ e ] -> (
9669696696
match e.expression_desc with
96697-
(* TODO: check if value or unpack *)
9669896697
| _ -> (
9669996698
match output_prefix with
96700-
| Some output_prefix ->
96699+
| Some output_prefix -> (
9670196700
let output_dir = Filename.dirname output_prefix in
9670296701

9670396702
(* TODO: pull this function out to top-level *)
96704-
let rec module_id_of_expression = function
96705-
| J.Var (J.Qualified (module_id, _)) -> [ module_id ]
96703+
let rec module_of_expression = function
96704+
| J.Var (J.Qualified (module_id, value)) ->
96705+
[ (module_id, value) ]
9670696706
| J.Caml_block (exprs, _, _, _) ->
9670796707
exprs
9670896708
|> List.map (fun (e : J.expression) ->
96709-
module_id_of_expression e.expression_desc)
96709+
module_of_expression e.expression_desc)
9671096710
|> List.concat
9671196711
| _ -> []
9671296712
in
9671396713

96714-
let module_id =
96715-
match module_id_of_expression e.expression_desc with
96714+
let module_id, value =
96715+
match module_of_expression e.expression_desc with
9671696716
| [ module_name ] -> module_name
9671796717
| _ -> assert false
9671896718
(* TODO: graceful error message here *)
@@ -96724,10 +96724,34 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9672496724
(* TODO: where is Js_package_info.module_system ? *)
9672596725
Js_packages_info.NodeJS
9672696726
in
96727-
E.call
96728-
~info:{ arity = Full; call_info = Call_na }
96729-
(E.js_global "import")
96730-
[ E.str path ]
96727+
let arg_of_callback_fn = Ident.create "m" in
96728+
match value with
96729+
| Some value ->
96730+
E.call
96731+
~info:{ arity = Full; call_info = Call_na }
96732+
(E.dot
96733+
(E.call
96734+
~info:{ arity = Full; call_info = Call_na }
96735+
(E.js_global "import")
96736+
[ E.str path ])
96737+
"then")
96738+
[
96739+
E.ocaml_fun ~return_unit:false ~async:false
96740+
[ arg_of_callback_fn ]
96741+
[
96742+
{
96743+
statement_desc =
96744+
J.Return
96745+
(E.dot (E.var arg_of_callback_fn) value);
96746+
comment = None;
96747+
};
96748+
];
96749+
]
96750+
| None ->
96751+
E.call
96752+
~info:{ arity = Full; call_info = Call_na }
96753+
(E.js_global "import")
96754+
[ E.str path ])
9673196755
| None -> assert false))
9673296756
| _ -> assert false)
9673396757
| Pjs_function_length -> E.function_length (Ext_list.singleton_exn args)

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96694,25 +96694,25 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9669496694
match args with
9669596695
| [ e ] -> (
9669696696
match e.expression_desc with
96697-
(* TODO: check if value or unpack *)
9669896697
| _ -> (
9669996698
match output_prefix with
96700-
| Some output_prefix ->
96699+
| Some output_prefix -> (
9670196700
let output_dir = Filename.dirname output_prefix in
9670296701

9670396702
(* TODO: pull this function out to top-level *)
96704-
let rec module_id_of_expression = function
96705-
| J.Var (J.Qualified (module_id, _)) -> [ module_id ]
96703+
let rec module_of_expression = function
96704+
| J.Var (J.Qualified (module_id, value)) ->
96705+
[ (module_id, value) ]
9670696706
| J.Caml_block (exprs, _, _, _) ->
9670796707
exprs
9670896708
|> List.map (fun (e : J.expression) ->
96709-
module_id_of_expression e.expression_desc)
96709+
module_of_expression e.expression_desc)
9671096710
|> List.concat
9671196711
| _ -> []
9671296712
in
9671396713

96714-
let module_id =
96715-
match module_id_of_expression e.expression_desc with
96714+
let module_id, value =
96715+
match module_of_expression e.expression_desc with
9671696716
| [ module_name ] -> module_name
9671796717
| _ -> assert false
9671896718
(* TODO: graceful error message here *)
@@ -96724,10 +96724,34 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9672496724
(* TODO: where is Js_package_info.module_system ? *)
9672596725
Js_packages_info.NodeJS
9672696726
in
96727-
E.call
96728-
~info:{ arity = Full; call_info = Call_na }
96729-
(E.js_global "import")
96730-
[ E.str path ]
96727+
let arg_of_callback_fn = Ident.create "m" in
96728+
match value with
96729+
| Some value ->
96730+
E.call
96731+
~info:{ arity = Full; call_info = Call_na }
96732+
(E.dot
96733+
(E.call
96734+
~info:{ arity = Full; call_info = Call_na }
96735+
(E.js_global "import")
96736+
[ E.str path ])
96737+
"then")
96738+
[
96739+
E.ocaml_fun ~return_unit:false ~async:false
96740+
[ arg_of_callback_fn ]
96741+
[
96742+
{
96743+
statement_desc =
96744+
J.Return
96745+
(E.dot (E.var arg_of_callback_fn) value);
96746+
comment = None;
96747+
};
96748+
];
96749+
]
96750+
| None ->
96751+
E.call
96752+
~info:{ arity = Full; call_info = Call_na }
96753+
(E.js_global "import")
96754+
[ E.str path ])
9673196755
| None -> assert false))
9673296756
| _ -> assert false)
9673396757
| Pjs_function_length -> E.function_length (Ext_list.singleton_exn args)

lib/4.06.1/whole_compiler.ml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265888,25 +265888,25 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
265888265888
match args with
265889265889
| [ e ] -> (
265890265890
match e.expression_desc with
265891-
(* TODO: check if value or unpack *)
265892265891
| _ -> (
265893265892
match output_prefix with
265894-
| Some output_prefix ->
265893+
| Some output_prefix -> (
265895265894
let output_dir = Filename.dirname output_prefix in
265896265895

265897265896
(* TODO: pull this function out to top-level *)
265898-
let rec module_id_of_expression = function
265899-
| J.Var (J.Qualified (module_id, _)) -> [ module_id ]
265897+
let rec module_of_expression = function
265898+
| J.Var (J.Qualified (module_id, value)) ->
265899+
[ (module_id, value) ]
265900265900
| J.Caml_block (exprs, _, _, _) ->
265901265901
exprs
265902265902
|> List.map (fun (e : J.expression) ->
265903-
module_id_of_expression e.expression_desc)
265903+
module_of_expression e.expression_desc)
265904265904
|> List.concat
265905265905
| _ -> []
265906265906
in
265907265907

265908-
let module_id =
265909-
match module_id_of_expression e.expression_desc with
265908+
let module_id, value =
265909+
match module_of_expression e.expression_desc with
265910265910
| [ module_name ] -> module_name
265911265911
| _ -> assert false
265912265912
(* TODO: graceful error message here *)
@@ -265918,10 +265918,34 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
265918265918
(* TODO: where is Js_package_info.module_system ? *)
265919265919
Js_packages_info.NodeJS
265920265920
in
265921-
E.call
265922-
~info:{ arity = Full; call_info = Call_na }
265923-
(E.js_global "import")
265924-
[ E.str path ]
265921+
let arg_of_callback_fn = Ident.create "m" in
265922+
match value with
265923+
| Some value ->
265924+
E.call
265925+
~info:{ arity = Full; call_info = Call_na }
265926+
(E.dot
265927+
(E.call
265928+
~info:{ arity = Full; call_info = Call_na }
265929+
(E.js_global "import")
265930+
[ E.str path ])
265931+
"then")
265932+
[
265933+
E.ocaml_fun ~return_unit:false ~async:false
265934+
[ arg_of_callback_fn ]
265935+
[
265936+
{
265937+
statement_desc =
265938+
J.Return
265939+
(E.dot (E.var arg_of_callback_fn) value);
265940+
comment = None;
265941+
};
265942+
];
265943+
]
265944+
| None ->
265945+
E.call
265946+
~info:{ arity = Full; call_info = Call_na }
265947+
(E.js_global "import")
265948+
[ E.str path ])
265925265949
| None -> assert false))
265926265950
| _ -> assert false)
265927265951
| Pjs_function_length -> E.function_length (Ext_list.singleton_exn args)

0 commit comments

Comments
 (0)