Skip to content

Emit files referenced with new URL("./file", import.meta.url) in server builds #4930

Closed

Description

@pi0x

Context: electric-sql/pglite#1120, unjs/unwasm#124 (closed)

Problem

Packages built with emscripten or wasm-bindgen find their runtime files with a path relative to their own module, for example @electric-sql/pglite:

new URL("./pglite.wasm", import.meta.url)
new URL("./pglite.data", import.meta.url)
new URL("../hstore.tar.gz", import.meta.url) // every contrib extension

After Nitro bundles the package, these URLs point next to the output chunk, where the files don't exist:

ENOENT: no such file or directory, open '.output/server/_libs/pglite.data'

Vite (client builds), webpack 5 and Parcel treat this pattern as an asset reference. Nitro server builds don't, so today the only workarounds are copying the files by hand, serverAssets (base64-inlined, about 23 MB for pglite), or tracing the whole package as external. None of these need any change in pglite.

Proposal

Emit the referenced files as assets and rewrite the URLs in server builds, for presets that can read files at runtime (Node, Bun, Deno, Vercel/Netlify Node functions).

  • Rolldown: enable experimental.resolveNewUrlToAsset. I checked it with @electric-sql/[email protected] plus hstore, with no plugin: pglite.wasm, initdb.wasm, pglite.data and hstore.tar.gz are each emitted once, the relative URLs from nested chunks are correct, and the output runs from a folder without node_modules.
  • Vite builder: check how Vite handles new URL in server environments. The server build must also write the files (ssrEmitAssets) and resolve them relative to import.meta.url, not base: "/".
  • Rollup builder: either rolldown-only, or a small fallback transform. unwasm#124 had a prototype; if it's ported, fix the problems a review found:
    • keep import.meta.url as the second argument, so new URL(import.meta.ROLLUP_FILE_URL_x, import.meta.url). With Vite's default base: "/" the one-argument form throws Invalid URL.
    • parse instead of using a regex, so text inside strings and comments isn't rewritten.
    • allow a trailing comma (Prettier's multi-line style).
    • cache file reads (pglite's index.js references the 10 MB pglite.wasm three times).
    • skip dev mode, where the original URL still resolves.
  • Workers-style presets (cloudflare-* and similar): keep it off. Rewriting doesn't help there, because Workers can't compile Wasm from fetched bytes and has no filesystem. pglite on Workers needs the modules passed in (pgliteWasmModule, initdbWasmModule, fsBundle), for example from db0's pglite connector once pglite exports its binaries.

Open questions

  • resolveNewUrlToAsset is experimental. Its behaviour for JS/TS references (new URL("./worker.js", import.meta.url)) is still being discussed (resolveNewUrlToAsset breaks with TS assets rolldown/rolldown#7258); today they're copied as-is, not bundled.
  • Not yet checked with rolldown's native option: trailing commas, strings and comments, and pglite's CJS build, which uses a shim variable instead of import.meta.url. Nitro resolves the ESM entry, so that case probably doesn't come up.

Activity

  1. self-assigned this
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions