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.

132 lines
3.9 KiB

11 months 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. 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: 'es2015',
  67. cssTarget: 'chrome80',
  68. outDir: OUTPUT_DIR,
  69. rollupOptions: {
  70. // 关闭除屑优化,防止删除重要代码,导致打包后功能出现异常
  71. treeshake: false,
  72. output: {
  73. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  74. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  75. // manualChunks配置 (依赖包从大到小排列)
  76. manualChunks: {
  77. 'tinymce-vendor': ['tinymce','@tinymce/tinymce-vue'],
  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. 'jeecg-online-vendor': ['@jeecg/online'],
  83. 'cron-parser-vendor': ['cron-parser'],
  84. },
  85. },
  86. },
  87. // 关闭brotliSize显示可以稍微减少打包时间
  88. reportCompressedSize: false,
  89. // 提高超大静态资源警告大小
  90. chunkSizeWarningLimit: 2000,
  91. },
  92. esbuild: {
  93. //清除全局的console.log和debug
  94. drop: isBuild ? ['console', 'debugger'] : [],
  95. },
  96. define: {
  97. // setting vue-i18-next
  98. // Suppress warning
  99. __INTLIFY_PROD_DEVTOOLS__: false,
  100. __APP_INFO__: JSON.stringify(__APP_INFO__),
  101. },
  102. css: {
  103. preprocessorOptions: {
  104. less: {
  105. modifyVars: generateModifyVars(),
  106. javascriptEnabled: true,
  107. },
  108. },
  109. },
  110. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  111. plugins: createVitePlugins(viteEnv, isBuild),
  112. // 预加载构建配置(首屏性能)
  113. optimizeDeps: {
  114. esbuildOptions: {
  115. target: 'es2020',
  116. },
  117. exclude: [
  118. //升级vite4后,需要排除online依赖
  119. '@jeecg/online',
  120. ],
  121. },
  122. };
  123. };