electric-sql / electric-sql/pglite
Bun browser bundler fails (module & fsBundle URLs rewritten to file:// URLs)
- Dominant language
- TypeScript
- Stars
- 16k
- Forks
- 442
- Avg merge
- 20h 19m
- Merged PRs (30d)
- 7
Description
Similar to https://github.com/electric-sql/pglite/issues/414, but that seems to be a different error. This could very well be a Bun bug, and I'll file an issue there too and link it here.
When attempting to use PGLite (__0.3.7__) in a frontend with Bun (__1.2.20__), the built-in bundler attempts to rewrite these `.wasm` and `.data` relative URLs (from [`pglite/packages/pglite/src/utils.ts`](https://github.com/electric-sql/pglite/blob/cab4ce64135a147f284ef5fa448ddcf17d22d666/packages/pglite/src/utils.ts#L41)):
```
const moduleUrl = new URL('../release/pglite.wasm', import.meta.url)
...
const fsBundleUrl = new URL('../release/pglite.data', import.meta.url)
```
I'm not sure exactly what the Bun browser bundler is _supposed_ to do with these, but it sets `import.meta.url` to the current `file://` ([docs](https://bun.com/docs/api/import-meta)), so we get these errors in the browser:
```
Not allowed to load local resource: file:///Users/j/bun_pglite_example/node_modules/@electric-sql/pglite/dist/pglite.wasm
Not allowed to load local resource: file:///Users/j/bun_pglite_example/node_modules/@electric-sql/pglite/dist/pglite.data
Uncaught (in promise) TypeError: Failed to fetch
Uncaught (in promise) TypeError: Failed to fetch
```
I have a minimal example to reproduce, with instructions, at https://github.com/julianenzian/bun_pglite_example, but basically:
```
import { PGlite } from "@electric-sql/pglite"
import pgliteWasmUrl from "../node_modules/@electric-sql/pglite/dist/pglite.wasm";
import pgliteDataUrl from "../node_modules/@electric-sql/pglite/dist/pglite.data";
console.log('pgliteWasmUrl:', pgliteWasmUrl);
console.log('pgliteDataUrl:', pgliteDataUrl);
const db = new PGlite('idb://my-pgdata');
```
Bun provides a (working) URL to the static assets; here's the console output:
```
pgliteWasmUrl: /_bun/asset/71cff5d3fbbb0e04.wasm
pgliteDataUrl: /_bun/asset/6be4333aadac69d8.data
```
I'm not sure if this is common / standard for WASM / etc, but I think this could work if PGlite accepted URLs to these instead of building its own. Thoughts? Happy to plumb it through if it sounds worthwhile, perhaps it would fix some other cases.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.