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

@@ -3,15 +3,18 @@
"appId": "com.bidding.app", "appId": "com.bidding.app",
"directories": { "directories": {
"output": "dist-electron", "output": "dist-electron",
"app": "./app" "app": "."
}, },
"files": [ "files": [
"app/**/*",
"dist/**/*", "dist/**/*",
"frontend/**/*", "frontend/dist/**/*",
".env", ".env",
"node_modules/**/*", "package.json"
"package.json", ],
"app/**/*" "asarUnpack": [
"dist/**/*",
"node_modules/**/*"
], ],
"win": { "win": {
"target": "nsis", "target": "nsis",

View File

@@ -36,6 +36,13 @@ function createWindow() {
// 开发环境下打开开发者工具 // 开发环境下打开开发者工具
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools(); 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', () => { mainWindow.on('closed', () => {
@@ -92,14 +99,25 @@ function waitForBackend(port = 3000, maxRetries = 30, interval = 1000) {
* 启动后端服务 * 启动后端服务
*/ */
async function startBackend() { 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)) { if (!fs.existsSync(backendPath)) {
console.error('后端服务构建文件不存在,请先执行 npm run build'); console.error('后端服务构建文件不存在,路径:', backendPath);
console.error('请先执行 npm run build');
return; return;
} }
console.log('启动后端服务:', backendPath);
// 启动后端服务 // 启动后端服务
backendProcess = spawn('node', [backendPath], { backendProcess = spawn('node', [backendPath], {
env: { env: {

View File

@@ -5,6 +5,7 @@
"author": "", "author": "",
"private": true, "private": true,
"license": "UNLICENSED", "license": "UNLICENSED",
"main": "app/main.js",
"scripts": { "scripts": {
"build": "nest build", "build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
@@ -24,7 +25,7 @@
"sync": "ts-node -r tsconfig-paths/register src/scripts/sync.ts", "sync": "ts-node -r tsconfig-paths/register src/scripts/sync.ts",
"deploy": "powershell -ExecutionPolicy Bypass -File src/scripts/deploy.ps1", "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: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": { "dependencies": {
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",