vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const pkg = require('./package.json');
  10. function pathResolve(dir: string) {
  11. return resolve(__dirname, '.', dir);
  12. }
  13. export default ({ command, mode }: ConfigEnv): UserConfig => {
  14. const root = process.cwd();
  15. const env = loadEnv(mode, root);
  16. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  17. const viteEnv = wrapperEnv(env);
  18. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv;
  19. const isBuild = command === 'build';
  20. return {
  21. base: VITE_PUBLIC_PATH,
  22. root,
  23. resolve: {
  24. alias: [
  25. {
  26. // /@/xxxx => src/xxx
  27. find: /^\/@\//,
  28. replacement: pathResolve('src') + '/',
  29. },
  30. ],
  31. },
  32. server: {
  33. port: VITE_PORT,
  34. // Load proxy configuration from .env
  35. proxy: createProxy(VITE_PROXY),
  36. hmr: {
  37. overlay: true,
  38. },
  39. },
  40. build: {
  41. outDir: OUTPUT_DIR,
  42. polyfillDynamicImport: VITE_LEGACY,
  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. __VERSION__: pkg.version,
  56. // setting vue-i18-next
  57. // Suppress warning
  58. __VUE_I18N_LEGACY_API__: false,
  59. __VUE_I18N_FULL_INSTALL__: false,
  60. __INTLIFY_PROD_DEVTOOLS__: false,
  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: ['@iconify/iconify'],
  80. exclude: ['vue-demi'],
  81. },
  82. };
  83. };