vite.config.ts 2.9 KB

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