watch.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { spawn } from 'child_process'
  2. import { createServer, build } from 'vite'
  3. import electron from 'electron'
  4. import readline from 'readline'
  5. const query = new URLSearchParams(import.meta.url.split('?')[1])
  6. const debug = query.has('debug')
  7. /** The log will display on the next screen */
  8. function clearConsole() {
  9. const blank = '\n'.repeat(process.stdout.rows)
  10. console.log(blank)
  11. readline.cursorTo(process.stdout, 0, 0)
  12. readline.clearScreenDown(process.stdout)
  13. }
  14. /**
  15. * @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
  16. */
  17. function watchMain(server) {
  18. /**
  19. * @type {import('child_process').ChildProcessWithoutNullStreams | null}
  20. */
  21. let electronProcess = null
  22. const address = server.httpServer.address()
  23. const env = Object.assign(process.env, {
  24. VITE_DEV_SERVER_HOST: address.address,
  25. VITE_DEV_SERVER_PORT: address.port
  26. })
  27. /**
  28. * @type {import('vite').Plugin}
  29. */
  30. const startElectron = {
  31. name: 'electron-main-watcher',
  32. writeBundle() {
  33. clearConsole()
  34. if (electronProcess) {
  35. electronProcess.removeAllListeners()
  36. electronProcess.kill()
  37. }
  38. electronProcess = spawn(electron, ['.'], { env })
  39. electronProcess.once('exit', process.exit)
  40. // https://github.com/electron-vite/electron-vite-vue/pull/129
  41. electronProcess.stdout.on('data', data => {
  42. const str = data.toString().trim()
  43. str && console.log(str)
  44. })
  45. electronProcess.stderr.on('data', data => {
  46. const str = data.toString().trim()
  47. str && console.error(str)
  48. })
  49. }
  50. }
  51. return build({
  52. configFile: 'packages/main/vite.config.js',
  53. mode: 'development',
  54. plugins: [!debug && startElectron].filter(Boolean),
  55. build: {
  56. watch: {}
  57. }
  58. })
  59. }
  60. /**
  61. * @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
  62. */
  63. function watchPreload(server) {
  64. return build({
  65. configFile: 'packages/preload/vite.config.js',
  66. mode: 'development',
  67. plugins: [
  68. {
  69. name: 'electron-preload-watcher',
  70. writeBundle() {
  71. clearConsole()
  72. server.ws.send({ type: 'full-reload' })
  73. }
  74. }
  75. ],
  76. build: {
  77. watch: {}
  78. }
  79. })
  80. }
  81. // Block the CTRL + C shortcut on a Windows terminal and exit the application without displaying a query
  82. if (process.platform === 'win32') {
  83. readline.createInterface({ input: process.stdin, output: process.stdout }).on('SIGINT', process.exit)
  84. }
  85. // bootstrap
  86. const server = await createServer({ configFile: 'packages/renderer/vite.config.js' })
  87. await server.listen()
  88. await watchPreload(server)
  89. await watchMain(server)