100 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-01-27 10:48:32 +08:00
import { spawn } from 'child_process'
import { createServer, build } from 'vite'
import electron from 'electron'
import readline from 'readline'
2021-11-11 17:52:50 +08:00
2022-03-15 08:34:36 +08:00
const query = new URLSearchParams(import.meta.url.split('?')[1])
const debug = query.has('debug')
/** The log will display on the next screen */
function clearConsole() {
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}
2021-11-11 17:52:50 +08:00
/**
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
2021-11-11 17:52:50 +08:00
*/
function watchMain(server) {
2021-11-11 17:52:50 +08:00
/**
* @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,
})
2022-03-15 08:34:36 +08:00
/**
* @type {import('vite').Plugin}
*/
const startElectron = {
name: 'electron-main-watcher',
writeBundle() {
clearConsole()
if (electronProcess) {
electronProcess.removeAllListeners()
electronProcess.kill()
electronProcess = null
}
electronProcess = spawn(electron, ['.'], { env })
electronProcess.on('exit', process.exit)
2022-05-16 21:35:57 +08:00
// https://github.com/electron-vite/electron-vite-vue/pull/129
electronProcess.stdout.on('data', (data) => {
const str = data.toString().trim()
str && console.log(str)
})
electronProcess.stderr.on('data', (data) => {
const str = data.toString().trim()
str && console.error(str)
})
2022-03-15 08:34:36 +08:00
},
}
2021-11-11 17:52:50 +08:00
2022-01-27 10:48:32 +08:00
return build({
configFile: 'packages/main/vite.config.ts',
mode: 'development',
2022-03-15 08:34:36 +08:00
plugins: [!debug && startElectron].filter(Boolean),
build: {
2022-04-15 07:35:54 +08:00
watch: {},
},
2021-11-11 17:52:50 +08:00
})
}
/**
2022-01-27 10:48:32 +08:00
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
2021-11-11 17:52:50 +08:00
*/
2022-01-27 10:48:32 +08:00
function watchPreload(server) {
return build({
configFile: 'packages/preload/vite.config.ts',
mode: 'development',
2022-01-27 10:48:32 +08:00
plugins: [{
name: 'electron-preload-watcher',
writeBundle() {
clearConsole()
2022-01-27 10:48:32 +08:00
server.ws.send({ type: 'full-reload' })
},
}],
build: {
2022-04-15 07:35:54 +08:00
watch: {},
},
2021-11-11 17:52:50 +08:00
})
}
2022-05-13 23:16:04 +08:00
// Block the CTRL + C shortcut on a Windows terminal and exit the application without displaying a query
if (process.platform === 'win32') {
readline.createInterface({ input: process.stdin, output: process.stdout }).on('SIGINT', process.exit)
}
2021-11-11 17:52:50 +08:00
// bootstrap
2022-02-08 10:34:14 +08:00
const server = await createServer({ configFile: 'packages/renderer/vite.config.ts' })
2021-11-11 17:52:50 +08:00
2022-01-27 10:48:32 +08:00
await server.listen()
await watchPreload(server)
await watchMain(server)