Skip to content

Commit a538eb0

Browse files
committed
fix: handle dynamic imports with module aliases
Fixes #7403 When a module containing an alias (e.g. `module VS = VariantSpreads`) is dynamically imported, the compiler now correctly extracts the original module identifier from the Llet expression.
1 parent b7aa841 commit a538eb0

6 files changed

+54
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Fix regression in pattern matching for optional fields containing variants. https://github.com/rescript-lang/rescript/pull/7440
3636
- Fix missing checks for duplicate literals in variants with payloads. https://github.com/rescript-lang/rescript/pull/7441
3737
- Fix printer removing private for empty record. https://github.com/rescript-lang/rescript/pull/7448
38+
- Fix: handle dynamic imports with module aliases. https://github.com/rescript-lang/rescript/pull/7452
3839

3940
#### :house: Internal
4041

compiler/core/lam_convert.ml

+16
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,22 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
442442
in
443443
Lam.function_ ~attr ~arity:(List.length params) ~params
444444
~body:(convert_aux body)
445+
| Llet (_, _, _, Lprim (Pgetglobal id, args, _), _body) when dynamic_import
446+
->
447+
(*
448+
Normally `await M` produces (global M!)
449+
but when M contains an alias such as `module VS = VariantSpreads`,
450+
it produces something like this:
451+
452+
(let (let/1202 = (global M!))
453+
(makeblock [x;M;y;X] (field:x/0 let/1202)
454+
...)
455+
456+
Here, we need to extract the original module id from the Llet.
457+
*)
458+
may_depend may_depends (Lam_module_ident.of_ml ~dynamic_import id);
459+
assert (args = []);
460+
Lam.global_module ~dynamic_import id
445461
| Llet (kind, Pgenval, id, e, body) (*FIXME*) -> convert_let kind id e body
446462
| Lletrec (bindings, body) ->
447463
let bindings = Ext_list.map_snd bindings convert_aux in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
async function foo(z) {
5+
let Utils = await import("./ModuleWithAlias.mjs");
6+
return Utils.x + z | 0;
7+
}
8+
9+
export {
10+
foo,
11+
}
12+
/* No side effect */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let foo = async z => {
2+
module Utils = await ModuleWithAlias
3+
4+
Utils.x + z
5+
}

tests/tests/src/ModuleWithAlias.mjs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
let x = 12;
5+
6+
let VS;
7+
8+
let y = 13;
9+
10+
export {
11+
x,
12+
VS,
13+
y,
14+
}
15+
/* No side effect */

tests/tests/src/ModuleWithAlias.res

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let x = 12
2+
3+
module VS = VariantSpreads
4+
5+
let y = 13

0 commit comments

Comments
 (0)