- Notifications
You must be signed in to change notification settings - Fork 473
Dynamic import #5703
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
Dynamic import #5703
Changes from 1 commit
7968709 dd3c38d e92854f 236f32e b327950 18abaf8 d550479 738043f feeaa73 faf65be b5aec26 ae62547 6f0c22a 2b69527 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -25,6 +25,6 @@ | |
| (** Compile single lambda IR to JS IR *) | ||
| | ||
| val compile_recursive_lets : | ||
| string -> Js_packages_info.module_system -> Lam_compile_context.t -> (Ident.t * Lam.t) list -> Js_output.t | ||
| output_prefix:string -> Js_packages_info.module_system -> Lam_compile_context.t -> (Ident.t * Lam.t) list -> Js_output.t | ||
| ||
| | ||
| val compile_lambda : string -> Js_packages_info.module_system -> Lam_compile_context.t -> Lam.t -> Js_output.t | ||
| val compile_lambda : output_prefix:string -> Js_packages_info.module_system -> Lam_compile_context.t -> Lam.t -> Js_output.t | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -103,6 +103,8 @@ let translate output_prefix module_system loc (cxt : Lam_compile_context.t) | |
| | _ -> E.runtime_call Js_runtime_modules.option "nullable_to_opt" args | ||
| ) | ||
| | _ -> assert false) | ||
| (* Compile #import: The module argument for dynamic import is represented as a path, | ||
| and the module value is expressed through wrapping it with promise.then *) | ||
| | Pimport -> ( | ||
| Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment describing what the transformation does. | ||
| match args with | ||
| | [ e ] -> ( | ||
| | @@ -111,8 +113,8 @@ let translate output_prefix module_system loc (cxt : Lam_compile_context.t) | |
| let module_id, module_value = | ||
| match module_of_expression e.expression_desc with | ||
| | [ module_ ] -> module_ | ||
| | _ -> assert false | ||
| (* TODO: graceful error message here *) | ||
| | _ -> Location.raise_errorf ~loc | ||
| "Invalid argument: Dynamic import requires a module or a module value as its argument. Passing a value or local module is not allowed." | ||
| ||
| in | ||
| | ||
| let path = | ||
| | @@ -123,7 +125,9 @@ let translate output_prefix module_system loc (cxt : Lam_compile_context.t) | |
| match module_value with | ||
| | Some value -> wrap_then (import_of_path path) value | ||
| | None -> import_of_path path) | ||
| | _ -> assert false) | ||
| | [] | _ -> | ||
| Location.raise_errorf ~loc | ||
| "Invalid argument: Dynamic import must take a single module or module value as its argument.") | ||
| | Pjs_function_length -> E.function_length (Ext_list.singleton_exn args) | ||
| | Pcaml_obj_length -> E.obj_length (Ext_list.singleton_exn args) | ||
| | Pis_null -> E.is_null (Ext_list.singleton_exn args) | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -6,6 +6,7 @@ let create_await_expression (e : Parsetree.expression) = | |
| in | ||
| Ast_helper.Exp.apply ~loc unsafe_await [(Nolabel, e)] | ||
| | ||
| (* Transform `@res.await M` to unpack(@res.await Js.import(module(M: __M0__))) *) | ||
| let create_await_module_expression ~module_type_name (e : Parsetree.module_expr) | ||
| Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment explaining what this transformation does. | ||
| = | ||
| let open Ast_helper in | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -435,6 +435,8 @@ let local_module_name = | |
| incr v; | ||
| "local_" ^ string_of_int !v | ||
| | ||
| (* Unpack requires core_type package for type inference; | ||
| use module type bindings and a function to create safe local names instead. *) | ||
| let local_module_type_name = | ||
| ||
| let v = ref 0 in | ||
| fun ({txt} : Longident.t Location.loc) -> | ||
| | @@ -505,10 +507,10 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) = | |
| | _ -> expand_reverse acc (structure_mapper self rest) | ||
| in | ||
| aux [] stru | ||
| (* Dynamic import of module transformation: module M = @res.await Belt.List *) | ||
| | Pstr_module | ||
| Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment explaining what this transformation does. | ||
| ({pmb_expr = {pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as me} | ||
| as mb) | ||
| (* module M = @res.await Belt.List *) | ||
| when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes -> | ||
| let item = self.structure_item self item in | ||
| let safe_module_type_name = local_module_type_name {txt; loc} in | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the root cause breaking the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems delaying the evaluation of js_program causing the test failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See how it goes #6191
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bingo.