vite.config.ts 2.7 KB

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