vite.config.ts 1.9 KB

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