vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import { loadEnv } from 'vite';
  3. import { resolve } from 'path';
  4. import { generateModifyVars } from './build/config/themeConfig';
  5. import { createProxy } from './build/vite/proxy';
  6. import { wrapperEnv } from './build/utils';
  7. import { createVitePlugins } from './build/vite/plugin';
  8. import { OUTPUT_DIR } from './build/constant';
  9. function pathResolve(dir: string) {
  10. return resolve(__dirname, '.', dir);
  11. }
  12. export default ({ command, mode }: ConfigEnv): UserConfig => {
  13. const root = process.cwd();
  14. const env = loadEnv(mode, root);
  15. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  16. const viteEnv = wrapperEnv(env);
  17. const {
  18. VITE_PORT,
  19. VITE_PUBLIC_PATH,
  20. VITE_PROXY,
  21. VITE_DROP_CONSOLE,
  22. VITE_LEGACY,
  23. VITE_DYNAMIC_IMPORT,
  24. } = viteEnv;
  25. const isBuild = command === 'build';
  26. return {
  27. base: VITE_PUBLIC_PATH,
  28. root,
  29. resolve: {
  30. alias: [
  31. {
  32. // /@/xxxx => src/xxx
  33. find: /^\/@\//,
  34. replacement: pathResolve('src') + '/',
  35. },
  36. {
  37. // /@/xxxx => src/xxx
  38. find: /^\/#\//,
  39. replacement: pathResolve('types') + '/',
  40. },
  41. ],
  42. },
  43. server: {
  44. port: VITE_PORT,
  45. // Load proxy configuration from .env
  46. proxy: createProxy(VITE_PROXY),
  47. hmr: {
  48. overlay: true,
  49. },
  50. },
  51. build: {
  52. outDir: OUTPUT_DIR,
  53. polyfillDynamicImport: VITE_LEGACY,
  54. terserOptions: {
  55. compress: {
  56. keep_infinity: true,
  57. // Used to delete console in production environment
  58. drop_console: VITE_DROP_CONSOLE,
  59. },
  60. },
  61. // Turning off brotliSize display can slightly reduce packaging time
  62. brotliSize: false,
  63. chunkSizeWarningLimit: 1200,
  64. },
  65. define: {
  66. // setting vue-i18-next
  67. // Suppress warning
  68. __DYNAMIC_IMPORT__: VITE_DYNAMIC_IMPORT,
  69. __VUE_I18N_LEGACY_API__: false,
  70. __VUE_I18N_FULL_INSTALL__: false,
  71. __INTLIFY_PROD_DEVTOOLS__: false,
  72. },
  73. css: {
  74. preprocessorOptions: {
  75. less: {
  76. modifyVars: {
  77. // Used for global import to avoid the need to import each style file separately
  78. // reference: Avoid repeated references
  79. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  80. ...generateModifyVars(),
  81. },
  82. javascriptEnabled: true,
  83. },
  84. },
  85. },
  86. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  87. plugins: createVitePlugins(viteEnv, isBuild),
  88. optimizeDeps: {
  89. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  90. include: [
  91. '@iconify/iconify',
  92. 'ant-design-vue/es/locale/zh_CN',
  93. 'moment/dist/locale/zh-cn',
  94. 'ant-design-vue/es/locale/en_US',
  95. 'moment/dist/locale/eu',
  96. ],
  97. exclude: ['vue-demi'],
  98. },
  99. };
  100. };