Skip to content

Commit 04e413c

Browse files
fix(esm): keep every unknown import.meta access on the module's own object
Direct accesses read and wrote the bundle's shared import.meta unless an alias had written the name first, so direct writes leaked into other modules and a value an alias deleted came back. Route them all to the module's object, created once per parse.
1 parent 1e3df99 commit 04e413c

5 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎.changeset/026-import-meta-alias-mutations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Share unknown import.meta properties between aliases and direct accesses.
5+
Keep unknown `import.meta` properties per module, alike for aliases and direct use.

‎lib/dependencies/import-meta/ImportMetaPlugin.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const WEBPACK_VERSION = Number.parseInt(
6565
* @import Parser, {
6666
* Range,
6767
* Members,
68-
* DestructuringAssignmentProperty
68+
* DestructuringAssignmentProperty,
69+
* JavascriptParserState
6970
* } from "../../javascript/JavascriptParser"
7071
*/
7172
/** @import { RawRuntimeRequirements } from "../core/NullDependency" */
@@ -191,6 +192,12 @@ const importMetaVarName = (parser) => {
191192
return `__webpack_import_meta_${index}__`;
192193
};
193194

195+
/**
196+
* The binding of each parse's synthesized `import.meta`, so it is set up once per module.
197+
* @type {WeakMap<JavascriptParserState, string>}
198+
*/
199+
const importMetaObjects = new WeakMap();
200+
194201
/**
195202
* Checks whether the callee is the `import.meta.resolve` member.
196203
* @param {Expression | Super} callee call callee
@@ -413,6 +420,8 @@ class ImportMetaPlugin {
413420
* @returns {string} the object's local binding
414421
*/
415422
const getImportMetaObject = (expression) => {
423+
const existing = importMetaObjects.get(parser.state);
424+
if (existing !== undefined) return existing;
416425
const varName = importMetaVarName(parser);
417426
const knownProps = [];
418427
if (isImportMetaFieldEnabled(importMeta, "url")) {
@@ -469,6 +478,7 @@ class ImportMetaPlugin {
469478
);
470479
initDep.loc = parser.getLocation(expression);
471480
parser.state.module.addPresentationalDependency(initDep);
481+
importMetaObjects.set(parser.state, varName);
472482
return varName;
473483
};
474484
/**
@@ -482,10 +492,7 @@ class ImportMetaPlugin {
482492
preserveUnknown(importMeta) &&
483493
!IMPORT_META_FIELDS.has(members[0])
484494
) {
485-
const varName = getImportMetaObject(expression);
486-
// Alias writes create own properties; direct writes without one
487-
// still belong to the shared native import.meta.
488-
return `(Object.prototype.hasOwnProperty.call(${varName}, ${JSON.stringify(members[0])}) ? ${varName} : import.meta)${propertyAccess(members, 0)}`;
495+
return `${getImportMetaObject(expression)}${propertyAccess(members, 0)}`;
489496
}
490497
if (
491498
preserveUnknown(importMeta) ||

‎test/configCases/module/import-meta-alias-mutations/index.js‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { meta as otherMeta, readCustom } from "./other";
1+
import { meta as otherMeta, readCustom, readDirect } from "./other";
22

33
const meta = import.meta;
44

@@ -41,15 +41,19 @@ it("should keep alias mutations local to the module", () => {
4141
expect(import.meta.custom).toBe(42);
4242
});
4343

44-
it("should keep direct mutations visible through the shared prototype", () => {
45-
import.meta.shared = "HELLO";
46-
expect(meta.shared).toBe("HELLO");
47-
expect(otherMeta.shared).toBe("HELLO");
48-
const { shared } = import.meta;
49-
expect(shared).toBe("HELLO");
50-
import.meta.shared = "UPDATED";
51-
expect(otherMeta.shared).toBe("UPDATED");
52-
delete import.meta.shared;
53-
expect(meta.shared).toBeUndefined();
54-
expect(otherMeta.shared).toBeUndefined();
44+
it("should keep direct mutations local to the module", () => {
45+
import.meta.direct = "HELLO";
46+
expect(meta.direct).toBe("HELLO");
47+
expect(Object.keys(import.meta)).toContain("direct");
48+
expect(otherMeta.direct).toBeUndefined();
49+
expect(readDirect()).toBeUndefined();
50+
delete import.meta.direct;
51+
expect(meta.direct).toBeUndefined();
52+
});
53+
54+
it("should not bring back a value an alias deleted", () => {
55+
import.meta.restored = "direct";
56+
meta.restored = "alias";
57+
delete meta.restored;
58+
expect(import.meta.restored).toBeUndefined();
5559
});

‎test/configCases/module/import-meta-alias-mutations/other.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export const meta = import.meta;
66
export function readCustom() {
77
return import.meta.custom;
88
}
9+
10+
/**
11+
* @returns {string | undefined} the direct value belonging to this module
12+
*/
13+
export function readDirect() {
14+
return import.meta.direct;
15+
}

‎test/configCases/module/import-meta-distinct/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ it("should keep unknown properties observable", () => {
2222
import.meta.UNKNOWN_PROPERTY = "HELLO";
2323
const own = import.meta;
2424
expect(own.UNKNOWN_PROPERTY).toBe("HELLO");
25-
expect(getMeta().UNKNOWN_PROPERTY).toBe("HELLO");
25+
expect(getMeta().UNKNOWN_PROPERTY).toBeUndefined();
2626
});

0 commit comments

Comments
 (0)