vite.config.ts 2.9 KB

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