vite.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { defineConfig } from 'vite';
  2. import react from '@vitejs/plugin-react-swc';
  3. import path from 'path';
  4. // 允许通过环境变量配置部署子路径,例如 VITE_BASE_PATH=/app/
  5. const basePath = process.env.VITE_BASE_PATH || '/';
  6. export default defineConfig({
  7. base: basePath,
  8. plugins: [react()],
  9. resolve: {
  10. extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
  11. dedupe: ['react', 'react-dom', 'react-router-dom'],
  12. alias: {
  13. // 仅保留实际被业务代码使用到的包的版本别名
  14. 'sonner@2.0.3': 'sonner',
  15. 'lucide-react@0.487.0': 'lucide-react',
  16. 'class-variance-authority@0.7.1': 'class-variance-authority',
  17. '@radix-ui/react-tooltip@1.1.8': '@radix-ui/react-tooltip',
  18. '@radix-ui/react-switch@1.1.3': '@radix-ui/react-switch',
  19. '@radix-ui/react-slot@1.1.2': '@radix-ui/react-slot',
  20. '@radix-ui/react-dialog@1.1.6': '@radix-ui/react-dialog',
  21. '@': path.resolve(__dirname, './src'),
  22. },
  23. },
  24. // 开发模式:预构建所有重型依赖,避免浏览器发起数百个独立模块请求
  25. optimizeDeps: {
  26. include: [
  27. 'react',
  28. 'react-dom',
  29. 'react/jsx-runtime',
  30. 'react-router-dom',
  31. 'motion/react',
  32. 'react-markdown',
  33. 'remark-gfm',
  34. 'sonner',
  35. 'lucide-react',
  36. '@radix-ui/react-tooltip',
  37. '@radix-ui/react-switch',
  38. '@radix-ui/react-slot',
  39. '@radix-ui/react-dialog',
  40. ],
  41. },
  42. build: {
  43. // 与兼容矩阵(Chrome 80+ / Safari 14+ 等)对齐
  44. // es2019 在这些浏览器中均有良好支持
  45. target: 'es2019',
  46. outDir: 'build',
  47. // 生产构建:按职责拆分 chunk,避免单包过大
  48. rollupOptions: {
  49. output: {
  50. manualChunks: {
  51. // React 核心
  52. 'vendor-react': ['react', 'react-dom', 'react/jsx-runtime'],
  53. // 动画库(较大,单独拆出)
  54. 'vendor-motion': ['motion/react'],
  55. // Markdown 渲染(仅 Chat 页用到)
  56. 'vendor-markdown': ['react-markdown', 'remark-gfm'],
  57. // UI 基础组件
  58. 'vendor-ui': [
  59. 'sonner',
  60. 'lucide-react',
  61. '@radix-ui/react-tooltip',
  62. '@radix-ui/react-switch',
  63. '@radix-ui/react-slot',
  64. '@radix-ui/react-dialog',
  65. ],
  66. },
  67. },
  68. },
  69. },
  70. server: {
  71. port: 5173,
  72. open: true,
  73. },
  74. });