aliases.mjs
7.49 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const HERE = path.dirname(fileURLToPath(import.meta.url));
/** 仓库根:packages/vite-config/src -> ../../.. */
export const ROOT = path.resolve(HERE, '../../..');
/**
* 工作区源码包统一排除出依赖预打包(optimizeDeps),shell / 子应用配置共用。
*
* 这些包的 package.json main/types 直接指向 src/*.ts(无 dist 构建步骤),本意是
* 「源码直出、改了即生效」。但 Vite 默认把 node_modules 里的依赖(含 pnpm symlink
* 过去的 workspace 包)预打包进 node_modules/.vite/deps,结果在服务启动时固化:
* 改了 src 既不触发 HMR、缓存也不失效,表现就是「代码改了、typecheck 过了、
* 浏览器还是旧行为」,必须手动清缓存+重启。此坑已多次踩到,在此系统性根治。
*
* 排除后这些包走源码 transform,受 watcher 管辖,改 src 即时 HMR。
*/
/**
* 第三方重依赖必须显式列入 optimizeDeps.include,否则 wujie 子应用首次打开会「启动超时」。
*
* 成因:上面的 WORKSPACE_SRC_PACKAGES 被排除出预构建后,Vite 的依赖扫描器不会递归进
* @uj/ui / @uj/biz / @uj/platform-sdk 的源码,发现不了它们 import 的 antd / icons /
* antd/locale/zh_CN。浏览器首次请求到这些模块时 Vite 才临时预构建,随后打
* `optimized dependencies changed. reloading` 强制整页 reload —— wujie iframe 恰好在
* 握手窗口被重载,`<short>:ready` 丢失,主壳 15 秒内等不到上报就报启动失败。
* 启动时预构建完这批依赖,首次访问不再触发 reload。
*
* 子应用共用集:每个子应用都直接依赖(catalog:),且经 workspace 包间接引用。
* wujie 只在 shell 直接依赖(子应用 node_modules 无该链接),归到 SHELL 专有集,
* 否则子应用配了会因解析不到而启动失败。
*/
export const COMMON_PREBUNDLE_DEPS = [
'react',
'react-dom',
'react-dom/client',
'react/jsx-runtime',
'react-router-dom',
'antd',
'antd/locale/zh_CN',
'@ant-design/icons',
'@tanstack/react-query',
'dayjs',
];
export const SHELL_PREBUNDLE_DEPS = ['wujie'];
export const WORKSPACE_SRC_PACKAGES = [
'@uj/download-center',
'@uj/contracts',
'@uj/api-core',
'@uj/platform-core',
'@uj/platform-sdk',
'@uj/biz',
'@uj/design-tokens',
'@uj/ui',
'@uj/vite-config',
'@uj/eslint-config',
'@uj/typescript-config',
];
/**
* 单一真源:老项目 `src/**` 里的 `@/xxx` import 不动一个字,靠 alias 把不同职责
* 映射到不同包。这是 wujie 改造能「零 import 重写」的关键。
*
* 两类应用的差异只有前 4 条:
* - shell -> 指向 @uj/platform-core(真实 axios 实例 / React Context / zustand)
* - subapp -> 指向 @uj/platform-sdk/compat(props.request 代理 / 纯 localStorage 权限)
*
* ⚠️ 顺序敏感:具体前缀必须排在 `@/api`、`@/theme` 与 `@/(.*)` 兜底之前。
* Vite alias 走 @rollup/plugin-alias,正则捕获组用 `$1` 回填;组未参与匹配时
* 替换为空串,因此 `dir + '$1'` 同时覆盖 `@/x` 与 `@/x/y` 两种写法。
*
* @param {{ appDir: string, kind: 'shell' | 'subapp' }} options
*/
export function createAliases({ appDir, kind }) {
const isShell = kind === 'shell';
const pkg = (...p) => path.join(ROOT, 'packages', ...p);
const at = (...p) => path.join(appDir, 'src', ...p);
const apiCore = isShell
? pkg('platform-core', 'src', 'api', 'core')
: pkg('platform-sdk', 'src', 'compat', 'api-core');
const access = isShell
? pkg('platform-core', 'src', 'access')
: pkg('platform-sdk', 'src', 'compat', 'access');
const store = isShell
? pkg('platform-core', 'src', 'store')
: pkg('platform-sdk', 'src', 'compat', 'store');
const queryClient = isShell
? pkg('platform-core', 'src', 'queryClient.ts')
: pkg('platform-sdk', 'src', 'compat', 'queryClient.ts');
return [
// ---- host 独占运行时 vs 子应用兼容层 ----
{ find: /^@\/api\/core(\/.*)?$/, replacement: apiCore + '$1' },
{ find: /^@\/api\/core$/, replacement: apiCore },
{ find: /^@\/access(\/.*)?$/, replacement: access + '$1' },
{ find: /^@\/store(\/.*)?$/, replacement: store + '$1' },
{ find: /^@\/queryClient$/, replacement: queryClient },
// ---- 跨域共享(两边一致)----
{ find: /^@\/theme\/themeConfig$/, replacement: pkg('design-tokens', 'src', 'themeConfig.ts') },
{ find: /^@\/components(\/.*)?$/, replacement: pkg('biz', 'src', 'components') + '$1' },
{ find: /^@\/constants(\/.*)?$/, replacement: pkg('biz', 'src', 'constants') + '$1' },
{ find: /^@\/config(\/.*)?$/, replacement: pkg('biz', 'src', 'config') + '$1' },
{ find: /^@\/utils(\/.*)?$/, replacement: pkg('biz', 'src', 'utils') + '$1' },
// ---- 应用自身 ----
{ find: /^@\/api(\/.*)?$/, replacement: at('api') + '$1' },
{ find: /^@\/theme(\/.*)?$/, replacement: at('theme') + '$1' },
{ find: /^@uj\/api-core$/, replacement: pkg('api-core', 'src', 'index.ts') },
{ find: /^@uj\/ui$/, replacement: pkg('ui', 'src', 'index.ts') },
{ find: /^@\/(.*)$/, replacement: at('$1') },
];
}
/**
* 与 createAliases 同源的 tsconfig paths。编译器与打包器必须看到同一张表,
* 否则会出现「dev 能跑、tsc 报找不到模块」。
* 返回的路径相对 appDir,且一律以 ./ 或 ../ 开头:TS 4.1+ 的 paths 相对于声明它的
* tsconfig 解析,不需要 baseUrl(baseUrl 自 TS 6 起已弃用,非相对路径会报 TS5090)。
*/
export function createTsconfigPaths({ appDir, kind }) {
const isShell = kind === 'shell';
const rel = (...p) =>
path
.relative(appDir, path.join(ROOT, 'packages', ...p))
.split(path.sep)
.join('/');
const apiCore = isShell
? rel('platform-core', 'src', 'api', 'core')
: rel('platform-sdk', 'src', 'compat', 'api-core');
const access = isShell
? rel('platform-core', 'src', 'access')
: rel('platform-sdk', 'src', 'compat', 'access');
const store = isShell
? rel('platform-core', 'src', 'store')
: rel('platform-sdk', 'src', 'compat', 'store');
const queryClient = isShell
? rel('platform-core', 'src', 'queryClient.ts')
: rel('platform-sdk', 'src', 'compat', 'queryClient.ts');
return {
'@/api/core': [`${apiCore}/index.ts`],
'@/api/core/*': [`${apiCore}/*`],
'@/access': [`${access}/index.ts`],
'@/access/*': [`${access}/*`],
'@/store/*': [`${store}/*`],
'@/queryClient': [queryClient],
'@/theme/themeConfig': [`${rel('design-tokens', 'src', 'themeConfig.ts')}`],
'@/components': [`${rel('biz', 'src', 'components', 'index.ts')}`],
'@/components/*': [`${rel('biz', 'src', 'components')}/*`],
'@/constants/*': [`${rel('biz', 'src', 'constants')}/*`],
'@/config/*': [`${rel('biz', 'src', 'config')}/*`],
'@/utils': [`${rel('biz', 'src', 'utils', 'index.ts')}`],
'@/utils/*': [`${rel('biz', 'src', 'utils')}/*`],
'@/api': ['./src/api/index.ts'],
'@/api/*': ['./src/api/*'],
'@/theme/*': ['./src/theme/*'],
'@uj/ui': [`${rel('ui', 'src', 'index.ts')}`],
'@uj/contracts': [`${rel('contracts', 'src', 'index.ts')}`],
'@uj/platform-core': [`${rel('platform-core', 'src', 'index.ts')}`],
'@uj/platform-sdk': [`${rel('platform-sdk', 'src', 'index.ts')}`],
'@uj/biz': [`${rel('biz', 'src', 'index.ts')}`],
'@uj/design-tokens': [`${rel('design-tokens', 'src', 'index.ts')}`],
'@uj/api-core': [`${rel('api-core', 'src', 'index.ts')}`],
'@/*': ['./src/*'],
};
}