WEB/vite.config.ts
2026-09-10 18:04:04 +08:00

79 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { COS_DEV_PROXY_PREFIX, COS_DEV_PROXY_OFF, DEFAULT_COS_DEV_PROXY_TARGET } from './src/utils/cosDevProxyConfig'
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'VITE_')
// 本地开发:浏览器直传对象存储会被跨域(CORS)拦截,这里挂一条同源代理绕过。
// 仅 dev server 生效,生产构建/部署不包含该代理。
const cosProxyEnabled = env.VITE_COS_DEV_PROXY !== COS_DEV_PROXY_OFF
const cosProxyTarget = env.VITE_COS_DEV_PROXY_TARGET || DEFAULT_COS_DEV_PROXY_TARGET
const cosProxyPrefixRe = new RegExp(`^${COS_DEV_PROXY_PREFIX}`)
return {
plugins: [
vue(),
vueDevTools(),
createSvgIconsPlugin({
// Specify the icon folder to be cached
iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
// Specify symbolId format
symbolId: 'icon-[dir]-[name]',
}),
],
server: {
port: 5174,
host: '0.0.0.0',
// 允许所有主机访问开发服务器(如 test.wms.com、局域网 IP 等)
allowedHosts: true,
proxy: {
'/dev-api': {
target: 'http://localhost:9528', // Assuming Vue 2 backend is running here, user can change
changeOrigin: true,
rewrite: (path) => path.replace(/^\/dev-api/, '')
},
'/dl-api': {
target: 'https://dl.wms.gztbyc.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/dl-api/, '')
},
// 海康 SDK ISAPI 请求 → 本地代理 → NVR
'/ISAPI': {
target: 'http://localhost:17311',
changeOrigin: true
},
// 对象存储直传同源代理:/cos-proxy/<原路径> → <存储桶>/<原路径>
// 把浏览器直传变成同源请求,绕过存储桶 CORS 白名单限制(详见 src/utils/cosDevProxyConfig.ts)
...(cosProxyEnabled
? {
[COS_DEV_PROXY_PREFIX]: {
target: cosProxyTarget,
changeOrigin: true,
secure: true,
rewrite: (path) => path.replace(cosProxyPrefixRe, '') || '/',
configure: (proxy) => {
// 去掉浏览器带来的 Origin / Referer,避免存储桶按 CORS 规则判定请求
proxy.on('proxyReq', (proxyReq) => {
proxyReq.removeHeader('origin')
proxyReq.removeHeader('referer')
})
}
}
}
: {})
}
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
}
})