vite.config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { defineConfig, loadEnv } from 'vite'
  2. import eslint from 'vite-plugin-eslint'
  3. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  4. import tsconfigPaths from 'vite-tsconfig-paths'
  5. import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin'
  6. import react from '@vitejs/plugin-react'
  7. import path from 'path'
  8. import { visualizer } from 'rollup-plugin-visualizer'
  9. export default defineConfig(({ mode }) => {
  10. // const env = loadEnv(mode, process.cwd(), '')
  11. // const base = env.VITE_APP_BASE_PATH || '/'
  12. const isProduction = mode === 'production'
  13. return {
  14. base: '/',
  15. plugins: [
  16. react({
  17. // 添加 React 插件的优化配置
  18. babel: {
  19. parserOpts: {
  20. plugins: ['decorators-legacy', 'classProperties']
  21. }
  22. }
  23. }),
  24. eslint({
  25. // 配置选项
  26. fix: true, // 自动修复 ESLint 错误
  27. include: ['src/**/*.js', 'src/**/*.jsx', 'src/**/*.ts', 'src/**/*.tsx'], // 检查的文件范围
  28. exclude: ['node_modules'], // 排除的文件
  29. cache: false, // 是否启用缓存
  30. emitError: true, // 是否将 ESLint 错误输出到控制台
  31. emitWarning: true // 是否将 ESLint 警告输出到控制台
  32. }),
  33. vanillaExtractPlugin({
  34. identifiers: ({ debugId }) => `${debugId}`
  35. }),
  36. tsconfigPaths(),
  37. createSvgIconsPlugin({
  38. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
  39. symbolId: 'icon-[dir]-[name]'
  40. }),
  41. isProduction &&
  42. visualizer({
  43. open: true,
  44. gzipSize: true,
  45. brotliSize: true,
  46. template: 'treemap' // 使用树形图更直观
  47. })
  48. ].filter(Boolean),
  49. server: {
  50. open: true,
  51. host: true,
  52. port: 5657,
  53. proxy: {
  54. '/api': {
  55. target: 'https://pre.yaoeasier.com/',
  56. changeOrigin: true,
  57. rewrite: (path) => path.replace(/^\/api/, ''),
  58. secure: false
  59. }
  60. }
  61. },
  62. build: {
  63. target: 'esnext',
  64. minify: 'esbuild',
  65. sourcemap: !isProduction,
  66. cssCodeSplit: true,
  67. chunkSizeWarningLimit: 1500,
  68. rollupOptions: {
  69. output: {
  70. manualChunks: {
  71. 'vendor-core': ['react', 'react-dom', 'react-router'],
  72. 'vendor-ui': [
  73. 'antd',
  74. '@ant-design/icons',
  75. '@ant-design/cssinjs',
  76. 'framer-motion',
  77. 'styled-components'
  78. ],
  79. 'vendor-utils': ['axios', 'dayjs', 'i18next', 'zustand', '@iconify/react'],
  80. 'vendor-charts': ['apexcharts', 'react-apexcharts']
  81. }
  82. }
  83. }
  84. },
  85. resolve: {
  86. alias: {
  87. '@': path.join(__dirname, './src'),
  88. '#': path.join(__dirname, './types')
  89. }
  90. },
  91. // 优化依赖预构建
  92. optimizeDeps: {
  93. include: [
  94. 'react',
  95. 'react-dom',
  96. 'react-router',
  97. 'antd',
  98. '@ant-design/icons',
  99. 'axios',
  100. 'dayjs'
  101. ],
  102. exclude: ['@iconify/react'] // 排除不需要预构建的依赖
  103. },
  104. // esbuild 优化配置
  105. esbuild: {
  106. drop: isProduction ? ['console', 'debugger'] : [],
  107. legalComments: 'none',
  108. target: 'esnext'
  109. }
  110. }
  111. })