Skip to content

Commit 6c5c3dc

Browse files
committed
fix: support computed property access on import.meta with template literals
1 parent d6927b4 commit 6c5c3dc

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎.changeset/fuzzy-pigs-hide.md‎

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+
Support no-expression template literals in computed member access for `import.meta`.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,10 +4102,7 @@ class JavascriptParser extends Parser {
41024102
if (expression.object.type === "MemberExpression") {
41034103
// optimize the case where expression.object is a MemberExpression too.
41044104
// we can keep info here when calling walkMemberExpression directly
4105-
const property =
4106-
/** @type {Identifier} */
4107-
(expression.property).name ||
4108-
`${/** @type {Literal} */ (expression.property).value}`;
4105+
const property = members[members.length - 1];
41094106
name = name.slice(0, -property.length - 1);
41104107
members.pop();
41114108
const result = this.callHooksForInfo(
@@ -5223,9 +5220,10 @@ class JavascriptParser extends Parser {
52235220
const memberRanges = [];
52245221
while (expr.type === "MemberExpression") {
52255222
if (expr.computed) {
5226-
if (expr.property.type !== "Literal") break;
5227-
members.push(`${expr.property.value}`); // the literal
5228-
memberRanges.push(/** @type {Range} */ (expr.object.range)); // the range of the expression fragment before the literal
5223+
const member = this.evaluateExpression(expr.property).asString();
5224+
if (typeof member !== "string") break;
5225+
members.push(member);
5226+
memberRanges.push(/** @type {Range} */ (expr.object.range));
52295227
} else {
52305228
if (expr.property.type !== "Identifier") break;
52315229
members.push(expr.property.name); // the identifier
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
it("should support computed property access with literal string on import.meta", () => {
2+
const url = import.meta.url;
3+
expect(import.meta["url"]).toBe(url);
4+
});
5+
6+
it("should support computed property access with template string on import.meta", () => {
7+
const url = import.meta.url;
8+
expect(import.meta[`url`]).toBe(url);
9+
});
10+
11+
it("should support computed property access with string concatenation on import.meta", () => {
12+
const url = import.meta.url;
13+
expect(import.meta["u" + "rl"]).toBe(url);
14+
});
15+
16+
it("should support computed property access on import.meta.webpackHot", () => {
17+
if (import.meta.webpackHot) {
18+
expect(import.meta["webpackHot"]).toBe(import.meta.webpackHot);
19+
}
20+
});
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("../../../../types").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
devtool: false,
7+
target: "node"
8+
};

0 commit comments

Comments
 (0)