-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split custom electron plugin to separate files.
- Loading branch information
1 parent
71e7598
commit dcae8aa
Showing
4 changed files
with
108 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { ViteDevServer } from 'vite'; | ||
import { spawn, type ChildProcess } from 'node:child_process'; | ||
import electronPath from 'electron'; | ||
|
||
export class ViteElectronApp { | ||
current!: ChildProcess; | ||
|
||
busy = false; | ||
attemptedStartWhenBusy = false; | ||
|
||
constructor(public server: ViteDevServer) { | ||
this.start(); | ||
} | ||
|
||
/** Attempts to kill the current instance, then starts a new instace of the electron app. */ | ||
start() { | ||
if (this.busy) { | ||
this.attemptedStartWhenBusy = true; | ||
return; | ||
} | ||
this.busy = true; | ||
this.close(); | ||
|
||
// Spawn new electron process | ||
// eslint-disable-next-line @typescript-eslint/no-base-to-string | ||
this.current = spawn(String(electronPath), ['--inspect=9223', '.'], { stdio: 'inherit' }); | ||
|
||
// Stops the watch script when the application has been quit | ||
this.current.addListener('exit', () => { | ||
this.server.close().catch(console.error); | ||
}); | ||
|
||
this.busy = false; | ||
this.restartIfRequired(); | ||
} | ||
|
||
/** Kills the current instance */ | ||
close(restart = false) { | ||
if (!this.current) return; | ||
|
||
this.busy = true; | ||
|
||
this.current.removeAllListeners('exit'); | ||
this.current.on('exit', () => { | ||
this.busy = false; | ||
if (restart) this.start(); | ||
}); | ||
this.current.kill('SIGINT'); | ||
} | ||
|
||
restartIfRequired() { | ||
if (!this.attemptedStartWhenBusy) return; | ||
|
||
console.warn('... restarting because required...'); | ||
this.attemptedStartWhenBusy = false; | ||
this.close(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { PluginOption } from 'vite'; | ||
import { ViteElectronApp } from './ElectronViteApp'; | ||
import path from 'node:path'; | ||
import { readFile } from 'node:fs/promises'; | ||
|
||
export function viteElectronAppPlugin(): PluginOption { | ||
let electronApp: ViteElectronApp; | ||
|
||
return { | ||
name: 'Electron App reload on save', | ||
apply: 'serve', | ||
async hotUpdate({ file }) { | ||
if (this.environment.name !== 'client') return; | ||
console.debug('hotUpdate'); | ||
|
||
// Reload app on file change | ||
const relative = path.relative('.', file); | ||
|
||
// Invalidate modules manually | ||
const json = await readFile(path.join(process.cwd(), 'tsconfig.build.json')); | ||
const parsed: unknown = JSON.parse(json.toString()); | ||
|
||
if (typeof parsed === 'object' && parsed && 'include' in parsed && Array.isArray(parsed.include)) { | ||
for (const include of parsed.include) { | ||
console.log(include); | ||
if (typeof include === 'string' && path.matchesGlob(relative, include)) electronApp.start(); | ||
} | ||
} | ||
return []; | ||
}, | ||
configureServer(server) { | ||
electronApp = new ViteElectronApp(server); | ||
}, | ||
buildEnd() { | ||
electronApp.close(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters