vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { defineConfig } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import electron from 'vite-plugin-electron';
  4. import renderer from 'vite-plugin-electron-renderer';
  5. import AutoImport from 'unplugin-auto-import/vite';
  6. import Components from 'unplugin-vue-components/vite';
  7. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
  8. import { resolve } from 'path';
  9. export default defineConfig(({ command }) => {
  10. const isServe = command === 'serve';
  11. const isBuild = command === 'build';
  12. return {
  13. // Electron 打包后从 file:// 加载,需使用相对路径
  14. base: isBuild ? './' : '/',
  15. resolve: {
  16. alias: {
  17. '@': resolve(__dirname, 'src'),
  18. },
  19. },
  20. plugins: [
  21. vue({
  22. template: {
  23. compilerOptions: {
  24. // 将 webview 标记为自定义元素(Electron 特有标签)
  25. isCustomElement: (tag) => tag === 'webview',
  26. },
  27. },
  28. }),
  29. AutoImport({
  30. imports: ['vue', 'vue-router', 'pinia'],
  31. resolvers: [ElementPlusResolver()],
  32. dts: 'src/auto-imports.d.ts',
  33. }),
  34. Components({
  35. resolvers: [
  36. ElementPlusResolver(),
  37. // 自动解析 Element Plus 图标组件
  38. {
  39. type: 'component',
  40. resolve: (name: string) => {
  41. // Element Plus 图标组件通常是 PascalCase 且不以 El 开头
  42. const iconNames = [
  43. 'Fold', 'Expand', 'Loading', 'DocumentCopy', 'Message', 'Close',
  44. 'Refresh', 'Delete', 'Right', 'FolderDelete', 'DataAnalysis',
  45. 'User', 'Film', 'Upload', 'Clock', 'Setting', 'Document',
  46. 'Monitor', 'TrendCharts', 'ChatDotRound', 'ArrowDown', 'ArrowLeft',
  47. 'ArrowRight', 'VideoPlay', 'CircleCheck', 'CircleClose', 'Lock',
  48. 'Picture', 'Plus', 'Search', 'Edit', 'Download', 'MoreFilled',
  49. 'MagicStick',
  50. ];
  51. if (iconNames.includes(name)) {
  52. return { name, from: '@element-plus/icons-vue' };
  53. }
  54. },
  55. },
  56. ],
  57. dts: 'src/components.d.ts',
  58. }),
  59. electron([
  60. {
  61. entry: 'electron/main.ts',
  62. onstart(options) {
  63. if (process.env.VSCODE_DEBUG) {
  64. console.log('[startup] Electron App');
  65. } else {
  66. options.startup();
  67. }
  68. },
  69. vite: {
  70. build: {
  71. sourcemap: isServe,
  72. minify: isBuild,
  73. outDir: 'dist-electron',
  74. rollupOptions: {
  75. external: ['electron'],
  76. output: {
  77. format: 'cjs',
  78. },
  79. },
  80. },
  81. },
  82. },
  83. {
  84. entry: 'electron/preload.ts',
  85. onstart(options) {
  86. options.reload();
  87. },
  88. vite: {
  89. build: {
  90. sourcemap: isServe ? 'inline' : undefined,
  91. minify: isBuild,
  92. outDir: 'dist-electron',
  93. rollupOptions: {
  94. external: ['electron'],
  95. output: {
  96. format: 'cjs',
  97. },
  98. },
  99. },
  100. },
  101. },
  102. ]),
  103. renderer(),
  104. ],
  105. css: {
  106. preprocessorOptions: {
  107. scss: {
  108. additionalData: `@use "@/styles/variables.scss" as *;`,
  109. },
  110. },
  111. },
  112. server: {
  113. port: 5173,
  114. proxy: {
  115. '/api': {
  116. target: 'http://127.0.0.1:3000',
  117. changeOrigin: true,
  118. },
  119. '/ws': {
  120. target: 'ws://127.0.0.1:3000',
  121. ws: true,
  122. },
  123. },
  124. },
  125. build: {
  126. outDir: 'dist',
  127. emptyOutDir: true,
  128. },
  129. };
  130. });