vite.config.ts 3.4 KB

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