diff --git a/CHANGELOG.md b/CHANGELOG.md index f53857a..b798169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.15.3 (2023-11-07) + +- 20907ef chore: recover `v0.15.1` + ## 0.15.2 (2023-11-06) - 9719203 chore: better exit `electron` logic diff --git a/README.md b/README.md index 8bd1eac..16bfdc5 100644 --- a/README.md +++ b/README.md @@ -323,3 +323,10 @@ Modules in `node_modules` are not bundled during development, it's fast! │ const log = require('electron-log') │ ┗—————————————————————————————————————┛ ``` + +## FAQ + +- `electron` does not exit correctly | 使用ctrl + c 停止代码,electron后台不关闭 | [#122](https://github.com/electron-vite/vite-plugin-electron/pull/122), [#168](https://github.com/electron-vite/vite-plugin-electron/issues/168) + + * Add `tree-kill` into you dependencies, `npm i -D tree-kill`. + diff --git a/package.json b/package.json index 8b47c33..5da56e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-electron", - "version": "0.15.2", + "version": "0.15.3", "description": "Electron 🔗 Vite", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 7456975..740159e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,7 +121,7 @@ export async function startup(argv = ['.', '--no-sandbox']) { const electron = await import('electron') const electronPath = (electron.default ?? electron) - startup.exit() + await startup.exit() // Start Electron.app process.electronApp = spawn(electronPath, argv, { stdio: 'inherit' }) @@ -131,14 +131,34 @@ export async function startup(argv = ['.', '--no-sandbox']) { if (!startup.hookedProcessExit) { startup.hookedProcessExit = true - process.once('exit', startup.exit) + process.once('exit', () => { + startup.exit() + // When the process exits, `tree-kill` does not have enough time to complete execution, so `electronApp` needs to be killed immediately. + process.electronApp.kill() + }) } } startup.hookedProcessExit = false startup.exit = async () => { if (process.electronApp) { process.electronApp.removeAllListeners() - // SIGINT can be sent to the process tree #122 - process.electronApp.kill('SIGINT') + + await import('tree-kill') + .then(m => m.default( + process.electronApp.pid!, + error => error && process.electronApp.kill(), + )) + .catch(e => { + process.electronApp.kill() + + if (e.code === 'ERR_MODULE_NOT_FOUND') { + console.log( + '[vite-plugin-electron]', + 'Please install tree-kill to exit all associated processes, run "npm i tree-kill -D".', + ) + } else { + console.error(e) + } + }) } }