Emit analyzable new URL(import.meta.url) for ESM module output#21194
Emit analyzable new URL(import.meta.url) for ESM module output#21194alexander-akait wants to merge 8 commits into
Conversation
🦋 Changeset detectedLatest commit: c3814e9 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 (c3814e9). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@c3814e9
yarn add -D webpack@https://pkg.pr.new/webpack@c3814e9
pnpm add -D webpack@https://pkg.pr.new/webpack@c3814e9 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21194 +/- ##
==========================================
+ Coverage 92.70% 92.71% +0.01%
==========================================
Files 588 588
Lines 64091 64188 +97
Branches 17785 17827 +42
==========================================
+ Hits 59416 59513 +97
Misses 4675 4675
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will degrade performance by 21.6%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Memory | benchmark "lodash", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
125.5 KB | 859.6 KB | -85.4% |
| ⚡ | Memory | benchmark "side-effects-reexport", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
1,208.6 KB | 765.1 KB | +57.98% |
| ⚡ | Memory | benchmark "asset-modules-bytes", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
326.8 KB | 245.5 KB | +33.07% |
| ⚡ | Memory | benchmark "many-chunks-commonjs", scenario '{"name":"mode-production","mode":"production"}' |
9.1 MB | 7.4 MB | +23.09% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing claude/ecma-bundler-analyzability-j0jjqb (c3814e9) with main (8596a28)
Footnotes
-
18 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
…ator options Avoids a watch+cache code-generation deadlock when a new URL asset is left unaffected, and renames the runtime-template helper to supportsAnalyzableEsm for reuse.
Introduces test/roundTripCases with a RoundTripTestCases runner that re-bundles each case's output through webpack and asserts non-JS assets survive.
Summary
For ESM module output, webpack rewrote
new URL("./asset", import.meta.url)intonew URL(__webpack_require__(id), __webpack_require__.b)— droppingimport.meta.urland routing the reference through runtime globals, so neither webpack nor other bundlers could statically follow the asset.When
output.moduleis enabled it now emits the canonical, universally-recognized formnew URL("./asset.<hash>.png", import.meta.url)with a literal specifier and nativeimport.meta.url:publicPath: "auto"→ path relative to the consuming chunk;publicPath(/…orscheme://…) → the resolved absolute URL.When an asset is consumed only via
new URLand the publicPath is a chunk-independent absolute string, the now-unusedmodule.exports = __webpack_require__.p + "…"wrapper (and its publicPath runtime global) is dropped — the asset is exposed asasset-url, exactly like CSS/HTML url assets. A dynamic/relative publicPath,url: "relative", and all non-module output fall back to the existing behavior.What kind of change does this PR introduce?
feat
Did you add tests for your changes?
Yes —
test/configCases/asset-modules/url-module-analyzable(auto publicPath, literal relative specifier) andurl-module-public-path(absolute publicPath, wrapper dropped), plustest/URLRoundTrip.unittest.js, which feeds the emitted bundle back into webpack and asserts thenew URLasset is re-detected and re-emitted.Does this PR introduce a breaking change?
No. Only ESM module output is affected and the analyzable form is behaviorally equivalent; CommonJS/IIFE output is unchanged.
If relevant, what needs to be documented once your changes are merged or what have you already documented?
n/a
Use of AI
Yes. Implemented with Claude Code (Anthropic): AI researched the codebase, wrote the implementation and tests, and ran the targeted suites; all changes were reviewed before submitting.
Generated by Claude Code