watch.mjs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.ts',
  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.ts',
  66. mode: 'development',
  67. plugins: [{
  68. name: 'electron-preload-watcher',
  69. writeBundle() {
  70. clearConsole()
  71. server.ws.send({ type: 'full-reload' })
  72. },
  73. }],
  74. build: {
  75. watch: {},
  76. },
  77. })
  78. }
  79. // Block the CTRL + C shortcut on a Windows terminal and exit the application without displaying a query
  80. if (process.platform === 'win32') {
  81. readline.createInterface({ input: process.stdin, output: process.stdout }).on('SIGINT', process.exit)
  82. }
  83. // bootstrap
  84. const server = await createServer({ configFile: 'packages/renderer/vite.config.ts' })
  85. await server.listen()
  86. await watchPreload(server)
  87. await watchMain(server)