vite.config.ts 3.1 KB

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