@xfe-repo/web-router 1.2.1 → 1.2.3
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/index.d.mts +53 -0
- package/dist/index.d.ts +2 -8
- package/dist/index.js +12 -52
- package/dist/index.mjs +417 -0
- package/package.json +22 -6
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { History } from 'history';
|
|
2
|
+
import { LoadableComponent, loadableReady } from '@loadable/component';
|
|
3
|
+
import { ParsedQuery } from '@xfe-repo/web-utils/tools';
|
|
4
|
+
import React, { ReactElement, PropsWithChildren, ComponentType } from 'react';
|
|
5
|
+
import { RegisterComponentStatic } from '@xfe-repo/web-register';
|
|
6
|
+
import { LinkProps as LinkProps$1 } from 'react-router-dom';
|
|
7
|
+
export * from 'react-router-dom';
|
|
8
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
9
|
+
|
|
10
|
+
type PageComponent = LoadableComponent<any> & RegisterComponentStatic;
|
|
11
|
+
type RouteConfig = {
|
|
12
|
+
path: string;
|
|
13
|
+
Component: PageComponent;
|
|
14
|
+
};
|
|
15
|
+
type GetRoutesConfig = {
|
|
16
|
+
pagesIndexPath: string[];
|
|
17
|
+
getRoutePath?: (path: string) => string;
|
|
18
|
+
getPageComponents: (path: string) => Promise<any>;
|
|
19
|
+
fallback?: ReactElement;
|
|
20
|
+
};
|
|
21
|
+
declare const routerIndexRegx: RegExp;
|
|
22
|
+
declare const routerDynamicRegx: RegExp;
|
|
23
|
+
declare const getRoutes: (config: GetRoutesConfig) => RouteConfig[];
|
|
24
|
+
|
|
25
|
+
type RouterProps = PropsWithChildren<{
|
|
26
|
+
navigator: NavigatorType;
|
|
27
|
+
routes: RouteConfig[];
|
|
28
|
+
basename?: string;
|
|
29
|
+
NotFoundPageComponent?: ComponentType;
|
|
30
|
+
}>;
|
|
31
|
+
declare const Router: React.MemoExoticComponent<(props: RouterProps) => react_jsx_runtime.JSX.Element>;
|
|
32
|
+
|
|
33
|
+
type LinkProps = Omit<LinkProps$1, 'to'> & {
|
|
34
|
+
to: string;
|
|
35
|
+
preload?: boolean;
|
|
36
|
+
};
|
|
37
|
+
declare const Link: React.MemoExoticComponent<(props: LinkProps) => react_jsx_runtime.JSX.Element>;
|
|
38
|
+
|
|
39
|
+
interface NavigatorType extends History {
|
|
40
|
+
preload: (path: string, isReplace?: boolean, query?: ParsedQuery<string | number | boolean>) => Promise<void>;
|
|
41
|
+
leave: (path: string) => void;
|
|
42
|
+
}
|
|
43
|
+
type CreateNavigatorOptions = {
|
|
44
|
+
mode: 'static' | 'browser' | 'hash';
|
|
45
|
+
routes: RouteConfig[];
|
|
46
|
+
initPathname?: string;
|
|
47
|
+
redirect?: (path: string) => void;
|
|
48
|
+
};
|
|
49
|
+
declare function createNavigator(options: CreateNavigatorOptions): NavigatorType;
|
|
50
|
+
declare const getClientNavigator: () => NavigatorType;
|
|
51
|
+
declare const routerReady: typeof loadableReady;
|
|
52
|
+
|
|
53
|
+
export { type CreateNavigatorOptions, type GetRoutesConfig, Link, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerDynamicRegx, routerIndexRegx, routerReady };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { History } from 'history';
|
|
2
2
|
import { LoadableComponent, loadableReady } from '@loadable/component';
|
|
3
|
-
import { ParsedQuery } from '
|
|
3
|
+
import { ParsedQuery } from '@xfe-repo/web-utils/tools';
|
|
4
4
|
import React, { ReactElement, PropsWithChildren, ComponentType } from 'react';
|
|
5
5
|
import { RegisterComponentStatic } from '@xfe-repo/web-register';
|
|
6
6
|
import { LinkProps as LinkProps$1 } from 'react-router-dom';
|
|
@@ -32,17 +32,11 @@ declare const Router: React.MemoExoticComponent<(props: RouterProps) => react_js
|
|
|
32
32
|
|
|
33
33
|
type LinkProps = Omit<LinkProps$1, 'to'> & {
|
|
34
34
|
to: string;
|
|
35
|
-
reLaunch?: boolean;
|
|
36
35
|
preload?: boolean;
|
|
37
36
|
};
|
|
38
37
|
declare const Link: React.MemoExoticComponent<(props: LinkProps) => react_jsx_runtime.JSX.Element>;
|
|
39
38
|
|
|
40
|
-
type NavigatorStack = {
|
|
41
|
-
pathname: string;
|
|
42
|
-
};
|
|
43
39
|
interface NavigatorType extends History {
|
|
44
|
-
stack: NavigatorStack[];
|
|
45
|
-
reLaunch: (path: string, state?: any) => void;
|
|
46
40
|
preload: (path: string, isReplace?: boolean, query?: ParsedQuery<string | number | boolean>) => Promise<void>;
|
|
47
41
|
leave: (path: string) => void;
|
|
48
42
|
}
|
|
@@ -56,4 +50,4 @@ declare function createNavigator(options: CreateNavigatorOptions): NavigatorType
|
|
|
56
50
|
declare const getClientNavigator: () => NavigatorType;
|
|
57
51
|
declare const routerReady: typeof loadableReady;
|
|
58
52
|
|
|
59
|
-
export { type CreateNavigatorOptions, type GetRoutesConfig, Link, type
|
|
53
|
+
export { type CreateNavigatorOptions, type GetRoutesConfig, Link, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerDynamicRegx, routerIndexRegx, routerReady };
|
package/dist/index.js
CHANGED
|
@@ -290,8 +290,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
290
290
|
var import_history = require("history");
|
|
291
291
|
var import_component2 = require("@loadable/component");
|
|
292
292
|
var import_react_router_dom3 = require("react-router-dom");
|
|
293
|
-
var
|
|
294
|
-
var import_env = require("@xfe-repo/web-utils/env");
|
|
293
|
+
var import_tools = require("@xfe-repo/web-utils/tools");
|
|
295
294
|
__reExport(src_exports, require("react-router-dom"), module.exports);
|
|
296
295
|
// src/routes.ts
|
|
297
296
|
var import_component = __toESM(require("@loadable/component"));
|
|
@@ -366,36 +365,29 @@ var import_react2 = require("react");
|
|
|
366
365
|
var import_react_router_dom2 = require("react-router-dom");
|
|
367
366
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
368
367
|
var Link = (0, import_react2.memo)(function(props) {
|
|
369
|
-
var to = props.to, children = props.children,
|
|
368
|
+
var to = props.to, children = props.children, preload = props.preload, replace = props.replace, className = props.className, target = props.target;
|
|
370
369
|
var _navigator2 = (0, import_react2.useMemo)(function() {
|
|
371
370
|
return getClientNavigator();
|
|
372
371
|
}, []);
|
|
373
372
|
if (/http(s)?:\/{2}/.test(to)) {
|
|
374
373
|
var handleLink = function(e) {
|
|
375
374
|
e.preventDefault();
|
|
376
|
-
|
|
375
|
+
if (target === "_blank") {
|
|
376
|
+
window.open(to);
|
|
377
|
+
} else {
|
|
378
|
+
window.location.href = to;
|
|
379
|
+
}
|
|
377
380
|
};
|
|
378
381
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
379
382
|
href: to,
|
|
380
383
|
onClick: handleLink,
|
|
381
384
|
className: className,
|
|
382
|
-
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
if (reLaunch) {
|
|
386
|
-
var handleLink1 = function(e) {
|
|
387
|
-
e.preventDefault();
|
|
388
|
-
_navigator2.reLaunch(to);
|
|
389
|
-
};
|
|
390
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
391
|
-
href: to,
|
|
392
|
-
onClick: handleLink1,
|
|
393
|
-
className: className,
|
|
385
|
+
target: target,
|
|
394
386
|
children: children
|
|
395
387
|
});
|
|
396
388
|
}
|
|
397
389
|
if (preload) {
|
|
398
|
-
var
|
|
390
|
+
var handleLink1 = function() {
|
|
399
391
|
var _ref = _async_to_generator(function(e) {
|
|
400
392
|
return _ts_generator(this, function(_state) {
|
|
401
393
|
switch(_state.label){
|
|
@@ -413,13 +405,13 @@ var Link = (0, import_react2.memo)(function(props) {
|
|
|
413
405
|
}
|
|
414
406
|
});
|
|
415
407
|
});
|
|
416
|
-
return function
|
|
408
|
+
return function handleLink1(e) {
|
|
417
409
|
return _ref.apply(this, arguments);
|
|
418
410
|
};
|
|
419
411
|
}();
|
|
420
412
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
421
413
|
href: to,
|
|
422
|
-
onClick:
|
|
414
|
+
onClick: handleLink1,
|
|
423
415
|
className: className,
|
|
424
416
|
children: children
|
|
425
417
|
});
|
|
@@ -443,38 +435,6 @@ function createNavigator(options) {
|
|
|
443
435
|
originHistory = (0, import_history.createHashHistory)();
|
|
444
436
|
}
|
|
445
437
|
_navigator = originHistory;
|
|
446
|
-
var initialStack = [
|
|
447
|
-
{
|
|
448
|
-
pathname: _navigator.location.pathname
|
|
449
|
-
}
|
|
450
|
-
];
|
|
451
|
-
var stack = _navigator.stack = initialStack;
|
|
452
|
-
_navigator.listen(function(param) {
|
|
453
|
-
var location = param.location, action = param.action;
|
|
454
|
-
var pathname = location.pathname;
|
|
455
|
-
if (action === "PUSH") {
|
|
456
|
-
stack.push({
|
|
457
|
-
pathname: pathname
|
|
458
|
-
});
|
|
459
|
-
} else if (action === "REPLACE") {
|
|
460
|
-
stack.pop();
|
|
461
|
-
stack.push({
|
|
462
|
-
pathname: pathname
|
|
463
|
-
});
|
|
464
|
-
} else if (action === "POP") {
|
|
465
|
-
stack.pop();
|
|
466
|
-
}
|
|
467
|
-
if (!import_env.isProduction) console.log("router", action, pathname, stack);
|
|
468
|
-
});
|
|
469
|
-
_navigator.reLaunch = function(path, state) {
|
|
470
|
-
var _stack_;
|
|
471
|
-
var shouldBlack = -stack.length + 1;
|
|
472
|
-
if (shouldBlack < 0) _navigator.go(shouldBlack);
|
|
473
|
-
if (((_stack_ = stack[0]) === null || _stack_ === void 0 ? void 0 : _stack_.pathname) === path) return;
|
|
474
|
-
setTimeout(function() {
|
|
475
|
-
_navigator.replace(path, state);
|
|
476
|
-
}, 1);
|
|
477
|
-
};
|
|
478
438
|
_navigator.preload = function() {
|
|
479
439
|
var _ref = _async_to_generator(function(path, isReplace, query) {
|
|
480
440
|
var _, _this, _path_split, tmp, originPath, tmp1, search, matchPath, currentRoute, _pageModule_default_emitOnLoad, _pageModule_default, pageModule, _currentRoute_Component_emitOnLoad, _currentRoute_Component;
|
|
@@ -484,7 +444,7 @@ function createNavigator(options) {
|
|
|
484
444
|
_path_split = _sliced_to_array(path.split("?"), 2), tmp = _path_split[0], originPath = tmp === void 0 ? "" : tmp, tmp1 = _path_split[1], search = tmp1 === void 0 ? "" : tmp1;
|
|
485
445
|
matchPath = originPath.toLowerCase();
|
|
486
446
|
currentRoute = (_this = (0, import_react_router_dom3.matchRoutes)(routes, matchPath)) === null || _this === void 0 ? void 0 : (_ = _this[0]) === null || _ === void 0 ? void 0 : _.route;
|
|
487
|
-
query = query ||
|
|
447
|
+
query = query || import_tools.queryString.parse(search, {
|
|
488
448
|
parseBooleans: true,
|
|
489
449
|
parseNumbers: true
|
|
490
450
|
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function _array_like_to_array(arr, len) {
|
|
3
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
4
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
5
|
+
return arr2;
|
|
6
|
+
}
|
|
7
|
+
function _array_with_holes(arr) {
|
|
8
|
+
if (Array.isArray(arr)) return arr;
|
|
9
|
+
}
|
|
10
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
11
|
+
try {
|
|
12
|
+
var info = gen[key](arg);
|
|
13
|
+
var value = info.value;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (info.done) {
|
|
19
|
+
resolve(value);
|
|
20
|
+
} else {
|
|
21
|
+
Promise.resolve(value).then(_next, _throw);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function _async_to_generator(fn) {
|
|
25
|
+
return function() {
|
|
26
|
+
var self = this, args = arguments;
|
|
27
|
+
return new Promise(function(resolve, reject) {
|
|
28
|
+
var gen = fn.apply(self, args);
|
|
29
|
+
function _next(value) {
|
|
30
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
31
|
+
}
|
|
32
|
+
function _throw(err) {
|
|
33
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
34
|
+
}
|
|
35
|
+
_next(undefined);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function _define_property(obj, key, value) {
|
|
40
|
+
if (key in obj) {
|
|
41
|
+
Object.defineProperty(obj, key, {
|
|
42
|
+
value: value,
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
obj[key] = value;
|
|
49
|
+
}
|
|
50
|
+
return obj;
|
|
51
|
+
}
|
|
52
|
+
function _iterable_to_array_limit(arr, i) {
|
|
53
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
54
|
+
if (_i == null) return;
|
|
55
|
+
var _arr = [];
|
|
56
|
+
var _n = true;
|
|
57
|
+
var _d = false;
|
|
58
|
+
var _s, _e;
|
|
59
|
+
try {
|
|
60
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
61
|
+
_arr.push(_s.value);
|
|
62
|
+
if (i && _arr.length === i) break;
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
_d = true;
|
|
66
|
+
_e = err;
|
|
67
|
+
} finally{
|
|
68
|
+
try {
|
|
69
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
70
|
+
} finally{
|
|
71
|
+
if (_d) throw _e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return _arr;
|
|
75
|
+
}
|
|
76
|
+
function _non_iterable_rest() {
|
|
77
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
78
|
+
}
|
|
79
|
+
function _object_spread(target) {
|
|
80
|
+
for(var i = 1; i < arguments.length; i++){
|
|
81
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
82
|
+
var ownKeys = Object.keys(source);
|
|
83
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
84
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
85
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
ownKeys.forEach(function(key) {
|
|
89
|
+
_define_property(target, key, source[key]);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return target;
|
|
93
|
+
}
|
|
94
|
+
function _sliced_to_array(arr, i) {
|
|
95
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
96
|
+
}
|
|
97
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
98
|
+
if (!o) return;
|
|
99
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
100
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
101
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
102
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
103
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
104
|
+
}
|
|
105
|
+
function _ts_generator(thisArg, body) {
|
|
106
|
+
var f, y, t, g, _ = {
|
|
107
|
+
label: 0,
|
|
108
|
+
sent: function() {
|
|
109
|
+
if (t[0] & 1) throw t[1];
|
|
110
|
+
return t[1];
|
|
111
|
+
},
|
|
112
|
+
trys: [],
|
|
113
|
+
ops: []
|
|
114
|
+
};
|
|
115
|
+
return g = {
|
|
116
|
+
next: verb(0),
|
|
117
|
+
"throw": verb(1),
|
|
118
|
+
"return": verb(2)
|
|
119
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
120
|
+
return this;
|
|
121
|
+
}), g;
|
|
122
|
+
function verb(n) {
|
|
123
|
+
return function(v) {
|
|
124
|
+
return step([
|
|
125
|
+
n,
|
|
126
|
+
v
|
|
127
|
+
]);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function step(op) {
|
|
131
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
132
|
+
while(_)try {
|
|
133
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
134
|
+
if (y = 0, t) op = [
|
|
135
|
+
op[0] & 2,
|
|
136
|
+
t.value
|
|
137
|
+
];
|
|
138
|
+
switch(op[0]){
|
|
139
|
+
case 0:
|
|
140
|
+
case 1:
|
|
141
|
+
t = op;
|
|
142
|
+
break;
|
|
143
|
+
case 4:
|
|
144
|
+
_.label++;
|
|
145
|
+
return {
|
|
146
|
+
value: op[1],
|
|
147
|
+
done: false
|
|
148
|
+
};
|
|
149
|
+
case 5:
|
|
150
|
+
_.label++;
|
|
151
|
+
y = op[1];
|
|
152
|
+
op = [
|
|
153
|
+
0
|
|
154
|
+
];
|
|
155
|
+
continue;
|
|
156
|
+
case 7:
|
|
157
|
+
op = _.ops.pop();
|
|
158
|
+
_.trys.pop();
|
|
159
|
+
continue;
|
|
160
|
+
default:
|
|
161
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
162
|
+
_ = 0;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
166
|
+
_.label = op[1];
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
170
|
+
_.label = t[1];
|
|
171
|
+
t = op;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
if (t && _.label < t[2]) {
|
|
175
|
+
_.label = t[2];
|
|
176
|
+
_.ops.push(op);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
if (t[2]) _.ops.pop();
|
|
180
|
+
_.trys.pop();
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
op = body.call(thisArg, _);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
op = [
|
|
186
|
+
6,
|
|
187
|
+
e
|
|
188
|
+
];
|
|
189
|
+
y = 0;
|
|
190
|
+
} finally{
|
|
191
|
+
f = t = 0;
|
|
192
|
+
}
|
|
193
|
+
if (op[0] & 5) throw op[1];
|
|
194
|
+
return {
|
|
195
|
+
value: op[0] ? op[1] : void 0,
|
|
196
|
+
done: true
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
import { createHashHistory, createBrowserHistory, createMemoryHistory } from "history";
|
|
201
|
+
import { loadableReady } from "@loadable/component";
|
|
202
|
+
import { matchRoutes } from "react-router-dom";
|
|
203
|
+
import { queryString } from "@xfe-repo/web-utils/tools";
|
|
204
|
+
export * from "react-router-dom";
|
|
205
|
+
// src/routes.ts
|
|
206
|
+
import loadable from "@loadable/component";
|
|
207
|
+
var routerIndexRegx = /^(?!.*component)(.*)?\/index(\[[^.]+])?\.(ts|js)x?$/;
|
|
208
|
+
var routerDynamicRegx = /\[([^.]+)]/;
|
|
209
|
+
var getRoutes = function(config) {
|
|
210
|
+
var pagesIndexPath = config.pagesIndexPath, getRoutePath = config.getRoutePath, getPageComponents = config.getPageComponents, fallback = config.fallback;
|
|
211
|
+
var routes = [];
|
|
212
|
+
pagesIndexPath.forEach(function(pageIndexPath) {
|
|
213
|
+
var _pageIndexPath_match;
|
|
214
|
+
if (pageIndexPath.startsWith("./")) return;
|
|
215
|
+
var pageComponent = loadable(function() {
|
|
216
|
+
return getPageComponents(pageIndexPath);
|
|
217
|
+
}, {
|
|
218
|
+
fallback: fallback
|
|
219
|
+
});
|
|
220
|
+
var routerPath = getRoutePath ? getRoutePath(pageIndexPath) : pageIndexPath.replace(routerIndexRegx, "$1");
|
|
221
|
+
if (routerPath === "home" || routerPath === "index") routerPath = "/";
|
|
222
|
+
var dynamicRoute = ((_pageIndexPath_match = pageIndexPath.match(routerDynamicRegx)) === null || _pageIndexPath_match === void 0 ? void 0 : _pageIndexPath_match[1]) || "";
|
|
223
|
+
if (dynamicRoute) routerPath = "".concat(routerPath, "/").concat(dynamicRoute);
|
|
224
|
+
var config2 = {
|
|
225
|
+
path: routerPath,
|
|
226
|
+
Component: pageComponent
|
|
227
|
+
};
|
|
228
|
+
routes.push(config2);
|
|
229
|
+
});
|
|
230
|
+
return routes;
|
|
231
|
+
};
|
|
232
|
+
// src/Router.tsx
|
|
233
|
+
import { memo, useState, useEffect } from "react";
|
|
234
|
+
import { Router as OriginalRouter, Routes, Route } from "react-router-dom";
|
|
235
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
236
|
+
var Router = memo(function(props) {
|
|
237
|
+
var navigator = props.navigator, routes = props.routes, _props_basename = props.basename, basename = _props_basename === void 0 ? "/" : _props_basename, children = props.children, NotFoundPageComponent = props.NotFoundPageComponent;
|
|
238
|
+
var _useState = _sliced_to_array(useState({
|
|
239
|
+
action: navigator.action,
|
|
240
|
+
location: navigator.location
|
|
241
|
+
}), 2), navigatorState = _useState[0], setNavigatorState = _useState[1];
|
|
242
|
+
useEffect(function() {
|
|
243
|
+
return navigator.listen(setNavigatorState);
|
|
244
|
+
}, [
|
|
245
|
+
navigator
|
|
246
|
+
]);
|
|
247
|
+
return /* @__PURE__ */ jsx(OriginalRouter, {
|
|
248
|
+
navigator: navigator,
|
|
249
|
+
navigationType: navigatorState.action,
|
|
250
|
+
location: navigatorState.location,
|
|
251
|
+
basename: basename,
|
|
252
|
+
children: /* @__PURE__ */ jsx(Routes, {
|
|
253
|
+
children: /* @__PURE__ */ jsxs(Route, {
|
|
254
|
+
path: "/",
|
|
255
|
+
element: children,
|
|
256
|
+
children: [
|
|
257
|
+
routes.map(function(param) {
|
|
258
|
+
var path = param.path, Component = param.Component;
|
|
259
|
+
return /* @__PURE__ */ jsx(Route, {
|
|
260
|
+
path: path,
|
|
261
|
+
Component: Component
|
|
262
|
+
}, path);
|
|
263
|
+
}),
|
|
264
|
+
NotFoundPageComponent && /* @__PURE__ */ jsx(Route, {
|
|
265
|
+
path: "*",
|
|
266
|
+
Component: NotFoundPageComponent
|
|
267
|
+
})
|
|
268
|
+
]
|
|
269
|
+
})
|
|
270
|
+
})
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
// src/Link.tsx
|
|
274
|
+
import { memo as memo2, useMemo } from "react";
|
|
275
|
+
import { Link as OriginalLink } from "react-router-dom";
|
|
276
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
277
|
+
var Link = memo2(function(props) {
|
|
278
|
+
var to = props.to, children = props.children, preload = props.preload, replace = props.replace, className = props.className, target = props.target;
|
|
279
|
+
var _navigator2 = useMemo(function() {
|
|
280
|
+
return getClientNavigator();
|
|
281
|
+
}, []);
|
|
282
|
+
if (/http(s)?:\/{2}/.test(to)) {
|
|
283
|
+
var handleLink = function(e) {
|
|
284
|
+
e.preventDefault();
|
|
285
|
+
if (target === "_blank") {
|
|
286
|
+
window.open(to);
|
|
287
|
+
} else {
|
|
288
|
+
window.location.href = to;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
return /* @__PURE__ */ jsx2("a", {
|
|
292
|
+
href: to,
|
|
293
|
+
onClick: handleLink,
|
|
294
|
+
className: className,
|
|
295
|
+
target: target,
|
|
296
|
+
children: children
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
if (preload) {
|
|
300
|
+
var handleLink1 = function() {
|
|
301
|
+
var _ref = _async_to_generator(function(e) {
|
|
302
|
+
return _ts_generator(this, function(_state) {
|
|
303
|
+
switch(_state.label){
|
|
304
|
+
case 0:
|
|
305
|
+
e.preventDefault();
|
|
306
|
+
return [
|
|
307
|
+
4,
|
|
308
|
+
_navigator2.preload(to, replace)
|
|
309
|
+
];
|
|
310
|
+
case 1:
|
|
311
|
+
_state.sent();
|
|
312
|
+
return [
|
|
313
|
+
2
|
|
314
|
+
];
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
return function handleLink1(e) {
|
|
319
|
+
return _ref.apply(this, arguments);
|
|
320
|
+
};
|
|
321
|
+
}();
|
|
322
|
+
return /* @__PURE__ */ jsx2("a", {
|
|
323
|
+
href: to,
|
|
324
|
+
onClick: handleLink1,
|
|
325
|
+
className: className,
|
|
326
|
+
children: children
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return /* @__PURE__ */ jsx2(OriginalLink, _object_spread({}, props));
|
|
330
|
+
});
|
|
331
|
+
// src/index.ts
|
|
332
|
+
var _navigator;
|
|
333
|
+
function createNavigator(options) {
|
|
334
|
+
var mode = options.mode, _options_initPathname = options.initPathname, initPathname = _options_initPathname === void 0 ? "/" : _options_initPathname, routes = options.routes;
|
|
335
|
+
var originHistory;
|
|
336
|
+
if (mode === "static") {
|
|
337
|
+
originHistory = createMemoryHistory({
|
|
338
|
+
initialEntries: [
|
|
339
|
+
initPathname
|
|
340
|
+
]
|
|
341
|
+
});
|
|
342
|
+
} else if (mode === "browser") {
|
|
343
|
+
originHistory = createBrowserHistory();
|
|
344
|
+
} else {
|
|
345
|
+
originHistory = createHashHistory();
|
|
346
|
+
}
|
|
347
|
+
_navigator = originHistory;
|
|
348
|
+
_navigator.preload = function() {
|
|
349
|
+
var _ref = _async_to_generator(function(path, isReplace, query) {
|
|
350
|
+
var _matchRoutes_, _matchRoutes, _path_split, tmp, originPath, tmp1, search, matchPath, currentRoute, _pageModule_default_emitOnLoad, _pageModule_default, pageModule, _currentRoute_Component_emitOnLoad, _currentRoute_Component;
|
|
351
|
+
return _ts_generator(this, function(_state) {
|
|
352
|
+
switch(_state.label){
|
|
353
|
+
case 0:
|
|
354
|
+
_path_split = _sliced_to_array(path.split("?"), 2), tmp = _path_split[0], originPath = tmp === void 0 ? "" : tmp, tmp1 = _path_split[1], search = tmp1 === void 0 ? "" : tmp1;
|
|
355
|
+
matchPath = originPath.toLowerCase();
|
|
356
|
+
currentRoute = (_matchRoutes = matchRoutes(routes, matchPath)) === null || _matchRoutes === void 0 ? void 0 : (_matchRoutes_ = _matchRoutes[0]) === null || _matchRoutes_ === void 0 ? void 0 : _matchRoutes_.route;
|
|
357
|
+
query = query || queryString.parse(search, {
|
|
358
|
+
parseBooleans: true,
|
|
359
|
+
parseNumbers: true
|
|
360
|
+
});
|
|
361
|
+
if (!!(currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.config)) return [
|
|
362
|
+
3,
|
|
363
|
+
3
|
|
364
|
+
];
|
|
365
|
+
return [
|
|
366
|
+
4,
|
|
367
|
+
currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.load()
|
|
368
|
+
];
|
|
369
|
+
case 1:
|
|
370
|
+
pageModule = _state.sent();
|
|
371
|
+
return [
|
|
372
|
+
4,
|
|
373
|
+
(_pageModule_default = pageModule.default) === null || _pageModule_default === void 0 ? void 0 : (_pageModule_default_emitOnLoad = _pageModule_default.emitOnLoad) === null || _pageModule_default_emitOnLoad === void 0 ? void 0 : _pageModule_default_emitOnLoad.call(_pageModule_default, {
|
|
374
|
+
isPreload: true,
|
|
375
|
+
query: query
|
|
376
|
+
})
|
|
377
|
+
];
|
|
378
|
+
case 2:
|
|
379
|
+
_state.sent();
|
|
380
|
+
return [
|
|
381
|
+
3,
|
|
382
|
+
5
|
|
383
|
+
];
|
|
384
|
+
case 3:
|
|
385
|
+
return [
|
|
386
|
+
4,
|
|
387
|
+
currentRoute === null || currentRoute === void 0 ? void 0 : (_currentRoute_Component = currentRoute.Component) === null || _currentRoute_Component === void 0 ? void 0 : (_currentRoute_Component_emitOnLoad = _currentRoute_Component.emitOnLoad) === null || _currentRoute_Component_emitOnLoad === void 0 ? void 0 : _currentRoute_Component_emitOnLoad.call(_currentRoute_Component, {
|
|
388
|
+
isPreload: true,
|
|
389
|
+
query: query
|
|
390
|
+
})
|
|
391
|
+
];
|
|
392
|
+
case 4:
|
|
393
|
+
_state.sent();
|
|
394
|
+
_state.label = 5;
|
|
395
|
+
case 5:
|
|
396
|
+
if (isReplace) {
|
|
397
|
+
_navigator.replace(path);
|
|
398
|
+
} else {
|
|
399
|
+
_navigator.push(path);
|
|
400
|
+
}
|
|
401
|
+
return [
|
|
402
|
+
2
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
return function(path, isReplace, query) {
|
|
408
|
+
return _ref.apply(this, arguments);
|
|
409
|
+
};
|
|
410
|
+
}();
|
|
411
|
+
return _navigator;
|
|
412
|
+
}
|
|
413
|
+
var getClientNavigator = function() {
|
|
414
|
+
return _navigator;
|
|
415
|
+
};
|
|
416
|
+
var routerReady = loadableReady;
|
|
417
|
+
export { Link, Router, createNavigator, getClientNavigator, getRoutes, routerDynamicRegx, routerIndexRegx, routerReady };
|
package/package.json
CHANGED
|
@@ -1,26 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xfe-repo/web-router",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
7
19
|
"files": [
|
|
8
20
|
"dist"
|
|
9
21
|
],
|
|
10
22
|
"dependencies": {
|
|
11
23
|
"@loadable/component": "^5.16.4",
|
|
12
24
|
"history": "^5.3.0",
|
|
13
|
-
"
|
|
14
|
-
"react-router-dom": "6.28.0",
|
|
15
|
-
"@xfe-repo/web-utils": "1.2.1"
|
|
25
|
+
"react-router-dom": "6.28.0"
|
|
16
26
|
},
|
|
17
27
|
"devDependencies": {
|
|
18
28
|
"@types/loadable__component": "^5.13.9",
|
|
19
29
|
"@types/node": "^20.16.5",
|
|
20
30
|
"@types/react": "^18",
|
|
21
|
-
"@xfe-repo/typescript-config": "0.0.6",
|
|
22
31
|
"@xfe-repo/eslint-config": "0.0.5",
|
|
23
|
-
"@xfe-repo/
|
|
32
|
+
"@xfe-repo/typescript-config": "0.0.6",
|
|
33
|
+
"@xfe-repo/web-register": "1.3.5"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@xfe-repo/web-utils": "1.3.7"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
40
|
},
|
|
25
41
|
"scripts": {
|
|
26
42
|
"build": "tsup",
|