ai处理打包后白屏的问题

This commit is contained in:
chenqiang 2025-08-12 18:17:07 +08:00
parent 7e8c417c48
commit 6a81060e77
3 changed files with 61 additions and 49 deletions

View File

@ -1,5 +1,6 @@
// 确保所有导入都使用 ES 模块语法
import { app, BrowserWindow, ipcMain, dialog } from "electron";
import { fs } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { migrateDatabases } from './db/migration.js';
@ -82,6 +83,7 @@ function createWindow() {
preload: path.join(__dirname, "../electron/preload.js"),
nodeIntegration: false,
contextIsolation: true,
openDevTools: true,
},
});
@ -91,7 +93,7 @@ function createWindow() {
if (process.env.NODE_ENV === "development") {
mainWindow.loadURL("http://localhost:5173/");
mainWindow.webContents.openDevTools();
} else {
} else {
// 使用应用路径来构建更可靠的加载路径
const appPath = app.getAppPath();
console.log(`应用路径: ${appPath}`);
@ -110,32 +112,30 @@ function createWindow() {
// 检查哪个路径存在
for (const pathOption of pathsToTry) {
console.log(`尝试检查路径: ${pathOption}`);
try {
if (fs.existsSync(pathOption)) {
indexPath = pathOption;
console.log(`找到文件: ${indexPath}`);
break;
}
} catch (error) {
console.error(`检查路径时出错: ${error}`);
}
}
if (indexPath) {
try {
// 使用file://协议加载本地文件,避免路径问题
mainWindow.loadFile(indexPath);
// 添加加载完成事件监听
mainWindow.webContents.on('did-finish-load', () => {
console.log('文件加载完成');
// 显示开发者工具便于调试
mainWindow.webContents.openDevTools();
});
// 添加加载失败事件监听
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
console.error(`文件加载失败: ${errorCode} - ${errorDescription}`);
dialog.showErrorBox('加载失败', `无法加载应用界面: ${errorDescription}`);
});
// 添加页面崩溃事件监听
mainWindow.webContents.on('crashed', (event, killed) => {
console.error(`页面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
dialog.showErrorBox('页面崩溃', `应用界面崩溃: ${killed ? '被杀死' : '意外崩溃'}`);
});
} catch (error) {
console.error(`加载文件时出错: ${error}`);
dialog.showErrorBox('加载失败', `无法加载应用界面: ${error.message}`);
@ -144,7 +144,8 @@ function createWindow() {
console.error(`所有路径都不存在: ${pathsToTry.join(', ')}`);
dialog.showErrorBox('文件不存在', `无法找到应用界面文件,请检查打包配置`);
}
}
}
// 添加窗口关闭前的确认对话框
mainWindow.on('close', (event) => {

View File

@ -1,4 +1,4 @@
// 将导入createWebHistory改为createWebHashHistory
// 确保使用createWebHashHistory
import { createRouter, createWebHashHistory } from 'vue-router'
import WelcomeView from '@/views/WelcomeView.vue'
import ExamingView from '@/views/user/ExamingView.vue'

View File

@ -2,10 +2,11 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import electron from 'vite-plugin-electron'
// 只保留一个 fileURLToPath 导入
import { fileURLToPath } from 'url';
export default defineConfig({
// 添加base配置确保静态资源使用相对路径
base: './',
plugins: [
vue(),
vueDevTools(),
@ -22,4 +23,14 @@ export default defineConfig({
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
build: {
// 确保打包后的文件使用相对路径
rollupOptions: {
output: {
assetFileNames: 'assets/[name]-[hash].[ext]',
chunkFileNames: 'chunks/[name]-[hash].js',
entryFileNames: 'entry-[name]-[hash].js',
}
}
}
})