@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,16 @@
|
|
|
1
|
+
import { RouterMode } from "../types";
|
|
2
|
+
import { AbstractHistory } from "./abstract.mjs";
|
|
3
|
+
import { HtmlHistory } from "./html.mjs";
|
|
4
|
+
export function createHistory({
|
|
5
|
+
router,
|
|
6
|
+
mode
|
|
7
|
+
}) {
|
|
8
|
+
switch (mode) {
|
|
9
|
+
case RouterMode.HISTORY:
|
|
10
|
+
return new HtmlHistory(router);
|
|
11
|
+
case RouterMode.ABSTRACT:
|
|
12
|
+
return new AbstractHistory(router);
|
|
13
|
+
default:
|
|
14
|
+
throw new Error("not support mode");
|
|
15
|
+
}
|
|
16
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { compile, match, pathToRegexp } from "path-to-regexp";
|
|
2
|
+
import {
|
|
3
|
+
decode,
|
|
4
|
+
encodePath,
|
|
5
|
+
normalizeLocation,
|
|
6
|
+
normalizePath,
|
|
7
|
+
parsePath,
|
|
8
|
+
stringifyPath
|
|
9
|
+
} from "../utils/index.mjs";
|
|
10
|
+
class RouteMatcher {
|
|
11
|
+
/*
|
|
12
|
+
* 路由匹配规则
|
|
13
|
+
*/
|
|
14
|
+
routeMatches;
|
|
15
|
+
/*
|
|
16
|
+
* 原始路由配置
|
|
17
|
+
*/
|
|
18
|
+
// protected routes: RouteConfig[];
|
|
19
|
+
constructor(routes) {
|
|
20
|
+
this.routeMatches = createRouteMatches(routes);
|
|
21
|
+
}
|
|
22
|
+
/*
|
|
23
|
+
* 根据配置匹配对应的路由
|
|
24
|
+
*/
|
|
25
|
+
match(rawLocation, {
|
|
26
|
+
base,
|
|
27
|
+
redirectedFrom
|
|
28
|
+
} = { base: "" }) {
|
|
29
|
+
let path = "";
|
|
30
|
+
let query = {};
|
|
31
|
+
let queryArray = {};
|
|
32
|
+
let params = {};
|
|
33
|
+
let hash = "";
|
|
34
|
+
let state = {};
|
|
35
|
+
const parsedOption = parsePath(rawLocation.path);
|
|
36
|
+
path = parsedOption.pathname;
|
|
37
|
+
query = rawLocation.query || parsedOption.query || {};
|
|
38
|
+
queryArray = rawLocation.queryArray || parsedOption.queryArray || {};
|
|
39
|
+
hash = rawLocation.hash || parsedOption.hash || "";
|
|
40
|
+
state = rawLocation.state || {};
|
|
41
|
+
const routeMatch = this.routeMatches.find(({ match: match2 }) => {
|
|
42
|
+
return match2(path);
|
|
43
|
+
});
|
|
44
|
+
if (routeMatch) {
|
|
45
|
+
const {
|
|
46
|
+
component,
|
|
47
|
+
asyncComponent,
|
|
48
|
+
compile: compile2,
|
|
49
|
+
meta,
|
|
50
|
+
redirect,
|
|
51
|
+
matched,
|
|
52
|
+
parse
|
|
53
|
+
} = routeMatch.internalRedirect || routeMatch;
|
|
54
|
+
params = rawLocation.params || parse(path).params || {};
|
|
55
|
+
const realPath = normalizePath(
|
|
56
|
+
compile2({
|
|
57
|
+
query,
|
|
58
|
+
queryArray,
|
|
59
|
+
params,
|
|
60
|
+
hash
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
const {
|
|
64
|
+
params: realParams,
|
|
65
|
+
query: realQuery,
|
|
66
|
+
queryArray: realQueryArray,
|
|
67
|
+
hash: realHash
|
|
68
|
+
} = parse(realPath);
|
|
69
|
+
const routeRecord = {
|
|
70
|
+
base,
|
|
71
|
+
path: normalizePath(
|
|
72
|
+
compile2({
|
|
73
|
+
params: realParams
|
|
74
|
+
})
|
|
75
|
+
),
|
|
76
|
+
fullPath: realPath,
|
|
77
|
+
params: realParams,
|
|
78
|
+
query: realQuery,
|
|
79
|
+
queryArray: realQueryArray,
|
|
80
|
+
hash: realHash,
|
|
81
|
+
state,
|
|
82
|
+
component,
|
|
83
|
+
asyncComponent,
|
|
84
|
+
meta,
|
|
85
|
+
redirect,
|
|
86
|
+
redirectedFrom,
|
|
87
|
+
matched
|
|
88
|
+
};
|
|
89
|
+
if (redirect) {
|
|
90
|
+
const normalizedLocation = normalizeLocation(
|
|
91
|
+
typeof redirect === "function" ? redirect(routeRecord) : redirect,
|
|
92
|
+
base
|
|
93
|
+
);
|
|
94
|
+
return this.match(normalizedLocation, {
|
|
95
|
+
base,
|
|
96
|
+
redirectedFrom: routeRecord
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return routeRecord;
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/*
|
|
104
|
+
* 获取当前路由匹配规则
|
|
105
|
+
*/
|
|
106
|
+
getRoutes() {
|
|
107
|
+
return this.routeMatches;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 新增单个路由匹配规则
|
|
111
|
+
*/
|
|
112
|
+
// public addRoute(route: RouteConfig) {
|
|
113
|
+
// this.routes.push(route);
|
|
114
|
+
// this.routeMatches = createRouteMatches(this.routes);
|
|
115
|
+
// }
|
|
116
|
+
/**
|
|
117
|
+
* 新增多个路由匹配规则
|
|
118
|
+
*/
|
|
119
|
+
// public addRoutes(routes: RouteConfig[]) {
|
|
120
|
+
// this.routes.push(...routes);
|
|
121
|
+
// this.routeMatches = createRouteMatches(this.routes);
|
|
122
|
+
// }
|
|
123
|
+
}
|
|
124
|
+
export function createRouterMatcher(routes) {
|
|
125
|
+
return new RouteMatcher(routes);
|
|
126
|
+
}
|
|
127
|
+
function createRouteMatches(routes, parent) {
|
|
128
|
+
const routeMatches = [];
|
|
129
|
+
for (const route of routes) {
|
|
130
|
+
routeMatches.push(
|
|
131
|
+
...createRouteMatch(
|
|
132
|
+
{
|
|
133
|
+
...route,
|
|
134
|
+
path: route.path instanceof Array ? route.path : [route.path]
|
|
135
|
+
},
|
|
136
|
+
parent
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return routeMatches;
|
|
141
|
+
}
|
|
142
|
+
function createRouteMatch(route, parent) {
|
|
143
|
+
const pathList = route.path instanceof Array ? route.path : [route.path];
|
|
144
|
+
const routeMatches = pathList.reduce(
|
|
145
|
+
(acc, item, index) => {
|
|
146
|
+
const { children } = route;
|
|
147
|
+
const path = normalizePath(item, parent == null ? void 0 : parent.path);
|
|
148
|
+
let regex;
|
|
149
|
+
try {
|
|
150
|
+
regex = pathToRegexp(path);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.warn(
|
|
153
|
+
`@create route rule failed on path: ${path}`,
|
|
154
|
+
route
|
|
155
|
+
);
|
|
156
|
+
return acc;
|
|
157
|
+
}
|
|
158
|
+
const toPath = compile(path, { encode: encodePath });
|
|
159
|
+
const parseParams = match(path, { decode });
|
|
160
|
+
const current = {
|
|
161
|
+
regex,
|
|
162
|
+
match: (path2) => {
|
|
163
|
+
return regex.test(path2);
|
|
164
|
+
},
|
|
165
|
+
parse: (path2) => {
|
|
166
|
+
const { pathname, query, queryArray, hash } = parsePath(path2);
|
|
167
|
+
const { params } = parseParams(pathname) || { params: {} };
|
|
168
|
+
return {
|
|
169
|
+
params: Object.assign({}, params),
|
|
170
|
+
// parse的 params 是使用 Object.create(null) 创建的没有原型的对象,需要进行包装处理
|
|
171
|
+
query,
|
|
172
|
+
queryArray,
|
|
173
|
+
hash
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
compile: ({ params = {}, query = {}, queryArray = {}, hash = "" } = {
|
|
177
|
+
params: {},
|
|
178
|
+
query: {},
|
|
179
|
+
queryArray: {},
|
|
180
|
+
hash: ""
|
|
181
|
+
}) => {
|
|
182
|
+
const pathString = toPath(params);
|
|
183
|
+
return stringifyPath({
|
|
184
|
+
pathname: pathString,
|
|
185
|
+
query,
|
|
186
|
+
queryArray,
|
|
187
|
+
hash
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
path,
|
|
191
|
+
appType: route.appType || (parent == null ? void 0 : parent.appType) || "",
|
|
192
|
+
component: route.component,
|
|
193
|
+
asyncComponent: route.asyncComponent,
|
|
194
|
+
meta: route.meta || {},
|
|
195
|
+
redirect: route.redirect,
|
|
196
|
+
/**
|
|
197
|
+
* 第一个 path 作为基准,后续 path 会内部重定向到第一个 path
|
|
198
|
+
* 同时如果父路由存在内部跳转,子路由也需要处理内部跳转
|
|
199
|
+
*/
|
|
200
|
+
internalRedirect: index > 0 || (parent == null ? void 0 : parent.internalRedirect) ? createRouteMatch(
|
|
201
|
+
{
|
|
202
|
+
...route,
|
|
203
|
+
path: pathList[0]
|
|
204
|
+
},
|
|
205
|
+
(parent == null ? void 0 : parent.internalRedirect) || parent
|
|
206
|
+
) : void 0,
|
|
207
|
+
matched: [...(parent == null ? void 0 : parent.matched) || [], route]
|
|
208
|
+
};
|
|
209
|
+
if (children && children.length > 0) {
|
|
210
|
+
acc.push(...createRouteMatches(children, current));
|
|
211
|
+
}
|
|
212
|
+
acc.push(current);
|
|
213
|
+
return acc;
|
|
214
|
+
},
|
|
215
|
+
[]
|
|
216
|
+
);
|
|
217
|
+
return route.path instanceof Array ? routeMatches : routeMatches[routeMatches.length - 1];
|
|
218
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRouterMatcher } from './create-matcher';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRouterMatcher } from "./create-matcher.mjs";
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type HistoryState, type NavigationGuard, type NavigationGuardAfter, type RegisteredConfig, type RegisteredConfigMap, type Route, type RouteRecord, type RouterBase, type RouterHistory, type RouterInitOptions, type RouterInstance, type RouterMatcher, RouterMode, type RouterOptions, type RouterRawLocation, type RouterScrollBehavior } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 路由类
|
|
4
|
+
*/
|
|
5
|
+
export declare class Router implements RouterInstance {
|
|
6
|
+
/**
|
|
7
|
+
* 当前路由对象的上级路由对象
|
|
8
|
+
*/
|
|
9
|
+
parent: RouterInstance | null;
|
|
10
|
+
options: RouterOptions;
|
|
11
|
+
/**
|
|
12
|
+
* 路由固定前置路径
|
|
13
|
+
* 需要注意的是如果使用函数返回 base,需要尽量保证相同的路径返回相同base
|
|
14
|
+
*/
|
|
15
|
+
base: RouterBase;
|
|
16
|
+
mode: RouterMode;
|
|
17
|
+
matcher: RouterMatcher;
|
|
18
|
+
history: RouterHistory;
|
|
19
|
+
scrollBehavior: RouterScrollBehavior;
|
|
20
|
+
route: Route;
|
|
21
|
+
constructor(options: RouterOptions);
|
|
22
|
+
updateRoute(route: RouteRecord): void;
|
|
23
|
+
protected applyRoute(route: RouteRecord): void;
|
|
24
|
+
resolve(location: RouterRawLocation): RouteRecord;
|
|
25
|
+
/**
|
|
26
|
+
* 新增单个路由匹配规则
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* 新增多个路由匹配规则
|
|
30
|
+
*/
|
|
31
|
+
init(options?: RouterInitOptions): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 卸载方法
|
|
34
|
+
*/
|
|
35
|
+
destroy(): Promise<void>;
|
|
36
|
+
registeredConfigMap: RegisteredConfigMap;
|
|
37
|
+
register(name: string, config: (router: RouterInstance) => RegisteredConfig): void;
|
|
38
|
+
readonly guards: {
|
|
39
|
+
beforeEach: NavigationGuard[];
|
|
40
|
+
afterEach: NavigationGuardAfter[];
|
|
41
|
+
};
|
|
42
|
+
beforeEach(guard: NavigationGuard): void;
|
|
43
|
+
unBindBeforeEach(guard: NavigationGuard): void;
|
|
44
|
+
afterEach(guard: NavigationGuardAfter): void;
|
|
45
|
+
unBindAfterEach(guard: NavigationGuardAfter): void;
|
|
46
|
+
push(location: RouterRawLocation): Promise<void>;
|
|
47
|
+
replace(location: RouterRawLocation): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* 当前路由弹层id,用于区分不同的路由弹层
|
|
50
|
+
*/
|
|
51
|
+
layerId: number;
|
|
52
|
+
/**
|
|
53
|
+
* 路由弹层配置
|
|
54
|
+
* key为路由弹层id
|
|
55
|
+
*/
|
|
56
|
+
layerConfigList: Array<{
|
|
57
|
+
/**
|
|
58
|
+
* 路由弹层id
|
|
59
|
+
*/
|
|
60
|
+
id: number;
|
|
61
|
+
/**
|
|
62
|
+
* 路由弹层深度
|
|
63
|
+
*/
|
|
64
|
+
depth: number;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* 路由弹层id与路由实例的map
|
|
68
|
+
*/
|
|
69
|
+
layerMap: Record<number, {
|
|
70
|
+
router: RouterInstance;
|
|
71
|
+
config: RegisteredConfig;
|
|
72
|
+
destroyed: boolean;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* 路由是否冻结
|
|
76
|
+
*/
|
|
77
|
+
isFrozen: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 路由冻结方法
|
|
80
|
+
*/
|
|
81
|
+
freeze(): void;
|
|
82
|
+
/**
|
|
83
|
+
* 路由解冻方法
|
|
84
|
+
*/
|
|
85
|
+
unfreeze(): void;
|
|
86
|
+
/**
|
|
87
|
+
* 打开路由弹层方法,会创建新的路由实例并调用注册的 register 方法
|
|
88
|
+
* 服务端会使用 push 作为替代
|
|
89
|
+
*/
|
|
90
|
+
pushLayer(location: RouterRawLocation): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* 更新路由弹层方法
|
|
93
|
+
* @param state 参数为history.state
|
|
94
|
+
* @description 没有传入 state 时使用当前配置更新 history.state,传入了 state 时使用传入的 state 更新当前配置
|
|
95
|
+
*/
|
|
96
|
+
checkLayerState(state: HistoryState): boolean;
|
|
97
|
+
updateLayerState(route: RouteRecord): void;
|
|
98
|
+
/**
|
|
99
|
+
* 新开浏览器窗口的方法,在服务端会调用 push 作为替代
|
|
100
|
+
*/
|
|
101
|
+
pushWindow(location: RouterRawLocation): void;
|
|
102
|
+
/**
|
|
103
|
+
* 替换当前浏览器窗口的方法,在服务端会调用 replace 作为替代
|
|
104
|
+
*/
|
|
105
|
+
replaceWindow(location: RouterRawLocation): void;
|
|
106
|
+
go(delta?: number): void;
|
|
107
|
+
forward(): void;
|
|
108
|
+
back(): void;
|
|
109
|
+
getRoutes(): any;
|
|
110
|
+
}
|
|
111
|
+
export declare function createRouter(options: RouterOptions): RouterInstance;
|