fix(service-party-app): 修复sql.js在打包后无法加载的问题

问题: Electron打包后sql.js模块报"Cannot find module"错误

解决方案:
1. 使用extraResources将sql-wasm.wasm复制到resources目录
2. 修改database.ts使用wasmBinary方式加载WASM文件
3. 直接读取WASM文件作为ArrayBuffer,避免模块解析问题

修改的文件:
- package.json: 添加extraResources配置复制WASM文件
- database.ts: 使用fs.readFileSync读取WASM并传递给initSqlJs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-28 20:50:28 -08:00
parent 761e03ebb0
commit a2508ab0fd
2 changed files with 24 additions and 10 deletions

View File

@ -11,15 +11,15 @@ import { v4 as uuidv4 } from 'uuid';
// =============================================================================
function getSqlJsWasmPath(): string {
// 在开发环境中WASM 文件在 node_modules 中
// 在生产环境中WASM 文件被解压到 app.asar.unpacked 目录
// 在生产环境中WASM 文件被复制到 extraResources 目录
const isDev = !app.isPackaged;
if (isDev) {
// 开发环境: 使用 node_modules 中的文件
return path.join(__dirname, '../../node_modules/sql.js/dist/sql-wasm.wasm');
} else {
// 生产环境: 使用 asar.unpacked 中的文件
return path.join(process.resourcesPath, 'app.asar.unpacked/node_modules/sql.js/dist/sql-wasm.wasm');
// 生产环境: 使用 extraResources 中的文件
return path.join(process.resourcesPath, 'sql-wasm.wasm');
}
}
@ -108,17 +108,28 @@ export class DatabaseManager {
private async initialize(): Promise<void> {
// 获取 WASM 文件路径
const wasmPath = getSqlJsWasmPath();
console.log('[Database] App packaged:', app.isPackaged);
console.log('[Database] Resources path:', process.resourcesPath);
console.log('[Database] WASM path:', wasmPath);
console.log('[Database] WASM exists:', fs.existsSync(wasmPath));
// 初始化 sql.js (加载 WASM)
// 如果 WASM 文件不存在,让 sql.js 自己处理(会尝试从 CDN 加载或使用内置版本)
const config: { locateFile?: (file: string) => string } = {};
// 使用 wasmBinary 直接加载 WASM 文件,这在打包环境中更可靠
let config: { wasmBinary?: ArrayBuffer; locateFile?: (file: string) => string } = {};
if (fs.existsSync(wasmPath)) {
// 直接读取 WASM 文件作为 ArrayBuffer - 这种方式更可靠
const wasmBuffer = fs.readFileSync(wasmPath);
config.wasmBinary = wasmBuffer.buffer.slice(
wasmBuffer.byteOffset,
wasmBuffer.byteOffset + wasmBuffer.byteLength
);
console.log('[Database] WASM loaded as binary, size:', wasmBuffer.length);
} else {
console.warn('[Database] WASM file not found, sql.js will try to load from default location');
// 作为备用方案,使用 locateFile
config.locateFile = (file: string) => {
if (file === 'sql-wasm.wasm') {
return wasmPath;
}
console.log('[Database] locateFile called for:', file);
return file;
};
}

View File

@ -73,8 +73,7 @@
"files": [
"dist/**/*",
"dist-electron/**/*",
"wasm/**/*",
"node_modules/sql.js/**/*"
"wasm/**/*"
],
"extraResources": [
{
@ -83,6 +82,10 @@
"filter": [
"**/*"
]
},
{
"from": "node_modules/sql.js/dist/sql-wasm.wasm",
"to": "sql-wasm.wasm"
}
],
"win": {