vite.config.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { rmSync } from 'node:fs'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import electron from 'vite-plugin-electron'
  5. import renderer from 'vite-plugin-electron-renderer'
  6. import pkg from './package.json'
  7. // https://vitejs.dev/config/
  8. export default defineConfig(({ command }) => {
  9. rmSync('dist-electron', { recursive: true, force: true })
  10. const isServe = command === 'serve'
  11. const isBuild = command === 'build'
  12. const sourcemap = isServe || !!process.env.VSCODE_DEBUG
  13. return {
  14. plugins: [
  15. vue(),
  16. electron([
  17. {
  18. // Main-Process entry file of the Electron App.
  19. entry: 'electron/main/index.ts',
  20. onstart(options) {
  21. if (process.env.VSCODE_DEBUG) {
  22. console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
  23. } else {
  24. options.startup()
  25. }
  26. },
  27. vite: {
  28. build: {
  29. sourcemap,
  30. minify: isBuild,
  31. outDir: 'dist-electron/main',
  32. rollupOptions: {
  33. external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
  34. },
  35. },
  36. },
  37. },
  38. {
  39. entry: 'electron/preload/index.ts',
  40. onstart(options) {
  41. // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
  42. // instead of restarting the entire Electron App.
  43. options.reload()
  44. },
  45. vite: {
  46. build: {
  47. sourcemap: sourcemap ? 'inline' : undefined, // #332
  48. minify: isBuild,
  49. outDir: 'dist-electron/preload',
  50. rollupOptions: {
  51. external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
  52. },
  53. },
  54. },
  55. }
  56. ]),
  57. // Use Node.js API in the Renderer-process
  58. renderer(),
  59. ],
  60. server: process.env.VSCODE_DEBUG && (() => {
  61. const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
  62. return {
  63. host: url.hostname,
  64. port: +url.port,
  65. }
  66. })(),
  67. clearScreen: false,
  68. }
  69. })