Created
March 19, 2026 01:12
-
-
Save 3p3r/99ea49ac7f7c03f69d81f32fea4370b6 to your computer and use it in GitHub Desktop.
webpack shim for sql.js web builds
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
| // 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