vite.config.ts 3.6 KB

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