Skip to content

Commit 57de004

Browse files
fix(defer): give a deferred namespace the spec's exotic shape
A Module Namespace Exotic Object is not extensible, its keys are the sorted exports followed by @@toStringTag, and each export reads back as a non-configurable writable data property. The deferred namespace proxy left its target extensible, so `[[IsExtensible]]` was true, `ownKeys` reported `__esModule`, and a descriptor came back as the getter webpack backs the export with. The export names are known at build time, so they are handed to the runtime, which puts them on the target and seals it. Only a real ES module namespace takes this path: a synthesized one (JSON, asset, CommonJS interop) keeps the previous shape, including `__esModule`. Un-skips test262's deferred-namespace-object/exotic-object-behavior. ignore-private-name-access moves to the host list: it needs `nonextensible-applies-to-private`, which the pinned Node.js does not implement, so it fails the same way unbundled.
1 parent e507e93 commit 57de004

4 files changed

Lines changed: 276 additions & 220 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Give a deferred namespace the spec's exotic shape when the exports are known.

‎lib/RuntimeTemplate.js‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,26 @@ const isAsyncExternal = (/** @type {Module} */ module) =>
441441
Boolean(/** @type {BuildMeta} */ (module.buildMeta).async) &&
442442
module instanceof getExternalModule();
443443

444+
/**
445+
* The module's export names, sorted, when the graph knows them exhaustively.
446+
* @param {ModuleGraph} moduleGraph the module graph
447+
* @param {Module} module the deferred module
448+
* @returns {string[] | null} sorted export names, or null when unknown
449+
*/
450+
const getKnownExportNames = (moduleGraph, module) => {
451+
const exportsInfo = moduleGraph.getExportsInfo(module);
452+
if (exportsInfo.otherExportsInfo.provided !== false) return null;
453+
const names = [];
454+
for (const exportInfo of exportsInfo.orderedExports) {
455+
if (exportInfo.provided !== true) return null;
456+
if (exportInfo.name === "then" || exportInfo.name === "__esModule") {
457+
continue;
458+
}
459+
names.push(exportInfo.name);
460+
}
461+
return names.sort();
462+
};
463+
444464
class RuntimeTemplate {
445465
/**
446466
* Creates an instance of RuntimeTemplate.
@@ -1991,6 +2011,15 @@ class RuntimeTemplate {
19912011

19922012
let mode = getMakeDeferredNamespaceModeFromExportsType(exportsType);
19932013
if (mode) mode = `${mode} | 16`;
2014+
// The spec's namespace keys are the sorted exports, so hand them over
2015+
// when they are fully known; otherwise the runtime stays permissive.
2016+
const deferredExportNames = getKnownExportNames(
2017+
chunkGraph.moduleGraph,
2018+
module
2019+
);
2020+
mode += deferredExportNames
2021+
? `, ${JSON.stringify(deferredExportNames)}`
2022+
: "";
19942023

19952024
const asyncDeps = Array.from(
19962025
getOutgoingAsyncModules(chunkGraph.moduleGraph, module),
@@ -2468,9 +2497,10 @@ class RuntimeTemplate {
24682497
runtimeRequirements.add(RuntimeGlobals.makeDeferredNamespaceObject);
24692498
const id = chunkGraph.getModuleId(module);
24702499
const type = getMakeDeferredNamespaceModeFromExportsType(exportsType);
2471-
const init = `${
2472-
RuntimeGlobals.makeDeferredNamespaceObject
2473-
}(${JSON.stringify(id)}, ${type})`;
2500+
const names = getKnownExportNames(chunkGraph.moduleGraph, module);
2501+
const init = `${RuntimeGlobals.makeDeferredNamespaceObject}(${JSON.stringify(
2502+
id
2503+
)}, ${type}${names ? `, ${JSON.stringify(names)}` : ""})`;
24742504

24752505
return `/*#__PURE__*/ ${
24762506
asiSafe ? "" : asiSafe === false ? ";" : "Object"

0 commit comments

Comments
 (0)