vite.config.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { fileURLToPath, URL } from 'node:url'
  2. import vue from '@vitejs/plugin-vue'
  3. import { defineConfig, loadEnv, type ProxyOptions } from 'vite'
  4. function cookieToToken(proxy: { on: (ev: string, fn: (...args: any[]) => void) => void }) {
  5. proxy.on('proxyReq', (proxyReq: { setHeader: (k: string, v: string) => void }, req: { headers: { cookie?: string } }) => {
  6. const cookie = req.headers.cookie || ''
  7. const hit = /(?:^|;\s*)DmsToken=([^;]+)/.exec(cookie)
  8. if (!hit) return
  9. const token = decodeURIComponent(hit[1])
  10. proxyReq.setHeader('Token', token)
  11. proxyReq.setHeader('token', token)
  12. })
  13. }
  14. function dmsProxy(gateway: string): Record<string, ProxyOptions> {
  15. return {
  16. '/proxy_oauth': {
  17. target: gateway,
  18. changeOrigin: true,
  19. },
  20. '/proxy_dms': {
  21. target: gateway,
  22. changeOrigin: true,
  23. configure: cookieToToken,
  24. },
  25. }
  26. }
  27. export default defineConfig(({ mode }) => {
  28. const env = loadEnv(mode, process.cwd(), '')
  29. const gateway = env.DMS_GATEWAY || 'http://121.43.55.7:2101'
  30. const proxy = dmsProxy(gateway)
  31. return {
  32. plugins: [vue()],
  33. resolve: {
  34. alias: {
  35. '@': fileURLToPath(new URL('./src', import.meta.url)),
  36. },
  37. },
  38. server: {
  39. host: true,
  40. port: 5173,
  41. proxy,
  42. },
  43. preview: {
  44. proxy,
  45. },
  46. }
  47. })