Skip to content

Commit f87ab2f

Browse files
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.
1 parent 591d827 commit f87ab2f

8 files changed

Lines changed: 56 additions & 13 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. A namespace re-exported as a named export is not itself wrapped 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.
48644864
* @since 5.111.0
48654865
* @experimental
48664866
*/

‎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

‎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. A namespace re-exported as a named export is not itself wrapped 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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
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 it on the imported module, not on the importer. A namespace re-exported as a named export is not itself wrapped yet.",
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.",
90129012
"multiple": false,
90139013
"simpleType": "boolean",
90149014
},
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as self1 from "./index.js";
2+
import * as self2 from "./index.js";
3+
import { testNs } from "./other.js";
4+
5+
export var a = 1;
6+
7+
// Mirrors test262 language/module-code/instn-star-equality.js, which asks for
8+
// a single namespace per module however the importer reached it.
9+
it("should hand out one namespace object for a self-import", () => {
10+
expect(self1).toBe(self2);
11+
expect(Object.getPrototypeOf(self1)).toBe(null);
12+
});
13+
14+
it("should hand out one namespace object for a re-exported namespace", () => {
15+
// a plain exports object carries `Symbol.toStringTag` too, so the null
16+
// prototype is what tells the two apart
17+
expect(Object.getPrototypeOf(testNs)).toBe(null);
18+
expect(testNs).toBe(self1);
19+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as testNs from "./index.js";
2+
3+
export { testNs };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
module: {
6+
rules: [{ test: /index\.js$/, parser: { specNamespaceObject: true } }]
7+
}
8+
};

‎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. A namespace re-exported as a named export is not itself wrapped 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.
1490414904
* @since 5.111.0
1490514905
* @experimental
1490614906
*/

0 commit comments

Comments
 (0)