vite.config.ts 5.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 ({ 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: {
  63. ...createProxy(VITE_PROXY),
  64. },
  65. },
  66. build: {
  67. minify: 'esbuild',
  68. target: 'es2020',
  69. cssTarget: 'chrome80',
  70. outDir: OUTPUT_DIR,
  71. rollupOptions: {
  72. output: {
  73. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  74. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  75. // manualChunks配置 (依赖包从大到小排列)
  76. manualChunks: {
  77. 'tinymce-vendor': ['tinymce'],
  78. 'echarts-vendor': ['echarts'],
  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. 'codemirror-vendor': ['codemirror'],
  82. //'emoji-mart-vue-fast': ['emoji-mart-vue-fast'],
  83. // 'jeecg-online-vendor': ['@jeecg/online'],
  84. // 将 Lodash 库的代码单独打包
  85. 'lodash-es-vendor': ['lodash-es'],
  86. 'html2canvas-vendor': ['html2canvas'],
  87. // vue vue-router合并打包
  88. vue: ['vue', 'vue-router'],
  89. 'cron-parser-vendor': ['cron-parser'],
  90. },
  91. },
  92. },
  93. // 关闭brotliSize显示可以稍微减少打包时间
  94. reportCompressedSize: false,
  95. // 提高超大静态资源警告大小
  96. chunkSizeWarningLimit: 2000,
  97. },
  98. esbuild: {
  99. //清除全局的console.log和debug
  100. drop: isBuild ? ['console', 'debugger'] : [],
  101. },
  102. define: {
  103. // setting vue-i18-next
  104. // Suppress warning
  105. __INTLIFY_PROD_DEVTOOLS__: false,
  106. __APP_INFO__: JSON.stringify(__APP_INFO__),
  107. },
  108. css: {
  109. preprocessorOptions: {
  110. less: {
  111. modifyVars: generateModifyVars(),
  112. javascriptEnabled: true,
  113. },
  114. },
  115. },
  116. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  117. plugins: createVitePlugins(viteEnv, isBuild),
  118. // 预加载构建配置(首屏性能)
  119. optimizeDeps: {
  120. esbuildOptions: {
  121. target: 'es2020',
  122. },
  123. exclude: [
  124. //升级vite4后,需要排除online依赖
  125. '@jeecg/online',
  126. ],
  127. // 提前预加载依赖,缩短首屏访问时间
  128. include: [
  129. '@vue/runtime-core',
  130. '@vue/shared',
  131. '@iconify/iconify',
  132. 'ant-design-vue/es/locale/zh_CN',
  133. 'ant-design-vue/es/locale/en_US',
  134. '@ant-design/colors',
  135. '@ant-design/icons-vue',
  136. '@vueuse/core',
  137. '@vueuse/shared',
  138. '@zxcvbn-ts/core',
  139. 'ant-design-vue',
  140. 'axios',
  141. 'china-area-data',
  142. 'clipboard',
  143. 'cron-parser',
  144. 'crypto-js/aes',
  145. 'crypto-js/enc-base64',
  146. 'crypto-js/enc-utf8',
  147. 'crypto-js/md5',
  148. 'crypto-js/mode-ecb',
  149. 'crypto-js/pad-pkcs7',
  150. 'dom-align',
  151. 'echarts',
  152. 'echarts/charts',
  153. 'echarts/components',
  154. 'echarts/core',
  155. 'echarts/renderers',
  156. 'lodash-es',
  157. 'nprogress',
  158. 'path-to-regexp',
  159. 'pinia',
  160. 'qs',
  161. 'vue',
  162. 'vue-i18n',
  163. 'vue-router',
  164. 'vue-types',
  165. ],
  166. },
  167. };
  168. };