vite.config.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 async ({ command, mode }: ConfigEnv): Promise<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. // update-begin--author:liaozhiyang---date:20260306---for:【QQYUN-14801】vite启动的时候,预构建一些入口页面,访问时快一些
  78. // 启动时预构建
  79. warmup: {
  80. clientFiles: [
  81. './src/main.ts',
  82. './src/App.vue',
  83. './src/views/system/loginmini/MiniLogin.vue',
  84. 'src/layouts/default/index.vue'
  85. ],
  86. },
  87. // update-end--author:liaozhiyang---date:20260306---for:【QQYUN-14801】vite启动的时候,预构建一些入口页面,访问时快一些
  88. },
  89. build: {
  90. minify: 'esbuild',
  91. target: 'es2015',
  92. cssTarget: 'chrome80',
  93. outDir: OUTPUT_DIR,
  94. rollupOptions: {
  95. // 关闭除屑优化,防止删除重要代码,导致打包后功能出现异常
  96. // treeshake: false,
  97. output: {
  98. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  99. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  100. // manualChunks配置 (依赖包从大到小排列)
  101. manualChunks: {
  102. // vue vue-router合并打包
  103. 'vue-vendor': ['vue', 'vue-router'],
  104. 'emoji-mart-vue-fast': ['emoji-mart-vue-fast'],
  105. },
  106. },
  107. },
  108. // 关闭brotliSize显示可以稍微减少打包时间
  109. reportCompressedSize: false,
  110. // 提高超大静态资源警告大小
  111. chunkSizeWarningLimit: 2000,
  112. },
  113. esbuild: {
  114. //清除全局的console.log和debug
  115. drop: isBuild ? ['console', 'debugger'] : [],
  116. },
  117. define: {
  118. // setting vue-i18-next
  119. // Suppress warning
  120. __INTLIFY_PROD_DEVTOOLS__: false,
  121. __APP_INFO__: JSON.stringify(__APP_INFO__),
  122. },
  123. css: {
  124. preprocessorOptions: {
  125. less: {
  126. modifyVars: generateModifyVars(),
  127. javascriptEnabled: true,
  128. },
  129. },
  130. },
  131. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  132. plugins: await createVitePlugins(viteEnv, isBuild, isQiankunMicro),
  133. optimizeDeps: {
  134. esbuildOptions: {
  135. target: 'es2020',
  136. },
  137. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  138. include: [
  139. // 强制预构建clipboard,解决Vite6对CommonJS模块的严格检查
  140. 'clipboard',
  141. '@vue/shared',
  142. '@iconify/iconify',
  143. 'ant-design-vue/es/locale/zh_CN',
  144. 'ant-design-vue/es/locale/en_US',
  145. // update-begin--author:scott---date:20260427---for: 集成 @jeecg/aiflow(预编译 lib 在 node_modules)时,
  146. // Vite 默认不扫描 node_modules 里已打包的 mjs,导致 ant-design-vue/es/vc-picker/generate/dayjs.js
  147. // 引入的 dayjs 插件子路径(UMD/CJS)未被预打包,运行时报 "does not provide an export named 'default'"。
  148. // 显式列出 vc-picker 用到的全部 dayjs 插件,强制 esbuild 预打包成 ESM。
  149. 'dayjs/plugin/advancedFormat',
  150. 'dayjs/plugin/customParseFormat',
  151. 'dayjs/plugin/weekday',
  152. 'dayjs/plugin/localeData',
  153. 'dayjs/plugin/weekOfYear',
  154. 'dayjs/plugin/weekYear',
  155. 'dayjs/plugin/quarterOfYear',
  156. // update-end--author:scott---date:20260427---for: 集成 @jeecg/aiflow 时 dayjs 插件 default 导出报错
  157. ],
  158. exclude: [
  159. //升级vite4后,需要排除online和aiflow依赖
  160. '@jeecg/aiflow',
  161. ],
  162. },
  163. };
  164. };