fix(service-party-app): 修复sql.js在打包后找不到模块的问题

## 问题
打包后的应用运行时报错 "Cannot find module 'sql.js'"

## 解决方案

### 1. electron-builder 配置
- 添加 `asarUnpack` 配置,将 sql.js 解压到 asar.unpacked 目录
- 将 `node_modules/sql.js/**/*` 添加到 files 列表

### 2. database.ts 修改
- 添加 `getSqlJsWasmPath()` 函数,根据环境返回正确的 WASM 路径
- 开发环境: node_modules/sql.js/dist/sql-wasm.wasm
- 生产环境: app.asar.unpacked/node_modules/sql.js/dist/sql-wasm.wasm
- 使用 locateFile 配置指定 WASM 文件位置

🤖 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:41:01 -08:00
parent f5cbc855f6
commit 761e03ebb0
3 changed files with 42 additions and 3 deletions

View File

@ -478,7 +478,8 @@
"Bash(git commit -m \"$\\(cat <<''EOF''\nfeat\\(service-party-app\\): 添加 Windows 一键编译脚本\n\n添加 build-windows.bat 脚本,支持:\n- 检查 Node.js 和 Go 环境\n- 编译 TSS 子进程 \\(tss-party.exe\\)\n- 安装 npm 依赖\n- 编译 Electron 应用\n\n使用方法: 双击运行 build-windows.bat\n\n🤖 Generated with [Claude Code]\\(https://claude.com/claude-code\\)\n\nCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>\nEOF\n\\)\")",
"Bash(./node_modules/.bin/tsc:*)",
"Bash(npm ls:*)",
"Bash(npm run build:win:*)"
"Bash(npm run build:win:*)",
"Bash(npm run clean:*)"
],
"deny": [],
"ask": []

View File

@ -6,6 +6,23 @@ import initSqlJs from 'sql.js';
import type { Database as SqlJsDatabase, SqlJsStatic } from 'sql.js';
import { v4 as uuidv4 } from 'uuid';
// =============================================================================
// sql.js WASM 文件路径
// =============================================================================
function getSqlJsWasmPath(): string {
// 在开发环境中WASM 文件在 node_modules 中
// 在生产环境中WASM 文件被解压到 app.asar.unpacked 目录
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');
}
}
// =============================================================================
// 数据库路径
// =============================================================================
@ -89,8 +106,24 @@ export class DatabaseManager {
*
*/
private async initialize(): Promise<void> {
// 获取 WASM 文件路径
const wasmPath = getSqlJsWasmPath();
console.log('[Database] WASM path:', wasmPath);
console.log('[Database] WASM exists:', fs.existsSync(wasmPath));
// 初始化 sql.js (加载 WASM)
this.SQL = await initSqlJs();
// 如果 WASM 文件不存在,让 sql.js 自己处理(会尝试从 CDN 加载或使用内置版本)
const config: { locateFile?: (file: string) => string } = {};
if (fs.existsSync(wasmPath)) {
config.locateFile = (file: string) => {
if (file === 'sql-wasm.wasm') {
return wasmPath;
}
return file;
};
}
this.SQL = await initSqlJs(config);
// 如果数据库文件存在,加载它
if (fs.existsSync(this.dbPath)) {

View File

@ -66,10 +66,15 @@
"buildResources": "resources",
"output": "release"
},
"asar": true,
"asarUnpack": [
"node_modules/sql.js/**/*"
],
"files": [
"dist/**/*",
"dist-electron/**/*",
"wasm/**/*"
"wasm/**/*",
"node_modules/sql.js/**/*"
],
"extraResources": [
{