Files
mai-bot/dashboard/electron/main/index.ts
DrSmoothl e6862e227b feat(electron): scaffold electron-vite project structure
Initialize Wave 1 Electron integration with:
- electron v40.6.1, electron-vite v5.0.0, electron-builder v26.8.1
- Main entry point: electron/main/index.ts with app:// protocol registration
- Preload placeholder: electron/preload/index.ts for Wave 2 IPC
- electron.vite.config.ts with unified renderer chunk splitting strategy
- tsconfig.electron.json with ESNext module support
- Updated package.json with electron dependencies and scripts:
  - electron:dev, electron:build, electron:preview
- All original scripts (dev, build, test, etc) remain unchanged
- TypeScript compilation verified: tsc --noEmit passes
- Ready for Wave 2: IPC bridge and contextBridge exposure
2026-03-03 00:57:50 +08:00

99 lines
2.2 KiB
TypeScript

import { app, BrowserWindow, protocol } from 'electron'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
let mainWindow: BrowserWindow | null = null
/**
* Register app:// custom protocol BEFORE app.whenReady()
* This is critical for electron-vite to work correctly
*/
function registerAppProtocol() {
protocol.registerSchemesAsPrivileged([
{
scheme: 'app',
privileges: {
secure: true,
standard: true,
allowServiceWorkers: true,
},
},
])
}
/**
* Create the main application window
*/
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
nodeIntegration: false,
contextIsolation: true,
},
})
// Load the app using app:// protocol
// electron-vite will handle serving the renderer from app://host/index.html
if (process.env.VITE_DEV_SERVER_URL) {
// Development: load from electron-vite dev server
mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL)
} else {
// Production: load from bundled renderer
mainWindow.loadURL('app://host/index.html')
}
mainWindow.on('closed', () => {
mainWindow = null
})
}
/**
* Register app:// protocol handler (for production)
*/
function registerAppProtocolHandler() {
protocol.handle('app', (request) => {
const filePath = new URL(request.url).pathname
return new Response(
`Cannot handle app:// requests. Renderer should be served by electron-vite.`
)
})
}
/**
* App event: when app is ready
*/
app.on('ready', () => {
registerAppProtocolHandler()
createWindow()
})
/**
* App event: when all windows are closed (non-macOS behavior)
*/
app.on('window-all-closed', () => {
// On macOS, applications typically stay open until the user quits
if (process.platform !== 'darwin') {
app.quit()
}
})
/**
* App event: when app is activated (macOS)
*/
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
// Register protocol BEFORE app.whenReady()
registerAppProtocol()