vite.config.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { defineConfig, loadEnv } from 'vite';
  2. import react from '@vitejs/plugin-react';
  3. import { visualizer } from 'rollup-plugin-visualizer';
  4. // https://vite.dev/config/
  5. export default defineConfig(({ mode }) => {
  6. const env = loadEnv(mode, process.cwd(), '');
  7. const apiTarget = env.VITE_API_BASE_URL || 'http://localhost:8000';
  8. return {
  9. plugins: [
  10. react(),
  11. // Bundle 分析工具(通过 ANALYZE=true 环境变量启用)
  12. process.env.ANALYZE === 'true' && visualizer({
  13. open: false,
  14. filename: 'dist/stats.html',
  15. gzipSize: true,
  16. brotliSize: true,
  17. template: 'treemap',
  18. }),
  19. ].filter(Boolean),
  20. build: {
  21. // Keep production output minified for Lighthouse and real deployments.
  22. minify: 'oxc',
  23. cssMinify: 'lightningcss',
  24. sourcemap: false,
  25. reportCompressedSize: true,
  26. // Large optional libraries are split by dynamic imports.
  27. chunkSizeWarningLimit: 600,
  28. rollupOptions: {
  29. output: {
  30. // Do not eagerly preload every dependency of the application entry.
  31. // Dynamic panels and export libraries should be fetched only when used.
  32. hoistTransitiveImports: false,
  33. manualChunks(id: string) {
  34. if (/node_modules[\\/](?:react|react-dom)[\\/]/.test(id)) {
  35. return 'react-vendor';
  36. }
  37. if (/node_modules[\\/](?:axios|zustand|uuid)[\\/]/.test(id)) {
  38. return 'utils-vendor';
  39. }
  40. },
  41. },
  42. },
  43. // 启用 CSS 代码分割
  44. cssCodeSplit: true,
  45. // 小于此阈值的资源将内联为 base64
  46. assetsInlineLimit: 4096, // 4KB
  47. },
  48. // 开发服务器配置
  49. server: {
  50. port: 5173,
  51. host: true,
  52. strictPort: true,
  53. open: false,
  54. proxy: {
  55. '/api': {
  56. target: apiTarget,
  57. changeOrigin: true,
  58. },
  59. },
  60. },
  61. preview: {
  62. port: 4173,
  63. host: true,
  64. strictPort: true,
  65. proxy: {
  66. '/api': {
  67. target: apiTarget,
  68. changeOrigin: true,
  69. },
  70. },
  71. },
  72. // 依赖预构建优化
  73. optimizeDeps: {
  74. include: [
  75. 'react',
  76. 'react-dom',
  77. 'axios',
  78. 'zustand',
  79. 'antd',
  80. '@ant-design/icons',
  81. ],
  82. // 排除不需要预构建的大型依赖
  83. exclude: [
  84. 'mammoth',
  85. ],
  86. },
  87. };
  88. });