vite.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import vue from '@vitejs/plugin-vue'
  2. import { resolve } from 'path'
  3. import type { UserConfig, ConfigEnv, ProxyOptions } from 'vite'
  4. import { isProd, loadEnv } from '/@/utils/vite'
  5. import { svgBuilder } from '/@/components/icon/svg/index'
  6. const pathResolve = (dir: string): any => {
  7. return resolve(__dirname, '.', dir)
  8. }
  9. // https://vitejs.cn/config/
  10. const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
  11. const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR, VITE_PROXY_URL } = loadEnv(mode)
  12. const alias: Record<string, string> = {
  13. '/@': pathResolve('./src/'),
  14. assets: pathResolve('./src/assets'),
  15. 'vue-i18n': isProd(mode) ? 'vue-i18n/dist/vue-i18n.cjs.prod.js' : 'vue-i18n/dist/vue-i18n.cjs.js',
  16. }
  17. let proxy: Record<string, string | ProxyOptions> = {}
  18. if (VITE_PROXY_URL) {
  19. proxy = {
  20. '/': {
  21. target: VITE_PROXY_URL,
  22. changeOrigin: true,
  23. },
  24. }
  25. }
  26. return {
  27. plugins: [vue(), svgBuilder('./src/assets/icons/')],
  28. root: process.cwd(),
  29. resolve: { alias },
  30. base: VITE_BASE_PATH,
  31. server: {
  32. host: '0.0.0.0',
  33. port: VITE_PORT,
  34. open: VITE_OPEN,
  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. css: {
  54. postcss: {
  55. plugins: [
  56. {
  57. postcssPlugin: 'internal:charset-removal',
  58. AtRule: {
  59. charset: (atRule) => {
  60. if (atRule.name === 'charset') {
  61. atRule.remove()
  62. }
  63. },
  64. },
  65. },
  66. ],
  67. },
  68. },
  69. }
  70. }
  71. export default viteConfig