vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. alias: [
  24. {
  25. // /@/xxxx => src/xxx
  26. find: /^\/@\//,
  27. replacement: pathResolve('src') + '/',
  28. },
  29. ],
  30. server: {
  31. port: VITE_PORT,
  32. // Load proxy configuration from .env
  33. proxy: createProxy(VITE_PROXY),
  34. hmr: {
  35. overlay: true,
  36. },
  37. },
  38. build: {
  39. outDir: OUTPUT_DIR,
  40. polyfillDynamicImport: VITE_LEGACY,
  41. terserOptions: {
  42. compress: {
  43. keep_infinity: true,
  44. // Used to delete console in production environment
  45. drop_console: VITE_DROP_CONSOLE,
  46. },
  47. },
  48. // Turning off brotliSize display can slightly reduce packaging time
  49. brotliSize: false,
  50. chunkSizeWarningLimit: 1200,
  51. },
  52. define: {
  53. __VERSION__: pkg.version,
  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. },
  60. css: {
  61. preprocessorOptions: {
  62. less: {
  63. modifyVars: {
  64. // Used for global import to avoid the need to import each style file separately
  65. // reference: Avoid repeated references
  66. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  67. ...generateModifyVars(),
  68. },
  69. javascriptEnabled: true,
  70. },
  71. },
  72. },
  73. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  74. plugins: createVitePlugins(viteEnv, isBuild),
  75. optimizeDeps: {
  76. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  77. include: ['@iconify/iconify'],
  78. },
  79. };
  80. };