kongdeqiang
2026-10-12 285bf234bd50ce54fcae7bb777f8fa579b91266e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
 
/**
 * Vite 配置。
 *
 * /api 通过 proxy 转发到 SpringBoot,这样做的好处是:
 *   1. 开发环境不存在跨域问题,前端所有请求都是同源的
 *   2. **SSE 的 EventSource 可以走同源相对路径**,不必把后端地址写进前端代码
 *   3. 生产环境由 nginx 做同样的转发,前后端行为完全一致
 */
export default defineConfig({
  plugins: [vue()],
 
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
 
  server: {
    host: '0.0.0.0', // 允许局域网访问
    port: 9999,
    proxy: {
      '/api': {
        target: 'http://localhost:9091',
        changeOrigin: true,
        // SSE 必须关闭代理层的缓冲,否则事件会被攒着一起发,
        // 打字机效果会变成「卡住很久然后一次性刷出全部内容」。
        configure: (proxy) => {
          proxy.on('proxyRes', (proxyRes) => {
            proxyRes.headers['cache-control'] = 'no-cache, no-transform'
          })
        }
      }
    }
  },
 
  build: {
    outDir: 'dist',
    // 关掉 sourcemap 以减小产物体积;需要排查线上问题时再打开
    sourcemap: false,
    chunkSizeWarningLimit: 1500
  }
})