vite.config.ts
2.31 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
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';
/**
* 数据产物体检。
*
* public/data/ 是 scripts/preprocess.mjs 的产物,且被 .gitignore 忽略。
* 拉了代码但没跑 `pnpm data:build` 时,页面会表现为「地图正常但一个点都没有」——
* 这是最容易被误判成"地图接错了"的假故障,所以在 dev server 启动时就明确报出来。
*/
function dataReadinessPlugin(root: string) {
const DATA_DIR = path.resolve(root, 'public/data');
const REQUIRED = ['meta.json', 'incidents.json'];
return {
name: 'data-readiness',
configureServer(server: any) {
const missing = REQUIRED.filter((f) => !fs.existsSync(path.join(DATA_DIR, f)));
if (missing.length) {
server.config.logger.warn(
`\n [data] 缺少数据产物(${DATA_DIR}):\n` +
missing.map((f) => ` - ${f}`).join('\n') +
`\n 请先执行:pnpm data:build\n`,
);
} 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)),
},
},
server: {
// 监听全部网卡:演示时需要挂到局域网大屏 / 别人机器上跑
host: '0.0.0.0',
// 与 uj-store 的 5173 错开,两个项目可同时起
port: 5174,
strictPort: false,
},
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',
},
});