vite.config.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. const serverOptions: Recordable = {}
  27. // ----- [begin] 【JEECG作为乾坤子应用】 -----
  28. const {VITE_GLOB_QIANKUN_MICRO_APP_NAME, VITE_GLOB_QIANKUN_MICRO_APP_ENTRY} = viteEnv;
  29. const isQiankunMicro = VITE_GLOB_QIANKUN_MICRO_APP_NAME != null && VITE_GLOB_QIANKUN_MICRO_APP_NAME !== '';
  30. if (isQiankunMicro && !isBuild) {
  31. serverOptions.cors = true;
  32. serverOptions.origin = VITE_GLOB_QIANKUN_MICRO_APP_ENTRY!.split('/').slice(0, 3).join('/');
  33. }
  34. // ----- [end] 【JEECG作为乾坤子应用】 -----
  35. console.log('[init] Start Port: ', VITE_PORT);
  36. console.debug('[init] Vite Proxy Config: ', VITE_PROXY);
  37. return {
  38. base: isQiankunMicro ? VITE_GLOB_QIANKUN_MICRO_APP_ENTRY : VITE_PUBLIC_PATH,
  39. root,
  40. resolve: {
  41. alias: [
  42. {
  43. find: 'vue-i18n',
  44. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  45. },
  46. // /@/xxxx => src/xxxx
  47. {
  48. find: /\/@\//,
  49. replacement: pathResolve('src') + '/',
  50. },
  51. // /#/xxxx => types/xxxx
  52. {
  53. find: /\/#\//,
  54. replacement: pathResolve('types') + '/',
  55. },
  56. {
  57. find: /@\//,
  58. replacement: pathResolve('src') + '/',
  59. },
  60. // /#/xxxx => types/xxxx
  61. {
  62. find: /#\//,
  63. replacement: pathResolve('types') + '/',
  64. },
  65. ],
  66. },
  67. server: {
  68. // Listening on all local IPs
  69. host: true,
  70. // @ts-ignore
  71. https: false,
  72. port: VITE_PORT,
  73. // Load proxy configuration from .env
  74. proxy: createProxy(VITE_PROXY),
  75. // 合并 server 配置
  76. ...serverOptions,
  77. },
  78. build: {
  79. minify: 'esbuild',
  80. target: 'es2015',
  81. cssTarget: 'chrome80',
  82. outDir: OUTPUT_DIR,
  83. rollupOptions: {
  84. // 关闭除屑优化,防止删除重要代码,导致打包后功能出现异常
  85. treeshake: false,
  86. output: {
  87. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  88. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  89. // manualChunks配置 (依赖包从大到小排列)
  90. manualChunks: {
  91. // vue vue-router合并打包
  92. 'vue-vendor': ['vue', 'vue-router'],
  93. 'antd-vue-vendor': ['ant-design-vue','@ant-design/icons-vue','@ant-design/colors'],
  94. 'vxe-table-vendor': ['vxe-table','vxe-table-plugin-antd','xe-utils'],
  95. 'emoji-mart-vue-fast': ['emoji-mart-vue-fast'],
  96. 'china-area-data-vendor': ['china-area-data']
  97. },
  98. },
  99. },
  100. // 关闭brotliSize显示可以稍微减少打包时间
  101. reportCompressedSize: false,
  102. // 提高超大静态资源警告大小
  103. chunkSizeWarningLimit: 2000,
  104. },
  105. esbuild: {
  106. //清除全局的console.log和debug
  107. drop: isBuild ? ['console', 'debugger'] : [],
  108. },
  109. define: {
  110. // setting vue-i18-next
  111. // Suppress warning
  112. __INTLIFY_PROD_DEVTOOLS__: false,
  113. __APP_INFO__: JSON.stringify(__APP_INFO__),
  114. },
  115. css: {
  116. preprocessorOptions: {
  117. less: {
  118. modifyVars: generateModifyVars(),
  119. javascriptEnabled: true,
  120. },
  121. },
  122. },
  123. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  124. // 预加载构建配置(首屏性能)
  125. plugins: createVitePlugins(viteEnv, isBuild, isQiankunMicro),
  126. optimizeDeps: {
  127. esbuildOptions: {
  128. target: 'es2020',
  129. },
  130. exclude: [
  131. //升级vite4后,需要排除online依赖
  132. '@jeecg/online',
  133. '@jeecg/aiflow',
  134. ],
  135. // 强制预构建clipboard,解决Vite6对CommonJS模块的严格检查
  136. include: ['clipboard']
  137. },
  138. };
  139. };