vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 => types/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. cssCodeSplit: false,
  46. // minify: 'esbuild',
  47. outDir: OUTPUT_DIR,
  48. polyfillDynamicImport: VITE_LEGACY,
  49. terserOptions: {
  50. compress: {
  51. keep_infinity: true,
  52. // Used to delete console in production environment
  53. drop_console: VITE_DROP_CONSOLE,
  54. },
  55. },
  56. // Turning off brotliSize display can slightly reduce packaging time
  57. brotliSize: false,
  58. chunkSizeWarningLimit: 1200,
  59. },
  60. define: {
  61. // setting vue-i18-next
  62. // Suppress warning
  63. __VUE_I18N_LEGACY_API__: false,
  64. __VUE_I18N_FULL_INSTALL__: false,
  65. __INTLIFY_PROD_DEVTOOLS__: false,
  66. },
  67. css: {
  68. preprocessorOptions: {
  69. less: {
  70. modifyVars: {
  71. // Used for global import to avoid the need to import each style file separately
  72. // reference: Avoid repeated references
  73. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  74. ...generateModifyVars(),
  75. },
  76. javascriptEnabled: true,
  77. },
  78. },
  79. },
  80. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  81. plugins: createVitePlugins(viteEnv, isBuild),
  82. optimizeDeps: {
  83. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  84. include: [
  85. '@iconify/iconify',
  86. 'ant-design-vue/es/locale/zh_CN',
  87. 'moment/dist/locale/zh-cn',
  88. 'ant-design-vue/es/locale/en_US',
  89. 'moment/dist/locale/eu',
  90. ],
  91. exclude: ['vue-demi'],
  92. },
  93. };
  94. };