vite-plugin-electron: Restart does not close old Electron instance

Problem

My app has a before-quit handler which waits for the webview to be ready. But when the plugin makes Electron restart, the webview page/code is gone, so it can’t tell the app to finish quitting. This means I end up with many Electron instances running at the same time

Solution

Keep the Vite webpage loaded until the app quits

Workaround

Disable restarting:

onstart({ startup }) {
  if (process.electronApp) {
    console.log('\x1b[35mNot restarting\x1b[0m')
  } else {
    startup()
  }
}

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 26 (11 by maintainers)

Commits related to this issue

Most upvoted comments

vite-plugin-electron v0.28.8 vite v5.3.1

In mac, when I make modifications in the main process main.js, the BrowserWindow instance of electron will be re-created, What should I do?

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    electron([
      {
        entry: "electron/main.js",
      },
      {
        entry: "electron/preload.js",
      },
    ]),
    electronRenderer(),
  ],
})

Yes I do have the problem on the latest version (v0.28.7).

However, it works if I call treeKillSync in my own Vite config, or even just process.kill(process.electronApp.pid):

onstart({ startup }) {
	if (process.electronApp) {
		treeKillSync(process.electronApp.pid)
		// or
		process.kill(process.electronApp.pid)
	}
	startup()
},

It would be neat if it did a graceful shutdown with SIGTERM by waiting for the child_process to exit