vite.config.ts 2.8 KB

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