rolldown: track native `resolveNewUrlToAsset` for `new URL(..., import.meta.url)` assets

Open
#4,931 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
12/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Active
Tech stack
javascript, typescript, wasm
Domain
build-system

Research direction

Start with src/build/plugins/asset-urls.ts in Nitro and compare its behavior with rolldown's experimental.resolveNewUrlToAsset; reproduce the three failing cases against rolldown 1.2.15 before changing anything. The issue is a tracking item: the real work spans upstream rolldown fixes and open decisions about missing files, absolute paths and JS/TS handling, so a newcomer cannot pick a single self-contained edit from it. Done means the upstream option is stable and Nitro's plugin can be removed without changing output paths.

Written by the indexing model from the issue text.

Description

pending rolldown

Follow-up of #4930. Nitro emits files referenced with new URL("./file", import.meta.url) through its own plugin (src/build/plugins/asset-urls.ts, added in #4932). Rolldown has a native option for this, experimental.resolveNewUrlToAsset, which we'd like to use instead once the problems below are fixed upstream (stabilization is tracked in rolldown/rolldown#9561).

The output file names are the same with both (assets/[name]-[hash][extname]), so switching later doesn't change output paths.

Bugs (rolldown 1.2.15)
import { rolldown } from "rolldown";

const build = (input, plugins = []) =>
  rolldown({ input, plugins, experimental: { resolveNewUrlToAsset: true } })
    .then((b) => b.generate({ format: "esm" }));

// 1. src/load.js: export const u = new URL("./m.wasm", import.meta.url).href;
await build("src/load.js", [{ name: "wasm", load: (id) => id.endsWith(".wasm") ? "export default 1" : undefined }]);

// 2. src/enc.js: export const u = new URL("./my%20file.txt", import.meta.url).href;  (file is `my file.txt`)
await build("src/enc.js");

// 3. src/query.js:
//    export const q = new URL("./a.txt?v=1", import.meta.url).href;
//    export const h = new URL("./a.txt#frag", import.meta.url).href;
await build("src/query.js");
  • Internal error when a load hook handles the referenced file. References go through resolveId (with kind: "new-url") and load of all plugins. Case 1 fails with [UNHANDLEABLE_ERROR] Something went wrong inside rolldown, please report this problem ... Encountered a module with type 'asset' during AST parsing. In Nitro, unwasm (on by default) has such a hook, so building @electric-sql/pglite fails. It builds with wasm: false.
  • Percent-encoded paths are not decoded. Case 2 fails with [UNRESOLVED_IMPORT] Could not resolve './my%20file.txt'.
  • Query and hash are dropped. Case 3 becomes new URL("assets/a-fTpYetjL.txt", import.meta.url) for both, so url.search and url.hash are lost (e.g. sprite.svg#icon).
Behavior differences (not bugs, but need a decision before switching)
  • A reference to a file that doesn't exist at build time is a hard UNRESOLVED_IMPORT error. Nitro's plugin leaves the URL as it is. A resolveId hook returning external: true for these keeps the URL, but resolveId can't be filtered by kind, so it would run in JS for every import.
  • Absolute paths are copied into the output (new URL("/etc/hosts", import.meta.url) emits assets/hosts-[hash]). Nitro's plugin skips them.
  • JS and TS files are copied as-is, without bundling (rolldown/rolldown#7258). Nitro's plugin skips them.
  • The option is experimental and may change in minor versions; rolldown/rolldown#9561 plans to rename it (discover.newUrl).

What works the same as Nitro's plugin: strings and comments are not rewritten, trailing commas and template literals without expressions work, each file is emitted once, and relative paths from nested chunks are correct. Nitro's other transforms (e.g. replace) don't change the contents of emitted files.

Opened with AI assistance.

Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.