elec_rag_ui/electron-builder.config.ts
ssurui d512ad41f9 perf: 精简打包体积,砍掉从未被使用的 Piper 引擎和渲染层专用依赖
排查确认两处真实浪费:

1. resources/tts/piper/ 里除 espeak-ng-data 外的所有文件(Piper 神经
   语音模型、二进制、自带 onnxruntime、phonemize 库、tashkeel 阿拉伯语
   音标模型,合计 ~113M)从未被实际调用——detectBackend() 第一优先级
   永远命中 VITS(小雅),Piper 引擎分支代码走不到,只有它 bundle 里的
   espeak-ng-data 目录被 VITS 借用当 sherpa-onnx 校验的 dataDir。
   systemTts.ts 新增 findPiperEspeakDataDir(),VITS 分支改为只依赖这一
   个子目录,不再要求 Piper 引擎的 bin/model/config 存在。

2. electron-builder.config.ts 的 files 补充排除 onnxruntime-web(
   @huggingface/transformers 的硬依赖但项目强制走 onnxruntime-node,
   完全未用)、naive-ui/date-fns/highlight.js/lodash 系(只在
   src/renderer/ 被引用,electron-vite build 时已被 Vite 打包进
   out/renderer/*.js,主进程不需要原始 node_modules),约省 227M。

两项合计预计单包体积从 ~789M 降到 ~560M 左右,待真机重新打包安装、
实际听一次语音播报确认功能未受影响。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 00:19:14 +08:00

86 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Configuration } from 'electron-builder'
import { execSync } from 'child_process'
import { readFileSync } from 'fs'
import { resolve } from 'path'
const releaseVersion = (() => {
try {
const tag = execSync('git describe --tags --abbrev=0').toString().trim()
return tag.replace(/^v/, '')
} catch {
// 无 git 环境(PAD rsync 部署):读取 .app-version 文件
try {
const tag = readFileSync(resolve('.app-version'), 'utf-8').trim()
return tag.replace(/^v/, '')
} catch {
return '0.0.0'
}
}
})()
const config: Configuration = {
appId: 'com.elec-rag-ui.app',
extraMetadata: { version: releaseVersion },
productName: '智能电子手册',
directories: {
buildResources: 'resources',
output: 'dist'
},
// resources/icon.png 单独列出:src/main/index.ts 用相对路径 __dirname/../../resources/icon.png
// 定位窗口图标(打包后 __dirname 是 <app>/out/main),必须让它落在 <app>/resources/ 下
//
// 排除项:这几个包只在 src/renderer/ 被引用,electron-vite build 时已经被 Vite
// 静态打包进 out/renderer/*.js,主进程运行时不会去 node_modules 里 require 它们;
// onnxruntime-web 是 @huggingface/transformers 的硬依赖但项目强制走 onnxruntime-node,
// 从未被使用。排除后单包体积减少约 227M(naive-ui/date-fns/highlight.js/lodash 系
// 渲染层已打包、onnxruntime-web 完全未用,详见排查记录)。
files: [
'out/**/*',
'resources/icon.png',
'!node_modules/onnxruntime-web/**',
'!node_modules/naive-ui/**',
'!node_modules/date-fns/**',
'!node_modules/highlight.js/**',
'!node_modules/lodash/**',
'!node_modules/lodash-es/**'
],
asarUnpack: [
'node_modules/@lancedb/**',
'node_modules/apache-arrow/**',
'node_modules/onnxruntime-node/**',
'node_modules/sherpa-onnx-node/**',
'node_modules/sherpa-onnx-linux-x64/**'
],
// 打包时内嵌模型:LOCAL_MODEL_PATH=/path/to/bge-small-zh-v1.5 npm run package:mac
extraResources: [
...(process.env.LOCAL_MODEL_PATH
? [{ from: process.env.LOCAL_MODEL_PATH, to: 'models/bge-small-zh-v1.5' }]
: []),
{
from: 'models/asr/sherpa-onnx-streaming-paraformer-bilingual-zh-en',
to: 'models/asr/sherpa-onnx-streaming-paraformer-bilingual-zh-en'
},
// 内嵌离线 TTS 引擎(espeak-ng + 数据 + 依赖库),避免目标系统无 speech-dispatcher/espeak
{
from: 'resources/tts',
to: 'tts'
}
],
linux: {
// Linux 禁用 asar:sherpa-onnx-node 通过相对路径加载 sherpa-onnx-linux-x64 原生模块,
// 在 asar 归档内 __dirname 指向虚拟路径,导致 require 无法定位 .node 文件。
asar: false,
target: [
{ target: 'AppImage', arch: ['x64', 'arm64'] },
{ target: 'deb', arch: ['x64', 'arm64'] }
],
category: 'Utility'
},
// 加快 deb 打包速度:gzip 比 xz 快很多,体积略大
deb: {
compression: 'gz' as const
}
}
export default config