mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-01-19 20:07:06 +08:00
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
import { spawn } from 'child_process'
|
|
import { createServer, build } from 'vite'
|
|
import electron from 'electron'
|
|
|
|
/**
|
|
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
|
|
*/
|
|
function watchMain(server) {
|
|
/**
|
|
* @type {import('child_process').ChildProcessWithoutNullStreams | null}
|
|
*/
|
|
let electronProcess = null
|
|
const address = server.httpServer.address()
|
|
const env = Object.assign(process.env, {
|
|
VITE_DEV_SERVER_HOST: address.address,
|
|
VITE_DEV_SERVER_PORT: address.port,
|
|
})
|
|
|
|
return build({
|
|
configFile: 'packages/main/vite.config.ts',
|
|
mode: 'development',
|
|
plugins: [{
|
|
name: 'electron-main-watcher',
|
|
writeBundle() {
|
|
electronProcess && electronProcess.kill()
|
|
electronProcess = spawn(electron, ['.'], { stdio: 'inherit', env })
|
|
},
|
|
}],
|
|
build: {
|
|
watch: true,
|
|
},
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
|
|
*/
|
|
function watchPreload(server) {
|
|
return build({
|
|
configFile: 'packages/preload/vite.config.ts',
|
|
mode: 'development',
|
|
plugins: [{
|
|
name: 'electron-preload-watcher',
|
|
writeBundle() {
|
|
server.ws.send({ type: 'full-reload' })
|
|
},
|
|
}],
|
|
build: {
|
|
watch: true,
|
|
},
|
|
})
|
|
}
|
|
|
|
// bootstrap
|
|
const server = await createServer({ configFile: 'packages/renderer/vite.config.ts' })
|
|
|
|
await server.listen()
|
|
await watchPreload(server)
|
|
await watchMain(server)
|