vite.config 增加 manualChunks 按库拆分与 chunkSizeWarningLimit;china-division 换用不含街道级的 pca-code.json(省 1.75MB);CreatePendingOrderDialog 改为异步组件,xlsx/china-division 不再进首屏。构建告警清零。
111 lines
4.3 KiB
TypeScript
111 lines
4.3 KiB
TypeScript
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))
|
||
},
|
||
},
|
||
build: {
|
||
// 单库 chunk 本身就可能超 500kB(echarts / element-plus 全量引入),阈值按实际基线设置
|
||
chunkSizeWarningLimit: 1200,
|
||
rollupOptions: {
|
||
output: {
|
||
/**
|
||
* 按库拆分第三方依赖,避免全量 element-plus / echarts 等挤进入口 chunk
|
||
* (入口曾达 3.9MB)。拆成独立 chunk 后可并行加载,且升级应用代码时第三方 chunk 哈希不变、命中长期缓存。
|
||
*/
|
||
manualChunks(id) {
|
||
if (!id.includes('node_modules')) return
|
||
if (id.includes('echarts') || id.includes('zrender')) return 'vendor-echarts'
|
||
if (id.includes('/three/')) return 'vendor-three'
|
||
if (id.includes('xlsx')) return 'vendor-xlsx'
|
||
if (id.includes('element-plus')) return 'vendor-element-plus'
|
||
if (id.includes('china-division')) return 'vendor-china-division'
|
||
if (
|
||
id.includes('/vue/') ||
|
||
id.includes('/vue-') ||
|
||
id.includes('/@vue/') ||
|
||
id.includes('/pinia') ||
|
||
id.includes('@vueuse')
|
||
) {
|
||
// 含 vue-demi / vue-router / vue-i18n / vue-draggable-plus / pinia 等所有依赖 vue 的库,
|
||
// 否则这些库落到 vendor 会与 vendor-vue 形成循环 chunk
|
||
return 'vendor-vue'
|
||
}
|
||
return 'vendor'
|
||
}
|
||
}
|
||
}
|
||
},
|
||
}
|
||
})
|