Skip to content

Commit 1badd0f

Browse files
committed
lib: fix js importing mjs handling
use namespace rather than dynamic in js importing mjs case Fixes: #20189
1 parent ae80de1 commit 1badd0f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

lib/sharing/ConsumeSharedModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class ConsumeSharedModule extends Module {
165165
* "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
166166
*/
167167
getExportsType(moduleGraph, strict) {
168+
if (this.buildMeta && this.buildMeta.exportsType === "namespace") {
169+
return "namespace";
170+
}
168171
return "dynamic";
169172
}
170173

lib/sharing/ConsumeSharedPlugin.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,47 @@ class ConsumeSharedPlugin {
371371
);
372372
}
373373
);
374+
375+
compilation.hooks.finishModules.tapAsync(
376+
{ name: PLUGIN_NAME, stage: 10 },
377+
(modules, callback) => {
378+
for (const module of modules) {
379+
if (
380+
!(module instanceof ConsumeSharedModule) ||
381+
!module.options.import
382+
) {
383+
continue;
384+
}
385+
386+
let dependency;
387+
if (module.options.eager) {
388+
dependency = module.dependencies[0];
389+
} else {
390+
const block = module.blocks[0];
391+
if (block) {
392+
dependency = block.dependencies[0];
393+
}
394+
}
395+
396+
if (dependency) {
397+
const fallbackModule =
398+
compilation.moduleGraph.getModule(dependency);
399+
if (
400+
fallbackModule &&
401+
fallbackModule.buildMeta &&
402+
fallbackModule.buildInfo
403+
) {
404+
module.buildMeta = { ...fallbackModule.buildMeta };
405+
module.buildInfo = { ...fallbackModule.buildInfo };
406+
compilation.moduleGraph
407+
.getExportsInfo(module)
408+
.setUnknownExportsProvided();
409+
}
410+
}
411+
}
412+
callback();
413+
}
414+
);
374415
}
375416
);
376417
}

0 commit comments

Comments
 (0)