Skip to content

Commit 638d0f3

Browse files
committed
feat: support main process control hot-reload
1 parent c58fbf9 commit 638d0f3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ export default function electron(options: ElectronOptions | ElectronOptions[]):
9292
options.onstart.call(this, {
9393
startup,
9494
// Why not use Vite's built-in `/@vite/client` to implement Hot reload?
95-
// Because Vite only inserts `/@vite/client` into the `*.html` entry file.
95+
// Because Vite only inserts `/@vite/client` into the `*.html` entry file, the preload scripts are usually a `*.js` file.
9696
// @see - https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/server/middlewares/indexHtml.ts#L399
9797
reload() {
9898
if (process.electronApp) {
9999
(server.hot || server.ws).send({ type: 'full-reload' })
100+
101+
// For Electron apps that don't need to use the renderer process.
102+
startup.send('electron-vite&type=hot-reload')
100103
} else {
101104
startup()
102105
}
@@ -165,7 +168,10 @@ export async function startup(
165168
await startup.exit()
166169

167170
// Start Electron.app
168-
process.electronApp = spawn(electronPath, argv, { stdio: 'inherit', ...options })
171+
process.electronApp = spawn(electronPath, argv, {
172+
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
173+
...options,
174+
})
169175

170176
// Exit command after Electron.app exits
171177
process.electronApp.once('exit', process.exit)
@@ -175,6 +181,14 @@ export async function startup(
175181
process.once('exit', startup.exit)
176182
}
177183
}
184+
185+
startup.send = (message: string) => {
186+
if (process.electronApp) {
187+
// Based on { stdio: [,,, 'ipc'] }
188+
process.electronApp.send?.(message)
189+
}
190+
}
191+
178192
startup.hookedProcessExit = false
179193
startup.exit = async () => {
180194
if (process.electronApp) {

0 commit comments

Comments
 (0)