优化electron启动速度

This commit is contained in:
ACT丶流星雨 2026-03-31 01:38:27 +08:00
parent 25d301520b
commit 674b7cf15b

View File

@ -3,6 +3,10 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import Module from "module"; import Module from "module";
// 加速 Electron 启动:跳过 GPU 信息收集,减少初始化耗时
app.commandLine.appendSwitch("disable-gpu-shader-disk-cache");
app.commandLine.appendSwitch("disable-features", "CalculateNativeWinOcclusion");
declare const __APP_VERSION__: string | undefined; declare const __APP_VERSION__: string | undefined;
/** /**
@ -164,33 +168,43 @@ function closeLoading(): void {
} }
} }
function createMainWindow(): void { function createMainWindow(): Promise<void> {
const win = new BrowserWindow({ return new Promise((resolve) => {
width: 1000, const win = new BrowserWindow({
height: 700, width: 1000,
minWidth: 800, height: 700,
minHeight: 500, minWidth: 800,
frame: false, minHeight: 500,
show: true, frame: false,
autoHideMenuBar: true, show: false,
resizable: true, autoHideMenuBar: true,
thickFrame: true, resizable: true,
}); thickFrame: true,
mainWindow = win; });
win.setMenuBarVisibility(false); mainWindow = win;
win.removeMenu(); win.setMenuBarVisibility(false);
win.removeMenu();
win.on("closed", () => { win.on("closed", () => {
mainWindow = null; mainWindow = null;
}); });
const isDev = process.env.NODE_ENV === "dev" || !app.isPackaged; win.once("ready-to-show", () => {
if (process.env.VITE_DEV) { closeLoading();
void win.loadURL("http://localhost:50188"); win.show();
} else { resolve();
const htmlPath = isDev ? path.join(process.cwd(), "data", "web", "index.html") : path.join(app.getPath("userData"), "data", "web", "index.html"); });
void win.loadFile(htmlPath);
} const isDev = process.env.NODE_ENV === "dev" || !app.isPackaged;
if (process.env.VITE_DEV) {
void win.loadURL("http://localhost:50188");
} else {
const htmlPath = isDev
? path.join(process.cwd(), "data", "web", "index.html")
: path.join(app.getPath("userData"), "data", "web", "index.html");
void win.loadFile(htmlPath);
}
});
} }
let closeServeFn: (() => Promise<void>) | undefined; let closeServeFn: (() => Promise<void>) | undefined;
@ -207,13 +221,14 @@ protocol.registerSchemesAsPrivileged([
]); ]);
app.whenReady().then(async () => { app.whenReady().then(async () => {
// 先显示加载窗口 // 立即显示加载窗口data URL + backgroundColor瞬间可见
showLoading(); showLoading();
try { try {
let servePath: string; let servePath: string;
if (app.isPackaged) { if (app.isPackaged) {
// 生产环境:从 extraResources 初始化数据到用户目录,然后从用户目录加载后端服务 // 生产环境:让出主线程一次,确保 loading 窗口渲染后再做耗时文件拷贝
await new Promise((r) => setTimeout(r, 0));
initializeData(); initializeData();
servePath = path.join(app.getPath("userData"), "data", "serve", "app.js"); servePath = path.join(app.getPath("userData"), "data", "serve", "app.js");
} else { } else {
@ -284,13 +299,11 @@ app.whenReady().then(async () => {
}); });
}); });
// 服务启动成功,关闭加载窗口,创建主窗口 // 服务启动成功,创建主窗口(主窗口 ready-to-show 时自动关闭loading
closeLoading(); await createMainWindow();
createMainWindow();
} catch (err) { } catch (err) {
console.error("[服务启动失败]:", err); console.error("[服务启动失败]:", err);
closeLoading(); await createMainWindow();
createMainWindow();
} }
}); });