vite.config.ts 2.8 KB

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