Repository navigation
fix(pglite): only build asset URLs when PGlite loads the assets - #1175
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The esbuild section of the bundler docs says to pass
pgliteWasmModule,initdbWasmModuleandfsBundlewhen PGlite cannot find its own files. This does not work when the bundle has no usableimport.meta.url. For example, esbuild replacesimport.metawith{}in IIFE output, soimport.meta.urlisundefined.PGlite still builds these URLs even when all three options are passed:
new URL('../release/pglite.data', import.meta.url)inPGlite#init, before it checksoptions.fsBundlenew URL('../release/pglite.wasm', import.meta.url)in theinstantiateWasmcallback, before it checksoptions.pgliteWasmModulenew URL('../release/initdb.wasm', import.meta.url)in theinstantiateWasmcallback ofinitdbnew URL('./x', undefined)throws, soPGlite.create()fails withTypeError: Failed to construct 'URL': Invalid URLeven though the URL is never used.Fix
The URLs are now built only when PGlite needs to load the file:
pglite.data: thenew URL(...)moved into the branch that callsgetFsBundle.pglite.wasmandinitdb.wasm:instantiateWasminpglite-utilsnow takes a function that returns the URL. It returns early when a module is passed and only calls the function otherwise.pglite-utilsis private and these are its only two callers.The
new URL('...', import.meta.url)expressions are unchanged, so bundlers that rewrite this pattern (Vite, webpack, Rollup) still find them. The background downloads instartArtifactDownloadwere already guarded by the options and are not changed.Tests
I added
tests/targets/web/chromium-iife-bundle.test.web.js. It bundlesdist/index.jswith esbuild as an IIFE, loads it in Chromium through Playwright, passes the three options as the docs describe, and runsSELECT 1. It serves files withpage.route, so it does not depend on the http-server used by the other web tests.esbuildis added as a dev dependency of@electric-sql/pglite. It is the same version tsup already uses, so the lockfile only gets a new importer entry.TypeError: Failed to construct 'URL': Invalid URL. With the fix it passes.chromium-memory.test.web.jsstill passes.pnpm test:basic(329 passed, 1 skipped),pnpm test:node,pnpm stylecheckandpnpm typecheckpass.I used the
release/files from the latestmainCI run (pglite-interim-build-files-node-v22.x) and did not rebuild the WASM. I only ran the Chromium web tests locally, not Firefox or WebKit.A patch changeset is included.