Skip to content

Instantly share code, notes, and snippets.

@3p3r
Created March 19, 2026 01:12
Show Gist options
  • Select an option

  • Save 3p3r/99ea49ac7f7c03f69d81f32fea4370b6 to your computer and use it in GitHub Desktop.

Select an option

Save 3p3r/99ea49ac7f7c03f69d81f32fea4370b6 to your computer and use it in GitHub Desktop.
webpack shim for sql.js web builds
// Import sql-wasm as a module - webpack will bundle it
// @ts-expect-error
import initSqlJsModule from '!!file-loader!sql.js/dist/sql-wasm.js';
// @ts-expect-error - webpack will handle this as an asset
import wasmUrl from 'sql.js/dist/sql-wasm.wasm';
let sqlJsPromise: Promise<any> | null = null;
// In browser, we need to provide the WASM file URL
export default function initSqlJsWrapper(config?: any) {
if (!sqlJsPromise) {
sqlJsPromise = new Promise((resolve, reject) => {
// Load the sql-wasm.js script dynamically
const script = document.createElement('script');
script.src = initSqlJsModule;
script.onload = () => {
if (typeof window !== 'undefined' && typeof window.initSqlJs === 'function') {
resolve(window.initSqlJs);
} else {
reject(new Error('initSqlJs not found on window'));
}
};
script.onerror = reject;
document.head.appendChild(script);
});
}
return sqlJsPromise.then((initSqlJs) =>
initSqlJs({
locateFile: (_file: string) => {
// Return the webpack-resolved URL for the wasm file
return wasmUrl;
},
...config,
}),
);
}
// @ts-expect-error - re-export all named exports
export * from 'sql.js';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment