diff --git a/package.json b/package.json index 74be7d90..9ef4b219 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "build": "VITE_GIT_HASH=`git log -1 --pretty=format:%h` vite build --config vite.config.mts", "lint": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src", "lint-fix": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --fix src", - "prebuild": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src && node sw_combine.js", + "prebuild": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src && node sw_combine.js && node scripts/copy-wasm.js", "test:all": "npm run test:prerequisites && npm run build && npm run test:web && npm run test:mobile", "test:prerequisites": "node scripts/check-prerequisites.js", "test:web": "npx playwright test -c playwright.config-local.ts --trace on", diff --git a/scripts/copy-wasm.js b/scripts/copy-wasm.js new file mode 100644 index 00000000..fed236be --- /dev/null +++ b/scripts/copy-wasm.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +const path = require('path'); + +// Create public/wasm directory if it doesn't exist +const wasmDir = path.join(__dirname, '../public/wasm'); +if (!fs.existsSync(wasmDir)) { + fs.mkdirSync(wasmDir, { recursive: true }); +} + +// Copy the WASM file from node_modules to public/wasm +const sourceFile = path.join(__dirname, '../node_modules/@jlongster/sql.js/dist/sql-wasm.wasm'); +const targetFile = path.join(wasmDir, 'sql-wasm.wasm'); + +fs.copyFileSync(sourceFile, targetFile); +console.log('WASM file copied successfully!'); \ No newline at end of file diff --git a/src/registerSQLWorker.js b/src/registerSQLWorker.js index 68b9ac4b..d728a8e9 100644 --- a/src/registerSQLWorker.js +++ b/src/registerSQLWorker.js @@ -4,7 +4,12 @@ import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend'; async function run() { console.log("----- initSqlJs"); - let SQL = await initSqlJs({ locateFile: file => file }); + let SQL = await initSqlJs({ + locateFile: file => { + // In Vite, we need to use the full URL to the WASM file + return new URL(`/node_modules/@jlongster/sql.js/dist/${file}`, import.meta.url).href; + } + }); let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend()); SQL.register_for_idb(sqlFS); diff --git a/vite.config.ts b/vite.config.ts index ac4dc310..61a62e08 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -23,7 +23,7 @@ export default defineConfig({ mainFields: ['module', 'jsnext:main', 'jsnext', 'main'], }, optimizeDeps: { - include: ['nostr-tools', 'nostr-tools/nip06', 'nostr-tools/core'], + include: ['nostr-tools', 'nostr-tools/nip06', 'nostr-tools/core', '@jlongster/sql.js'], esbuildOptions: { define: { global: 'globalThis' @@ -48,5 +48,6 @@ export default defineConfig({ } } } - } + }, + assetsInclude: ['**/*.wasm'] }); \ No newline at end of file