Skip to content

Commit 1320792

Browse files
feat(parser): add specNamespaceObject for spec namespace objects (#22049)
* feat(parser): add specNamespaceObject for spec namespace objects `import * as ns` hands out `__webpack_exports__`, a plain object carrying `__esModule`, where the spec hands out a Module Namespace Exotic Object with a null prototype, sorted keys and non-writable bindings. The new parser option wraps it in a proxy that reports those, per module rather than per build: a spec namespace keeps every exported name, so it blocks pruning and costs runtime code that a normal build should not pay for. The option is read from the module being imported, never the importer, so every importer of one module is handed the same namespace object. A module that opts in also stops being concatenated, since concatenation would materialize the namespace inline and drop the exotic object silently. Takes 35 test262 cases off the skip list. * feat(parser): wrap a concatenated importer's namespace reference too A concatenation root reads an external module through its own binding, so the option was ignored exactly where production puts it. Wrapping that binding covers every reference at once and the runtime caches per exports object, so identity holds. Guarded on a namespace exportsType, since wrapping an interop path would hide `__esModule` from it. * fix(parser): return from the namespace callbacks, and key by the module Two real defects a review caught. `expressionFunction` emits no `return` when arrow functions are off, so with `output.environment.arrowFunction` false the filter dropped every export name and the proxy read through to the target: both callbacks now use `returningFunction`. The materialized namespace is built per importer, so caching by it handed two importers two proxies for one module; the cache now takes the module's own exports object as its key. Mangling and the decoupled namespace object are skipped for such a module besides, since a spec namespace has to report the names the module actually exports. * refactor(parser): drop the unreachable spec namespace materialization `MangleExportsPlugin` never mangles a module that hands out a spec namespace, so `_materializedNamespaceObject` always declines for one and the branch wrapping its result could not run. Removing it leaves the runtime helper keyed by the exports object alone. A strict CommonJS `require` inside a concatenation reaches the imported module through a lazy accessor rather than an eager binding; no case drove that path, so add one. * fix(parser): wrap a namespace re-exported as a named export `export { ns }` emitted the raw exports object, so an importer reading it was handed a different object than `import * as ns` gives, breaking the single namespace per module that test262 instn-star-equality.js asks for. The schema note saying named re-exports were not wrapped goes with it. * feat(parser): drop the experimental marker from specNamespaceObject Module Namespace Exotic Objects are ECMA-262 10.4.6 and have been stable since ES2015, so the option implements a settled spec rather than a proposal. The one caveat the marker stood for, a namespace re-exported as a named export, is fixed. * fix(parser): keep require() on the exports object inside a concatenation Concatenation wrapped the shared external binding, so a CommonJS `require` in the same group was handed the spec namespace and lost `__esModule`, which a default-interop helper reads. The same sources built without concatenation gave it the exports object, so the option's behaviour depended on whether concatenation ran. Wrap at the reference instead, where `moduleExportsAccess` already marks a `require` apart from an import. Costs 3-5 gzip bytes on the cases that concatenate.
1 parent 3a69d29 commit 1320792

44 files changed

Lines changed: 544 additions & 56 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `module.parser.javascript.specNamespaceObject` for spec namespace objects.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,6 +4859,11 @@ export interface JavascriptParserOptions {
48594859
* Enable experimental tc39 proposal https://github.com/tc39/proposal-source-phase-imports. This allows importing modules at source phase.
48604860
*/
48614861
sourceImport?: boolean;
4862+
/**
4863+
* Hand out a spec-compliant Module Namespace Exotic Object for 'import * as ns' and 'import()' of this module instead of the plain exports object. Requires 'Proxy' in the target environment, keeps every exported name, and costs runtime code, so enable it per module. Set it on the imported module, not on the importer.
4864+
* @since 5.111.0
4865+
*/
4866+
specNamespaceObject?: boolean;
48624867
/**
48634868
* @deprecated
48644869
* Deprecated in favor of "exportsPresence". Emit errors instead of warnings when imported names don't exist in imported module.

‎lib/Module.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const makeSerializable = require("./util/makeSerializable");
190190
* Defines the build info properties common to all module types.
191191
* Module type specific properties live in the `Known*BuildInfo` typedef of the dedicated module class.
192192
* @typedef {object} KnownBuildInfo
193+
* @property {boolean=} specNamespaceObject the module hands out a spec Module Namespace Exotic Object
193194
* @property {boolean=} cacheable
194195
* @property {string[]=} notCacheableReasons reasons why the module is not cacheable (e.g. paths of loaders that marked it)
195196
* @property {boolean=} strict

‎lib/RuntimeGlobals.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ module.exports.setAnonymousDefaultName = "__webpack_require__.dn";
458458
*/
459459
module.exports.shareScopeMap = "__webpack_require__.S";
460460

461+
/**
462+
* wrap an exports object in a spec Module Namespace Exotic Object
463+
*/
464+
module.exports.specNamespaceObject = "__webpack_require__.ns";
465+
461466
/**
462467
* startup signal from runtime
463468
* This will be called when the runtime chunk has been loaded.

‎lib/RuntimePlugin.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ const getGetWorkletBootstrapRuntimeModule = lazyModule(() =>
8181
const getGlobalRuntimeModule = lazyModule(() =>
8282
require("./runtime/GlobalRuntimeModule")
8383
);
84+
const getSpecNamespaceObjectRuntimeModule = lazyModule(() =>
85+
require("./runtime/SpecNamespaceObjectRuntimeModule")
86+
);
8487
const getHasOwnPropertyRuntimeModule = lazyModule(() =>
8588
require("./runtime/HasOwnPropertyRuntimeModule")
8689
);
@@ -214,6 +217,7 @@ const SIMPLE_TREE_RUNTIME_MODULES = [
214217
getCreateFakeNamespaceObjectRuntimeModule
215218
],
216219
[RuntimeGlobals.hasOwnProperty, getHasOwnPropertyRuntimeModule],
220+
[RuntimeGlobals.specNamespaceObject, getSpecNamespaceObjectRuntimeModule],
217221
[
218222
RuntimeGlobals.compatGetDefaultExport,
219223
getCompatGetDefaultExportRuntimeModule

‎lib/RuntimeTemplate.js‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ const isAsyncExternal = (/** @type {Module} */ module) =>
445445
Boolean(/** @type {BuildMeta} */ (module.buildMeta).async) &&
446446
module instanceof getExternalModule();
447447

448+
/**
449+
* The imported module decides, never the importer: two importers of one
450+
* module have to be handed the same namespace object.
451+
* @param {Module} module the imported module
452+
* @returns {boolean} true when its namespace has to be a spec exotic object
453+
*/
454+
const usesSpecNamespaceObject = (module) =>
455+
Boolean(module.buildInfo && module.buildInfo.specNamespaceObject);
456+
448457
class RuntimeTemplate {
449458
/**
450459
* Creates an instance of RuntimeTemplate.
@@ -1955,14 +1964,21 @@ class RuntimeTemplate {
19551964
});
19561965
const exportsType = module.getExportsType(chunkGraph.moduleGraph, strict);
19571966
switch (exportsType) {
1958-
case "namespace":
1959-
return this.moduleRaw({
1967+
case "namespace": {
1968+
const raw = this.moduleRaw({
19601969
module,
19611970
chunkGraph,
19621971
request,
19631972
weak,
19641973
runtimeRequirements
19651974
});
1975+
1976+
if (!usesSpecNamespaceObject(module)) return raw;
1977+
1978+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
1979+
1980+
return `${RuntimeGlobals.specNamespaceObject}(${raw})`;
1981+
}
19661982
case "default-with-named":
19671983
runtimeRequirements.add(RuntimeGlobals.createFakeNamespaceObject);
19681984
return `${RuntimeGlobals.createFakeNamespaceObject}(${moduleId}, 3)`;
@@ -2149,6 +2165,12 @@ class RuntimeTemplate {
21492165
`${comment}${idExpr}`
21502166
)})`;
21512167
}
2168+
2169+
if (usesSpecNamespaceObject(module)) {
2170+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2171+
appending += `.then(${RuntimeGlobals.specNamespaceObject})`;
2172+
}
2173+
21522174
break;
21532175
case "dynamic":
21542176
fakeType |= 4;
@@ -2602,6 +2624,16 @@ class RuntimeTemplate {
26022624
// if we hit here, the importVar is either
26032625
// - already a ES module namespace object
26042626
// - or imported by a way that does not need interop.
2627+
if (
2628+
exportsType === "namespace" &&
2629+
exportName.length === 0 &&
2630+
usesSpecNamespaceObject(module)
2631+
) {
2632+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2633+
2634+
return `${RuntimeGlobals.specNamespaceObject}(${importVar})`;
2635+
}
2636+
26052637
return importVar;
26062638
}
26072639

‎lib/config/defaults.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ const applyJavascriptParserOptionsDefaults = (
897897
D(parserOptions, "wrappedContextRecursive", true);
898898
D(parserOptions, "wrappedContextCritical", false);
899899
D(parserOptions, "strictThisContextOnImports", false);
900+
D(parserOptions, "specNamespaceObject", false);
900901
D(parserOptions, "importMeta", outputModule ? "preserve-unknown" : true);
901902
D(parserOptions, "dynamicImportMode", "lazy");
902903
D(parserOptions, "dynamicImportPrefetch", false);

‎lib/dependencies/HarmonyExportImportedSpecifierDependency.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,20 +1582,33 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
15821582
);
15831583
break;
15841584

1585-
case "reexport-namespace-object":
1585+
case "reexport-namespace-object": {
1586+
let value = namespaceImportVar;
1587+
1588+
// The re-exported binding is the namespace itself, so it has to be
1589+
// the same object every importer of that module is handed.
1590+
if (
1591+
importedModule.buildInfo &&
1592+
importedModule.buildInfo.specNamespaceObject
1593+
) {
1594+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
1595+
value = `${RuntimeGlobals.specNamespaceObject}(${value})`;
1596+
}
1597+
15861598
initFragments.push(
15871599
this.getReexportFragment(
15881600
module,
15891601
"reexport module object",
15901602
moduleGraph
15911603
.getExportsInfo(module)
15921604
.getUsedName(/** @type {string} */ (mode.name), runtime),
1593-
namespaceImportVar,
1605+
value,
15941606
"",
15951607
runtimeRequirements
15961608
)
15971609
);
15981610
break;
1611+
}
15991612

16001613
case "normal-reexport": {
16011614
// loop-invariants hoisted out of the per-item loop below (a barrel

‎lib/dependencies/HarmonyModulesPlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const TopLevelAwaitDependency = require("./TopLevelAwaitDependency");
3535
*/
3636
/** @import Compiler from "../Compiler" */
3737
/** @import Parser from "../javascript/JavascriptParser" */
38+
/** @import { BuildInfo } from "../Module" */
3839

3940
/**
4041
* Defines the harmony modules plugin options type used by this module.
@@ -159,6 +160,15 @@ class HarmonyModulesPlugin {
159160
return;
160161
}
161162

163+
if (parserOptions.specNamespaceObject) {
164+
// Recorded on the module, because an importer asks the module it
165+
// imports whether its namespace is a spec exotic object.
166+
parser.hooks.program.tap(PLUGIN_NAME, () => {
167+
/** @type {BuildInfo} */
168+
(parser.state.module.buildInfo).specNamespaceObject = true;
169+
});
170+
}
171+
162172
new HarmonyDetectionParserPlugin().apply(parser);
163173
new HarmonyImportDependencyParserPlugin(parserOptions).apply(parser);
164174
new HarmonyExportDependencyParserPlugin(parserOptions).apply(parser);

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ const findIdentifiers = (source) => {
248248
* @property {boolean} deferred the module is deferred at least once
249249
* @property {boolean} deferredNamespaceObjectUsed deferred namespace object that being used in a not-analyzable way so it must be materialized
250250
* @property {string | undefined} deferredNamespaceObjectName deferred namespace object that being used in a not-analyzable way so it must be materialized
251+
* @property {boolean} specNamespaceObjectUsed an import reads the spec namespace object the module hands out
251252
* @property {boolean} interopNamespaceObjectUsed "default-with-named" namespace
252253
* @property {string | undefined} interopNamespaceObjectName "default-with-named" namespace
253254
* @property {boolean} interopNamespaceObject2Used "default-only" namespace
@@ -669,7 +670,7 @@ const getFinalBinding = (
669670
exportName,
670671
accessorCall: info.wrapped
671672
};
672-
case "external":
673+
case "external": {
673674
if (deferred) {
674675
info.deferredNamespaceObjectUsed = true;
675676
return {
@@ -679,13 +680,30 @@ const getFinalBinding = (
679680
exportName
680681
};
681682
}
683+
684+
// `require()` reads the exports object rather than a namespace, so
685+
// only an import gets the spec object and interop keeps working.
686+
const spec =
687+
!moduleExportsAccess &&
688+
exportsType === "namespace" &&
689+
Boolean(
690+
/** @type {BuildInfo} */
691+
(info.module.buildInfo).specNamespaceObject
692+
);
693+
const rawName = getExternalModuleExpr(info);
694+
695+
if (spec) info.specNamespaceObjectUsed = true;
696+
682697
return {
683698
info,
684-
rawName: getExternalModuleExpr(info),
699+
rawName: spec
700+
? `${RuntimeGlobals.specNamespaceObject}(${rawName})`
701+
: rawName,
685702
ids: exportName,
686703
exportName,
687-
accessorCall: info.wrapped
704+
accessorCall: spec || info.wrapped
688705
};
706+
}
689707
}
690708
}
691709
const exportsInfo = moduleGraph.getExportsInfo(info.module);
@@ -1823,6 +1841,15 @@ class ConcatenatedModule extends Module {
18231841
return info.escapeNamespaceObjectName;
18241842
}
18251843
const exportsInfo = moduleGraph.getExportsInfo(info.module);
1844+
// Its names are not mangled, and the wrapped binding is already the
1845+
// namespace, so a decoupled object would only shadow it.
1846+
if (
1847+
/** @type {BuildInfo} */
1848+
(info.module.buildInfo).specNamespaceObject
1849+
) {
1850+
return undefined;
1851+
}
1852+
18261853
let mangled = false;
18271854
for (const exportInfo of exportsInfo.orderedExports) {
18281855
if (exportInfo.provided === false) continue;
@@ -2721,10 +2748,18 @@ ${defineGetters}`
27212748
runtimeRequirements
27222749
});
27232750
const moduleId = JSON.stringify(chunkGraph.getModuleId(info.module));
2751+
const required = `${RuntimeGlobals.require}(${moduleId})`;
2752+
2753+
// The wrapping happens per reference, so only the helper has to be
2754+
// declared here.
2755+
if (info.specNamespaceObjectUsed) {
2756+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2757+
}
2758+
27242759
const body =
27252760
condition === "true"
2726-
? `return ${RuntimeGlobals.require}(${moduleId});`
2727-
: `if (${condition}) return ${RuntimeGlobals.require}(${moduleId});`;
2761+
? `return ${required};`
2762+
: `if (${condition}) return ${required};`;
27282763
result.add(
27292764
`\n// EXTERNAL MODULE: ${info.module.readableIdentifier(
27302765
requestShortener
@@ -2890,7 +2925,15 @@ ${defineGetters}`
28902925
// External module bindings may be wrapped in
28912926
// runtime-condition `if` blocks but referenced outside,
28922927
// so they must remain function-scoped (`var`).
2893-
result.add(`var ${info.name} = __webpack_require__(${moduleId});`);
2928+
const required = `__webpack_require__(${moduleId})`;
2929+
2930+
// The wrapping happens per reference, so only the helper has
2931+
// to be declared here.
2932+
if (info.specNamespaceObjectUsed) {
2933+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2934+
}
2935+
2936+
result.add(`var ${info.name} = ${required};`);
28942937

28952938
// Decoupled namespace object for an escaping mangled external
28962939
// module must come after its import variable is defined.
@@ -3198,6 +3241,7 @@ ${defineGetters}`
31983241
namespaceFnName: undefined,
31993242
escapeNamespaceObjectName: undefined,
32003243
deferredName: undefined,
3244+
specNamespaceObjectUsed: false,
32013245
interopNamespaceObjectUsed: false,
32023246
interopNamespaceObjectName: undefined,
32033247
interopNamespaceObject2Used: false,

0 commit comments

Comments
 (0)