合同小程序后台管理系统前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

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