Skip to content

fix: handle dynamic imports with module aliases #7452

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- Fix regression in pattern matching for optional fields containing variants. https://github.com/rescript-lang/rescript/pull/7440
- Fix missing checks for duplicate literals in variants with payloads. https://github.com/rescript-lang/rescript/pull/7441
- Fix printer removing private for empty record. https://github.com/rescript-lang/rescript/pull/7448
- Fix: handle dynamic imports with module aliases. https://github.com/rescript-lang/rescript/pull/7452

#### :house: Internal

Expand Down
16 changes: 16 additions & 0 deletions compiler/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,22 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
in
Lam.function_ ~attr ~arity:(List.length params) ~params
~body:(convert_aux body)
| Llet (_, _, _, Lprim (Pgetglobal id, args, _), _body) when dynamic_import
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mununki is this the correct way to handle the case?

->
(*
Normally `await M` produces (global M!)
but when M contains an alias such as `module VS = VariantSpreads`,
it produces something like this:
(let (let/1202 = (global M!))
(makeblock [x;M;y;X] (field:x/0 let/1202)
...)
Here, we need to extract the original module id from the Llet.
*)
may_depend may_depends (Lam_module_ident.of_ml ~dynamic_import id);
assert (args = []);
Lam.global_module ~dynamic_import id
| Llet (kind, Pgenval, id, e, body) (*FIXME*) -> convert_let kind id e body
| Lletrec (bindings, body) ->
let bindings = Ext_list.map_snd bindings convert_aux in
Expand Down
12 changes: 12 additions & 0 deletions tests/tests/src/DynamicImportModuleWithAlias.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


async function foo(z) {
let Utils = await import("./ModuleWithAlias.mjs");
return Utils.x + z | 0;
}

export {
foo,
}
/* No side effect */
5 changes: 5 additions & 0 deletions tests/tests/src/DynamicImportModuleWithAlias.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let foo = async z => {
module Utils = await ModuleWithAlias

Utils.x + z
}
15 changes: 15 additions & 0 deletions tests/tests/src/ModuleWithAlias.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


let x = 12;

let VS;

let y = 13;

export {
x,
VS,
y,
}
/* No side effect */
5 changes: 5 additions & 0 deletions tests/tests/src/ModuleWithAlias.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x = 12

module VS = VariantSpreads

let y = 13
Loading