Fix black screen: wait for Vite before Electron, retry loadURL, show boot fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ivan Fontosh
2026-09-14 07:39:46 +08:00
parent 45105cd069
commit df50b6b680
6 changed files with 102 additions and 10 deletions
+35 -3
View File
@@ -3,9 +3,11 @@ import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
import http from 'node:http';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '..');
const DEV_URL = 'http://127.0.0.1:5174/';
async function buildMain() {
await build({
@@ -35,24 +37,54 @@ function electronPath() {
return path.join(root, 'node_modules', 'electron', 'cli.js');
}
function waitForUrl(url, timeoutMs = 60000) {
const start = Date.now();
return new Promise((resolve, reject) => {
const tick = () => {
const req = http.get(url, (res) => {
res.resume();
resolve(true);
});
req.on('error', () => {
if (Date.now() - start > timeoutMs) {
reject(new Error(`Timed out waiting for ${url}`));
return;
}
setTimeout(tick, 250);
});
};
tick();
});
}
await buildMain();
const vite = spawn(
process.platform === 'win32' ? 'npx.cmd' : 'npx',
['vite', '--config', 'vite.config.ts'],
['vite', '--config', 'vite.config.ts', '--host', '127.0.0.1', '--port', '5174'],
{ cwd: root, stdio: 'inherit', shell: true },
);
await new Promise((r) => setTimeout(r, 2000));
console.log('Waiting for Vite…');
await waitForUrl(DEV_URL);
console.log('Vite is ready, launching Electron…');
const elec = spawn(electronPath(), ['.'], {
cwd: root,
stdio: 'inherit',
shell: true,
env: { ...process.env, VITE_DEV_SERVER_URL: 'http://localhost:5174' },
env: { ...process.env, VITE_DEV_SERVER_URL: DEV_URL },
});
elec.on('exit', (code) => {
vite.kill();
process.exit(code ?? 0);
});
vite.on('exit', (code) => {
if (code && code !== 0) {
console.error('Vite exited with code', code);
elec.kill();
process.exit(code);
}
});