vite.config.ts
3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { fileURLToPath, URL } from 'node:url';
import fs from 'node:fs';
import path from 'node:path';
/**
* 数据产物体检。
*
* 出险数据集不内置、也不随仓库分发,只能由用户通过顶栏「导入数据」进入浏览器
* IndexedDB。因此「没有 public/data 产物」是常态、不是故障,dev server 启动只做
* 信息提示(离线生成管线已从仓库移除)。
*
* 行政区边界是另一回事:它作为常驻静态资源 `public/geo/shenzhen.geojson` 随构建走,
* 这里顺带确认它在,缺了才会告警(那说明构建没带上底图资源)。
*/
function dataReadinessPlugin(root: string) {
const DATA_DIR = path.resolve(root, 'public/data');
const GEO = path.resolve(root, 'public/geo/shenzhen.geojson');
return {
name: 'data-readiness',
configureServer(server: any) {
if (!fs.existsSync(GEO)) {
server.config.logger.warn(
`\n [data] 缺少底图资源 public/geo/shenzhen.geojson,区界聚焦将不可用。\n`,
);
}
if (!fs.existsSync(path.join(DATA_DIR, 'incidents.json'))) {
server.config.logger.info(
' [data] 未内置出险数据 · 请通过顶栏「导入数据」选择本地 xlsx / csv 导入',
{ timestamp: true },
);
} else {
const st = fs.statSync(path.join(DATA_DIR, 'incidents.json'));
server.config.logger.info(
` [data] 已就绪 · incidents.json ${(st.size / 1024 / 1024).toFixed(2)} MB`,
{ timestamp: true },
);
}
},
};
}
export default defineConfig({
plugins: [react(), dataReadinessPlugin(process.cwd())],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
// 组件库源码直引:即使 workspace 软链异常,dev / 构建也能解析
'@hm/ui': fileURLToPath(new URL('./packages/ui/src/index.ts', import.meta.url)),
// 自绘图层内核同样源码直引:它没有构建产物,改一行就该在 dev 里立刻看见
'@hm/map-overlay': fileURLToPath(
new URL('./packages/map-overlay/src/index.ts', import.meta.url),
),
},
},
server: {
// 监听全部网卡:演示时需要挂到局域网大屏 / 别人机器上跑
host: '0.0.0.0',
// 与 uj-store 的 5173 错开,两个项目可同时起
port: 5174,
strictPort: false,
// 登录后台服务(packages/server,默认 8787)同源接入:dev 下把 /api 透传过去,
// 前端无需关心端口、也避开 CORS;生产由 Nginx 同源反代到 Node 进程
proxy: {
'/api': {
target: 'http://localhost:8787',
changeOrigin: true,
},
},
},
build: {
// 高德 JS API 是外链 script,不进 bundle;但 react/antd 体积可观,拆开便于并行下载
rollupOptions: {
output: {
manualChunks: {
react: ['react', 'react-dom', 'react-router-dom'],
antd: ['antd', '@ant-design/icons'],
},
},
},
chunkSizeWarningLimit: 1200,
},
worker: {
format: 'es',
},
});