diff --git a/CHANGELOG.md b/CHANGELOG.md index 34079500d0..6c07f0ad4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/compiler/core/lam_convert.ml b/compiler/core/lam_convert.ml index a12b25ac57..c62eed5d0b 100644 --- a/compiler/core/lam_convert.ml +++ b/compiler/core/lam_convert.ml @@ -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 + -> + (* + 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 diff --git a/tests/tests/src/DynamicImportModuleWithAlias.mjs b/tests/tests/src/DynamicImportModuleWithAlias.mjs new file mode 100644 index 0000000000..cf6a3c2bbb --- /dev/null +++ b/tests/tests/src/DynamicImportModuleWithAlias.mjs @@ -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 */ diff --git a/tests/tests/src/DynamicImportModuleWithAlias.res b/tests/tests/src/DynamicImportModuleWithAlias.res new file mode 100644 index 0000000000..c68d3270c8 --- /dev/null +++ b/tests/tests/src/DynamicImportModuleWithAlias.res @@ -0,0 +1,5 @@ +let foo = async z => { + module Utils = await ModuleWithAlias + + Utils.x + z +} diff --git a/tests/tests/src/ModuleWithAlias.mjs b/tests/tests/src/ModuleWithAlias.mjs new file mode 100644 index 0000000000..4610eac51e --- /dev/null +++ b/tests/tests/src/ModuleWithAlias.mjs @@ -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 */ diff --git a/tests/tests/src/ModuleWithAlias.res b/tests/tests/src/ModuleWithAlias.res new file mode 100644 index 0000000000..aff0c6804c --- /dev/null +++ b/tests/tests/src/ModuleWithAlias.res @@ -0,0 +1,5 @@ +let x = 12 + +module VS = VariantSpreads + +let y = 13