2026-04-17 17:20:41 +08:00
|
|
|
|
import tailwindcss from '@tailwindcss/vite';
|
|
|
|
|
|
import react from '@vitejs/plugin-react';
|
2026-04-21 14:57:52 +08:00
|
|
|
|
import path, { resolve } from 'path';
|
|
|
|
|
|
import { defineConfig, loadEnv } from 'vite';
|
2026-05-12 08:34:13 +08:00
|
|
|
|
import cesiumPlugin from 'vite-plugin-cesium';
|
|
|
|
|
|
import { viteStaticCopy } from "vite-plugin-static-copy";
|
2026-04-24 16:18:24 +08:00
|
|
|
|
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Vite 配置 - 性能优化版
|
|
|
|
|
|
// - 代码分包 (manualChunks)
|
|
|
|
|
|
// - 路径别名
|
|
|
|
|
|
// - CSS 代码分割
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
const ENV = { DEV: 'development', PROD: 'production' };
|
2026-04-17 17:20:41 +08:00
|
|
|
|
|
2026-04-21 14:57:52 +08:00
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
|
|
|
|
const env = loadEnv(mode, '.', '');
|
|
|
|
|
|
return {
|
2026-07-13 16:25:44 +08:00
|
|
|
|
|
2026-04-24 16:18:24 +08:00
|
|
|
|
plugins: [
|
|
|
|
|
|
react(),
|
|
|
|
|
|
tailwindcss(),
|
2026-07-08 11:03:29 +08:00
|
|
|
|
|
2026-05-12 08:34:13 +08:00
|
|
|
|
cesiumPlugin(), viteStaticCopy({
|
|
|
|
|
|
targets: [
|
|
|
|
|
|
{
|
|
|
|
|
|
src: "node_modules/cesium/Build/Cesium",
|
|
|
|
|
|
dest: ""
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-04-24 16:18:24 +08:00
|
|
|
|
],
|
|
|
|
|
|
|
2026-04-21 14:57:52 +08:00
|
|
|
|
resolve: {
|
|
|
|
|
|
alias: {
|
|
|
|
|
|
'@': resolve('src'),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-04-24 16:18:24 +08:00
|
|
|
|
|
|
|
|
|
|
// ---- 性能优化构建配置 ----
|
|
|
|
|
|
build: {
|
|
|
|
|
|
// 启用 CSS 代码分割(避免样式重复打包)
|
|
|
|
|
|
cssCodeSplit: true,
|
|
|
|
|
|
// 生成 sourcemap(生产可关闭)
|
|
|
|
|
|
sourcemap: false,
|
|
|
|
|
|
// chunk 大小警告阈值
|
|
|
|
|
|
chunkSizeWarningLimit: 1000, // KB
|
|
|
|
|
|
|
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
|
output: {
|
|
|
|
|
|
// ---- 手动分包策略 ----
|
2026-09-20 13:05:11 +08:00
|
|
|
|
// 将第三方库按类型拆分,利于浏览器并行加载与长期缓存
|
2026-09-21 15:13:28 +08:00
|
|
|
|
//manualChunks(id) {
|
|
|
|
|
|
// if (!id.includes('node_modules')) return;
|
|
|
|
|
|
|
|
|
|
|
|
// // 1. React 生态
|
|
|
|
|
|
// if (
|
|
|
|
|
|
// id.includes('node_modules/react') ||
|
|
|
|
|
|
// id.includes('node_modules/react-dom') ||
|
|
|
|
|
|
// id.includes('node_modules/react-router') ||
|
|
|
|
|
|
// id.includes('node_modules/react-redux') ||
|
|
|
|
|
|
// id.includes('node_modules/@reduxjs') ||
|
|
|
|
|
|
// id.includes('node_modules/scheduler')
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
// return 'react-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 2. Ant Design 组件库
|
|
|
|
|
|
// if (
|
|
|
|
|
|
// id.includes('node_modules/antd') ||
|
|
|
|
|
|
// id.includes('node_modules/@ant-design') ||
|
|
|
|
|
|
// id.includes('node_modules/@rc-component') ||
|
|
|
|
|
|
// id.includes('node_modules/rc-')
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
// return 'antd-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 3. 图表库(echarts / recharts 体积较大)
|
|
|
|
|
|
// if (
|
|
|
|
|
|
// id.includes('node_modules/echarts') ||
|
|
|
|
|
|
// id.includes('node_modules/recharts') ||
|
|
|
|
|
|
// id.includes('node_modules/zrender')
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
// return 'charts-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 4. 实时音视频 SDK
|
|
|
|
|
|
// if (
|
|
|
|
|
|
// id.includes('node_modules/agora-rtc-sdk-ng') ||
|
|
|
|
|
|
// id.includes('node_modules/@volcengine/rtc')
|
|
|
|
|
|
// ) {
|
|
|
|
|
|
// return 'rtc-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 5. 仅在特定页面使用的重库,单独拆分按需加载(不进首屏 vendor)
|
|
|
|
|
|
// if (id.includes('node_modules/mqtt')) return 'mqtt-vendor';
|
|
|
|
|
|
// if (id.includes('node_modules/socket.io-client')) return 'socket-vendor';
|
|
|
|
|
|
// if (id.includes('node_modules/docx')) return 'docx-vendor';
|
|
|
|
|
|
// if (id.includes('node_modules/qrcode')) return 'qrcode-vendor';
|
|
|
|
|
|
// if (id.includes('node_modules/country-state-city')) return 'csc-vendor';
|
|
|
|
|
|
// if (id.includes('node_modules/flv.js')) return 'flv-vendor';
|
|
|
|
|
|
|
|
|
|
|
|
// // 6. 其他 node_modules(首屏通用依赖:axios、i18next、lodash 等)
|
|
|
|
|
|
// return 'vendor';
|
|
|
|
|
|
//},
|
2026-04-24 16:18:24 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
define: {
|
|
|
|
|
|
// Cesium 路径注入
|
2026-05-12 08:34:13 +08:00
|
|
|
|
CESIUM_BASE_URL: JSON.stringify(
|
|
|
|
|
|
'/Cesium'
|
|
|
|
|
|
),
|
2026-04-24 16:18:24 +08:00
|
|
|
|
// 环境变量注入
|
|
|
|
|
|
'import.meta.env.VITE_API_BASE_URL': JSON.stringify(env.VITE_API_BASE_URL ?? env.baseURL ?? ''),
|
2026-07-13 16:25:44 +08:00
|
|
|
|
'process.env': {}
|
2026-04-24 16:18:24 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
2026-04-21 14:57:52 +08:00
|
|
|
|
server: {
|
2026-04-24 16:18:24 +08:00
|
|
|
|
port: 5173,
|
|
|
|
|
|
// HMR
|
2026-04-21 14:57:52 +08:00
|
|
|
|
hmr: process.env.DISABLE_HMR !== 'true',
|
2026-04-24 16:18:24 +08:00
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// ---- 优化用户体验 ----
|
|
|
|
|
|
esbuild: {
|
|
|
|
|
|
// 日志中排除特定的包警告
|
|
|
|
|
|
logOverride: { 'this-is-undefined-in-esm': 'silent' },
|
2026-04-21 14:57:52 +08:00
|
|
|
|
},
|
|
|
|
|
|
};
|
2026-04-17 17:20:41 +08:00
|
|
|
|
});
|