fix: don't resolve new URL() directory references as modules#21312
Conversation
`new URL('./', import.meta.url)` (and '.', '..', any trailing-slash
request) points at a directory, not an asset. Resolving it as a module
either errored or bundled the directory's entry with a wrong URL. Keep
the construction as-is and evaluate import.meta.url to the runtime base
URI, matching Node and browsers.
Closes #16878
🦋 Changeset detectedLatest commit: dfbc910 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 |
Types CoverageCoverage after merging fix/url-import-meta-directory into main will be
Coverage Report |
|
This PR is packaged and the instant preview is available (c2628cf). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@c2628cf
yarn add -D webpack@https://pkg.pr.new/webpack@c2628cf
pnpm add -D webpack@https://pkg.pr.new/webpack@c2628cf |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21312 +/- ##
==========================================
- Coverage 92.86% 92.85% -0.02%
==========================================
Files 594 594
Lines 65210 65218 +8
Branches 18143 18145 +2
==========================================
Hits 60559 60559
- Misses 4651 4659 +8
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:
|
There was a problem hiding this comment.
Pull request overview
Fixes webpack’s handling of new URL(<dir-like>, import.meta.url) so directory references (e.g. "./", ".", "..", or trailing-slash paths) are no longer resolved as modules/assets. Instead, webpack leaves the new URL(...) expression intact and only replaces import.meta.url with the runtime base URI, aligning behavior with Node and browsers.
Changes:
- Add directory-request detection in
URLParserPluginand short-circuit to keepnew URL(...)untouched (while still rewritingimport.meta.urltoRuntimeGlobals.baseURI). - Add a config-case test covering
"./",".","../", and"./folder/"directory-like inputs. - Add a patch changeset documenting the behavior change.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/url/URLParserPlugin.js |
Detects directory-like requests and preserves new URL(...) at runtime by only substituting import.meta.url with the runtime base URI. |
test/configCases/url/import-meta-url-directory/index.js |
New test entry asserting directory references remain runtime new URL(...) and resolve to URLs ending with / (or /folder/). |
test/configCases/url/import-meta-url-directory/webpack.config.js |
Adds the minimal config-case setup (web target) for the new test. |
.changeset/url-import-meta-directory.md |
Patch changeset describing the pass-through behavior for directory references. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Merging this PR will not alter performance
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
Summary
Closes #16878.
new URL("./", import.meta.url)(and.,.., or any trailing-slash request) points at a directory, not an asset. Webpack resolved it as a module, which either errored (Parsed request is a directory) or bundled the directory's entry module with a wrong content-hash URL. Now such directory references are kept as-is and onlyimport.meta.urlis evaluated to the runtime base URI, matching Node and browsers.What kind of change does this PR introduce?
fix
Did you add tests for your changes?
Yes —
test/configCases/url/import-meta-url-directory.Does this PR introduce a breaking change?
No. Only affects directory requests that previously failed to build (or produced a wrong URL); dynamic/context
new URL(...)and static asset references are unchanged.If relevant, what needs to be documented once your changes are merged or what have you already documented?
n/a
Use of AI
AI (Claude Code) was used to help locate the relevant code in
URLParserPlugin, draft the fix and test case, and run the suite. All changes were reviewed and verified by me.Generated by Claude Code