vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import react from '@vitejs/plugin-react';
  2. import { defineConfig, loadEnv } from 'vite';
  3. import removeConsole from 'vite-plugin-remove-console';
  4. import topLevelAwait from 'vite-plugin-top-level-await';
  5. import wasm from 'vite-plugin-wasm';
  6. import { viteBuildInfo } from './build/buildInfo';
  7. import { configCompressPlugin } from './build/compress';
  8. import svgConvert from './build/svgConvert';
  9. import { root, alias, wrapperEnv } from './build/utils';
  10. // https://vitejs.dev/config/
  11. export default defineConfig(({ mode }) => {
  12. const env = wrapperEnv(loadEnv(mode, root));
  13. const isProd = mode === 'production';
  14. return {
  15. base: env.VITE_BUILD_PUBLIC_PATH,
  16. plugins: [
  17. react(),
  18. wasm(),
  19. topLevelAwait(),
  20. isProd &&
  21. removeConsole({
  22. includes: ['log', 'warn'],
  23. external: ['error'],
  24. }),
  25. svgConvert(),
  26. viteBuildInfo(),
  27. configCompressPlugin(env.VITE_BUILD_COMPRESSION!),
  28. ].filter(Boolean),
  29. resolve: { alias },
  30. optimizeDeps: {
  31. exclude: ['brotli-wasm'],
  32. },
  33. css: {
  34. preprocessorOptions: {
  35. less: {
  36. javascriptEnabled: true,
  37. },
  38. scss: {
  39. additionalData: `@use "@/styles/variables" as *;`,
  40. },
  41. },
  42. },
  43. server: {
  44. port: env.VITE_DEV_PORT,
  45. host: '0.0.0.0',
  46. open: true,
  47. proxy: {
  48. '/dev/api/v1': {
  49. target: env.VITE_DEV_PROXY_TARGET_API_BASE_URL,
  50. changeOrigin: true,
  51. rewrite: (path) => path.replace(/^\/dev\/api\/v1/, ''),
  52. secure: false,
  53. configure: (proxy) => {
  54. proxy.on('error', (err) => {
  55. console.log('proxy error', err);
  56. });
  57. proxy.on('proxyReq', (_, req) => {
  58. console.log(
  59. 'Sending Request to the Target:',
  60. req.method,
  61. req.url,
  62. env.VITE_DEV_PROXY_TARGET_API_BASE_URL
  63. );
  64. });
  65. proxy.on('proxyRes', (proxyRes, req) => {
  66. console.log(
  67. 'Received Response from the Target:',
  68. proxyRes.statusCode,
  69. req.url
  70. );
  71. });
  72. },
  73. },
  74. },
  75. warmup: {
  76. clientFiles: ['./index.html', './src/{pages,components}/*'],
  77. },
  78. },
  79. build: {
  80. outDir: 'dist',
  81. sourcemap: !isProd,
  82. target: ['es2015', 'chrome87', 'safari13', 'firefox78', 'edge88'],
  83. cssTarget: ['chrome87', 'safari13', 'firefox78', 'edge88'],
  84. rollupOptions: {
  85. output: {
  86. manualChunks: {
  87. 'react-vendor': ['react', 'react-dom', 'react-router-dom'],
  88. 'antd-vendor': ['antd'],
  89. 'utils-vendor': ['lodash-es', 'ramda'],
  90. },
  91. },
  92. },
  93. chunkSizeWarningLimit: 1500,
  94. },
  95. };
  96. });