vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import pkg from './package.json';
  3. import dayjs from 'dayjs';
  4. import { loadEnv } from 'vite';
  5. import { resolve } from 'path';
  6. import { generateModifyVars } from './build/generate/generateModifyVars';
  7. import { createProxy } from './build/vite/proxy';
  8. import { wrapperEnv } from './build/utils';
  9. import { createVitePlugins } from './build/vite/plugin';
  10. import { OUTPUT_DIR } from './build/constant';
  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: dayjs().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. console.log('createProxy(VITE_PROXY)', createProxy(VITE_PROXY));
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. root,
  30. logLevel: 'info',
  31. resolve: {
  32. alias: [
  33. {
  34. find: 'vue-i18n',
  35. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  36. },
  37. // /@/xxxx => src/xxxx
  38. {
  39. find: /\/@\//,
  40. replacement: pathResolve('src') + '/',
  41. },
  42. // /#/xxxx => types/xxxx
  43. {
  44. find: /\/#\//,
  45. replacement: pathResolve('types') + '/',
  46. },
  47. ],
  48. },
  49. server: {
  50. // Listening on all local IPs
  51. host: true,
  52. port: VITE_PORT,
  53. // Load proxy configuration from .env
  54. proxy: createProxy(VITE_PROXY),
  55. },
  56. build: {
  57. // minify: false,
  58. minify: 'terser',
  59. // target: 'es2015',
  60. // cssTarget: 'chrome86',
  61. outDir: OUTPUT_DIR,
  62. terserOptions: {
  63. compress: {
  64. keep_infinity: true,
  65. // Used to delete console in production environment
  66. drop_console: VITE_DROP_CONSOLE,
  67. },
  68. },
  69. // Turning off brotliSize display can slightly reduce packaging time
  70. brotliSize: false,
  71. chunkSizeWarningLimit: 2000,
  72. },
  73. define: {
  74. // setting vue-i18-next
  75. // Suppress warning
  76. __INTLIFY_PROD_DEVTOOLS__: false,
  77. __APP_INFO__: JSON.stringify(__APP_INFO__),
  78. },
  79. css: {
  80. preprocessorOptions: {
  81. less: {
  82. modifyVars: generateModifyVars(),
  83. javascriptEnabled: true,
  84. },
  85. },
  86. },
  87. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  88. plugins: createVitePlugins(viteEnv, isBuild),
  89. optimizeDeps: {
  90. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  91. include: [
  92. '@vue/runtime-core',
  93. '@vue/shared',
  94. '@iconify/iconify',
  95. 'ant-design-vue/es/locale/zh_CN',
  96. 'ant-design-vue/es/locale/en_US',
  97. ],
  98. },
  99. };
  100. };