vite.config.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import vue from '@vitejs/plugin-vue'
  2. import { resolve } from 'path'
  3. import { loadEnv } from 'vite'
  4. import type { UserConfig, ConfigEnv, ProxyOptions } from 'vite'
  5. import { isProd, customHotUpdate } from '/@/utils/vite'
  6. import { svgBuilder } from '/@/components/icon/svg/index'
  7. const pathResolve = (dir: string): any => {
  8. return resolve(__dirname, '.', dir)
  9. }
  10. // https://vitejs.cn/config/
  11. const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
  12. const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR, VITE_PROXY_URL } = loadEnv(mode, process.cwd())
  13. const alias: Record<string, string> = {
  14. '/@': pathResolve('./src/'),
  15. assets: pathResolve('./src/assets'),
  16. 'vue-i18n': isProd(mode) ? 'vue-i18n/dist/vue-i18n.cjs.prod.js' : 'vue-i18n/dist/vue-i18n.cjs.js',
  17. }
  18. let proxy: Record<string, string | ProxyOptions> = {}
  19. if (VITE_PROXY_URL) {
  20. proxy = {
  21. '/': {
  22. target: VITE_PROXY_URL,
  23. changeOrigin: true,
  24. },
  25. }
  26. }
  27. return {
  28. plugins: [vue(), svgBuilder('./src/assets/icons/'), customHotUpdate()],
  29. root: process.cwd(),
  30. resolve: { alias },
  31. base: VITE_BASE_PATH,
  32. server: {
  33. port: parseInt(VITE_PORT),
  34. open: VITE_OPEN != 'false',
  35. proxy: proxy,
  36. },
  37. build: {
  38. cssCodeSplit: false,
  39. sourcemap: false,
  40. outDir: VITE_OUT_DIR,
  41. emptyOutDir: true,
  42. chunkSizeWarningLimit: 1500,
  43. rollupOptions: {
  44. output: {
  45. manualChunks: {
  46. // 分包配置,配置完成自动按需加载
  47. vue: ['vue', 'vue-router', 'pinia', 'vue-i18n', 'element-plus'],
  48. echarts: ['echarts'],
  49. },
  50. },
  51. },
  52. },
  53. }
  54. }
  55. export default viteConfig