vite.config.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /** 路径查找 */
  12. function pathResolve(dir: string) {
  13. return resolve(process.cwd(), '.', dir);
  14. }
  15. const { dependencies, devDependencies, name, version } = pkg;
  16. const __APP_INFO__ = {
  17. pkg: { dependencies, devDependencies, name, version },
  18. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  19. };
  20. export default ({ command, mode }: ConfigEnv): UserConfig => {
  21. const root = process.cwd();
  22. const env = loadEnv(mode, root);
  23. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  24. const viteEnv = wrapperEnv(env);
  25. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
  26. const isBuild = command === 'build';
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. root,
  30. resolve: {
  31. alias: [
  32. // /@/xxxx => src/xxxx
  33. {
  34. find: /\/@\//,
  35. replacement: pathResolve('src') + '/',
  36. },
  37. // /#/xxxx => types/xxxx
  38. {
  39. find: /\/#\//,
  40. replacement: pathResolve('types') + '/',
  41. },
  42. // @/xxxx => src/xxxx
  43. {
  44. find: /@\//,
  45. replacement: pathResolve('src') + '/',
  46. },
  47. // #/xxxx => types/xxxx
  48. {
  49. find: /#\//,
  50. replacement: pathResolve('types') + '/',
  51. },
  52. ],
  53. },
  54. // 服务端渲染
  55. server: {
  56. // 是否开启 https
  57. https: false,
  58. // 端口号
  59. port: VITE_PORT,
  60. host: true,
  61. // Load proxy configuration from .env
  62. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  63. proxy: createProxy(VITE_PROXY),
  64. },
  65. esbuild: {
  66. pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
  67. },
  68. build: {
  69. target: 'es2015',
  70. cssTarget: 'chrome80',
  71. outDir: OUTPUT_DIR,
  72. // minify: 'terser',
  73. /**
  74. * 当 minify=“minify:'terser'” 解开注释
  75. * Uncomment when minify="minify:'terser'"
  76. */
  77. // terserOptions: {
  78. // compress: {
  79. // keep_infinity: true,
  80. // drop_console: VITE_DROP_CONSOLE,
  81. // },
  82. // },
  83. // Turning off brotliSize display can slightly reduce packaging time
  84. // brotliSize: false,
  85. sourcemap: false,
  86. // 消除打包大小超过500kb警告
  87. chunkSizeWarningLimit: 4000,
  88. },
  89. define: {
  90. // Suppress warning
  91. __INTLIFY_PROD_DEVTOOLS__: false,
  92. __APP_INFO__: JSON.stringify(__APP_INFO__),
  93. },
  94. css: {
  95. preprocessorOptions: {
  96. less: {
  97. modifyVars: generateModifyVars(),
  98. javascriptEnabled: true,
  99. },
  100. },
  101. },
  102. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  103. plugins: createVitePlugins(viteEnv, isBuild),
  104. optimizeDeps: {
  105. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  106. include: [
  107. 'qs',
  108. 'mitt',
  109. 'dayjs',
  110. 'axios',
  111. 'pinia',
  112. 'echarts',
  113. 'js-cookie',
  114. '@vueuse/core',
  115. '@vueuse/shared',
  116. '@pureadmin/utils',
  117. 'responsive-storage',
  118. 'element-resize-detector',
  119. 'vue-types',
  120. '@wangeditor/editor-for-vue',
  121. 'vue3-colorpicker',
  122. 'path-to-regexp',
  123. 'sortablejs',
  124. '@ant-design/icons-vue',
  125. '@vue/runtime-core',
  126. '@codemirror/lang-javascript',
  127. '@codemirror/lang-java',
  128. '@codemirror/lang-json',
  129. 'codemirror',
  130. 'ant-design-vue/es/locale/zh_CN',
  131. ],
  132. },
  133. };
  134. };