@esmx/router 3.0.0-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/history/abstract.d.ts +29 -0
- package/dist/history/abstract.mjs +107 -0
- package/dist/history/base.d.ts +79 -0
- package/dist/history/base.mjs +275 -0
- package/dist/history/html.d.ts +22 -0
- package/dist/history/html.mjs +181 -0
- package/dist/history/index.d.ts +7 -0
- package/dist/history/index.mjs +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/matcher/create-matcher.d.ts +5 -0
- package/dist/matcher/create-matcher.mjs +218 -0
- package/dist/matcher/create-matcher.spec.d.ts +1 -0
- package/dist/matcher/create-matcher.spec.mjs +0 -0
- package/dist/matcher/index.d.ts +1 -0
- package/dist/matcher/index.mjs +1 -0
- package/dist/router.d.ts +111 -0
- package/dist/router.mjs +399 -0
- package/dist/task-pipe/index.d.ts +1 -0
- package/dist/task-pipe/index.mjs +1 -0
- package/dist/task-pipe/task.d.ts +30 -0
- package/dist/task-pipe/task.mjs +66 -0
- package/dist/utils/bom.d.ts +5 -0
- package/dist/utils/bom.mjs +10 -0
- package/dist/utils/encoding.d.ts +48 -0
- package/dist/utils/encoding.mjs +44 -0
- package/dist/utils/guards.d.ts +9 -0
- package/dist/utils/guards.mjs +12 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.mjs +27 -0
- package/dist/utils/path.d.ts +60 -0
- package/dist/utils/path.mjs +264 -0
- package/dist/utils/path.spec.d.ts +1 -0
- package/dist/utils/path.spec.mjs +30 -0
- package/dist/utils/scroll.d.ts +25 -0
- package/dist/utils/scroll.mjs +59 -0
- package/dist/utils/utils.d.ts +16 -0
- package/dist/utils/utils.mjs +11 -0
- package/dist/utils/warn.d.ts +2 -0
- package/dist/utils/warn.mjs +12 -0
- package/package.json +66 -0
- package/src/history/abstract.ts +149 -0
- package/src/history/base.ts +408 -0
- package/src/history/html.ts +231 -0
- package/src/history/index.ts +20 -0
- package/src/index.ts +3 -0
- package/src/matcher/create-matcher.spec.ts +3 -0
- package/src/matcher/create-matcher.ts +293 -0
- package/src/matcher/index.ts +1 -0
- package/src/router.ts +521 -0
- package/src/task-pipe/index.ts +1 -0
- package/src/task-pipe/task.ts +97 -0
- package/src/utils/bom.ts +14 -0
- package/src/utils/encoding.ts +153 -0
- package/src/utils/guards.ts +25 -0
- package/src/utils/index.ts +27 -0
- package/src/utils/path.spec.ts +44 -0
- package/src/utils/path.ts +397 -0
- package/src/utils/scroll.ts +120 -0
- package/src/utils/utils.ts +30 -0
- package/src/utils/warn.ts +13 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RouterInstance,
|
|
3
|
+
type RouterRawLocation,
|
|
4
|
+
StateLayerConfigKey
|
|
5
|
+
} from '../types';
|
|
6
|
+
import {
|
|
7
|
+
computeScrollPosition,
|
|
8
|
+
getKeepScrollPosition,
|
|
9
|
+
getSavedScrollPosition,
|
|
10
|
+
isPathWithProtocolOrDomain,
|
|
11
|
+
normalizeLocation,
|
|
12
|
+
openWindow,
|
|
13
|
+
saveScrollPosition,
|
|
14
|
+
scrollToPosition
|
|
15
|
+
} from '../utils';
|
|
16
|
+
import { BaseRouterHistory } from './base';
|
|
17
|
+
|
|
18
|
+
export class HtmlHistory extends BaseRouterHistory {
|
|
19
|
+
constructor(router: RouterInstance) {
|
|
20
|
+
super(router);
|
|
21
|
+
|
|
22
|
+
if ('scrollRestoration' in window.history) {
|
|
23
|
+
// 只有在 html 模式下才需要修改历史滚动模式
|
|
24
|
+
window.history.scrollRestoration = 'manual';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 获取当前地址,包括 path query hash
|
|
29
|
+
getCurrentLocation() {
|
|
30
|
+
const { href } = window.location;
|
|
31
|
+
const { state } = window.history;
|
|
32
|
+
const { path, base, ...rest } = normalizeLocation(
|
|
33
|
+
href,
|
|
34
|
+
this.router.base
|
|
35
|
+
);
|
|
36
|
+
return {
|
|
37
|
+
path: path.replace(new RegExp(`^(${base})`), ''),
|
|
38
|
+
base,
|
|
39
|
+
...rest,
|
|
40
|
+
state
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
onPopState = (e: PopStateEvent) => {
|
|
45
|
+
if (this.isFrozen) return;
|
|
46
|
+
if (this.router.checkLayerState(e.state)) return;
|
|
47
|
+
|
|
48
|
+
const current = Object.assign({}, this.current);
|
|
49
|
+
|
|
50
|
+
// 当路由变化时触发跳转事件
|
|
51
|
+
this.transitionTo(this.getCurrentLocation(), async (route) => {
|
|
52
|
+
const { state } = window.history;
|
|
53
|
+
saveScrollPosition(current.fullPath, computeScrollPosition());
|
|
54
|
+
setTimeout(async () => {
|
|
55
|
+
const keepScrollPosition = state.keepScrollPosition;
|
|
56
|
+
if (keepScrollPosition) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const savedPosition = getSavedScrollPosition(route.fullPath);
|
|
60
|
+
const position = await this.router.scrollBehavior(
|
|
61
|
+
current,
|
|
62
|
+
route,
|
|
63
|
+
savedPosition
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const { nextTick } = this.router.options;
|
|
67
|
+
if (position) {
|
|
68
|
+
nextTick && (await nextTick());
|
|
69
|
+
scrollToPosition(position);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
async init({ replace }: { replace?: boolean } = { replace: true }) {
|
|
76
|
+
const { initUrl } = this.router.options;
|
|
77
|
+
let route = this.getCurrentLocation();
|
|
78
|
+
if (initUrl !== undefined) {
|
|
79
|
+
// 存在 initUrl 则用 initUrl 进行初始化
|
|
80
|
+
route = this.resolve(initUrl) as any;
|
|
81
|
+
} else {
|
|
82
|
+
const state = history.state || {};
|
|
83
|
+
route.state = {
|
|
84
|
+
...state,
|
|
85
|
+
_ancientRoute: state._ancientRoute ?? true // 最古历史的标记, 在调用返回事件时如果有这个标记则直接调用没有历史记录的钩子
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (replace) {
|
|
89
|
+
await this.replace(route as RouterRawLocation);
|
|
90
|
+
} else {
|
|
91
|
+
await this.push(route as RouterRawLocation);
|
|
92
|
+
}
|
|
93
|
+
this.setupListeners();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 设置监听函数
|
|
97
|
+
setupListeners() {
|
|
98
|
+
window.addEventListener('popstate', this.onPopState);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
destroy() {
|
|
102
|
+
window.removeEventListener('popstate', this.onPopState);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
pushWindow(location: RouterRawLocation) {
|
|
106
|
+
if (this.isFrozen) return;
|
|
107
|
+
this.handleOutside(location, false, true);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
replaceWindow(location: RouterRawLocation) {
|
|
111
|
+
if (this.isFrozen) return;
|
|
112
|
+
this.handleOutside(location, true, true);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 处理外站跳转逻辑
|
|
116
|
+
handleOutside(
|
|
117
|
+
location: RouterRawLocation,
|
|
118
|
+
replace = false,
|
|
119
|
+
// 是否是 pushWindow/replaceWindow 触发的
|
|
120
|
+
isTriggerWithWindow = false
|
|
121
|
+
) {
|
|
122
|
+
const { flag, route } = isPathWithProtocolOrDomain(location);
|
|
123
|
+
const router = this.router;
|
|
124
|
+
const { handleOutside, validateOutside } = router.options;
|
|
125
|
+
|
|
126
|
+
// 不以域名开头 或 域名相同 都认为是同域
|
|
127
|
+
const isSameHost = !flag || window.location.hostname === route.hostname;
|
|
128
|
+
|
|
129
|
+
// 如果 不是 pushWindow/replaceWindow 触发的
|
|
130
|
+
if (!isTriggerWithWindow) {
|
|
131
|
+
// 如果域名相同 和 非外站(存在就算同域也会被视为外站的情况) 则跳出
|
|
132
|
+
if (isSameHost && !validateOutside?.({ router, location, route })) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 如果有配置跳转外站函数,则执行配置函数
|
|
138
|
+
// 如果配置函数返回 true 则认为其处理了打开逻辑,跳出
|
|
139
|
+
if (
|
|
140
|
+
handleOutside?.({
|
|
141
|
+
router,
|
|
142
|
+
route,
|
|
143
|
+
replace,
|
|
144
|
+
isTriggerWithWindow,
|
|
145
|
+
isSameHost
|
|
146
|
+
})
|
|
147
|
+
) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (replace) {
|
|
152
|
+
window.location.replace(route.href);
|
|
153
|
+
} else {
|
|
154
|
+
const { hostname, href } = route;
|
|
155
|
+
openWindow(href, hostname);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 新增路由记录跳转
|
|
162
|
+
async push(location: RouterRawLocation) {
|
|
163
|
+
await this.jump(location, false);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 替换当前路由记录跳转
|
|
167
|
+
async replace(location: RouterRawLocation) {
|
|
168
|
+
await this.jump(location, true);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 跳转方法
|
|
172
|
+
async jump(location: RouterRawLocation, replace = false) {
|
|
173
|
+
if (this.isFrozen) return;
|
|
174
|
+
if (this.handleOutside(location, replace)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const current = Object.assign({}, this.current);
|
|
179
|
+
await this.transitionTo(location, (route) => {
|
|
180
|
+
const keepScrollPosition = getKeepScrollPosition(location);
|
|
181
|
+
if (!keepScrollPosition) {
|
|
182
|
+
saveScrollPosition(current.fullPath, computeScrollPosition());
|
|
183
|
+
scrollToPosition({ left: 0, top: 0 });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const state = Object.assign(
|
|
187
|
+
replace
|
|
188
|
+
? { ...history.state, ...route.state }
|
|
189
|
+
: { ...route.state, _ancientRoute: false },
|
|
190
|
+
{ keepScrollPosition }
|
|
191
|
+
);
|
|
192
|
+
window.history[replace ? 'replaceState' : 'pushState'](
|
|
193
|
+
state,
|
|
194
|
+
'',
|
|
195
|
+
route.fullPath
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
this.router.updateLayerState(route);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
go(delta: number): void {
|
|
203
|
+
if (this.isFrozen) return;
|
|
204
|
+
window.history.go(delta);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
forward(): void {
|
|
208
|
+
if (this.isFrozen) return;
|
|
209
|
+
window.history.forward();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
protected timer: NodeJS.Timeout | null = null;
|
|
213
|
+
|
|
214
|
+
back(): void {
|
|
215
|
+
if (this.isFrozen) return;
|
|
216
|
+
const oldState = history.state;
|
|
217
|
+
const noBackNavigation = this.router.options.noBackNavigation;
|
|
218
|
+
if (oldState._ancientRoute === true) {
|
|
219
|
+
noBackNavigation && noBackNavigation(this.router);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
window.history.back();
|
|
224
|
+
this.timer = setTimeout(() => {
|
|
225
|
+
if (history.state === oldState) {
|
|
226
|
+
noBackNavigation && noBackNavigation(this.router);
|
|
227
|
+
}
|
|
228
|
+
this.timer = null;
|
|
229
|
+
}, 80);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type RouterInstance, RouterMode } from '../types';
|
|
2
|
+
import { AbstractHistory } from './abstract';
|
|
3
|
+
import { HtmlHistory } from './html';
|
|
4
|
+
|
|
5
|
+
export function createHistory({
|
|
6
|
+
router,
|
|
7
|
+
mode
|
|
8
|
+
}: {
|
|
9
|
+
router: RouterInstance;
|
|
10
|
+
mode: RouterMode;
|
|
11
|
+
}) {
|
|
12
|
+
switch (mode) {
|
|
13
|
+
case RouterMode.HISTORY:
|
|
14
|
+
return new HtmlHistory(router);
|
|
15
|
+
case RouterMode.ABSTRACT:
|
|
16
|
+
return new AbstractHistory(router);
|
|
17
|
+
default:
|
|
18
|
+
throw new Error('not support mode');
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { compile, match, pathToRegexp } from 'path-to-regexp';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
HistoryState,
|
|
5
|
+
RouteConfig,
|
|
6
|
+
RouteMatch,
|
|
7
|
+
RouteRecord,
|
|
8
|
+
RouterLocation,
|
|
9
|
+
RouterMatcher
|
|
10
|
+
} from '../types';
|
|
11
|
+
import {
|
|
12
|
+
decode,
|
|
13
|
+
encodePath,
|
|
14
|
+
normalizeLocation,
|
|
15
|
+
normalizePath,
|
|
16
|
+
parsePath,
|
|
17
|
+
stringifyPath
|
|
18
|
+
} from '../utils';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 路由匹配器
|
|
22
|
+
*/
|
|
23
|
+
class RouteMatcher {
|
|
24
|
+
/*
|
|
25
|
+
* 路由匹配规则
|
|
26
|
+
*/
|
|
27
|
+
protected routeMatches: RouteMatch[];
|
|
28
|
+
|
|
29
|
+
/*
|
|
30
|
+
* 原始路由配置
|
|
31
|
+
*/
|
|
32
|
+
// protected routes: RouteConfig[];
|
|
33
|
+
|
|
34
|
+
constructor(routes: RouteConfig[]) {
|
|
35
|
+
// this.routes = routes;
|
|
36
|
+
this.routeMatches = createRouteMatches(routes);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
* 根据配置匹配对应的路由
|
|
41
|
+
*/
|
|
42
|
+
public match(
|
|
43
|
+
rawLocation: RouterLocation,
|
|
44
|
+
{
|
|
45
|
+
base,
|
|
46
|
+
redirectedFrom
|
|
47
|
+
}: { base: string; redirectedFrom?: RouteRecord } = { base: '' }
|
|
48
|
+
): RouteRecord | null {
|
|
49
|
+
let path = '';
|
|
50
|
+
/* 按 Hanson 要求加入 undefined 类型 */
|
|
51
|
+
let query: Record<string, string | undefined> = {};
|
|
52
|
+
let queryArray: Record<string, string[]> = {};
|
|
53
|
+
let params: Record<string, string> = {};
|
|
54
|
+
let hash = '';
|
|
55
|
+
let state: HistoryState = {};
|
|
56
|
+
|
|
57
|
+
const parsedOption = parsePath(rawLocation.path);
|
|
58
|
+
path = parsedOption.pathname;
|
|
59
|
+
query = rawLocation.query || parsedOption.query || {};
|
|
60
|
+
queryArray = rawLocation.queryArray || parsedOption.queryArray || {};
|
|
61
|
+
hash = rawLocation.hash || parsedOption.hash || '';
|
|
62
|
+
state = rawLocation.state || {};
|
|
63
|
+
|
|
64
|
+
const routeMatch = this.routeMatches.find(({ match }) => {
|
|
65
|
+
return match(path);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (routeMatch) {
|
|
69
|
+
const {
|
|
70
|
+
component,
|
|
71
|
+
asyncComponent,
|
|
72
|
+
compile,
|
|
73
|
+
meta,
|
|
74
|
+
redirect,
|
|
75
|
+
matched,
|
|
76
|
+
parse
|
|
77
|
+
} = routeMatch.internalRedirect || routeMatch; // 优先使用内部重定向
|
|
78
|
+
|
|
79
|
+
params = rawLocation.params || parse(path).params || {};
|
|
80
|
+
|
|
81
|
+
const realPath = normalizePath(
|
|
82
|
+
compile({
|
|
83
|
+
query,
|
|
84
|
+
queryArray,
|
|
85
|
+
params,
|
|
86
|
+
hash
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const {
|
|
91
|
+
params: realParams,
|
|
92
|
+
query: realQuery,
|
|
93
|
+
queryArray: realQueryArray,
|
|
94
|
+
hash: realHash
|
|
95
|
+
} = parse(realPath);
|
|
96
|
+
|
|
97
|
+
const routeRecord = {
|
|
98
|
+
base,
|
|
99
|
+
path: normalizePath(
|
|
100
|
+
compile({
|
|
101
|
+
params: realParams
|
|
102
|
+
})
|
|
103
|
+
),
|
|
104
|
+
fullPath: realPath,
|
|
105
|
+
params: realParams,
|
|
106
|
+
query: realQuery,
|
|
107
|
+
queryArray: realQueryArray,
|
|
108
|
+
hash: realHash,
|
|
109
|
+
state,
|
|
110
|
+
component,
|
|
111
|
+
asyncComponent,
|
|
112
|
+
meta,
|
|
113
|
+
redirect,
|
|
114
|
+
redirectedFrom,
|
|
115
|
+
matched
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (redirect) {
|
|
119
|
+
const normalizedLocation = normalizeLocation(
|
|
120
|
+
typeof redirect === 'function'
|
|
121
|
+
? redirect(routeRecord)
|
|
122
|
+
: redirect,
|
|
123
|
+
base
|
|
124
|
+
);
|
|
125
|
+
return this.match(normalizedLocation, {
|
|
126
|
+
base,
|
|
127
|
+
redirectedFrom: routeRecord
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return routeRecord;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/*
|
|
136
|
+
* 获取当前路由匹配规则
|
|
137
|
+
*/
|
|
138
|
+
public getRoutes(): RouteMatch[] {
|
|
139
|
+
return this.routeMatches;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 新增单个路由匹配规则
|
|
144
|
+
*/
|
|
145
|
+
// public addRoute(route: RouteConfig) {
|
|
146
|
+
// this.routes.push(route);
|
|
147
|
+
// this.routeMatches = createRouteMatches(this.routes);
|
|
148
|
+
// }
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 新增多个路由匹配规则
|
|
152
|
+
*/
|
|
153
|
+
// public addRoutes(routes: RouteConfig[]) {
|
|
154
|
+
// this.routes.push(...routes);
|
|
155
|
+
// this.routeMatches = createRouteMatches(this.routes);
|
|
156
|
+
// }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 创建路由匹配器
|
|
161
|
+
*/
|
|
162
|
+
export function createRouterMatcher(routes: RouteConfig[]): RouterMatcher {
|
|
163
|
+
return new RouteMatcher(routes);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 生成打平的路由匹配规则
|
|
168
|
+
*/
|
|
169
|
+
function createRouteMatches(
|
|
170
|
+
routes: RouteConfig[],
|
|
171
|
+
parent?: RouteMatch
|
|
172
|
+
): RouteMatch[] {
|
|
173
|
+
const routeMatches: RouteMatch[] = [];
|
|
174
|
+
for (const route of routes) {
|
|
175
|
+
routeMatches.push(
|
|
176
|
+
...createRouteMatch(
|
|
177
|
+
{
|
|
178
|
+
...route,
|
|
179
|
+
path:
|
|
180
|
+
route.path instanceof Array ? route.path : [route.path]
|
|
181
|
+
},
|
|
182
|
+
parent
|
|
183
|
+
)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return routeMatches;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 生成单个路由匹配规则
|
|
191
|
+
*/
|
|
192
|
+
function createRouteMatch(
|
|
193
|
+
route: RouteConfig & { path: string },
|
|
194
|
+
parent?: RouteMatch
|
|
195
|
+
): RouteMatch;
|
|
196
|
+
function createRouteMatch(
|
|
197
|
+
route: RouteConfig & { path: string[] },
|
|
198
|
+
parent?: RouteMatch
|
|
199
|
+
): RouteMatch[];
|
|
200
|
+
function createRouteMatch(
|
|
201
|
+
route: RouteConfig,
|
|
202
|
+
parent?: RouteMatch
|
|
203
|
+
): RouteMatch | RouteMatch[] {
|
|
204
|
+
const pathList = route.path instanceof Array ? route.path : [route.path];
|
|
205
|
+
const routeMatches: RouteMatch[] = pathList.reduce<RouteMatch[]>(
|
|
206
|
+
(acc, item, index) => {
|
|
207
|
+
const { children } = route;
|
|
208
|
+
const path = normalizePath(item, parent?.path);
|
|
209
|
+
let regex: RegExp;
|
|
210
|
+
try {
|
|
211
|
+
regex = pathToRegexp(path);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.warn(
|
|
214
|
+
`@create route rule failed on path: ${path}`,
|
|
215
|
+
route
|
|
216
|
+
);
|
|
217
|
+
return acc;
|
|
218
|
+
}
|
|
219
|
+
const toPath = compile(path, { encode: encodePath });
|
|
220
|
+
const parseParams = match(path, { decode });
|
|
221
|
+
const current: RouteMatch = {
|
|
222
|
+
regex,
|
|
223
|
+
match: (path: string) => {
|
|
224
|
+
return regex.test(path);
|
|
225
|
+
},
|
|
226
|
+
parse: (
|
|
227
|
+
path: string
|
|
228
|
+
): {
|
|
229
|
+
params: Record<string, string>;
|
|
230
|
+
query: Record<string, string>;
|
|
231
|
+
queryArray: Record<string, string[]>;
|
|
232
|
+
hash: string;
|
|
233
|
+
} => {
|
|
234
|
+
const { pathname, query, queryArray, hash } =
|
|
235
|
+
parsePath(path);
|
|
236
|
+
const { params } = parseParams(pathname) || { params: {} };
|
|
237
|
+
return {
|
|
238
|
+
params: Object.assign({}, params), // parse的 params 是使用 Object.create(null) 创建的没有原型的对象,需要进行包装处理
|
|
239
|
+
query,
|
|
240
|
+
queryArray,
|
|
241
|
+
hash
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
compile: (
|
|
245
|
+
{ params = {}, query = {}, queryArray = {}, hash = '' } = {
|
|
246
|
+
params: {},
|
|
247
|
+
query: {},
|
|
248
|
+
queryArray: {},
|
|
249
|
+
hash: ''
|
|
250
|
+
}
|
|
251
|
+
) => {
|
|
252
|
+
const pathString = toPath(params);
|
|
253
|
+
return stringifyPath({
|
|
254
|
+
pathname: pathString,
|
|
255
|
+
query,
|
|
256
|
+
queryArray,
|
|
257
|
+
hash
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
path,
|
|
261
|
+
appType: route.appType || parent?.appType || '',
|
|
262
|
+
component: route.component,
|
|
263
|
+
asyncComponent: route.asyncComponent,
|
|
264
|
+
meta: route.meta || {},
|
|
265
|
+
redirect: route.redirect,
|
|
266
|
+
/**
|
|
267
|
+
* 第一个 path 作为基准,后续 path 会内部重定向到第一个 path
|
|
268
|
+
* 同时如果父路由存在内部跳转,子路由也需要处理内部跳转
|
|
269
|
+
*/
|
|
270
|
+
internalRedirect:
|
|
271
|
+
index > 0 || parent?.internalRedirect
|
|
272
|
+
? createRouteMatch(
|
|
273
|
+
{
|
|
274
|
+
...route,
|
|
275
|
+
path: pathList[0]
|
|
276
|
+
},
|
|
277
|
+
parent?.internalRedirect || parent
|
|
278
|
+
)
|
|
279
|
+
: undefined,
|
|
280
|
+
matched: [...(parent?.matched || []), route]
|
|
281
|
+
};
|
|
282
|
+
if (children && children.length > 0) {
|
|
283
|
+
acc.push(...createRouteMatches(children, current));
|
|
284
|
+
}
|
|
285
|
+
acc.push(current);
|
|
286
|
+
return acc;
|
|
287
|
+
},
|
|
288
|
+
[]
|
|
289
|
+
);
|
|
290
|
+
return route.path instanceof Array
|
|
291
|
+
? routeMatches
|
|
292
|
+
: routeMatches[routeMatches.length - 1];
|
|
293
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRouterMatcher } from './create-matcher';
|