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)
|
|
|
|
|
|
// - gzip 压缩
|
|
|
|
|
|
// - 路径别名
|
|
|
|
|
|
// - 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-05-06 16:16:29 +08:00
|
|
|
|
//manualChunks(id) {
|
|
|
|
|
|
// // 1. 将 React 生态单独打包(利用浏览器缓存)
|
|
|
|
|
|
// if (id.includes('node_modules/react') || id.includes('node_modules/react-dom')) {
|
|
|
|
|
|
// return 'react-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // 2. Ant Design 单独打包
|
|
|
|
|
|
// if (id.includes('node_modules/antd') || id.includes('node_modules/@ant-design')) {
|
|
|
|
|
|
// return 'antd-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // 3. Cesium 单独打包(体积巨大)
|
|
|
|
|
|
// if (id.includes('node_modules/cesium') || id.includes('cesium')) {
|
|
|
|
|
|
// return 'cesium-vendor';
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // 4. 其他 node_modules 合并
|
|
|
|
|
|
// if (id.includes('node_modules')) {
|
|
|
|
|
|
// 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
|
|
|
|
});
|