fix(electron): 修复生产环境后端路径问题并优化构建配置

调整 electron-builder 配置文件以正确处理打包路径
修复生产环境下后端服务路径问题
添加对开发工具中 Autofill 错误信息的过滤
更新构建脚本确保前端先构建
This commit is contained in:
dmy
2026-01-15 01:30:47 +08:00
parent 5a7cbc6daa
commit 0f510554ed
3 changed files with 30 additions and 8 deletions

View File

@@ -36,6 +36,13 @@ function createWindow() {
// 开发环境下打开开发者工具
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools();
// 过滤掉 DevTools 的 Autofill 相关错误
mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => {
if (message.includes('Autofill.enable') || message.includes('Autofill.setAddresses')) {
event.preventDefault();
}
});
}
mainWindow.on('closed', () => {
@@ -92,14 +99,25 @@ function waitForBackend(port = 3000, maxRetries = 30, interval = 1000) {
* 启动后端服务
*/
async function startBackend() {
const backendPath = path.join(__dirname, '..', 'dist', 'main.js');
let backendPath;
if (process.resourcesPath) {
// 生产环境:使用 app.asar.unpacked 中的文件
backendPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'dist', 'main.js');
} else {
// 开发环境
backendPath = path.join(__dirname, '..', 'dist', 'main.js');
}
// 检查后端构建文件是否存在
if (!fs.existsSync(backendPath)) {
console.error('后端服务构建文件不存在,请先执行 npm run build');
console.error('后端服务构建文件不存在,路径:', backendPath);
console.error('请先执行 npm run build');
return;
}
console.log('启动后端服务:', backendPath);
// 启动后端服务
backendProcess = spawn('node', [backendPath], {
env: {