vite.config.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import legacy from '@vitejs/plugin-legacy';
  2. import react from '@vitejs/plugin-react';
  3. import { defineConfig, loadEnv } from 'vite';
  4. import removeConsole from 'vite-plugin-remove-console';
  5. import topLevelAwait from 'vite-plugin-top-level-await';
  6. import wasm from 'vite-plugin-wasm';
  7. import { viteBuildInfo } from './build/buildInfo';
  8. import { configCompressPlugin } from './build/compress';
  9. import svgConvert from './build/svgConvert';
  10. import { root, alias, wrapperEnv } from './build/utils';
  11. // https://vitejs.dev/config/
  12. export default defineConfig(({ mode }) => {
  13. const env = wrapperEnv(loadEnv(mode, root));
  14. const isProd = mode === 'production';
  15. // @vitejs/plugin-legacy 在 config 阶段会无条件把 build.target 覆盖成含具体浏览器版本(如 safari13)的
  16. // esbuild target。而 vite-plugin-top-level-await(brotli-wasm 依赖顶层 await)在转换 TLA 产物时会读取
  17. // 这个 target,esbuild 0.28 无法把 TLA 生成的解构赋值降级到任何具体 safari 目标(safari13/14 均报
  18. // "Transforming destructuring ... not supported yet",仅纯 ES 版本如 es2022 可通过)。
  19. //
  20. // 此内联插件把现代 chunk 的 build.target 钉为 es2022:
  21. // - enforce: 'post' 且排在 topLevelAwait() 之前 → 两个 post 插件间按数组顺序执行,
  22. // 故本插件的 config hook 先于 TLA 运行,TLA 备份到的即 es2022,转换得以通过。
  23. // - 现代浏览器(safari13+/chrome87+)原生支持解构,es2022 产物运行无碍;
  24. // 真正的旧浏览器仍由 plugin-legacy 的 legacy chunk(babel 全量转译)兜底。
  25. const pinModernTargetPlugin = {
  26. name: 'pin-modern-target-es2022',
  27. enforce: 'post' as const,
  28. config(config: { build?: { target?: unknown } }) {
  29. config.build = config.build || {};
  30. config.build.target = 'es2022';
  31. },
  32. };
  33. return {
  34. base: env.VITE_BUILD_PUBLIC_PATH,
  35. plugins: [
  36. react(),
  37. wasm(),
  38. pinModernTargetPlugin,
  39. topLevelAwait(),
  40. legacy({
  41. targets: [
  42. 'chrome >= 87',
  43. 'safari >= 13',
  44. 'firefox >= 78',
  45. 'edge >= 88',
  46. 'not IE 11',
  47. ],
  48. }),
  49. isProd &&
  50. removeConsole({
  51. includes: ['log', 'warn'],
  52. external: ['error'],
  53. }),
  54. svgConvert(),
  55. viteBuildInfo(),
  56. configCompressPlugin(env.VITE_BUILD_COMPRESSION!),
  57. ].filter(Boolean),
  58. resolve: { alias },
  59. optimizeDeps: {
  60. exclude: ['brotli-wasm'],
  61. },
  62. css: {
  63. preprocessorOptions: {
  64. less: {
  65. javascriptEnabled: true,
  66. },
  67. scss: {
  68. additionalData: `@use "@/styles/variables" as *;`,
  69. },
  70. },
  71. },
  72. server: {
  73. port: env.VITE_DEV_PORT,
  74. host: '0.0.0.0',
  75. open: true,
  76. proxy: {
  77. '/dev/api/v1': {
  78. target: env.VITE_DEV_PROXY_TARGET_API_BASE_URL,
  79. changeOrigin: true,
  80. rewrite: (path) => path.replace(/^\/dev\/api\/v1/, ''),
  81. secure: false,
  82. configure: (proxy) => {
  83. proxy.on('error', (err) => {
  84. console.log('proxy error', err);
  85. });
  86. proxy.on('proxyReq', (_, req) => {
  87. console.log(
  88. 'Sending Request to the Target:',
  89. req.method,
  90. req.url,
  91. env.VITE_DEV_PROXY_TARGET_API_BASE_URL
  92. );
  93. });
  94. proxy.on('proxyRes', (proxyRes, req) => {
  95. console.log(
  96. 'Received Response from the Target:',
  97. proxyRes.statusCode,
  98. req.url
  99. );
  100. });
  101. },
  102. },
  103. },
  104. warmup: {
  105. clientFiles: ['./index.html', './src/{pages,components}/*'],
  106. },
  107. },
  108. build: {
  109. outDir: 'dist',
  110. sourcemap: !isProd,
  111. // build.target 由上方 pinModernTargetPlugin 在 config 阶段统一钉为 'es2022'(详见该插件注释),
  112. // 此处不再设置,避免与之冲突。
  113. // 与 plugin-legacy targets 对齐,使 CSS 压缩/转换兼容同一批浏览器(plugin-legacy 不处理 CSS)
  114. cssTarget: ['chrome87', 'safari13', 'firefox78', 'edge88'],
  115. rollupOptions: {
  116. output: {
  117. // 产物文件名:入口/代码块用 [name]-[hash:8].js,静态资源用 [name]-[hash:8][extname]
  118. entryFileNames: 'assets/[name]-[hash].js',
  119. chunkFileNames: 'assets/[name]-[hash].js',
  120. assetFileNames: 'assets/[name]-[hash][extname]',
  121. manualChunks: {
  122. 'react-vendor': ['react', 'react-dom', 'react-router-dom'],
  123. 'antd-vendor': ['antd', '@ant-design/icons'],
  124. 'utils-vendor': ['lodash-es', 'ramda'],
  125. 'i18n-vendor': [
  126. 'i18next',
  127. 'react-i18next',
  128. 'i18next-browser-languagedetector',
  129. ],
  130. },
  131. },
  132. },
  133. chunkSizeWarningLimit: 1500,
  134. },
  135. };
  136. });