Repository navigation
Commit fa2038a
fix(new-url): keep the query and fragment when rewriting
Fixes case 3 of #11325. Case 1 is #11418 and case 2 is #11419; this one
is independent of both.
## Problem
With `experimental.resolveNewUrlToAsset`, the query and fragment were
dropped when `new URL()` was rewritten:
```js
new URL("./sprite.svg?v=1", import.meta.url) // -> new URL("assets/sprite-[hash].svg", import.meta.url)
new URL("./sprite.svg#icon", import.meta.url) // -> new URL("assets/sprite-[hash].svg", import.meta.url)
```
so `url.search` and `url.hash` were lost, which breaks uses like SVG
sprite fragments.
## Fix
In the `new URL()` finalizer, after computing the asset path, append the
part of the original specifier that follows the path (everything from
the first `?` or `#`, using `clean_url`). This matches Vite's
`vite:asset-import-meta-url`, which keeps the postfix when it rewrites
the URL. Resolution and the emitted asset are unchanged: all of `?v=1`,
`#icon`, `?v=1#icon` and the plain path still share one emitted file.
## Tests
New fixture `topics/new_url/keep_query_and_hash` covering a query, a
fragment, both, and the plain path. With snapshot updates disabled
(`INSTA_UPDATE=no CI=true`) it fails on `main` and passes here. No
existing snapshot changed.
`cargo test -p rolldown` passes except `test262_module_code`, which
fails the same way on `main` locally because the `test262` submodule is
not checked out. `cargo fmt --check` and `cargo clippy -p rolldown
--all-targets` are clean.
AI disclosure: I used Claude Code to help trace the code path and write
the change and test. I reviewed the change and ran the tests above
myself.
Co-authored-by: Yunfei He <i.heyunfei@gmail.com>new URL() (#11420)1 parent 3ebda9a commit fa2038a
5 files changed
Lines changed: 37 additions & 1 deletion
File tree
- crates/rolldown
- src/module_finalizers
- tests/rolldown/topics/new_url/keep_query_and_hash
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
1505 | 1506 | | |
1506 | 1507 | | |
1507 | 1508 | | |
1508 | | - | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
1509 | 1513 | | |
1510 | 1514 | | |
1511 | 1515 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments