fix: support no-expression template literals in computed member access#20889
Conversation
`import.meta[`url`]` was falling through to runtime member access while `import.meta["url"]` was statically resolved. Both are statically analyzable strings — the only difference is the AST node type (TemplateLiteral vs Literal). In `extractMemberExpressionChain`, accept a TemplateLiteral with no expressions in addition to a Literal, pushing `quasis[0].value.cooked`. BinaryExpression and other arbitrary expressions are intentionally NOT evaluated here — concatenations like `obj["c" + "lass"]` are sometimes written specifically to opt out of webpack's static analysis (see `test/configCases/css/css-modules/use-style.js`), so evaluating them would change semantics. In `walkMemberExpressionWithExpressionName`, read the property string from the already-extracted `members` array. The previous code looked at `expression.property.name || expression.property.value`, which doesn't work for a TemplateLiteral node (it has neither field).
🦋 Changeset detectedLatest commit: 2995319 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is packaged and the instant preview is available (5bcf0e2). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@5bcf0e2
yarn add -D webpack@https://pkg.pr.new/webpack@5bcf0e2
pnpm add -D webpack@https://pkg.pr.new/webpack@5bcf0e2 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #20889 +/- ##
==========================================
+ Coverage 82.98% 91.33% +8.35%
==========================================
Files 530 562 +32
Lines 53890 55609 +1719
Branches 14236 14700 +464
==========================================
+ Hits 44721 50791 +6070
+ Misses 9169 4818 -4351
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes webpack’s static member-chain analysis to treat no-expression template literals (e.g. obj[`prop`]) as statically known strings in computed member access, aligning behavior with quoted string literals (e.g. obj["prop"]).
Changes:
- Extend
extractMemberExpressionChainto acceptTemplateLiteralproperties with zero expressions and extract their cooked string value. - Update
walkMemberExpressionWithExpressionNameto derive the current property segment from the already-extractedmembersarray (supportsTemplateLiteralnodes). - Add a parsing config-case that asserts
import.meta["url"]andimport.meta[`url`]both resolve consistently.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/javascript/JavascriptParser.js |
Enables static member-chain extraction for computed properties using no-expression template literals and fixes property derivation during member-walk optimization. |
test/configCases/parsing/import-meta-computed/index.js |
Adds regression tests covering computed access via both string literal and template literal on import.meta. |
test/configCases/parsing/import-meta-computed/webpack.config.js |
Adds the config-case harness for the new parsing test. |
.changeset/fuzzy-pigs-hide.md |
Adds a patch changeset describing the behavior change. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Merging this PR will degrade performance by 53.95%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Memory | benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
232 KB | 163.8 KB | +41.67% |
| ⚡ | Memory | benchmark "many-chunks-commonjs", scenario '{"name":"mode-development","mode":"development"}' |
1,297.3 KB | 809.8 KB | +60.2% |
| ❌ | Memory | benchmark "devtool-eval-source-map", scenario '{"name":"mode-development","mode":"development"}' |
1.1 MB | 1.4 MB | -21.26% |
| ❌ | Memory | benchmark "devtool-eval", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
261.5 KB | 372.9 KB | -29.87% |
| ❌ | Memory | benchmark "json-modules", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
211.4 KB | 459.1 KB | -53.95% |
| ❌ | Memory | benchmark "asset-modules-inline", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
153.2 KB | 219.8 KB | -30.31% |
Comparing claude/fix-pr-20607-6FBRi (2995319) with main (45b2358)
fixes #20607