vite.config.ts 3.0 KB

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