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: 5173,
|
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
|
}
|
})
|