vite.config.ts 3.0 KB

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