aliases.mjs
5.28 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
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, '../../..');
/**
* 单一真源:老项目 `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/*'],
};
}