Browse Source

fix error loading WASM file

sql-absurd-sql
Trent Larson 2 weeks ago
parent
commit
54f269054f
  1. 2
      package.json
  2. 15
      scripts/copy-wasm.js
  3. 7
      src/registerSQLWorker.js
  4. 5
      vite.config.ts

2
package.json

@ -11,7 +11,7 @@
"build": "VITE_GIT_HASH=`git log -1 --pretty=format:%h` vite build --config vite.config.mts", "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": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src",
"lint-fix": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --fix 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:all": "npm run test:prerequisites && npm run build && npm run test:web && npm run test:mobile",
"test:prerequisites": "node scripts/check-prerequisites.js", "test:prerequisites": "node scripts/check-prerequisites.js",
"test:web": "npx playwright test -c playwright.config-local.ts --trace on", "test:web": "npx playwright test -c playwright.config-local.ts --trace on",

15
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!');

7
src/registerSQLWorker.js

@ -4,7 +4,12 @@ import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';
async function run() { async function run() {
console.log("----- initSqlJs"); 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()); let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
SQL.register_for_idb(sqlFS); SQL.register_for_idb(sqlFS);

5
vite.config.ts

@ -23,7 +23,7 @@ export default defineConfig({
mainFields: ['module', 'jsnext:main', 'jsnext', 'main'], mainFields: ['module', 'jsnext:main', 'jsnext', 'main'],
}, },
optimizeDeps: { optimizeDeps: {
include: ['nostr-tools', 'nostr-tools/nip06', 'nostr-tools/core'], include: ['nostr-tools', 'nostr-tools/nip06', 'nostr-tools/core', '@jlongster/sql.js'],
esbuildOptions: { esbuildOptions: {
define: { define: {
global: 'globalThis' global: 'globalThis'
@ -48,5 +48,6 @@ export default defineConfig({
} }
} }
} }
} },
assetsInclude: ['**/*.wasm']
}); });
Loading…
Cancel
Save