vite.config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import pkg from './package.json';
  3. import moment from 'moment';
  4. import { loadEnv } from 'vite';
  5. import { resolve } from 'path';
  6. //require('vue-jeecg-plugs/packages/utils')
  7. import { generateModifyVars } from './build/generate/generateModifyVars';
  8. import { createProxy } from './build/vite/proxy';
  9. import { wrapperEnv } from './build/utils';
  10. import { createVitePlugins } from './build/vite/plugin';
  11. import { OUTPUT_DIR } from './build/constant';
  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: moment().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. {
  33. find: 'vue-i18n',
  34. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  35. },
  36. // /@/xxxx => src/xxxx
  37. {
  38. find: /\/@\//,
  39. replacement: pathResolve('src') + '/',
  40. },
  41. // /#/xxxx => types/xxxx
  42. {
  43. find: /\/#\//,
  44. replacement: pathResolve('types') + '/',
  45. },
  46. ],
  47. },
  48. server: {
  49. // Listening on all local IPs
  50. host: true,
  51. port: VITE_PORT,
  52. // Load proxy configuration from .env
  53. proxy: createProxy(VITE_PROXY),
  54. },
  55. build: {
  56. target: 'es2015',
  57. // 【VUEN-872】css编译兼容低版本chrome内核(例如360浏览器)
  58. cssTarget: 'chrome80',
  59. outDir: OUTPUT_DIR,
  60. terserOptions: {
  61. compress: {
  62. keep_infinity: true,
  63. // Used to delete console in production environment
  64. drop_console: VITE_DROP_CONSOLE,
  65. },
  66. },
  67. // Turning off brotliSize display can slightly reduce packaging time
  68. brotliSize: false,
  69. chunkSizeWarningLimit: 2000,
  70. },
  71. define: {
  72. // setting vue-i18-next
  73. // Suppress warning
  74. __INTLIFY_PROD_DEVTOOLS__: false,
  75. __APP_INFO__: JSON.stringify(__APP_INFO__),
  76. },
  77. css: {
  78. preprocessorOptions: {
  79. less: {
  80. modifyVars: generateModifyVars(),
  81. javascriptEnabled: true,
  82. },
  83. },
  84. },
  85. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  86. plugins: createVitePlugins(viteEnv, isBuild),
  87. optimizeDeps: {
  88. esbuildOptions: {
  89. target: 'es2020',
  90. },
  91. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  92. include: [
  93. '@iconify/iconify',
  94. 'ant-design-vue/es/locale/zh_CN',
  95. 'moment/dist/locale/zh-cn',
  96. 'ant-design-vue/es/locale/en_US',
  97. 'moment/dist/locale/eu',
  98. ],
  99. exclude: ['vue-demi'],
  100. },
  101. };
  102. };