Skip to content

Commit 5e1f4ec

Browse files
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.
1 parent ae2fc3f commit 5e1f4ec

6 files changed

Lines changed: 50 additions & 17 deletions

File tree

‎declarations/WebpackOptions.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4860,7 +4860,7 @@ export interface JavascriptParserOptions {
48604860
*/
48614861
sourceImport?: boolean;
48624862
/**
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. An importer that is itself concatenated renders namespace access inline and does not consult this option yet.
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. A namespace re-exported as a named export is not itself wrapped yet.
48644864
* @since 5.111.0
48654865
* @experimental
48664866
*/

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,10 +2721,28 @@ ${defineGetters}`
27212721
runtimeRequirements
27222722
});
27232723
const moduleId = JSON.stringify(chunkGraph.getModuleId(info.module));
2724+
// Wrapping the accessor covers every reference at once, and the
2725+
// runtime caches per exports object so identity still holds.
2726+
let required = `${RuntimeGlobals.require}(${moduleId})`;
2727+
2728+
if (
2729+
/** @type {BuildInfo} */
2730+
(info.module.buildInfo).specNamespaceObject &&
2731+
getExportsType(
2732+
moduleGraph,
2733+
info,
2734+
/** @type {BuildMeta} */
2735+
(this.rootModule.buildMeta).strictHarmonyModule
2736+
) === "namespace"
2737+
) {
2738+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2739+
required = `${RuntimeGlobals.specNamespaceObject}(${required})`;
2740+
}
2741+
27242742
const body =
27252743
condition === "true"
2726-
? `return ${RuntimeGlobals.require}(${moduleId});`
2727-
: `if (${condition}) return ${RuntimeGlobals.require}(${moduleId});`;
2744+
? `return ${required};`
2745+
: `if (${condition}) return ${required};`;
27282746
result.add(
27292747
`\n// EXTERNAL MODULE: ${info.module.readableIdentifier(
27302748
requestShortener
@@ -2890,7 +2908,25 @@ ${defineGetters}`
28902908
// External module bindings may be wrapped in
28912909
// runtime-condition `if` blocks but referenced outside,
28922910
// so they must remain function-scoped (`var`).
2893-
result.add(`var ${info.name} = __webpack_require__(${moduleId});`);
2911+
let required = `__webpack_require__(${moduleId})`;
2912+
2913+
// The binding covers every reference at once, and the runtime
2914+
// caches per exports object so identity still holds.
2915+
if (
2916+
/** @type {BuildInfo} */
2917+
(info.module.buildInfo).specNamespaceObject &&
2918+
getExportsType(
2919+
moduleGraph,
2920+
info,
2921+
/** @type {BuildMeta} */
2922+
(this.rootModule.buildMeta).strictHarmonyModule
2923+
) === "namespace"
2924+
) {
2925+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2926+
required = `${RuntimeGlobals.specNamespaceObject}(${required})`;
2927+
}
2928+
2929+
result.add(`var ${info.name} = ${required};`);
28942930

28952931
// Decoupled namespace object for an escaping mangled external
28962932
// module must come after its import variable is defined.

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@
30383038
"type": "boolean"
30393039
},
30403040
"specNamespaceObject": {
3041-
"description": "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. An importer that is itself concatenated renders namespace access inline and does not consult this option yet.",
3041+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
30423042
"type": "boolean",
30433043
"experimental": true,
30443044
"added": "5.111.0"

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5318,13 +5318,13 @@ Object {
53185318
"module-parser-javascript-auto-spec-namespace-object": Object {
53195319
"configs": Array [
53205320
Object {
5321-
"description": "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 on the imported module, not on the importer.",
5321+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
53225322
"multiple": false,
53235323
"path": "module.parser.javascript/auto.specNamespaceObject",
53245324
"type": "boolean",
53255325
},
53265326
],
5327-
"description": "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 on the imported module, not on the importer.",
5327+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
53285328
"multiple": false,
53295329
"simpleType": "boolean",
53305330
},
@@ -6724,13 +6724,13 @@ Object {
67246724
"module-parser-javascript-dynamic-spec-namespace-object": Object {
67256725
"configs": Array [
67266726
Object {
6727-
"description": "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 on the imported module, not on the importer.",
6727+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
67286728
"multiple": false,
67296729
"path": "module.parser.javascript/dynamic.specNamespaceObject",
67306730
"type": "boolean",
67316731
},
67326732
],
6733-
"description": "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 on the imported module, not on the importer.",
6733+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
67346734
"multiple": false,
67356735
"simpleType": "boolean",
67366736
},
@@ -7970,13 +7970,13 @@ Object {
79707970
"module-parser-javascript-esm-spec-namespace-object": Object {
79717971
"configs": Array [
79727972
Object {
7973-
"description": "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 on the imported module, not on the importer.",
7973+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
79747974
"multiple": false,
79757975
"path": "module.parser.javascript/esm.specNamespaceObject",
79767976
"type": "boolean",
79777977
},
79787978
],
7979-
"description": "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 on the imported module, not on the importer.",
7979+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
79807980
"multiple": false,
79817981
"simpleType": "boolean",
79827982
},
@@ -9002,13 +9002,13 @@ Object {
90029002
"module-parser-javascript-spec-namespace-object": Object {
90039003
"configs": Array [
90049004
Object {
9005-
"description": "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 on the imported module, not on the importer.",
9005+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
90069006
"multiple": false,
90079007
"path": "module.parser.javascript.specNamespaceObject",
90089008
"type": "boolean",
90099009
},
90109010
],
9011-
"description": "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 on the imported module, not on the importer.",
9011+
"description": "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. A namespace re-exported as a named export is not itself wrapped yet.",
90129012
"multiple": false,
90139013
"simpleType": "boolean",
90149014
},

‎test/configCases/spec-namespace-objects/per-module/webpack.config.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
/** @type {import("../../../../").Configuration} */
44
module.exports = {
5-
// A concatenated importer renders namespace access itself, so it does not
6-
// consult the option yet; this case is about which module the option reads.
7-
optimization: { concatenateModules: false },
85
module: {
96
rules: [{ test: /spec\.js$/, parser: { specNamespaceObject: true } }]
107
}

‎types.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14900,7 +14900,7 @@ declare interface JavascriptParserOptions {
1490014900
sourceImport?: boolean;
1490114901

1490214902
/**
14903-
* 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. An importer that is itself concatenated renders namespace access inline and does not consult this option yet.
14903+
* 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. A namespace re-exported as a named export is not itself wrapped yet.
1490414904
* @since 5.111.0
1490514905
* @experimental
1490614906
*/

0 commit comments

Comments
 (0)