From 0f510554ed561452e871e2d72597fb033c07add0 Mon Sep 17 00:00:00 2001 From: dmy Date: Thu, 15 Jan 2026 01:30:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(electron):=20=E4=BF=AE=E5=A4=8D=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E7=8E=AF=E5=A2=83=E5=90=8E=E7=AB=AF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=B9=B6=E4=BC=98=E5=8C=96=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整 electron-builder 配置文件以正确处理打包路径 修复生产环境下后端服务路径问题 添加对开发工具中 Autofill 错误信息的过滤 更新构建脚本确保前端先构建 --- app/electron-builder.json | 13 ++++++++----- app/main.js | 22 ++++++++++++++++++++-- package.json | 3 ++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/electron-builder.json b/app/electron-builder.json index 939cd9a..183a09d 100644 --- a/app/electron-builder.json +++ b/app/electron-builder.json @@ -3,15 +3,18 @@ "appId": "com.bidding.app", "directories": { "output": "dist-electron", - "app": "./app" + "app": "." }, "files": [ + "app/**/*", "dist/**/*", - "frontend/**/*", + "frontend/dist/**/*", ".env", - "node_modules/**/*", - "package.json", - "app/**/*" + "package.json" + ], + "asarUnpack": [ + "dist/**/*", + "node_modules/**/*" ], "win": { "target": "nsis", diff --git a/app/main.js b/app/main.js index 4b6529e..f1b71bb 100644 --- a/app/main.js +++ b/app/main.js @@ -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: { diff --git a/package.json b/package.json index 85e74d3..2c58d34 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "author": "", "private": true, "license": "UNLICENSED", + "main": "app/main.js", "scripts": { "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", @@ -24,7 +25,7 @@ "sync": "ts-node -r tsconfig-paths/register src/scripts/sync.ts", "deploy": "powershell -ExecutionPolicy Bypass -File src/scripts/deploy.ps1", "electron:dev": "npm run -prefix frontend build && npm run build && set NODE_ENV=development&& electron ./app", - "electron:build": "npm run build && electron-builder --config ./app/electron-builder.json" + "electron:build": "npm run -prefix frontend build && npm run build && electron-builder --config ./app/electron-builder.json" }, "dependencies": { "@nestjs/common": "^11.0.1",