Skip to content

Commit 8c7e7c7

Browse files
Thunkarclaude
andauthored
fix(sqlite3mc-wasm): restore bundler-visible wasm resolution (#24529)
SQLite3MultipleCiphers 2.3.5 (#24293) stopped shipping the bundler-friendly build variant, so the package entry re-exports the plain sqlite3.mjs. That loader always resolves sqlite3.wasm through its Module['locateFile'] hook, which computes new URL(path, import.meta.url) with a dynamic path — invisible to bundlers. Bundled consumers (e.g. Vite apps using @aztec/kv-store/sqlite-opfs) request an unhashed sqlite3.wasm relative to the emitted chunk and 404 at runtime; dev servers are unaffected, so only production builds break. Wrap the init and inject emscriptenLocateFile (the vendored loader's supported escape hatch= resolving the wasm via a static `new URL('../vendor/jswasm/sqlite3.wasm', import.meta.url)` that bundlers detect, emit, and rewrite. Unbundled usage is unaffected: the static URL resolves to the real vendored path. Verified by building a Vite consumer against the patched dest/: the emitted worker chunk resolves the hashed wasm asset through the hook. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent f397aa5 commit 8c7e7c7

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

yarn-project/sqlite3mc-wasm/src/index.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@
22
* Re-exports sqlite3mc's ES module default (sqlite3InitModule) and the TypeScript types expected by downstream
33
* consumers. Mirrors the `@sqlite.org/sqlite-wasm` package default export — sqlite3mc is a strict API-compatible
44
* superset, so upstream types apply unchanged.
5+
*
6+
* BUNDLER COMPATIBILITY: SQLite3MultipleCiphers 2.3.5 stopped shipping the `-bundler-friendly` build variant, so
7+
* this entry re-exports the plain `sqlite3.mjs`. That loader always resolves `sqlite3.wasm` through its
8+
* `Module['locateFile']` hook, which computes `new URL(path, import.meta.url)` with a *dynamic* `path` — invisible
9+
* to bundlers, so bundled consumers request an unhashed `sqlite3.wasm` relative to the emitted chunk and 404 at
10+
* runtime (pre-2.3.5, the bundler-friendly variant carried a statically-analyzable reference instead). To stay
11+
* bundler-friendly we wrap the init and inject `emscriptenLocateFile` — the vendored loader's supported escape
12+
* hatch (`Module['locateFile']` defers to it when present on the init-module state) — resolving the wasm via a
13+
* static `new URL('…/sqlite3.wasm', import.meta.url)` that bundlers detect, emit, and rewrite. Unbundled usage is
14+
* unaffected: the static URL resolves to the real vendored path.
515
*/
6-
export { default } from '../vendor/jswasm/sqlite3.mjs';
16+
import sqlite3InitModuleUnwrapped from '../vendor/jswasm/sqlite3.mjs';
17+
18+
/** Statically analyzable wasm reference: bundlers emit the asset and rewrite this URL to its final location. */
19+
const SQLITE3_WASM_URL = new URL('../vendor/jswasm/sqlite3.wasm', import.meta.url);
20+
21+
type SqliteInitModuleState = {
22+
emscriptenLocateFile?: (path: string, prefix: string) => string;
23+
debugModule?: (...args: unknown[]) => void;
24+
};
25+
26+
const sqlite3InitModule: typeof sqlite3InitModuleUnwrapped = (...args) => {
27+
// `sqlite3.mjs` creates `globalThis.sqlite3InitModuleState` at import time and the inner Emscripten factory
28+
// captures it (and deletes the global) on init, binding it as `this` of `Module['locateFile']`. Mutate the live
29+
// object; recreate it if a previous init already consumed it (`debugModule` must exist — the factory calls it).
30+
const g = globalThis as { sqlite3InitModuleState?: SqliteInitModuleState };
31+
const state = (g.sqlite3InitModuleState ??= Object.assign(Object.create(null), {
32+
debugModule: () => {},
33+
}));
34+
state.emscriptenLocateFile = (path: string, prefix: string) =>
35+
path === 'sqlite3.wasm' ? SQLITE3_WASM_URL.href : new URL(path, prefix || import.meta.url).href;
36+
return sqlite3InitModuleUnwrapped(...args);
37+
};
38+
39+
export default sqlite3InitModule;
740
export type { Database, SAHPoolUtil, Sqlite3Static } from '@sqlite.org/sqlite-wasm';

0 commit comments

Comments
 (0)