Skip to content

fix: keep new URL() for directory request instead of treating as asset - #14949

Draft
elecmonkey wants to merge 1 commit into
mainfrom
elecmonkey/new-url-directory
Draft

fix: keep new URL() for directory request instead of treating as asset#14949
elecmonkey wants to merge 1 commit into
mainfrom
elecmonkey/new-url-directory

Conversation

@elecmonkey

@elecmonkey elecmonkey commented Jul 26, 2026

Copy link
Copy Markdown
Member

Summary

new URL('.', import.meta.url) is a valid ESM idiom that resolves to the current module's directory (the ESM equivalent of __dirname).

Rspack's URLPlugin treated any new URL(string, import.meta.url) as an asset reference, so a directory request (., .., or a trailing-slash path) was wrongly resolved to a module and emitted as a static asset, and the expression was rewritten to point at that emitted file — breaking the directory semantics.

Fix

Skip creating a URLDependency when the request is a directory (. / .. / ends with /) and keep new URL(...) as-is, so import.meta.url is preserved and the expression evaluates to the directory at runtime.

This aligns with webpack, whose URLParserPlugin has the same guard (isDirectoryRequestkeepNewURL): https://github.com/webpack/webpack/blob/9d017167aa3d8818e23281b2683f0f081ce0a7d0/lib/url/URLParserPlugin.js#L68-L87

Reference for the standard behavior:

@github-actions

github-actions Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

📦 Binary Size-limit

Comparing af814c2 to fix(cache): scope maxVersions by compiler (#14954) by hardfist

🙈 Size remains the same at 66.80MB

@github-actions

github-actions Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Gzip Size Change Gzip Change
popular-libs 1.7 MB 552.1 KB 0 0
react-10k 5.6 MB 1.3 MB 0 0
react-1k 823.1 KB 217.0 KB 0 0
react-5k 2.7 MB 663.8 KB 0 0
ui-components 4.9 MB 1.4 MB 0 0

Generated by Rsdoctor GitHub Action

@codspeed-hq

codspeed-hq Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 46 untouched benchmarks
⏩ 47 skipped benchmarks1


Comparing elecmonkey/new-url-directory (af814c2) with main (019c3aa)

Open in CodSpeed

Footnotes

  1. 47 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.

@elecmonkey

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e771817e2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +167 to +168
if request == "." || request == ".." || request.ends_with('/') {
return Some(true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid preserving import.meta in non-module bundles

When module.parser.javascript.url uses its default or relative mode and the output is a normal non-module bundle, returning Some(true) marks the entire new URL() expression as handled, so walk_new_expression never visits the second argument and ImportMetaPlugin cannot replace import.meta.url. The generated classic-script/CommonJS .js therefore retains new URL('.', import.meta.url), which is a syntax error outside an ES module; the preservation needs to be restricted to an ESM-compatible mode or the base argument must still be processed for other modes.

Useful? React with 👍 / 👎.

@elecmonkey
elecmonkey force-pushed the elecmonkey/new-url-directory branch 2 times, most recently from 9d44987 to 4e77181 Compare July 26, 2026 18:01
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 26, 2026

Copy link
Copy Markdown

Deploying rspack with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4e77181
Status: ✅  Deploy successful!
Preview URL: https://faddedbb.rspack-v2.pages.dev
Branch Preview URL: https://elecmonkey-new-url-directory.rspack-v2.pages.dev

View logs

@elecmonkey
elecmonkey marked this pull request as ready for review July 26, 2026 18:22
@elecmonkey
elecmonkey marked this pull request as draft July 26, 2026 18:26
@elecmonkey

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 877372f705

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// `import.meta.url` handling applies.
if request == "." || request == ".." || request.ends_with('/') {
if args.len() == 2 {
parser.walk_expression(&args[1].expr);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Walk the preserved first URL argument

When the first argument evaluates to a directory through a parser-time replacement—for example, DefinePlugin({ DIR: JSON.stringify(".") }) with new URL(DIR, import.meta.url)get_url_request sees ".", but this branch returns true after walking only the base argument. Returning true suppresses the normal walk of all arguments, so DefinePlugin's expression hook never emits the replacement and the bundle retains an undefined DIR, causing a runtime ReferenceError. Since this branch preserves the expression, it must also walk args[0].

Useful? React with 👍 / 👎.

@elecmonkey
elecmonkey force-pushed the elecmonkey/new-url-directory branch from defc42a to a21c963 Compare July 28, 2026 05:13
@elecmonkey
elecmonkey force-pushed the elecmonkey/new-url-directory branch from a21c963 to af814c2 Compare July 28, 2026 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant