vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 } = 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. find: /@\//,
  47. replacement: pathResolve('src') + '/',
  48. },
  49. // /#/xxxx => types/xxxx
  50. {
  51. find: /#\//,
  52. replacement: pathResolve('types') + '/',
  53. },
  54. ],
  55. },
  56. server: {
  57. // Listening on all local IPs
  58. host: true,
  59. https: false,
  60. port: VITE_PORT,
  61. // Load proxy configuration from .env
  62. proxy: createProxy(VITE_PROXY),
  63. },
  64. build: {
  65. minify: 'esbuild',
  66. target: 'es2015',
  67. cssTarget: 'chrome80',
  68. outDir: OUTPUT_DIR,
  69. rollupOptions: {
  70. // 关闭除屑优化,防止删除重要代码,导致打包后功能出现异常
  71. treeshake: false,
  72. output: {
  73. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  74. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  75. // manualChunks配置 (依赖包从大到小排列)
  76. manualChunks: {
  77. // vue vue-router合并打包
  78. 'vue-vendor': ['vue', 'vue-router'],
  79. 'antd-vue-vendor': ['ant-design-vue','@ant-design/icons-vue','@ant-design/colors'],
  80. 'vxe-table-vendor': ['vxe-table','vxe-table-plugin-antd','xe-utils'],
  81. 'emoji-mart-vue-fast': ['emoji-mart-vue-fast'],
  82. 'china-area-data-vendor': ['china-area-data']
  83. },
  84. },
  85. },
  86. // 关闭brotliSize显示可以稍微减少打包时间
  87. reportCompressedSize: false,
  88. // 提高超大静态资源警告大小
  89. chunkSizeWarningLimit: 2000,
  90. },
  91. esbuild: {
  92. //清除全局的console.log和debug
  93. drop: isBuild ? ['console', 'debugger'] : [],
  94. },
  95. define: {
  96. // setting vue-i18-next
  97. // Suppress warning
  98. __INTLIFY_PROD_DEVTOOLS__: false,
  99. __APP_INFO__: JSON.stringify(__APP_INFO__),
  100. },
  101. css: {
  102. preprocessorOptions: {
  103. less: {
  104. modifyVars: generateModifyVars(),
  105. javascriptEnabled: true,
  106. },
  107. },
  108. },
  109. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  110. plugins: createVitePlugins(viteEnv, isBuild),
  111. // 预加载构建配置(首屏性能)
  112. optimizeDeps: {
  113. esbuildOptions: {
  114. target: 'es2020',
  115. },
  116. exclude: [
  117. //升级vite4后,需要排除online依赖
  118. '@jeecg/online',
  119. ],
  120. },
  121. };
  122. };