Skip to content

feat: use import.meta.ROLLDOWN_FILE_URL_* for assets in JS - #22888

Merged
sapphi-red merged 3 commits into
mainfrom
feat/use-rollup-file-url-for-assets-in-js
Aug 21, 2026
Merged

feat: use import.meta.ROLLDOWN_FILE_URL_* for assets in JS#22888
sapphi-red merged 3 commits into
mainfrom
feat/use-rollup-file-url-for-assets-in-js

Conversation

@sapphi-red

@sapphi-red sapphi-red commented Jul 7, 2026

Copy link
Copy Markdown
Member

Part of the "Use of import.meta.ROLLUP_FILE_URL_referenceId" in #22709 (comment)

This change allows Rolldown plugins using import.meta.ROLLDOWN_FILE_URL_* to work well with the base option and renderBuiltUrl option. But this does not convert URL of those assets to data URL. For that #22709 (comment) is needed.
This PR migrates the asset plugin to use that approach.

@sapphi-red sapphi-red changed the title feat: use import.meta.ROLLUP_FILE_URL_* for assets in JS feat: use import.meta.ROLLDOWN_FILE_URL_* for assets in JS Jul 17, 2026
@sapphi-red
sapphi-red force-pushed the refactor/exclude-postfix-from-vite-asset branch from 8e95035 to 9e6975b Compare July 31, 2026 07:54
@sapphi-red
sapphi-red force-pushed the feat/use-rollup-file-url-for-assets-in-js branch from 2f1bf1f to 0cf8fd6 Compare July 31, 2026 07:54
@sapphi-red
sapphi-red force-pushed the refactor/exclude-postfix-from-vite-asset branch from 9e6975b to 220ccd8 Compare July 31, 2026 08:43
@sapphi-red
sapphi-red force-pushed the feat/use-rollup-file-url-for-assets-in-js branch from 0cf8fd6 to b0c338e Compare July 31, 2026 08:43
@sapphi-red sapphi-red added the p3-significant High priority enhancement (priority) label Jul 31, 2026
@sapphi-red sapphi-red added this to the 8.3 milestone Jul 31, 2026
@sapphi-red
sapphi-red marked this pull request as ready for review July 31, 2026 09:46
bluwy
bluwy previously approved these changes Aug 3, 2026
Base automatically changed from refactor/exclude-postfix-from-vite-asset to main August 21, 2026 10:11
@sapphi-red
sapphi-red force-pushed the feat/use-rollup-file-url-for-assets-in-js branch from b0c338e to 28d9165 Compare August 21, 2026 10:11
@sapphi-red
sapphi-red merged commit 4366ac4 into main Aug 21, 2026
29 of 30 checks passed
@sapphi-red
sapphi-red deleted the feat/use-rollup-file-url-for-assets-in-js branch August 21, 2026 10:18
This was referenced Sep 2, 2026
dai-shi added a commit to wakujs/waku that referenced this pull request Sep 4, 2026
Resolve server assets relative to the emitted chunk with newer Vite
versions.

Keep browser asset URLs handled by Vite.

Resolve server assets relative to the emitted chunk with newer Vite
versions.
Keep browser asset URLs handled by Vite.

## Background

`e2e/fixtures/rsc-asset` covers asset references originating inside an
RSC, in
two directions that have to resolve differently within the same `rsc`
environment:

- `new URL('./test-server.txt', import.meta.url)` + `readFileSync` needs
a URL
  the Node process can open on disk, relative to the emitted chunk.
- `./test-client.txt?no-inline` rendered as an `<a href>` needs a public
URL the
  browser can fetch (`/assets/...`).

Vite doesn't handle `new URL(..., import.meta.url)` for SSR, so the
fixture ships
its own plugin that rewrites the call into `this.emitFile()` +
`new URL(import.meta.ROLLUP_FILE_URL_<id>)`. That relied on Rollup's
default
`resolveFileUrl`, which produces `new URL('./x', import.meta.url).href`.

vitejs/vite#22888 ("use `import.meta.ROLLDOWN_FILE_URL_*` for assets in
JS")
added a `resolveFileUrl` hook to `vite:asset`. It was written for Vite's
own
emitters, but `resolveFileUrl` is a global Rollup hook, so it also
intercepts
references emitted by user plugins. Those have no `asFileUrl` metadata
attached,
so they fall through to `toOutputFilePathInJS`, which for a
server-consumer
environment always returns a base-joined path.

The fixture's server chunk therefore built as:

```js
const testServerTxtUrl = new URL("/assets/test-server-BTC3QXxI.txt");
```

`new URL()` on a root-relative path with no base throws, so the RSC
render failed
with `TypeError: Invalid URL`, `data-testid="server-file"` never
rendered, and
`[chromium-prd] rsc-asset › basic` timed out. The client `?no-inline`
half was
unaffected. Failing job: <CI job URL>

## This change

The plugin implements its own `resolveFileUrl`, scoped to the reference
IDs it
emitted, and returns `null` for everything else so Vite continues to
handle
browser asset URLs. `enforce: 'pre'` is required because
`resolveFileUrl` is a
*first* hook and `vite:asset` is registered ahead of normal user
plugins, so
without it the hook is never consulted.

Whether Vite intends to intercept third-party file references is a
question for
upstream; this fixture works either way, since its hook returns first.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p3-significant High priority enhancement (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants