vite.config.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: 'es2020',
  67. cssTarget: 'chrome80',
  68. outDir: OUTPUT_DIR,
  69. rollupOptions: {
  70. output: {
  71. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  72. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  73. // manualChunks配置 (依赖包从大到小排列)
  74. manualChunks: {
  75. 'tinymce-vendor': ['tinymce'],
  76. 'echarts-vendor': ['echarts'],
  77. 'antd-vue-vendor': ['ant-design-vue', '@ant-design/icons-vue', '@ant-design/colors'],
  78. 'vxe-table-vendor': ['vxe-table', 'vxe-table-plugin-antd', 'xe-utils'],
  79. 'codemirror-vendor': ['codemirror'],
  80. //'emoji-mart-vue-fast': ['emoji-mart-vue-fast'],
  81. // 'jeecg-online-vendor': ['@jeecg/online'],
  82. // 将 Lodash 库的代码单独打包
  83. 'lodash-es-vendor': ['lodash-es'],
  84. 'html2canvas-vendor': ['html2canvas'],
  85. // vue vue-router合并打包
  86. vue: ['vue', 'vue-router'],
  87. 'cron-parser-vendor': ['cron-parser'],
  88. },
  89. },
  90. },
  91. // 关闭brotliSize显示可以稍微减少打包时间
  92. reportCompressedSize: false,
  93. // 提高超大静态资源警告大小
  94. chunkSizeWarningLimit: 2000,
  95. },
  96. esbuild: {
  97. //清除全局的console.log和debug
  98. drop: isBuild ? ['console', 'debugger'] : [],
  99. },
  100. define: {
  101. // setting vue-i18-next
  102. // Suppress warning
  103. __INTLIFY_PROD_DEVTOOLS__: false,
  104. __APP_INFO__: JSON.stringify(__APP_INFO__),
  105. },
  106. css: {
  107. preprocessorOptions: {
  108. less: {
  109. modifyVars: generateModifyVars(),
  110. javascriptEnabled: true,
  111. },
  112. },
  113. },
  114. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  115. plugins: createVitePlugins(viteEnv, isBuild),
  116. // 预加载构建配置(首屏性能)
  117. optimizeDeps: {
  118. esbuildOptions: {
  119. target: 'es2020',
  120. },
  121. exclude: [
  122. //升级vite4后,需要排除online依赖
  123. '@jeecg/online',
  124. ],
  125. // 提前预加载依赖,缩短首屏访问时间
  126. include: [
  127. '@vue/runtime-core',
  128. '@vue/shared',
  129. '@iconify/iconify',
  130. 'ant-design-vue/es/locale/zh_CN',
  131. 'ant-design-vue/es/locale/en_US',
  132. '@ant-design/colors',
  133. '@ant-design/icons-vue',
  134. '@vueuse/core',
  135. '@vueuse/shared',
  136. '@zxcvbn-ts/core',
  137. 'ant-design-vue',
  138. 'axios',
  139. 'china-area-data',
  140. 'clipboard',
  141. 'codemirror',
  142. 'codemirror/addon/fold/brace-fold.js',
  143. 'codemirror/addon/fold/comment-fold.js',
  144. 'codemirror/addon/fold/foldcode.js',
  145. 'codemirror/addon/fold/foldgutter.js',
  146. 'codemirror/addon/fold/indent-fold.js',
  147. 'codemirror/addon/hint/anyword-hint.js',
  148. 'codemirror/addon/hint/show-hint.js',
  149. 'codemirror/addon/selection/active-line.js',
  150. 'codemirror/mode/clike/clike.js',
  151. 'codemirror/mode/css/css.js',
  152. 'codemirror/mode/javascript/javascript.js',
  153. 'codemirror/mode/markdown/markdown.js',
  154. 'codemirror/mode/python/python.js',
  155. 'codemirror/mode/r/r.js',
  156. 'codemirror/mode/shell/shell.js',
  157. 'codemirror/mode/sql/sql.js',
  158. 'codemirror/mode/swift/swift.js',
  159. 'codemirror/mode/vue/vue.js',
  160. 'codemirror/mode/xml/xml.js',
  161. 'cron-parser',
  162. 'cropperjs',
  163. 'crypto-js/aes',
  164. 'crypto-js/enc-base64',
  165. 'crypto-js/enc-utf8',
  166. 'crypto-js/md5',
  167. 'crypto-js/mode-ecb',
  168. 'crypto-js/pad-pkcs7',
  169. 'dom-align',
  170. 'echarts',
  171. 'echarts/charts',
  172. 'echarts/components',
  173. 'echarts/core',
  174. 'echarts/renderers',
  175. 'emoji-mart-vue-fast/src',
  176. 'intro.js',
  177. 'lodash-es',
  178. 'md5',
  179. 'nprogress',
  180. 'path-to-regexp',
  181. 'pinia',
  182. 'print-js',
  183. 'qrcode',
  184. 'qs',
  185. 'resize-observer-polyfill',
  186. 'showdown',
  187. 'sortablejs',
  188. 'tinymce/icons/default/icons',
  189. 'tinymce/plugins/advlist',
  190. 'tinymce/plugins/anchor',
  191. 'tinymce/plugins/autolink',
  192. 'tinymce/plugins/autosave',
  193. 'tinymce/plugins/code',
  194. 'tinymce/plugins/codesample',
  195. 'tinymce/plugins/contextmenu',
  196. 'tinymce/plugins/directionality',
  197. 'tinymce/plugins/fullscreen',
  198. 'tinymce/plugins/hr',
  199. 'tinymce/plugins/image',
  200. 'tinymce/plugins/insertdatetime',
  201. 'tinymce/plugins/link',
  202. 'tinymce/plugins/lists',
  203. 'tinymce/plugins/media',
  204. 'tinymce/plugins/nonbreaking',
  205. 'tinymce/plugins/noneditable',
  206. 'tinymce/plugins/pagebreak',
  207. 'tinymce/plugins/paste',
  208. 'tinymce/plugins/preview',
  209. 'tinymce/plugins/print',
  210. 'tinymce/plugins/save',
  211. 'tinymce/plugins/searchreplace',
  212. 'tinymce/plugins/spellchecker',
  213. 'tinymce/plugins/tabfocus',
  214. 'tinymce/plugins/table',
  215. 'tinymce/plugins/template',
  216. 'tinymce/plugins/textcolor',
  217. 'tinymce/plugins/textpattern',
  218. 'tinymce/plugins/visualblocks',
  219. 'tinymce/plugins/visualchars',
  220. 'tinymce/plugins/wordcount',
  221. 'tinymce/themes/silver',
  222. 'tinymce/tinymce',
  223. 'vditor',
  224. 'vue',
  225. 'vue-i18n',
  226. 'vue-print-nb-jeecg/src/printarea',
  227. 'vue-router',
  228. 'vue-types',
  229. 'vxe-table',
  230. 'vxe-table-plugin-antd',
  231. 'xe-utils',
  232. 'xss',
  233. ],
  234. },
  235. };
  236. };