wevu 6.16.0 → 6.16.2
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/README.md +8 -0
- package/dist/api.mjs +1 -2
- package/dist/chunk-CbfVNrr-.mjs +1 -0
- package/dist/compiler.mjs +1 -2
- package/dist/dev/api.mjs +3 -0
- package/dist/dev/compiler.mjs +3 -0
- package/dist/dev/fetch.mjs +3 -0
- package/dist/dev/index.mjs +8 -0
- package/dist/dev/jsx-runtime.mjs +1 -0
- package/dist/{ref-DuEgaxvT.mjs → dev/ref-BTTXccLL.mjs} +15 -0
- package/dist/dev/ref-BTTXccLL.mjs.map +1 -0
- package/dist/{router-Ck6g5XK5.mjs → dev/router-BZZnIx32.mjs} +14 -1
- package/dist/dev/router-BZZnIx32.mjs.map +1 -0
- package/dist/dev/router.mjs +1267 -0
- package/dist/dev/router.mjs.map +1 -0
- package/dist/{src-mHDyn6h5.mjs → dev/src-DcTVnwpE.mjs} +112 -6
- package/dist/dev/src-DcTVnwpE.mjs.map +1 -0
- package/dist/{store-BhbWIoTi.mjs → dev/store-Dr0zE_9B.mjs} +13 -1
- package/dist/dev/store-Dr0zE_9B.mjs.map +1 -0
- package/dist/dev/store.mjs +3 -0
- package/dist/dev/vue-demi.mjs +173 -0
- package/dist/dev/vue-demi.mjs.map +1 -0
- package/dist/dev/web-apis.mjs +5 -0
- package/dist/fetch.mjs +1 -2
- package/dist/index.mjs +1 -6
- package/dist/jsx-runtime.mjs +1 -1
- package/dist/ref-Bs--7iY3.mjs +1 -0
- package/dist/router-BtpVtHM4.mjs +1 -0
- package/dist/router.mjs +1 -1248
- package/dist/src-BFQLJ5K7.mjs +1 -0
- package/dist/store-B63-EIon.mjs +1 -0
- package/dist/store.mjs +1 -2
- package/dist/vue-demi.mjs +1 -24
- package/dist/web-apis.mjs +1 -3
- package/package.json +74 -2
- package/dist/chunk-D1SwGrFN.mjs +0 -27
|
@@ -0,0 +1,1267 @@
|
|
|
1
|
+
import { d as reactive } from "./ref-BTTXccLL.mjs";
|
|
2
|
+
import { A as onLoad, Q as getCurrentSetupContext, U as onShow, a as resolveCurrentRoute, c as normalizeRouteParams, d as createAbsoluteRoutePath, f as isDynamicRoutePath, g as injectGlobal, i as parsePathInput, l as parseQuery, n as useNativeRouter$1, o as normalizeHash, p as resolvePath, r as createRouteLocation, s as normalizeQuery, t as useNativePageRouter$1, u as stringifyQuery, ut as readonly, v as provideGlobal, z as onRouteDone } from "./router-BZZnIx32.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/routerInternal/params.ts
|
|
5
|
+
const PATH_PARAM_TOKEN_RE = /^:(\w+)(?:\([^)]*\))?([+*?])?$/;
|
|
6
|
+
function parsePathParamToken(segment) {
|
|
7
|
+
var _match$;
|
|
8
|
+
const match = PATH_PARAM_TOKEN_RE.exec(segment);
|
|
9
|
+
if (!match) return;
|
|
10
|
+
return {
|
|
11
|
+
key: match[1],
|
|
12
|
+
modifier: (_match$ = match[2]) !== null && _match$ !== void 0 ? _match$ : ""
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function normalizePathParamValues(value) {
|
|
16
|
+
if (value === void 0) return [];
|
|
17
|
+
if (Array.isArray(value)) return value;
|
|
18
|
+
return [value];
|
|
19
|
+
}
|
|
20
|
+
function stringifyPathParamSegment(value) {
|
|
21
|
+
return encodeURIComponent(value);
|
|
22
|
+
}
|
|
23
|
+
function decodeRouteParamSegment(value) {
|
|
24
|
+
try {
|
|
25
|
+
return decodeURIComponent(value);
|
|
26
|
+
} catch (_unused) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function resolveNamedRoutePath(pathTemplate, params, routeName) {
|
|
31
|
+
const resolvedSegments = [];
|
|
32
|
+
const consumedKeys = /* @__PURE__ */ new Set();
|
|
33
|
+
for (const rawSegment of pathTemplate.split("/")) {
|
|
34
|
+
if (!rawSegment) continue;
|
|
35
|
+
const token = parsePathParamToken(rawSegment);
|
|
36
|
+
if (!token) {
|
|
37
|
+
resolvedSegments.push(rawSegment);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const values = normalizePathParamValues(params[token.key]);
|
|
41
|
+
if (token.modifier === "") {
|
|
42
|
+
if (values.length !== 1) throw new Error(`Missing required param "${token.key}" for named route "${routeName}"`);
|
|
43
|
+
consumedKeys.add(token.key);
|
|
44
|
+
resolvedSegments.push(stringifyPathParamSegment(values[0]));
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (token.modifier === "?") {
|
|
48
|
+
if (values.length === 0) continue;
|
|
49
|
+
if (values.length > 1) throw new Error(`Param "${token.key}" for named route "${routeName}" must be a single value`);
|
|
50
|
+
consumedKeys.add(token.key);
|
|
51
|
+
resolvedSegments.push(stringifyPathParamSegment(values[0]));
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (token.modifier === "+") {
|
|
55
|
+
if (values.length === 0) throw new Error(`Missing required repeatable param "${token.key}" for named route "${routeName}"`);
|
|
56
|
+
consumedKeys.add(token.key);
|
|
57
|
+
for (const value of values) resolvedSegments.push(stringifyPathParamSegment(value));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (values.length > 0) consumedKeys.add(token.key);
|
|
61
|
+
for (const value of values) resolvedSegments.push(stringifyPathParamSegment(value));
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
path: resolvedSegments.join("/"),
|
|
65
|
+
consumedKeys
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function hasNonEmptyRouteParamValue(value) {
|
|
69
|
+
if (value === void 0) return false;
|
|
70
|
+
if (Array.isArray(value)) return value.length > 0;
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
function assertNoUnexpectedNamedRouteParams(params, consumedKeys, routeName) {
|
|
74
|
+
const unexpectedKeys = Object.keys(params).filter((key) => !consumedKeys.has(key)).filter((key) => hasNonEmptyRouteParamValue(params[key]));
|
|
75
|
+
if (unexpectedKeys.length === 0) return;
|
|
76
|
+
throw new Error(`Unexpected params for named route "${routeName}": ${unexpectedKeys.join(", ")}`);
|
|
77
|
+
}
|
|
78
|
+
function resolveNamedRouteLocation(to, lookup, paramsMode) {
|
|
79
|
+
const routeName = to.name;
|
|
80
|
+
if (typeof routeName !== "string" || !routeName) return to;
|
|
81
|
+
if (to.path !== void 0 || to.fullPath !== void 0) return to;
|
|
82
|
+
const routeRecord = lookup.recordByName.get(routeName);
|
|
83
|
+
if (!routeRecord) throw new Error(`Named route "${routeName}" is not defined in createRouter({ routes | namedRoutes })`);
|
|
84
|
+
const params = to.params ? normalizeRouteParams(to.params) : {};
|
|
85
|
+
const resolvedResult = resolveNamedRoutePath(routeRecord.path, params, routeName);
|
|
86
|
+
if (paramsMode === "strict") assertNoUnexpectedNamedRouteParams(params, resolvedResult.consumedKeys, routeName);
|
|
87
|
+
return {
|
|
88
|
+
...to,
|
|
89
|
+
path: resolvedResult.path ? `/${resolvedResult.path}` : "/",
|
|
90
|
+
params
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/routerInternal/matching.ts
|
|
96
|
+
function splitRoutePathSegments(path) {
|
|
97
|
+
return path.split("/").filter(Boolean);
|
|
98
|
+
}
|
|
99
|
+
function listRouteRecordMatchPaths(record) {
|
|
100
|
+
return [record.path, ...record.aliasPaths];
|
|
101
|
+
}
|
|
102
|
+
function resolveMatchedRouteRecordChain(record, lookup) {
|
|
103
|
+
const matchedRecords = [];
|
|
104
|
+
const visitedNames = /* @__PURE__ */ new Set();
|
|
105
|
+
let currentRecord = record;
|
|
106
|
+
while (currentRecord) {
|
|
107
|
+
if (visitedNames.has(currentRecord.name)) break;
|
|
108
|
+
visitedNames.add(currentRecord.name);
|
|
109
|
+
matchedRecords.unshift(currentRecord);
|
|
110
|
+
if (!currentRecord.parentName) break;
|
|
111
|
+
currentRecord = lookup.recordByName.get(currentRecord.parentName);
|
|
112
|
+
}
|
|
113
|
+
return matchedRecords;
|
|
114
|
+
}
|
|
115
|
+
function mergeMatchedRouteMeta(matchedRecords) {
|
|
116
|
+
let mergedRouteMeta;
|
|
117
|
+
for (const matchedRecord of matchedRecords) {
|
|
118
|
+
if (!matchedRecord.meta) continue;
|
|
119
|
+
if (!mergedRouteMeta) mergedRouteMeta = {};
|
|
120
|
+
Object.assign(mergedRouteMeta, matchedRecord.meta);
|
|
121
|
+
}
|
|
122
|
+
return mergedRouteMeta;
|
|
123
|
+
}
|
|
124
|
+
function collectRouteNamesForRemoval(routeName, recordByName) {
|
|
125
|
+
const namesToRemove = /* @__PURE__ */ new Set();
|
|
126
|
+
if (!recordByName.has(routeName)) return namesToRemove;
|
|
127
|
+
namesToRemove.add(routeName);
|
|
128
|
+
let expanded = true;
|
|
129
|
+
while (expanded) {
|
|
130
|
+
expanded = false;
|
|
131
|
+
for (const [name, record] of recordByName.entries()) {
|
|
132
|
+
if (!record.parentName || namesToRemove.has(name)) continue;
|
|
133
|
+
if (namesToRemove.has(record.parentName)) {
|
|
134
|
+
namesToRemove.add(name);
|
|
135
|
+
expanded = true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return namesToRemove;
|
|
140
|
+
}
|
|
141
|
+
function buildRouteParamsFromMatch(matchValues) {
|
|
142
|
+
const params = {};
|
|
143
|
+
for (const [key, values] of matchValues.entries()) {
|
|
144
|
+
if (values.length === 0) continue;
|
|
145
|
+
params[key] = values.length === 1 ? values[0] : values;
|
|
146
|
+
}
|
|
147
|
+
return params;
|
|
148
|
+
}
|
|
149
|
+
function matchRoutePathParams(pathTemplate, targetPath) {
|
|
150
|
+
const templateSegments = splitRoutePathSegments(pathTemplate);
|
|
151
|
+
const targetSegments = splitRoutePathSegments(targetPath);
|
|
152
|
+
const matchedValues = /* @__PURE__ */ new Map();
|
|
153
|
+
const pushValue = (key, value) => {
|
|
154
|
+
const previous = matchedValues.get(key);
|
|
155
|
+
if (previous) {
|
|
156
|
+
previous.push(value);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
matchedValues.set(key, [value]);
|
|
160
|
+
};
|
|
161
|
+
const popValue = (key) => {
|
|
162
|
+
const previous = matchedValues.get(key);
|
|
163
|
+
if (!previous || previous.length === 0) return;
|
|
164
|
+
previous.pop();
|
|
165
|
+
if (previous.length === 0) matchedValues.delete(key);
|
|
166
|
+
};
|
|
167
|
+
const consumeRepeatableToken = (key, startIndex, count) => {
|
|
168
|
+
for (let index = 0; index < count; index += 1) pushValue(key, decodeRouteParamSegment(targetSegments[startIndex + index]));
|
|
169
|
+
return () => {
|
|
170
|
+
for (let index = 0; index < count; index += 1) popValue(key);
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
const matchRecursively = (templateIndex, targetIndex) => {
|
|
174
|
+
if (templateIndex >= templateSegments.length) return targetIndex >= targetSegments.length;
|
|
175
|
+
const templateSegment = templateSegments[templateIndex];
|
|
176
|
+
const token = parsePathParamToken(templateSegment);
|
|
177
|
+
if (!token) {
|
|
178
|
+
if (targetIndex >= targetSegments.length || templateSegment !== targetSegments[targetIndex]) return false;
|
|
179
|
+
return matchRecursively(templateIndex + 1, targetIndex + 1);
|
|
180
|
+
}
|
|
181
|
+
if (token.modifier === "") {
|
|
182
|
+
if (targetIndex >= targetSegments.length) return false;
|
|
183
|
+
pushValue(token.key, decodeRouteParamSegment(targetSegments[targetIndex]));
|
|
184
|
+
const matched = matchRecursively(templateIndex + 1, targetIndex + 1);
|
|
185
|
+
if (!matched) popValue(token.key);
|
|
186
|
+
return matched;
|
|
187
|
+
}
|
|
188
|
+
if (token.modifier === "?") {
|
|
189
|
+
if (targetIndex < targetSegments.length) {
|
|
190
|
+
pushValue(token.key, decodeRouteParamSegment(targetSegments[targetIndex]));
|
|
191
|
+
if (matchRecursively(templateIndex + 1, targetIndex + 1)) return true;
|
|
192
|
+
popValue(token.key);
|
|
193
|
+
}
|
|
194
|
+
return matchRecursively(templateIndex + 1, targetIndex);
|
|
195
|
+
}
|
|
196
|
+
const minimumCount = token.modifier === "+" ? 1 : 0;
|
|
197
|
+
const maximumCount = targetSegments.length - targetIndex;
|
|
198
|
+
for (let count = maximumCount; count >= minimumCount; count -= 1) {
|
|
199
|
+
const rollback = consumeRepeatableToken(token.key, targetIndex, count);
|
|
200
|
+
if (matchRecursively(templateIndex + 1, targetIndex + count)) return true;
|
|
201
|
+
rollback();
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
};
|
|
205
|
+
if (!matchRecursively(0, 0)) return;
|
|
206
|
+
return buildRouteParamsFromMatch(matchedValues);
|
|
207
|
+
}
|
|
208
|
+
function resolveMatchedRouteRecord(target, lookup) {
|
|
209
|
+
if (target.name) {
|
|
210
|
+
const byName = lookup.recordByName.get(target.name);
|
|
211
|
+
if (byName) return {
|
|
212
|
+
record: byName,
|
|
213
|
+
matchedRecords: resolveMatchedRouteRecordChain(byName, lookup)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
const staticNamedRoute = lookup.nameByStaticPath.get(target.path);
|
|
217
|
+
if (staticNamedRoute) {
|
|
218
|
+
const record = lookup.recordByName.get(staticNamedRoute);
|
|
219
|
+
if (record) return {
|
|
220
|
+
record,
|
|
221
|
+
matchedRecords: resolveMatchedRouteRecordChain(record, lookup),
|
|
222
|
+
matchedPath: target.path
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
for (const record of lookup.recordByName.values()) for (const routePath of listRouteRecordMatchPaths(record)) {
|
|
226
|
+
if (!isDynamicRoutePath(routePath)) continue;
|
|
227
|
+
const matchedParams = matchRoutePathParams(routePath, target.path);
|
|
228
|
+
if (!matchedParams) continue;
|
|
229
|
+
return {
|
|
230
|
+
record,
|
|
231
|
+
matchedRecords: resolveMatchedRouteRecordChain(record, lookup),
|
|
232
|
+
matchedPath: routePath,
|
|
233
|
+
params: matchedParams
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/routerInternal/recordsWarnings.ts
|
|
240
|
+
function warnRouteConfig(message) {
|
|
241
|
+
if (typeof console === "undefined" || typeof console.warn !== "function") return;
|
|
242
|
+
console.warn(`[wevu/router] ${message}`);
|
|
243
|
+
}
|
|
244
|
+
function formatRoutePathForWarning(path) {
|
|
245
|
+
const normalizedPath = resolvePath(path, "");
|
|
246
|
+
if (!normalizedPath) return "/";
|
|
247
|
+
return createAbsoluteRoutePath(normalizedPath);
|
|
248
|
+
}
|
|
249
|
+
function hasCircularChildrenReference(routeRecord, ancestorRecords = /* @__PURE__ */ new Set()) {
|
|
250
|
+
if (ancestorRecords.has(routeRecord)) return true;
|
|
251
|
+
if (!Array.isArray(routeRecord.children) || routeRecord.children.length === 0) return false;
|
|
252
|
+
const nextAncestorRecords = new Set(ancestorRecords);
|
|
253
|
+
nextAncestorRecords.add(routeRecord);
|
|
254
|
+
for (const childRecord of routeRecord.children) if (hasCircularChildrenReference(childRecord, nextAncestorRecords)) return true;
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
function assertValidAddRouteInput(routeRecord) {
|
|
258
|
+
const routeName = typeof routeRecord.name === "string" ? routeRecord.name.trim() : "";
|
|
259
|
+
if (!routeName) throw new Error("Route name is required when adding a named route");
|
|
260
|
+
if (typeof routeRecord.path !== "string" || !routeRecord.path) throw new Error(`Route path is required when adding named route "${routeName}"`);
|
|
261
|
+
if (hasCircularChildrenReference(routeRecord)) throw new Error(`Circular children reference detected when adding named route "${routeName}"`);
|
|
262
|
+
}
|
|
263
|
+
function warnDuplicateRouteEntries(routeEntries) {
|
|
264
|
+
const latestRouteInfoByName = /* @__PURE__ */ new Map();
|
|
265
|
+
const duplicateMessages = [];
|
|
266
|
+
for (const routeEntry of routeEntries) {
|
|
267
|
+
var _routeEntry$source;
|
|
268
|
+
const routeName = typeof routeEntry.route.name === "string" ? routeEntry.route.name.trim() : "";
|
|
269
|
+
if (!routeName) continue;
|
|
270
|
+
const currentSource = (_routeEntry$source = routeEntry.source) !== null && _routeEntry$source !== void 0 ? _routeEntry$source : "namedRoutes";
|
|
271
|
+
const currentPath = formatRoutePathForWarning(routeEntry.route.path);
|
|
272
|
+
const previousInfo = latestRouteInfoByName.get(routeName);
|
|
273
|
+
if (previousInfo) duplicateMessages.push(`"${routeName}" (${previousInfo.source}:${previousInfo.path} -> ${currentSource}:${currentPath})`);
|
|
274
|
+
latestRouteInfoByName.set(routeName, {
|
|
275
|
+
source: currentSource,
|
|
276
|
+
path: currentPath
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (duplicateMessages.length === 0) return;
|
|
280
|
+
warnRouteConfig(`duplicate route names detected, later entries override earlier ones: ${duplicateMessages.join(", ")}`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/routerInternal/records.ts
|
|
285
|
+
function normalizeBeforeEnterGuards(beforeEnter) {
|
|
286
|
+
if (!beforeEnter) return [];
|
|
287
|
+
if (Array.isArray(beforeEnter)) return beforeEnter;
|
|
288
|
+
return [beforeEnter];
|
|
289
|
+
}
|
|
290
|
+
function resolveRouteRecordName(route, normalizedPath, source = "namedRoutes") {
|
|
291
|
+
if (typeof route.name === "string" && route.name.trim()) return route.name.trim();
|
|
292
|
+
if (source === "routes") return normalizedPath || "/";
|
|
293
|
+
return "";
|
|
294
|
+
}
|
|
295
|
+
function normalizeRouteRecordRaw(route, parentName, source = "namedRoutes") {
|
|
296
|
+
const normalizedPath = resolvePath(route.path, "");
|
|
297
|
+
if (!normalizedPath && route.path !== "/") return;
|
|
298
|
+
const routeName = resolveRouteRecordName(route, normalizedPath, source);
|
|
299
|
+
if (!routeName) return;
|
|
300
|
+
return {
|
|
301
|
+
name: routeName,
|
|
302
|
+
path: normalizedPath,
|
|
303
|
+
aliasPaths: normalizeRouteRecordAliasPaths(route.alias, normalizedPath),
|
|
304
|
+
parentName,
|
|
305
|
+
meta: route.meta,
|
|
306
|
+
beforeEnterGuards: normalizeBeforeEnterGuards(route.beforeEnter),
|
|
307
|
+
redirect: route.redirect
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function normalizeRouteRecordAliasPaths(alias, normalizedPath) {
|
|
311
|
+
if (!alias) return [];
|
|
312
|
+
const aliasList = Array.isArray(alias) ? alias : [alias];
|
|
313
|
+
const normalizedAliasPaths = [];
|
|
314
|
+
for (const aliasPath of aliasList) {
|
|
315
|
+
if (typeof aliasPath !== "string") continue;
|
|
316
|
+
const normalizedAliasPath = resolvePath(aliasPath, "");
|
|
317
|
+
if (!normalizedAliasPath || normalizedAliasPath === normalizedPath) continue;
|
|
318
|
+
if (!normalizedAliasPaths.includes(normalizedAliasPath)) normalizedAliasPaths.push(normalizedAliasPath);
|
|
319
|
+
}
|
|
320
|
+
return normalizedAliasPaths;
|
|
321
|
+
}
|
|
322
|
+
function normalizeNamedRouteEntries(namedRoutes, source = "namedRoutes") {
|
|
323
|
+
if (!namedRoutes) return [];
|
|
324
|
+
if (Array.isArray(namedRoutes)) return flattenNamedRouteRecords(namedRoutes, void 0, void 0, [], source, source);
|
|
325
|
+
const normalizedEntries = [];
|
|
326
|
+
for (const [rawName, rawPath] of Object.entries(namedRoutes)) {
|
|
327
|
+
const routeName = typeof rawName === "string" ? rawName.trim() : "";
|
|
328
|
+
if (!routeName) {
|
|
329
|
+
warnRouteConfig(`ignored route record at ${source}: route name is required`);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (typeof rawPath !== "string" || !rawPath) {
|
|
333
|
+
warnRouteConfig(`ignored route record "${routeName}" at ${source}: route path is required`);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
normalizedEntries.push({
|
|
337
|
+
route: {
|
|
338
|
+
name: routeName,
|
|
339
|
+
path: rawPath
|
|
340
|
+
},
|
|
341
|
+
source
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return normalizedEntries;
|
|
345
|
+
}
|
|
346
|
+
function resolveRouteOptionEntries(options) {
|
|
347
|
+
return [...normalizeNamedRouteEntries(options.routes, "routes"), ...normalizeNamedRouteEntries(options.namedRoutes, "namedRoutes")];
|
|
348
|
+
}
|
|
349
|
+
function normalizeNestedRoutePath(path, parentPath) {
|
|
350
|
+
if (!parentPath || path.startsWith("/")) return resolvePath(path, "");
|
|
351
|
+
return resolvePath(`${createAbsoluteRoutePath(parentPath)}/${path}`, "");
|
|
352
|
+
}
|
|
353
|
+
function normalizeAliasInputList(alias) {
|
|
354
|
+
if (!alias) return [];
|
|
355
|
+
if (Array.isArray(alias)) return alias.filter((item) => typeof item === "string" && item.length > 0);
|
|
356
|
+
return typeof alias === "string" && alias.length > 0 ? [alias] : [];
|
|
357
|
+
}
|
|
358
|
+
function mergeAliasPaths(aliasPaths) {
|
|
359
|
+
const mergedAliasPaths = [];
|
|
360
|
+
for (const aliasPath of aliasPaths) {
|
|
361
|
+
if (!aliasPath || mergedAliasPaths.includes(aliasPath)) continue;
|
|
362
|
+
mergedAliasPaths.push(aliasPath);
|
|
363
|
+
}
|
|
364
|
+
return mergedAliasPaths;
|
|
365
|
+
}
|
|
366
|
+
function createRouteRecordAliasValue(aliasPaths) {
|
|
367
|
+
if (aliasPaths.length === 0) return;
|
|
368
|
+
if (aliasPaths.length === 1) return createAbsoluteRoutePath(aliasPaths[0]);
|
|
369
|
+
return aliasPaths.map((aliasPath) => createAbsoluteRoutePath(aliasPath));
|
|
370
|
+
}
|
|
371
|
+
function flattenNamedRouteRecords(records, parentPath, parentName, parentAliasPaths = [], source, pathPrefix = "namedRoutes", ancestorRecords = /* @__PURE__ */ new Set()) {
|
|
372
|
+
const flattenedRecords = [];
|
|
373
|
+
for (const [index, record] of records.entries()) {
|
|
374
|
+
const routeConfigPath = `${pathPrefix}[${index}]`;
|
|
375
|
+
if (ancestorRecords.has(record)) {
|
|
376
|
+
warnRouteConfig(`ignored route record at ${routeConfigPath}: detected circular children reference`);
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (typeof record.path !== "string" || !record.path) {
|
|
380
|
+
warnRouteConfig(`ignored route record "${typeof (record === null || record === void 0 ? void 0 : record.name) === "string" ? record.name.trim() : ""}" at ${routeConfigPath}: route path is required`);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
const normalizedPath = normalizeNestedRoutePath(record.path, parentPath);
|
|
384
|
+
const routeName = resolveRouteRecordName(record, normalizedPath, source);
|
|
385
|
+
if (!routeName) {
|
|
386
|
+
warnRouteConfig(`ignored route record at ${routeConfigPath}: route name is required`);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const normalizedDirectAliasPaths = [];
|
|
390
|
+
const directAliasByNormalizedPath = /* @__PURE__ */ new Map();
|
|
391
|
+
for (const rawAliasPath of normalizeAliasInputList(record.alias)) {
|
|
392
|
+
const normalizedAliasPath = normalizeNestedRoutePath(rawAliasPath, parentPath);
|
|
393
|
+
if (!normalizedAliasPath) continue;
|
|
394
|
+
if (normalizedAliasPath === normalizedPath) {
|
|
395
|
+
warnRouteConfig(`ignored alias "${createAbsoluteRoutePath(normalizedAliasPath)}" for route "${routeName}" at ${routeConfigPath}: alias is same as route path`);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const duplicateAliasPath = directAliasByNormalizedPath.get(normalizedAliasPath);
|
|
399
|
+
if (duplicateAliasPath) {
|
|
400
|
+
warnRouteConfig(`ignored duplicate alias "${createAbsoluteRoutePath(normalizedAliasPath)}" for route "${routeName}" at ${routeConfigPath}: already declared by "${duplicateAliasPath}"`);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
directAliasByNormalizedPath.set(normalizedAliasPath, rawAliasPath);
|
|
404
|
+
normalizedDirectAliasPaths.push(normalizedAliasPath);
|
|
405
|
+
}
|
|
406
|
+
const normalizedInheritedAliasPaths = record.path.startsWith("/") ? [] : parentAliasPaths.map((parentAliasPath) => normalizeNestedRoutePath(record.path, parentAliasPath)).filter(Boolean).filter((aliasPath) => aliasPath !== normalizedPath);
|
|
407
|
+
const normalizedAliasPaths = mergeAliasPaths([...normalizedDirectAliasPaths, ...normalizedInheritedAliasPaths]);
|
|
408
|
+
const normalizedRecord = {
|
|
409
|
+
name: routeName,
|
|
410
|
+
path: normalizedPath ? createAbsoluteRoutePath(normalizedPath) : "/",
|
|
411
|
+
alias: createRouteRecordAliasValue(normalizedAliasPaths),
|
|
412
|
+
meta: record.meta,
|
|
413
|
+
beforeEnter: record.beforeEnter,
|
|
414
|
+
redirect: record.redirect
|
|
415
|
+
};
|
|
416
|
+
flattenedRecords.push({
|
|
417
|
+
route: normalizedRecord,
|
|
418
|
+
parentName,
|
|
419
|
+
source
|
|
420
|
+
});
|
|
421
|
+
if (Array.isArray(record.children) && record.children.length > 0) {
|
|
422
|
+
const nextAncestorRecords = new Set(ancestorRecords);
|
|
423
|
+
nextAncestorRecords.add(record);
|
|
424
|
+
flattenedRecords.push(...flattenNamedRouteRecords(record.children, normalizedPath, routeName, normalizedAliasPaths, source, `${routeConfigPath}.children`, nextAncestorRecords));
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return flattenedRecords;
|
|
428
|
+
}
|
|
429
|
+
function createNamedRouteNameByStaticPath(recordByName) {
|
|
430
|
+
const nameByStaticPath = /* @__PURE__ */ new Map();
|
|
431
|
+
for (const [name, record] of recordByName.entries()) {
|
|
432
|
+
if (!isDynamicRoutePath(record.path) && !nameByStaticPath.has(record.path)) nameByStaticPath.set(record.path, name);
|
|
433
|
+
for (const aliasPath of record.aliasPaths) if (!isDynamicRoutePath(aliasPath) && !nameByStaticPath.has(aliasPath)) nameByStaticPath.set(aliasPath, name);
|
|
434
|
+
}
|
|
435
|
+
return nameByStaticPath;
|
|
436
|
+
}
|
|
437
|
+
function createNamedRouteLookup(routeEntries) {
|
|
438
|
+
const recordByName = /* @__PURE__ */ new Map();
|
|
439
|
+
for (const routeRecord of routeEntries) {
|
|
440
|
+
const normalizedRecord = normalizeRouteRecordRaw(routeRecord.route, routeRecord.parentName, routeRecord.source);
|
|
441
|
+
if (!normalizedRecord) continue;
|
|
442
|
+
recordByName.set(normalizedRecord.name, normalizedRecord);
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
recordByName,
|
|
446
|
+
nameByStaticPath: createNamedRouteNameByStaticPath(recordByName)
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/routerInternal/snapshots.ts
|
|
452
|
+
function cloneRouteMeta(meta) {
|
|
453
|
+
if (!meta) return;
|
|
454
|
+
return { ...meta };
|
|
455
|
+
}
|
|
456
|
+
function freezeRouteRecordRawSnapshot(routeRecord) {
|
|
457
|
+
const snapshot = {
|
|
458
|
+
name: routeRecord.name,
|
|
459
|
+
path: routeRecord.path
|
|
460
|
+
};
|
|
461
|
+
if (routeRecord.meta !== void 0) snapshot.meta = cloneRouteMeta(routeRecord.meta);
|
|
462
|
+
if (Array.isArray(routeRecord.alias)) snapshot.alias = Object.freeze(routeRecord.alias.slice());
|
|
463
|
+
else if (routeRecord.alias !== void 0) snapshot.alias = routeRecord.alias;
|
|
464
|
+
if (Array.isArray(routeRecord.beforeEnter)) snapshot.beforeEnter = Object.freeze(routeRecord.beforeEnter.slice());
|
|
465
|
+
else if (routeRecord.beforeEnter !== void 0) snapshot.beforeEnter = routeRecord.beforeEnter;
|
|
466
|
+
if (routeRecord.redirect !== void 0) snapshot.redirect = routeRecord.redirect;
|
|
467
|
+
return Object.freeze(snapshot);
|
|
468
|
+
}
|
|
469
|
+
function createRouterOptionsSnapshot(normalizedTabBarEntries, normalizedNamedRoutes, paramsMode, maxRedirects, routeResolveCodec, rejectOnError) {
|
|
470
|
+
const tabBarEntriesSnapshot = Object.freeze(normalizedTabBarEntries.map((path) => createAbsoluteRoutePath(path)));
|
|
471
|
+
const routesSnapshot = Object.freeze(normalizedNamedRoutes.map(freezeRouteRecordRawSnapshot));
|
|
472
|
+
return Object.freeze({
|
|
473
|
+
tabBarEntries: tabBarEntriesSnapshot,
|
|
474
|
+
routes: routesSnapshot,
|
|
475
|
+
namedRoutes: routesSnapshot,
|
|
476
|
+
paramsMode,
|
|
477
|
+
maxRedirects,
|
|
478
|
+
parseQuery: routeResolveCodec.parseQuery,
|
|
479
|
+
stringifyQuery: routeResolveCodec.stringifyQuery,
|
|
480
|
+
rejectOnError
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
function normalizeRouteRecordMatched(record, matchedPath) {
|
|
484
|
+
const matchedRecord = {
|
|
485
|
+
name: record.name,
|
|
486
|
+
path: createAbsoluteRoutePath(record.path)
|
|
487
|
+
};
|
|
488
|
+
const normalizedMatchedPath = typeof matchedPath === "string" && matchedPath ? createAbsoluteRoutePath(resolvePath(matchedPath, "")) : void 0;
|
|
489
|
+
if (normalizedMatchedPath && normalizedMatchedPath !== matchedRecord.path) matchedRecord.aliasPath = normalizedMatchedPath;
|
|
490
|
+
if (record.meta !== void 0) matchedRecord.meta = cloneRouteMeta(record.meta);
|
|
491
|
+
return matchedRecord;
|
|
492
|
+
}
|
|
493
|
+
function cloneRouteRecordMatchedList(matched) {
|
|
494
|
+
if (!matched) return;
|
|
495
|
+
return matched.map((record) => ({
|
|
496
|
+
name: record.name,
|
|
497
|
+
path: record.path,
|
|
498
|
+
aliasPath: record.aliasPath,
|
|
499
|
+
meta: cloneRouteMeta(record.meta)
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
function cloneLocationQuery(query) {
|
|
503
|
+
const cloned = {};
|
|
504
|
+
for (const key of Object.keys(query)) {
|
|
505
|
+
const value = query[key];
|
|
506
|
+
cloned[key] = Array.isArray(value) ? value.slice() : value;
|
|
507
|
+
}
|
|
508
|
+
return cloned;
|
|
509
|
+
}
|
|
510
|
+
function cloneRouteParams(params) {
|
|
511
|
+
const cloned = {};
|
|
512
|
+
for (const key of Object.keys(params)) {
|
|
513
|
+
const value = params[key];
|
|
514
|
+
cloned[key] = Array.isArray(value) ? value.slice() : value;
|
|
515
|
+
}
|
|
516
|
+
return cloned;
|
|
517
|
+
}
|
|
518
|
+
function cloneRouteLocationRedirectedFrom(redirectedFrom) {
|
|
519
|
+
if (!redirectedFrom) return;
|
|
520
|
+
const cloned = {
|
|
521
|
+
path: redirectedFrom.path,
|
|
522
|
+
fullPath: redirectedFrom.fullPath,
|
|
523
|
+
query: cloneLocationQuery(redirectedFrom.query),
|
|
524
|
+
hash: redirectedFrom.hash,
|
|
525
|
+
params: cloneRouteParams(redirectedFrom.params)
|
|
526
|
+
};
|
|
527
|
+
if (redirectedFrom.name !== void 0) cloned.name = redirectedFrom.name;
|
|
528
|
+
if (redirectedFrom.meta !== void 0) cloned.meta = cloneRouteMeta(redirectedFrom.meta);
|
|
529
|
+
if (redirectedFrom.href !== void 0) cloned.href = redirectedFrom.href;
|
|
530
|
+
if (redirectedFrom.matched !== void 0) cloned.matched = cloneRouteRecordMatchedList(redirectedFrom.matched);
|
|
531
|
+
return cloned;
|
|
532
|
+
}
|
|
533
|
+
function createRedirectedFromSnapshot(route) {
|
|
534
|
+
const snapshot = {
|
|
535
|
+
path: route.path,
|
|
536
|
+
fullPath: route.fullPath,
|
|
537
|
+
query: cloneLocationQuery(route.query),
|
|
538
|
+
hash: route.hash,
|
|
539
|
+
params: cloneRouteParams(route.params)
|
|
540
|
+
};
|
|
541
|
+
if (route.name !== void 0) snapshot.name = route.name;
|
|
542
|
+
if (route.meta !== void 0) snapshot.meta = cloneRouteMeta(route.meta);
|
|
543
|
+
if (route.href !== void 0) snapshot.href = route.href;
|
|
544
|
+
if (route.matched !== void 0) snapshot.matched = cloneRouteRecordMatchedList(route.matched);
|
|
545
|
+
return snapshot;
|
|
546
|
+
}
|
|
547
|
+
function snapshotRouteLocation(route) {
|
|
548
|
+
const snapshot = {
|
|
549
|
+
path: route.path,
|
|
550
|
+
fullPath: route.fullPath,
|
|
551
|
+
query: cloneLocationQuery(route.query),
|
|
552
|
+
hash: route.hash,
|
|
553
|
+
params: cloneRouteParams(route.params)
|
|
554
|
+
};
|
|
555
|
+
if (route.meta !== void 0) snapshot.meta = cloneRouteMeta(route.meta);
|
|
556
|
+
if (route.href !== void 0) snapshot.href = route.href;
|
|
557
|
+
if (route.matched !== void 0) snapshot.matched = cloneRouteRecordMatchedList(route.matched);
|
|
558
|
+
if (route.redirectedFrom !== void 0) snapshot.redirectedFrom = cloneRouteLocationRedirectedFrom(route.redirectedFrom);
|
|
559
|
+
if (route.name !== void 0) snapshot.name = route.name;
|
|
560
|
+
return snapshot;
|
|
561
|
+
}
|
|
562
|
+
function createNativeRouteUrl(target, queryStringifier = stringifyQuery) {
|
|
563
|
+
const basePath = createAbsoluteRoutePath(target.path);
|
|
564
|
+
const queryString = queryStringifier(target.query);
|
|
565
|
+
return queryString ? `${basePath}?${queryString}` : basePath;
|
|
566
|
+
}
|
|
567
|
+
function hasLocationQuery(query) {
|
|
568
|
+
return Object.keys(query).length > 0;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/router/instance.ts
|
|
573
|
+
const ROUTER_INSTANCE_KEY = Symbol("wevu.router.instance");
|
|
574
|
+
let activeRouter;
|
|
575
|
+
function setActiveRouter(router) {
|
|
576
|
+
activeRouter = router;
|
|
577
|
+
provideGlobal(ROUTER_INSTANCE_KEY, router);
|
|
578
|
+
}
|
|
579
|
+
function getActiveRouter() {
|
|
580
|
+
var _activeRouter;
|
|
581
|
+
return (_activeRouter = activeRouter) !== null && _activeRouter !== void 0 ? _activeRouter : injectGlobal(ROUTER_INSTANCE_KEY, void 0);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region src/router/types.ts
|
|
586
|
+
const NavigationFailureType = {
|
|
587
|
+
unknown: 1,
|
|
588
|
+
aborted: 4,
|
|
589
|
+
cancelled: 8,
|
|
590
|
+
duplicated: 16
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
//#endregion
|
|
594
|
+
//#region src/router/navigationCore.ts
|
|
595
|
+
const DUPLICATED_RE = /already|same|duplicat|重复/;
|
|
596
|
+
const CANCELLED_RE = /cancel|取消/;
|
|
597
|
+
const ABORTED_RE = /abort|interrupt|中断/;
|
|
598
|
+
function normalizeNavigationErrorMessage(error) {
|
|
599
|
+
if (typeof error === "string") return error;
|
|
600
|
+
if (error && typeof error === "object") {
|
|
601
|
+
const message = error.errMsg;
|
|
602
|
+
if (typeof message === "string") return message;
|
|
603
|
+
if (error instanceof Error) return error.message;
|
|
604
|
+
}
|
|
605
|
+
return "";
|
|
606
|
+
}
|
|
607
|
+
function resolveNavigationFailureType(error) {
|
|
608
|
+
const normalizedMessage = normalizeNavigationErrorMessage(error).toLowerCase();
|
|
609
|
+
if (DUPLICATED_RE.test(normalizedMessage)) return NavigationFailureType.duplicated;
|
|
610
|
+
if (CANCELLED_RE.test(normalizedMessage)) return NavigationFailureType.cancelled;
|
|
611
|
+
if (ABORTED_RE.test(normalizedMessage)) return NavigationFailureType.aborted;
|
|
612
|
+
return NavigationFailureType.unknown;
|
|
613
|
+
}
|
|
614
|
+
function createNavigationFailure(type, to, from, cause) {
|
|
615
|
+
const message = normalizeNavigationErrorMessage(cause) || "Navigation failed";
|
|
616
|
+
const error = new Error(message);
|
|
617
|
+
error.__wevuNavigationFailure = true;
|
|
618
|
+
error.type = type;
|
|
619
|
+
error.to = to;
|
|
620
|
+
error.from = from;
|
|
621
|
+
error.cause = cause;
|
|
622
|
+
return error;
|
|
623
|
+
}
|
|
624
|
+
function isNavigationFailure(error, type) {
|
|
625
|
+
if (!error || typeof error !== "object") return false;
|
|
626
|
+
const navigationError = error;
|
|
627
|
+
if (navigationError.__wevuNavigationFailure !== true) return false;
|
|
628
|
+
if (type === void 0) return true;
|
|
629
|
+
return navigationError.type === type;
|
|
630
|
+
}
|
|
631
|
+
function isNavigationRedirectCandidate(value) {
|
|
632
|
+
if (!value || typeof value !== "object") return false;
|
|
633
|
+
return "to" in value;
|
|
634
|
+
}
|
|
635
|
+
function isRouteLocationRawCandidate(value) {
|
|
636
|
+
if (typeof value === "string") return true;
|
|
637
|
+
if (!value || typeof value !== "object") return false;
|
|
638
|
+
const candidate = value;
|
|
639
|
+
return "path" in candidate || "fullPath" in candidate || "query" in candidate || "hash" in candidate || "name" in candidate || "params" in candidate;
|
|
640
|
+
}
|
|
641
|
+
async function runNavigationGuards(guards, context, resolveRoute) {
|
|
642
|
+
for (const guard of guards) try {
|
|
643
|
+
const result = await guard(context.to, context.from, context);
|
|
644
|
+
if (isNavigationFailure(result)) return {
|
|
645
|
+
status: "failure",
|
|
646
|
+
failure: result
|
|
647
|
+
};
|
|
648
|
+
if (result === false) return {
|
|
649
|
+
status: "failure",
|
|
650
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Navigation aborted by guard")
|
|
651
|
+
};
|
|
652
|
+
if (isNavigationRedirectCandidate(result)) {
|
|
653
|
+
if (!context.to) return {
|
|
654
|
+
status: "failure",
|
|
655
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Redirect is not supported in back navigation guards")
|
|
656
|
+
};
|
|
657
|
+
return {
|
|
658
|
+
status: "redirect",
|
|
659
|
+
target: resolveRoute(result.to, context.to.path),
|
|
660
|
+
replace: result.replace
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
if (isRouteLocationRawCandidate(result)) {
|
|
664
|
+
if (!context.to) return {
|
|
665
|
+
status: "failure",
|
|
666
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, "Redirect is not supported in back navigation guards")
|
|
667
|
+
};
|
|
668
|
+
return {
|
|
669
|
+
status: "redirect",
|
|
670
|
+
target: resolveRoute(result, context.to.path)
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
} catch (error) {
|
|
674
|
+
return {
|
|
675
|
+
status: "failure",
|
|
676
|
+
failure: createNavigationFailure(NavigationFailureType.aborted, context.to, context.from, error)
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
return { status: "continue" };
|
|
680
|
+
}
|
|
681
|
+
function executeNavigationMethod(method, options, to, from) {
|
|
682
|
+
return new Promise((resolve) => {
|
|
683
|
+
let settled = false;
|
|
684
|
+
const finalize = (result) => {
|
|
685
|
+
if (settled) return;
|
|
686
|
+
settled = true;
|
|
687
|
+
resolve(result);
|
|
688
|
+
};
|
|
689
|
+
try {
|
|
690
|
+
const maybePromise = method({
|
|
691
|
+
...options,
|
|
692
|
+
success: () => finalize(),
|
|
693
|
+
fail: (error) => {
|
|
694
|
+
finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error));
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
if (maybePromise && typeof maybePromise.then === "function") maybePromise.then(() => finalize(), (error) => finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error)));
|
|
698
|
+
} catch (error) {
|
|
699
|
+
finalize(createNavigationFailure(resolveNavigationFailureType(error), to, from, error));
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
async function runAfterEachHooks(hooks, context) {
|
|
704
|
+
for (const hook of hooks) try {
|
|
705
|
+
await hook(context.to, context.from, context.failure, context);
|
|
706
|
+
} catch (_unused) {}
|
|
707
|
+
}
|
|
708
|
+
async function runNavigationErrorHooks(handlers, error, context) {
|
|
709
|
+
for (const handler of handlers) try {
|
|
710
|
+
await handler(error, context);
|
|
711
|
+
} catch (_unused2) {}
|
|
712
|
+
}
|
|
713
|
+
function isNativeFailureLikeError(value) {
|
|
714
|
+
if (!value || typeof value !== "object") return false;
|
|
715
|
+
return typeof value.errMsg === "string";
|
|
716
|
+
}
|
|
717
|
+
function shouldEmitNavigationError(failure) {
|
|
718
|
+
if (failure.type === NavigationFailureType.unknown) return true;
|
|
719
|
+
if (failure.cause instanceof Error) return true;
|
|
720
|
+
if (failure.type === NavigationFailureType.aborted && failure.cause !== void 0) {
|
|
721
|
+
if (typeof failure.cause === "string") return false;
|
|
722
|
+
if (isNativeFailureLikeError(failure.cause)) return false;
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
return false;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
//#endregion
|
|
729
|
+
//#region src/router/navigationResult.ts
|
|
730
|
+
function createNavigationRunResult(mode, from, to, failure) {
|
|
731
|
+
return {
|
|
732
|
+
mode,
|
|
733
|
+
from,
|
|
734
|
+
to,
|
|
735
|
+
failure
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function createNavigationResultController(options) {
|
|
739
|
+
const { afterEachHooks, errorHandlers, nativeRouter, rejectOnError } = options;
|
|
740
|
+
async function emitNavigationAfterEach(result) {
|
|
741
|
+
await runAfterEachHooks(afterEachHooks, {
|
|
742
|
+
mode: result.mode,
|
|
743
|
+
to: result.to,
|
|
744
|
+
from: result.from,
|
|
745
|
+
nativeRouter,
|
|
746
|
+
failure: result.failure
|
|
747
|
+
});
|
|
748
|
+
if (result.failure && shouldEmitNavigationError(result.failure)) {
|
|
749
|
+
var _result$failure$cause;
|
|
750
|
+
await runNavigationErrorHooks(errorHandlers, (_result$failure$cause = result.failure.cause) !== null && _result$failure$cause !== void 0 ? _result$failure$cause : result.failure, {
|
|
751
|
+
mode: result.mode,
|
|
752
|
+
to: result.to,
|
|
753
|
+
from: result.from,
|
|
754
|
+
nativeRouter,
|
|
755
|
+
failure: result.failure
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
function shouldRejectNavigationFailure(failure) {
|
|
760
|
+
return rejectOnError && shouldEmitNavigationError(failure);
|
|
761
|
+
}
|
|
762
|
+
async function settleNavigationResult(result) {
|
|
763
|
+
await emitNavigationAfterEach(result);
|
|
764
|
+
if (result.failure && shouldRejectNavigationFailure(result.failure)) throw result.failure;
|
|
765
|
+
return result.failure;
|
|
766
|
+
}
|
|
767
|
+
return { settleNavigationResult };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
//#endregion
|
|
771
|
+
//#region src/router/navigationTargetHelpers.ts
|
|
772
|
+
function createDuplicatedFailure(target, from) {
|
|
773
|
+
return createNavigationFailure(NavigationFailureType.duplicated, target, from, "Avoided redundant navigation to current location");
|
|
774
|
+
}
|
|
775
|
+
function createTabBarQueryFailure(target, from) {
|
|
776
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, "switchTab does not support query parameters");
|
|
777
|
+
}
|
|
778
|
+
function createTooManyRedirectsFailure(target, from, maxRedirects) {
|
|
779
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, `Navigation redirected more than ${maxRedirects} times`);
|
|
780
|
+
}
|
|
781
|
+
function createHashOnlyNavigationFailure(target, from) {
|
|
782
|
+
return createNavigationFailure(NavigationFailureType.aborted, target, from, "Hash-only navigation is not supported in mini-program router");
|
|
783
|
+
}
|
|
784
|
+
function createForwardNotSupportedFailure(from) {
|
|
785
|
+
return createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Forward navigation is not supported in mini-program router");
|
|
786
|
+
}
|
|
787
|
+
function applyRedirectedFrom(redirectedTarget, currentTarget) {
|
|
788
|
+
redirectedTarget.redirectedFrom = currentTarget.redirectedFrom ? cloneRouteLocationRedirectedFrom(currentTarget.redirectedFrom) : createRedirectedFromSnapshot(currentTarget);
|
|
789
|
+
}
|
|
790
|
+
async function resolveRouteRecordRedirect(redirect, to, from, resolveWithCodec) {
|
|
791
|
+
const redirectResult = typeof redirect === "function" ? await redirect(to, from) : redirect;
|
|
792
|
+
if (isNavigationRedirectCandidate(redirectResult)) return {
|
|
793
|
+
target: resolveWithCodec(redirectResult.to, to.path),
|
|
794
|
+
replace: redirectResult.replace
|
|
795
|
+
};
|
|
796
|
+
return {
|
|
797
|
+
target: resolveWithCodec(redirectResult, to.path),
|
|
798
|
+
replace: true
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
//#endregion
|
|
803
|
+
//#region src/router/navigationTarget.ts
|
|
804
|
+
async function navigateWithTarget(options) {
|
|
805
|
+
const { mode, target, from, nativeRouter, routeResolveCodec, namedRouteLookup, beforeEachGuards, beforeResolveGuards, maxRedirects, tabBarPathSet, resolveWithCodec } = options;
|
|
806
|
+
let currentTarget = target;
|
|
807
|
+
let currentMode = mode;
|
|
808
|
+
let redirectCount = 0;
|
|
809
|
+
while (true) {
|
|
810
|
+
var _matchedRouteResult$m;
|
|
811
|
+
const tabBarTarget = tabBarPathSet.has(currentTarget.path);
|
|
812
|
+
if (createNativeRouteUrl(currentTarget, routeResolveCodec.stringifyQuery) === createNativeRouteUrl(from, routeResolveCodec.stringifyQuery) && currentTarget.hash !== from.hash) return createNavigationRunResult(currentMode, from, currentTarget, createHashOnlyNavigationFailure(currentTarget, from));
|
|
813
|
+
if (tabBarTarget ? currentTarget.path === from.path : currentTarget.fullPath === from.fullPath) return createNavigationRunResult(currentMode, from, currentTarget, createDuplicatedFailure(currentTarget, from));
|
|
814
|
+
if (tabBarTarget && hasLocationQuery(currentTarget.query)) return createNavigationRunResult(currentMode, from, currentTarget, createTabBarQueryFailure(currentTarget, from));
|
|
815
|
+
const beforeEachResult = await runNavigationGuards(beforeEachGuards, {
|
|
816
|
+
mode: currentMode,
|
|
817
|
+
to: currentTarget,
|
|
818
|
+
from,
|
|
819
|
+
nativeRouter
|
|
820
|
+
}, resolveWithCodec);
|
|
821
|
+
if (beforeEachResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeEachResult.failure);
|
|
822
|
+
if (beforeEachResult.status === "redirect") {
|
|
823
|
+
const redirectedTarget = beforeEachResult.target;
|
|
824
|
+
const redirectedMode = beforeEachResult.replace === true ? "replace" : beforeEachResult.replace === false ? "push" : currentMode;
|
|
825
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
826
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
827
|
+
currentTarget = redirectedTarget;
|
|
828
|
+
currentMode = redirectedMode;
|
|
829
|
+
redirectCount += 1;
|
|
830
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
const matchedRouteResult = resolveMatchedRouteRecord(currentTarget, namedRouteLookup);
|
|
835
|
+
const matchedRouteRecords = (_matchedRouteResult$m = matchedRouteResult === null || matchedRouteResult === void 0 ? void 0 : matchedRouteResult.matchedRecords) !== null && _matchedRouteResult$m !== void 0 ? _matchedRouteResult$m : [];
|
|
836
|
+
let redirectedByRouteRecord = false;
|
|
837
|
+
for (const matchedRouteRecord of matchedRouteRecords) {
|
|
838
|
+
if (matchedRouteRecord.redirect === void 0) continue;
|
|
839
|
+
let redirectedByRecord;
|
|
840
|
+
try {
|
|
841
|
+
redirectedByRecord = await resolveRouteRecordRedirect(matchedRouteRecord.redirect, currentTarget, from, resolveWithCodec);
|
|
842
|
+
} catch (error) {
|
|
843
|
+
return createNavigationRunResult(currentMode, from, currentTarget, createNavigationFailure(NavigationFailureType.aborted, currentTarget, from, error));
|
|
844
|
+
}
|
|
845
|
+
const redirectedMode = redirectedByRecord.replace === false ? "push" : "replace";
|
|
846
|
+
const redirectedTarget = redirectedByRecord.target;
|
|
847
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
848
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
849
|
+
currentTarget = redirectedTarget;
|
|
850
|
+
currentMode = redirectedMode;
|
|
851
|
+
redirectCount += 1;
|
|
852
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
853
|
+
redirectedByRouteRecord = true;
|
|
854
|
+
break;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (redirectedByRouteRecord) continue;
|
|
858
|
+
let redirectedByBeforeEnter = false;
|
|
859
|
+
for (const matchedRouteRecord of matchedRouteRecords) {
|
|
860
|
+
if (matchedRouteRecord.beforeEnterGuards.length === 0) continue;
|
|
861
|
+
const beforeEnterResult = await runNavigationGuards(new Set(matchedRouteRecord.beforeEnterGuards), {
|
|
862
|
+
mode: currentMode,
|
|
863
|
+
to: currentTarget,
|
|
864
|
+
from,
|
|
865
|
+
nativeRouter
|
|
866
|
+
}, resolveWithCodec);
|
|
867
|
+
if (beforeEnterResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeEnterResult.failure);
|
|
868
|
+
if (beforeEnterResult.status === "redirect") {
|
|
869
|
+
const redirectedTarget = beforeEnterResult.target;
|
|
870
|
+
const redirectedMode = beforeEnterResult.replace === true ? "replace" : beforeEnterResult.replace === false ? "push" : currentMode;
|
|
871
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
872
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
873
|
+
currentTarget = redirectedTarget;
|
|
874
|
+
currentMode = redirectedMode;
|
|
875
|
+
redirectCount += 1;
|
|
876
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
877
|
+
redirectedByBeforeEnter = true;
|
|
878
|
+
break;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
if (redirectedByBeforeEnter) continue;
|
|
883
|
+
const beforeResolveResult = await runNavigationGuards(beforeResolveGuards, {
|
|
884
|
+
mode: currentMode,
|
|
885
|
+
to: currentTarget,
|
|
886
|
+
from,
|
|
887
|
+
nativeRouter
|
|
888
|
+
}, resolveWithCodec);
|
|
889
|
+
if (beforeResolveResult.status === "failure") return createNavigationRunResult(currentMode, from, currentTarget, beforeResolveResult.failure);
|
|
890
|
+
if (beforeResolveResult.status === "redirect") {
|
|
891
|
+
const redirectedTarget = beforeResolveResult.target;
|
|
892
|
+
const redirectedMode = beforeResolveResult.replace === true ? "replace" : beforeResolveResult.replace === false ? "push" : currentMode;
|
|
893
|
+
if (redirectedTarget.fullPath !== currentTarget.fullPath || redirectedMode !== currentMode) {
|
|
894
|
+
applyRedirectedFrom(redirectedTarget, currentTarget);
|
|
895
|
+
currentTarget = redirectedTarget;
|
|
896
|
+
currentMode = redirectedMode;
|
|
897
|
+
redirectCount += 1;
|
|
898
|
+
if (redirectCount > maxRedirects) return createNavigationRunResult(currentMode, from, currentTarget, createTooManyRedirectsFailure(currentTarget, from, maxRedirects));
|
|
899
|
+
continue;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
if (tabBarTarget) {
|
|
903
|
+
const result = await executeNavigationMethod(nativeRouter.switchTab, { url: createAbsoluteRoutePath(currentTarget.path) }, currentTarget, from);
|
|
904
|
+
if (isNavigationFailure(result)) return createNavigationRunResult(currentMode, from, currentTarget, result);
|
|
905
|
+
return createNavigationRunResult(currentMode, from, currentTarget);
|
|
906
|
+
}
|
|
907
|
+
const result = await executeNavigationMethod(currentMode === "push" ? nativeRouter.navigateTo : nativeRouter.redirectTo, { url: createNativeRouteUrl(currentTarget, routeResolveCodec.stringifyQuery) }, currentTarget, from);
|
|
908
|
+
if (isNavigationFailure(result)) return createNavigationRunResult(currentMode, from, currentTarget, result);
|
|
909
|
+
return createNavigationRunResult(currentMode, from, currentTarget);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
//#endregion
|
|
914
|
+
//#region src/router/navigationApi.ts
|
|
915
|
+
function createNavigationApi(options) {
|
|
916
|
+
const { nativeRouter, route, routeResolveCodec, namedRouteLookup, beforeEachGuards, beforeResolveGuards, maxRedirects, tabBarPathSet, resolveWithCodec, settleNavigationResult } = options;
|
|
917
|
+
async function push(to) {
|
|
918
|
+
const from = snapshotRouteLocation(route);
|
|
919
|
+
let target;
|
|
920
|
+
try {
|
|
921
|
+
target = resolveWithCodec(to, from.path);
|
|
922
|
+
} catch (error) {
|
|
923
|
+
return settleNavigationResult(createNavigationRunResult("push", from, void 0, createNavigationFailure(NavigationFailureType.unknown, void 0, from, error)));
|
|
924
|
+
}
|
|
925
|
+
return settleNavigationResult(await navigateWithTarget({
|
|
926
|
+
mode: "push",
|
|
927
|
+
target,
|
|
928
|
+
from,
|
|
929
|
+
nativeRouter,
|
|
930
|
+
routeResolveCodec,
|
|
931
|
+
namedRouteLookup,
|
|
932
|
+
beforeEachGuards,
|
|
933
|
+
beforeResolveGuards,
|
|
934
|
+
maxRedirects,
|
|
935
|
+
tabBarPathSet,
|
|
936
|
+
resolveWithCodec
|
|
937
|
+
}));
|
|
938
|
+
}
|
|
939
|
+
async function replace(to) {
|
|
940
|
+
const from = snapshotRouteLocation(route);
|
|
941
|
+
let target;
|
|
942
|
+
try {
|
|
943
|
+
target = resolveWithCodec(to, from.path);
|
|
944
|
+
} catch (error) {
|
|
945
|
+
return settleNavigationResult(createNavigationRunResult("replace", from, void 0, createNavigationFailure(NavigationFailureType.unknown, void 0, from, error)));
|
|
946
|
+
}
|
|
947
|
+
return settleNavigationResult(await navigateWithTarget({
|
|
948
|
+
mode: "replace",
|
|
949
|
+
target,
|
|
950
|
+
from,
|
|
951
|
+
nativeRouter,
|
|
952
|
+
routeResolveCodec,
|
|
953
|
+
namedRouteLookup,
|
|
954
|
+
beforeEachGuards,
|
|
955
|
+
beforeResolveGuards,
|
|
956
|
+
maxRedirects,
|
|
957
|
+
tabBarPathSet,
|
|
958
|
+
resolveWithCodec
|
|
959
|
+
}));
|
|
960
|
+
}
|
|
961
|
+
async function back(delta = 1) {
|
|
962
|
+
const from = snapshotRouteLocation(route);
|
|
963
|
+
const beforeEachResult = await runNavigationGuards(beforeEachGuards, {
|
|
964
|
+
mode: "back",
|
|
965
|
+
from,
|
|
966
|
+
nativeRouter
|
|
967
|
+
}, resolveWithCodec);
|
|
968
|
+
if (beforeEachResult.status === "failure") return settleNavigationResult(createNavigationRunResult("back", from, void 0, beforeEachResult.failure));
|
|
969
|
+
if (beforeEachResult.status === "redirect") return settleNavigationResult(createNavigationRunResult("back", from, void 0, createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Redirect is not supported in back navigation guards")));
|
|
970
|
+
const beforeResolveResult = await runNavigationGuards(beforeResolveGuards, {
|
|
971
|
+
mode: "back",
|
|
972
|
+
from,
|
|
973
|
+
nativeRouter
|
|
974
|
+
}, resolveWithCodec);
|
|
975
|
+
if (beforeResolveResult.status === "failure") return settleNavigationResult(createNavigationRunResult("back", from, void 0, beforeResolveResult.failure));
|
|
976
|
+
if (beforeResolveResult.status === "redirect") return settleNavigationResult(createNavigationRunResult("back", from, void 0, createNavigationFailure(NavigationFailureType.aborted, void 0, from, "Redirect is not supported in back navigation guards")));
|
|
977
|
+
const result = await executeNavigationMethod(nativeRouter.navigateBack, { delta }, void 0, from);
|
|
978
|
+
return settleNavigationResult(isNavigationFailure(result) ? createNavigationRunResult("back", from, void 0, result) : createNavigationRunResult("back", from));
|
|
979
|
+
}
|
|
980
|
+
async function go(delta) {
|
|
981
|
+
if (delta < 0) return back(Math.abs(delta));
|
|
982
|
+
if (delta === 0) return;
|
|
983
|
+
const from = snapshotRouteLocation(route);
|
|
984
|
+
return settleNavigationResult(createNavigationRunResult("back", from, void 0, createForwardNotSupportedFailure(from)));
|
|
985
|
+
}
|
|
986
|
+
async function forward() {
|
|
987
|
+
return go(1);
|
|
988
|
+
}
|
|
989
|
+
return {
|
|
990
|
+
push,
|
|
991
|
+
replace,
|
|
992
|
+
back,
|
|
993
|
+
go,
|
|
994
|
+
forward
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
//#endregion
|
|
999
|
+
//#region src/router/resolve.ts
|
|
1000
|
+
const DEFAULT_ROUTE_RESOLVE_CODEC = {
|
|
1001
|
+
parseQuery,
|
|
1002
|
+
stringifyQuery
|
|
1003
|
+
};
|
|
1004
|
+
function resolveRouteLocation(to, currentPath = "", codec = DEFAULT_ROUTE_RESOLVE_CODEC) {
|
|
1005
|
+
var _ref, _to$path, _parsedFromFullPath$q, _to$hash;
|
|
1006
|
+
if (typeof to === "string") {
|
|
1007
|
+
const parsed = parsePathInput(to, codec);
|
|
1008
|
+
return createRouteLocation(resolvePath(parsed.path, currentPath), parsed.query, parsed.hash, void 0, {}, codec.stringifyQuery);
|
|
1009
|
+
}
|
|
1010
|
+
const parsedFromFullPath = typeof to.fullPath === "string" ? parsePathInput(to.fullPath, codec) : void 0;
|
|
1011
|
+
return createRouteLocation(resolvePath((_ref = (_to$path = to.path) !== null && _to$path !== void 0 ? _to$path : parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.path) !== null && _ref !== void 0 ? _ref : currentPath, currentPath), to.query ? normalizeQuery(to.query) : (_parsedFromFullPath$q = parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.query) !== null && _parsedFromFullPath$q !== void 0 ? _parsedFromFullPath$q : {}, normalizeHash((_to$hash = to.hash) !== null && _to$hash !== void 0 ? _to$hash : parsedFromFullPath === null || parsedFromFullPath === void 0 ? void 0 : parsedFromFullPath.hash), typeof to.name === "string" ? to.name : void 0, to.params ? normalizeRouteParams(to.params) : {}, codec.stringifyQuery);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
//#endregion
|
|
1015
|
+
//#region src/router/routeRegistry.ts
|
|
1016
|
+
function normalizeRouteRecordForOutput(record) {
|
|
1017
|
+
const normalizedRoute = {
|
|
1018
|
+
name: record.name,
|
|
1019
|
+
path: record.path ? `/${record.path}` : "/"
|
|
1020
|
+
};
|
|
1021
|
+
if (record.meta !== void 0) normalizedRoute.meta = cloneRouteMeta(record.meta);
|
|
1022
|
+
if (record.aliasPaths.length === 1) normalizedRoute.alias = createAbsoluteRoutePath(record.aliasPaths[0]);
|
|
1023
|
+
else if (record.aliasPaths.length > 1) normalizedRoute.alias = record.aliasPaths.map((aliasPath) => createAbsoluteRoutePath(aliasPath));
|
|
1024
|
+
if (record.beforeEnterGuards.length === 1) normalizedRoute.beforeEnter = record.beforeEnterGuards[0];
|
|
1025
|
+
else if (record.beforeEnterGuards.length > 1) normalizedRoute.beforeEnter = record.beforeEnterGuards.slice();
|
|
1026
|
+
if (record.redirect !== void 0) normalizedRoute.redirect = record.redirect;
|
|
1027
|
+
return normalizedRoute;
|
|
1028
|
+
}
|
|
1029
|
+
function createRouteRegistry(namedRouteLookup) {
|
|
1030
|
+
const hasRoute = (name) => namedRouteLookup.recordByName.has(name);
|
|
1031
|
+
const getRoutes = () => Array.from(namedRouteLookup.recordByName.values(), normalizeRouteRecordForOutput);
|
|
1032
|
+
const addRoute = ((parentNameOrRoute, maybeRoute) => {
|
|
1033
|
+
const route = typeof parentNameOrRoute === "string" ? maybeRoute : parentNameOrRoute;
|
|
1034
|
+
if (!route) throw new Error("Route record is required when adding a child route");
|
|
1035
|
+
assertValidAddRouteInput(route);
|
|
1036
|
+
const parentRouteName = typeof parentNameOrRoute === "string" ? parentNameOrRoute : void 0;
|
|
1037
|
+
const parentRouteRecord = parentRouteName ? namedRouteLookup.recordByName.get(parentRouteName) : void 0;
|
|
1038
|
+
if (parentRouteName && !parentRouteRecord) throw new Error(`Parent route "${parentRouteName}" is not defined in createRouter({ routes | namedRoutes })`);
|
|
1039
|
+
const routeRecords = flattenNamedRouteRecords([route], parentRouteRecord === null || parentRouteRecord === void 0 ? void 0 : parentRouteRecord.path, parentRouteName, parentRouteRecord === null || parentRouteRecord === void 0 ? void 0 : parentRouteRecord.aliasPaths, void 0, "addRoute");
|
|
1040
|
+
if (routeRecords.length === 0) throw new Error("Route name and path are required when adding a named route");
|
|
1041
|
+
const addedRoutes = [];
|
|
1042
|
+
for (const routeRecord of routeRecords) {
|
|
1043
|
+
const normalizedRoute = normalizeRouteRecordRaw(routeRecord.route, routeRecord.parentName);
|
|
1044
|
+
if (!normalizedRoute) continue;
|
|
1045
|
+
const existingRoute = namedRouteLookup.recordByName.get(normalizedRoute.name);
|
|
1046
|
+
if (existingRoute) {
|
|
1047
|
+
const namesToRemove = collectRouteNamesForRemoval(existingRoute.name, namedRouteLookup.recordByName);
|
|
1048
|
+
for (const routeName of namesToRemove) namedRouteLookup.recordByName.delete(routeName);
|
|
1049
|
+
warnRouteConfig(`addRoute() replaced existing route "${normalizedRoute.name}" (${createAbsoluteRoutePath(existingRoute.path)} -> ${createAbsoluteRoutePath(normalizedRoute.path)}) and removed ${namesToRemove.size - 1} nested route(s)`);
|
|
1050
|
+
}
|
|
1051
|
+
namedRouteLookup.recordByName.set(normalizedRoute.name, normalizedRoute);
|
|
1052
|
+
addedRoutes.push(normalizedRoute);
|
|
1053
|
+
}
|
|
1054
|
+
namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1055
|
+
return () => {
|
|
1056
|
+
let changed = false;
|
|
1057
|
+
for (const addedRoute of addedRoutes) if (namedRouteLookup.recordByName.get(addedRoute.name) === addedRoute) {
|
|
1058
|
+
namedRouteLookup.recordByName.delete(addedRoute.name);
|
|
1059
|
+
changed = true;
|
|
1060
|
+
}
|
|
1061
|
+
if (changed) namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1062
|
+
};
|
|
1063
|
+
});
|
|
1064
|
+
function removeRoute(name) {
|
|
1065
|
+
const namesToRemove = collectRouteNamesForRemoval(name, namedRouteLookup.recordByName);
|
|
1066
|
+
if (namesToRemove.size === 0) return;
|
|
1067
|
+
let changed = false;
|
|
1068
|
+
for (const routeName of namesToRemove) changed = namedRouteLookup.recordByName.delete(routeName) || changed;
|
|
1069
|
+
if (changed) namedRouteLookup.nameByStaticPath = createNamedRouteNameByStaticPath(namedRouteLookup.recordByName);
|
|
1070
|
+
}
|
|
1071
|
+
function clearRoutes() {
|
|
1072
|
+
namedRouteLookup.recordByName.clear();
|
|
1073
|
+
namedRouteLookup.nameByStaticPath.clear();
|
|
1074
|
+
}
|
|
1075
|
+
return {
|
|
1076
|
+
hasRoute,
|
|
1077
|
+
getRoutes,
|
|
1078
|
+
addRoute,
|
|
1079
|
+
removeRoute,
|
|
1080
|
+
clearRoutes
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
//#endregion
|
|
1085
|
+
//#region src/router/useRoute.ts
|
|
1086
|
+
function useRoute() {
|
|
1087
|
+
if (!getCurrentSetupContext()) throw new Error("useRoute() 必须在 setup() 的同步阶段调用");
|
|
1088
|
+
const currentRoute = resolveCurrentRoute();
|
|
1089
|
+
const routeState = reactive({
|
|
1090
|
+
path: currentRoute.path,
|
|
1091
|
+
fullPath: currentRoute.fullPath,
|
|
1092
|
+
query: currentRoute.query,
|
|
1093
|
+
hash: currentRoute.hash,
|
|
1094
|
+
params: currentRoute.params,
|
|
1095
|
+
name: currentRoute.name
|
|
1096
|
+
});
|
|
1097
|
+
if (currentRoute.meta !== void 0) routeState.meta = currentRoute.meta;
|
|
1098
|
+
function syncRoute(queryOverride) {
|
|
1099
|
+
const nextRoute = resolveCurrentRoute(queryOverride);
|
|
1100
|
+
routeState.path = nextRoute.path;
|
|
1101
|
+
routeState.fullPath = nextRoute.fullPath;
|
|
1102
|
+
routeState.query = nextRoute.query;
|
|
1103
|
+
routeState.hash = nextRoute.hash;
|
|
1104
|
+
if (nextRoute.meta === void 0) delete routeState.meta;
|
|
1105
|
+
else routeState.meta = nextRoute.meta;
|
|
1106
|
+
routeState.params = nextRoute.params;
|
|
1107
|
+
routeState.name = nextRoute.name;
|
|
1108
|
+
}
|
|
1109
|
+
onLoad((query) => {
|
|
1110
|
+
syncRoute(query);
|
|
1111
|
+
});
|
|
1112
|
+
onShow(() => {
|
|
1113
|
+
syncRoute();
|
|
1114
|
+
});
|
|
1115
|
+
onRouteDone(() => {
|
|
1116
|
+
syncRoute();
|
|
1117
|
+
});
|
|
1118
|
+
return readonly(routeState);
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* @description 获取当前组件路径语义的原生 Router
|
|
1122
|
+
*/
|
|
1123
|
+
function useNativeRouter() {
|
|
1124
|
+
return useNativeRouter$1();
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* @description 获取当前页面路径语义的原生 Page Router
|
|
1128
|
+
*/
|
|
1129
|
+
function useNativePageRouter() {
|
|
1130
|
+
return useNativePageRouter$1();
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
//#endregion
|
|
1134
|
+
//#region src/router/createRouter.ts
|
|
1135
|
+
/**
|
|
1136
|
+
* @description 创建高阶路由导航器(对齐 Vue Router 的 createRouter 心智)
|
|
1137
|
+
*/
|
|
1138
|
+
function createRouter(options = {}) {
|
|
1139
|
+
var _options$maxRedirects, _options$paramsMode, _options$rejectOnErro, _options$parseQuery, _options$stringifyQue, _options$tabBarEntrie;
|
|
1140
|
+
const nativeRouter = useNativeRouter();
|
|
1141
|
+
const route = useRoute();
|
|
1142
|
+
const beforeEachGuards = /* @__PURE__ */ new Set();
|
|
1143
|
+
const beforeResolveGuards = /* @__PURE__ */ new Set();
|
|
1144
|
+
const afterEachHooks = /* @__PURE__ */ new Set();
|
|
1145
|
+
const errorHandlers = /* @__PURE__ */ new Set();
|
|
1146
|
+
const maxRedirects = (_options$maxRedirects = options.maxRedirects) !== null && _options$maxRedirects !== void 0 ? _options$maxRedirects : 10;
|
|
1147
|
+
const paramsMode = (_options$paramsMode = options.paramsMode) !== null && _options$paramsMode !== void 0 ? _options$paramsMode : "loose";
|
|
1148
|
+
const rejectOnError = (_options$rejectOnErro = options.rejectOnError) !== null && _options$rejectOnErro !== void 0 ? _options$rejectOnErro : true;
|
|
1149
|
+
const routeResolveCodec = {
|
|
1150
|
+
parseQuery: (_options$parseQuery = options.parseQuery) !== null && _options$parseQuery !== void 0 ? _options$parseQuery : parseQuery,
|
|
1151
|
+
stringifyQuery: (_options$stringifyQue = options.stringifyQuery) !== null && _options$stringifyQue !== void 0 ? _options$stringifyQue : stringifyQuery
|
|
1152
|
+
};
|
|
1153
|
+
const readyPromise = Promise.resolve();
|
|
1154
|
+
const routeEntries = resolveRouteOptionEntries(options);
|
|
1155
|
+
warnDuplicateRouteEntries(routeEntries);
|
|
1156
|
+
const namedRouteLookup = createNamedRouteLookup(routeEntries);
|
|
1157
|
+
const normalizedTabBarEntries = ((_options$tabBarEntrie = options.tabBarEntries) !== null && _options$tabBarEntrie !== void 0 ? _options$tabBarEntrie : []).map((path) => resolvePath(path, "")).filter(Boolean);
|
|
1158
|
+
const tabBarPathSet = new Set(normalizedTabBarEntries);
|
|
1159
|
+
const routeRegistry = createRouteRegistry(namedRouteLookup);
|
|
1160
|
+
const routerOptions = createRouterOptionsSnapshot(normalizedTabBarEntries, routeRegistry.getRoutes(), paramsMode, maxRedirects, routeResolveCodec, rejectOnError);
|
|
1161
|
+
function resolveWithCodec(to, currentPath) {
|
|
1162
|
+
const resolved = resolveRouteLocation(typeof to === "string" ? to : resolveNamedRouteLocation(to, namedRouteLookup, paramsMode), currentPath, routeResolveCodec);
|
|
1163
|
+
resolved.href = resolved.fullPath;
|
|
1164
|
+
const matchedResult = resolveMatchedRouteRecord(resolved, namedRouteLookup);
|
|
1165
|
+
if (matchedResult) {
|
|
1166
|
+
const matchedRecord = matchedResult.record;
|
|
1167
|
+
if (resolved.name === void 0) resolved.name = matchedRecord.name;
|
|
1168
|
+
const mergedRouteMeta = mergeMatchedRouteMeta(matchedResult.matchedRecords);
|
|
1169
|
+
if (mergedRouteMeta !== void 0) resolved.meta = cloneRouteMeta(mergedRouteMeta);
|
|
1170
|
+
if (matchedResult.params && Object.keys(resolved.params).length === 0) resolved.params = cloneRouteParams(matchedResult.params);
|
|
1171
|
+
resolved.matched = matchedResult.matchedRecords.map((record, index, records) => {
|
|
1172
|
+
return normalizeRouteRecordMatched(record, index === records.length - 1 ? matchedResult.matchedPath : void 0);
|
|
1173
|
+
});
|
|
1174
|
+
} else resolved.matched = [];
|
|
1175
|
+
return resolved;
|
|
1176
|
+
}
|
|
1177
|
+
function resolve(to) {
|
|
1178
|
+
return resolveWithCodec(to, route.path);
|
|
1179
|
+
}
|
|
1180
|
+
const navigationApi = createNavigationApi({
|
|
1181
|
+
nativeRouter,
|
|
1182
|
+
route,
|
|
1183
|
+
routeResolveCodec,
|
|
1184
|
+
namedRouteLookup,
|
|
1185
|
+
beforeEachGuards,
|
|
1186
|
+
beforeResolveGuards,
|
|
1187
|
+
maxRedirects,
|
|
1188
|
+
tabBarPathSet,
|
|
1189
|
+
resolveWithCodec,
|
|
1190
|
+
settleNavigationResult: createNavigationResultController({
|
|
1191
|
+
afterEachHooks,
|
|
1192
|
+
errorHandlers,
|
|
1193
|
+
nativeRouter,
|
|
1194
|
+
rejectOnError
|
|
1195
|
+
}).settleNavigationResult
|
|
1196
|
+
});
|
|
1197
|
+
function beforeEach(guard) {
|
|
1198
|
+
beforeEachGuards.add(guard);
|
|
1199
|
+
return () => {
|
|
1200
|
+
beforeEachGuards.delete(guard);
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
function beforeResolve(guard) {
|
|
1204
|
+
beforeResolveGuards.add(guard);
|
|
1205
|
+
return () => {
|
|
1206
|
+
beforeResolveGuards.delete(guard);
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
function afterEach(hook) {
|
|
1210
|
+
afterEachHooks.add(hook);
|
|
1211
|
+
return () => {
|
|
1212
|
+
afterEachHooks.delete(hook);
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
function onError(handler) {
|
|
1216
|
+
errorHandlers.add(handler);
|
|
1217
|
+
return () => {
|
|
1218
|
+
errorHandlers.delete(handler);
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1221
|
+
const router = {
|
|
1222
|
+
nativeRouter,
|
|
1223
|
+
options: routerOptions,
|
|
1224
|
+
currentRoute: route,
|
|
1225
|
+
install(app) {
|
|
1226
|
+
var _config;
|
|
1227
|
+
setActiveRouter(router);
|
|
1228
|
+
const globalProperties = app === null || app === void 0 || (_config = app.config) === null || _config === void 0 ? void 0 : _config.globalProperties;
|
|
1229
|
+
if (globalProperties) globalProperties.$router = router;
|
|
1230
|
+
},
|
|
1231
|
+
resolve,
|
|
1232
|
+
isReady() {
|
|
1233
|
+
return readyPromise;
|
|
1234
|
+
},
|
|
1235
|
+
push: navigationApi.push,
|
|
1236
|
+
replace: navigationApi.replace,
|
|
1237
|
+
back: navigationApi.back,
|
|
1238
|
+
go: navigationApi.go,
|
|
1239
|
+
forward: navigationApi.forward,
|
|
1240
|
+
hasRoute: routeRegistry.hasRoute,
|
|
1241
|
+
getRoutes: routeRegistry.getRoutes,
|
|
1242
|
+
addRoute: routeRegistry.addRoute,
|
|
1243
|
+
removeRoute: routeRegistry.removeRoute,
|
|
1244
|
+
clearRoutes: routeRegistry.clearRoutes,
|
|
1245
|
+
beforeEach,
|
|
1246
|
+
beforeResolve,
|
|
1247
|
+
afterEach,
|
|
1248
|
+
onError
|
|
1249
|
+
};
|
|
1250
|
+
setActiveRouter(router);
|
|
1251
|
+
return router;
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
//#endregion
|
|
1255
|
+
//#region src/router/useRouter.ts
|
|
1256
|
+
/**
|
|
1257
|
+
* @description 获取当前已创建的路由实例。
|
|
1258
|
+
*/
|
|
1259
|
+
function useRouter() {
|
|
1260
|
+
const router = getActiveRouter();
|
|
1261
|
+
if (router) return router;
|
|
1262
|
+
throw new Error("useRouter() 未找到已创建的 router 实例。请先在 setup() 中调用 createRouter()。");
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
//#endregion
|
|
1266
|
+
export { NavigationFailureType, createNavigationFailure, createRouter, isNavigationFailure, parseQuery, resolveRouteLocation, stringifyQuery, useNativePageRouter, useNativeRouter, useRoute, useRouter };
|
|
1267
|
+
//# sourceMappingURL=router.mjs.map
|