Files
studio-desktop/infrastructure/viteElectronAppPlugin.ts
filteredandGitHub 231b3821dc [Dev] Improve debugging & launching (#887)
* Allow vite build --watch without app launch

* Use default frontend dev URL in env example

* Fix regression in yarn start

* Add native vscode debugging and build watch tasks

* Update README with new debugging and build info

* Fix app won't start in vscode debugger
2025-02-12 10:02:57 +11:00

42 lines
1.3 KiB
TypeScript

import electronPath from 'electron';
import { type ChildProcess, spawn } from 'node:child_process';
import type { PluginOption, UserConfig } from 'vite';
/**
* Loads the electron app whenever vite is loaded in watch mode.
* Reloads the app after the bundle has been written and closed.
*
* Only operates in watch mode.
*/
export function viteElectronAppPlugin(): PluginOption {
const startApp = () => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
electronApp = spawn(String(electronPath), ['--inspect=9223', '.'], { stdio: 'inherit' });
// eslint-disable-next-line unicorn/no-process-exit
electronApp.addListener('exit', () => process.exit());
};
let electronApp: ChildProcess | null = null;
let mode: string | undefined;
return {
name: 'Load Electron app in watch mode',
apply: 'build',
config(config: UserConfig) {
mode = config.mode;
},
buildStart() {
// Only operate in watch mode.
if (this.meta.watchMode !== true || !electronApp) return;
electronApp.removeAllListeners();
electronApp.kill('SIGINT');
electronApp = null;
},
closeBundle() {
// Only operate in watch mode.
if (this.meta.watchMode === true && mode === 'startwatch') startApp();
},
};
}