Repository navigation
Description
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 extensionAfter 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]plushstore, with no plugin:pglite.wasm,initdb.wasm,pglite.dataandhstore.tar.gzare each emitted once, the relative URLs from nested chunks are correct, and the output runs from a folder withoutnode_modules. - Vite builder: check how Vite handles
new URLin server environments. The server build must also write the files (ssrEmitAssets) and resolve them relative toimport.meta.url, notbase: "/". - 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.urlas the second argument, sonew URL(import.meta.ROLLUP_FILE_URL_x, import.meta.url). With Vite's defaultbase: "/"the one-argument form throwsInvalid 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.jsreferences the 10 MBpglite.wasmthree times). - skip dev mode, where the original URL still resolves.
- keep
- 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
resolveNewUrlToAssetis experimental. Its behaviour for JS/TS references (new URL("./worker.js", import.meta.url)) is still being discussed (resolveNewUrlToAssetbreaks 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