西藏巴青项目

vite.config.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { defineConfig, loadEnv } from 'vite'
  2. import uni from '@dcloudio/vite-plugin-uni'
  3. import { LLM_PROXY_TARGET } from './config/llm.js'
  4. const llmTarget = String(LLM_PROXY_TARGET || '').replace(/\/$/, '')
  5. /** 与 manifest.json h5.router.base 一致:生产 /bqH5/,开发 / */
  6. function resolvePublicPath(mode, env) {
  7. if (mode !== 'production') {
  8. return '/'
  9. }
  10. const raw = String(env.VITE_APP_PUBLIC_PATH || 'bqH5').trim()
  11. if (!raw || raw === '/') {
  12. return '/'
  13. }
  14. return `/${raw.replace(/^\/+|\/+$/g, '')}/`
  15. }
  16. /**
  17. * uni-app Vue3 H5 开发代理
  18. * - /dev-api → 若依后端(.env.development 的 VITE_APP_API_HOST)
  19. * - /llm-dev-proxy → 大模型网关
  20. */
  21. export default defineConfig(({ mode }) => {
  22. const env = loadEnv(mode, process.cwd(), '')
  23. const base = resolvePublicPath(mode, env)
  24. const devApiTarget = String(env.VITE_APP_API_HOST || 'http://192.168.1.6:8010').replace(/\/$/, '')
  25. const devApiPrefix = String(env.VITE_APP_BASE_API || '/dev-api')
  26. return {
  27. base,
  28. plugins: [uni()],
  29. build: {
  30. // iOS Safari 对 modulepreload 更敏感,预加载未使用的 chunk 易拖慢首屏
  31. modulePreload: false
  32. },
  33. server: {
  34. proxy: {
  35. [devApiPrefix]: {
  36. target: devApiTarget,
  37. changeOrigin: true,
  38. rewrite: (path) => path.replace(new RegExp(`^${devApiPrefix}`), '')
  39. },
  40. '/llm-dev-proxy': {
  41. target: llmTarget,
  42. changeOrigin: true,
  43. rewrite: (path) => path.replace(/^\/llm-dev-proxy/, '')
  44. }
  45. }
  46. }
  47. }
  48. })