@xfe-repo/web-router 1.0.0
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 +3 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +545 -0
- package/dist/index.mjs +149 -0
- package/package.json +41 -0
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { History } from 'history';
|
|
2
|
+
import { LoadableComponent, loadableReady } from '@loadable/component';
|
|
3
|
+
import { ParsedQs } from 'qs';
|
|
4
|
+
import { RegisterComponentStatic } from '@xfe-repo/web-register';
|
|
5
|
+
export * from 'react-router-dom';
|
|
6
|
+
import React, { ReactElement, ComponentType } from 'react';
|
|
7
|
+
|
|
8
|
+
type PageComponent = LoadableComponent<any> & RegisterComponentStatic;
|
|
9
|
+
type RouteConfig = {
|
|
10
|
+
path: string;
|
|
11
|
+
Component: PageComponent;
|
|
12
|
+
};
|
|
13
|
+
declare const getRoutes: (pagesIndexPath: string[], getPageComponents: (path: string) => Promise<any>) => RouteConfig[];
|
|
14
|
+
|
|
15
|
+
type RouterProps = {
|
|
16
|
+
navigator: NavigatorType;
|
|
17
|
+
routes: RouteConfig[];
|
|
18
|
+
basename?: string;
|
|
19
|
+
children: ReactElement;
|
|
20
|
+
NotFoundPageComponent?: ComponentType;
|
|
21
|
+
};
|
|
22
|
+
declare const Router: React.MemoExoticComponent<(props: RouterProps) => JSX.Element>;
|
|
23
|
+
|
|
24
|
+
type LinkProps = {
|
|
25
|
+
to: string;
|
|
26
|
+
children?: ReactElement | string | ReactElement[];
|
|
27
|
+
reLaunch?: boolean;
|
|
28
|
+
replace?: boolean;
|
|
29
|
+
preload?: boolean;
|
|
30
|
+
className?: string;
|
|
31
|
+
};
|
|
32
|
+
declare const Link: (props: LinkProps) => JSX.Element;
|
|
33
|
+
|
|
34
|
+
type NavigatorStack = {
|
|
35
|
+
pathname: string;
|
|
36
|
+
};
|
|
37
|
+
interface NavigatorType extends History {
|
|
38
|
+
stack: NavigatorStack[];
|
|
39
|
+
reLaunch: (path: string, state?: any) => void;
|
|
40
|
+
preload: (path: string, isReplace?: boolean, query?: ParsedQs) => 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, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { History } from 'history';
|
|
2
|
+
import { LoadableComponent, loadableReady } from '@loadable/component';
|
|
3
|
+
import { ParsedQs } from 'qs';
|
|
4
|
+
import { RegisterComponentStatic } from '@xfe-repo/web-register';
|
|
5
|
+
export * from 'react-router-dom';
|
|
6
|
+
import React, { ReactElement, ComponentType } from 'react';
|
|
7
|
+
|
|
8
|
+
type PageComponent = LoadableComponent<any> & RegisterComponentStatic;
|
|
9
|
+
type RouteConfig = {
|
|
10
|
+
path: string;
|
|
11
|
+
Component: PageComponent;
|
|
12
|
+
};
|
|
13
|
+
declare const getRoutes: (pagesIndexPath: string[], getPageComponents: (path: string) => Promise<any>) => RouteConfig[];
|
|
14
|
+
|
|
15
|
+
type RouterProps = {
|
|
16
|
+
navigator: NavigatorType;
|
|
17
|
+
routes: RouteConfig[];
|
|
18
|
+
basename?: string;
|
|
19
|
+
children: ReactElement;
|
|
20
|
+
NotFoundPageComponent?: ComponentType;
|
|
21
|
+
};
|
|
22
|
+
declare const Router: React.MemoExoticComponent<(props: RouterProps) => JSX.Element>;
|
|
23
|
+
|
|
24
|
+
type LinkProps = {
|
|
25
|
+
to: string;
|
|
26
|
+
children?: ReactElement | string | ReactElement[];
|
|
27
|
+
reLaunch?: boolean;
|
|
28
|
+
replace?: boolean;
|
|
29
|
+
preload?: boolean;
|
|
30
|
+
className?: string;
|
|
31
|
+
};
|
|
32
|
+
declare const Link: (props: LinkProps) => JSX.Element;
|
|
33
|
+
|
|
34
|
+
type NavigatorStack = {
|
|
35
|
+
pathname: string;
|
|
36
|
+
};
|
|
37
|
+
interface NavigatorType extends History {
|
|
38
|
+
stack: NavigatorStack[];
|
|
39
|
+
reLaunch: (path: string, state?: any) => void;
|
|
40
|
+
preload: (path: string, isReplace?: boolean, query?: ParsedQs) => 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, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
"use strict";
|
|
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
|
+
var __create = Object.create;
|
|
201
|
+
var __defProp = Object.defineProperty;
|
|
202
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
203
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
204
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
205
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
206
|
+
var __export = function(target, all) {
|
|
207
|
+
for(var name in all)__defProp(target, name, {
|
|
208
|
+
get: all[name],
|
|
209
|
+
enumerable: true
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
var __copyProps = function(to, from, except, desc) {
|
|
213
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
214
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
215
|
+
try {
|
|
216
|
+
var _loop = function() {
|
|
217
|
+
var key = _step.value;
|
|
218
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
219
|
+
get: function() {
|
|
220
|
+
return from[key];
|
|
221
|
+
},
|
|
222
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
226
|
+
} catch (err) {
|
|
227
|
+
_didIteratorError = true;
|
|
228
|
+
_iteratorError = err;
|
|
229
|
+
} finally{
|
|
230
|
+
try {
|
|
231
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
232
|
+
_iterator.return();
|
|
233
|
+
}
|
|
234
|
+
} finally{
|
|
235
|
+
if (_didIteratorError) {
|
|
236
|
+
throw _iteratorError;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return to;
|
|
242
|
+
};
|
|
243
|
+
var __reExport = function(target, mod, secondTarget) {
|
|
244
|
+
return __copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default");
|
|
245
|
+
};
|
|
246
|
+
var __toESM = function(mod, isNodeMode, target) {
|
|
247
|
+
return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
|
|
248
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
249
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
250
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
251
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
252
|
+
value: mod,
|
|
253
|
+
enumerable: true
|
|
254
|
+
}) : target, mod);
|
|
255
|
+
};
|
|
256
|
+
var __toCommonJS = function(mod) {
|
|
257
|
+
return __copyProps(__defProp({}, "__esModule", {
|
|
258
|
+
value: true
|
|
259
|
+
}), mod);
|
|
260
|
+
};
|
|
261
|
+
// src/index.ts
|
|
262
|
+
var src_exports = {};
|
|
263
|
+
__export(src_exports, {
|
|
264
|
+
Link: function() {
|
|
265
|
+
return Link;
|
|
266
|
+
},
|
|
267
|
+
Router: function() {
|
|
268
|
+
return Router;
|
|
269
|
+
},
|
|
270
|
+
createNavigator: function() {
|
|
271
|
+
return createNavigator;
|
|
272
|
+
},
|
|
273
|
+
getClientNavigator: function() {
|
|
274
|
+
return getClientNavigator;
|
|
275
|
+
},
|
|
276
|
+
getRoutes: function() {
|
|
277
|
+
return getRoutes;
|
|
278
|
+
},
|
|
279
|
+
routerReady: function() {
|
|
280
|
+
return routerReady;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
module.exports = __toCommonJS(src_exports);
|
|
284
|
+
var import_history = require("history");
|
|
285
|
+
var import_component2 = require("@loadable/component");
|
|
286
|
+
var import_react_router_dom3 = require("react-router-dom");
|
|
287
|
+
var import_qs = __toESM(require("qs"));
|
|
288
|
+
__reExport(src_exports, require("react-router-dom"), module.exports);
|
|
289
|
+
// src/routes.ts
|
|
290
|
+
var import_react = require("react");
|
|
291
|
+
var import_component = __toESM(require("@loadable/component"));
|
|
292
|
+
var import_web_components = require("@xfe-repo/web-components");
|
|
293
|
+
var getRoutes = function(pagesIndexPath, getPageComponents) {
|
|
294
|
+
var routes = [];
|
|
295
|
+
pagesIndexPath.forEach(function(pageIndexPath) {
|
|
296
|
+
if (pageIndexPath.startsWith("./")) return;
|
|
297
|
+
var regExpResult = pageIndexPath.match(RegExp("(?<=pages\\/).*(?=\\/index.(?:tsx|js))"));
|
|
298
|
+
var pageRelativePath = regExpResult ? regExpResult[0].toLowerCase() : "";
|
|
299
|
+
var pageComponent = (0, import_component.default)(function() {
|
|
300
|
+
return getPageComponents(pageRelativePath);
|
|
301
|
+
}, {
|
|
302
|
+
fallback: (0, import_react.createElement)(import_web_components.Loading, {
|
|
303
|
+
loading: true
|
|
304
|
+
})
|
|
305
|
+
});
|
|
306
|
+
var routerPath = "/".concat(pageRelativePath);
|
|
307
|
+
if (routerPath === "/home" || routerPath === "/index") routerPath = "/";
|
|
308
|
+
var config = {
|
|
309
|
+
path: routerPath,
|
|
310
|
+
Component: pageComponent
|
|
311
|
+
};
|
|
312
|
+
routes.push(config);
|
|
313
|
+
});
|
|
314
|
+
return routes;
|
|
315
|
+
};
|
|
316
|
+
// src/Router.tsx
|
|
317
|
+
var import_react2 = require("react");
|
|
318
|
+
var import_react_router_dom = require("react-router-dom");
|
|
319
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
320
|
+
var Router = (0, import_react2.memo)(function(props) {
|
|
321
|
+
var navigator = props.navigator, routes = props.routes, _props_basename = props.basename, basename = _props_basename === void 0 ? "/" : _props_basename, children = props.children, NotFoundPageComponent = props.NotFoundPageComponent;
|
|
322
|
+
var _ref = _sliced_to_array((0, import_react2.useState)({
|
|
323
|
+
action: navigator.action,
|
|
324
|
+
location: navigator.location
|
|
325
|
+
}), 2), navigatorState = _ref[0], setNavigatorState = _ref[1];
|
|
326
|
+
(0, import_react2.useEffect)(function() {
|
|
327
|
+
return navigator.listen(setNavigatorState);
|
|
328
|
+
}, [
|
|
329
|
+
navigator
|
|
330
|
+
]);
|
|
331
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Router, {
|
|
332
|
+
navigator: navigator,
|
|
333
|
+
navigationType: navigatorState.action,
|
|
334
|
+
location: navigatorState.location,
|
|
335
|
+
basename: basename,
|
|
336
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Routes, {
|
|
337
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_router_dom.Route, {
|
|
338
|
+
path: "/",
|
|
339
|
+
element: children,
|
|
340
|
+
children: [
|
|
341
|
+
routes.map(function(param) {
|
|
342
|
+
var path = param.path, Component = param.Component;
|
|
343
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
|
344
|
+
path: path,
|
|
345
|
+
Component: Component
|
|
346
|
+
}, path);
|
|
347
|
+
}),
|
|
348
|
+
NotFoundPageComponent && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
|
349
|
+
path: "*",
|
|
350
|
+
Component: NotFoundPageComponent
|
|
351
|
+
})
|
|
352
|
+
]
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
// src/Link.tsx
|
|
358
|
+
var import_react3 = require("react");
|
|
359
|
+
var import_react_router_dom2 = require("react-router-dom");
|
|
360
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
361
|
+
var Link = function(props) {
|
|
362
|
+
var to = props.to, children = props.children, reLaunch = props.reLaunch, preload = props.preload, replace = props.replace, className = props.className;
|
|
363
|
+
var _navigator2 = (0, import_react3.useMemo)(function() {
|
|
364
|
+
return getClientNavigator();
|
|
365
|
+
}, []);
|
|
366
|
+
if (/http(s)?:\/{2}/.test(to)) {
|
|
367
|
+
var handleLink = function(e) {
|
|
368
|
+
e.preventDefault();
|
|
369
|
+
window.location.href = to;
|
|
370
|
+
};
|
|
371
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
372
|
+
href: to,
|
|
373
|
+
onClick: handleLink,
|
|
374
|
+
className: className,
|
|
375
|
+
children: children
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (reLaunch) {
|
|
379
|
+
var handleLink1 = function(e) {
|
|
380
|
+
e.preventDefault();
|
|
381
|
+
_navigator2.reLaunch(to);
|
|
382
|
+
};
|
|
383
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
384
|
+
href: to,
|
|
385
|
+
onClick: handleLink1,
|
|
386
|
+
className: className,
|
|
387
|
+
children: children
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
if (preload) {
|
|
391
|
+
var handleLink2 = function() {
|
|
392
|
+
var _ref = _async_to_generator(function(e) {
|
|
393
|
+
return _ts_generator(this, function(_state) {
|
|
394
|
+
switch(_state.label){
|
|
395
|
+
case 0:
|
|
396
|
+
e.preventDefault();
|
|
397
|
+
return [
|
|
398
|
+
4,
|
|
399
|
+
_navigator2.preload(to, replace)
|
|
400
|
+
];
|
|
401
|
+
case 1:
|
|
402
|
+
_state.sent();
|
|
403
|
+
return [
|
|
404
|
+
2
|
|
405
|
+
];
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
return function handleLink2(e) {
|
|
410
|
+
return _ref.apply(this, arguments);
|
|
411
|
+
};
|
|
412
|
+
}();
|
|
413
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", {
|
|
414
|
+
href: to,
|
|
415
|
+
onClick: handleLink2,
|
|
416
|
+
className: className,
|
|
417
|
+
children: children
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_router_dom2.Link, _object_spread({}, props));
|
|
421
|
+
};
|
|
422
|
+
// src/index.ts
|
|
423
|
+
var _navigator;
|
|
424
|
+
function createNavigator(options) {
|
|
425
|
+
var mode = options.mode, _options_initPathname = options.initPathname, initPathname = _options_initPathname === void 0 ? "/" : _options_initPathname, routes = options.routes;
|
|
426
|
+
var originHistory;
|
|
427
|
+
if (mode === "static") {
|
|
428
|
+
originHistory = (0, import_history.createMemoryHistory)({
|
|
429
|
+
initialEntries: [
|
|
430
|
+
initPathname
|
|
431
|
+
]
|
|
432
|
+
});
|
|
433
|
+
} else if (mode === "browser") {
|
|
434
|
+
originHistory = (0, import_history.createBrowserHistory)();
|
|
435
|
+
} else {
|
|
436
|
+
originHistory = (0, import_history.createHashHistory)();
|
|
437
|
+
}
|
|
438
|
+
_navigator = originHistory;
|
|
439
|
+
var initialStack = [
|
|
440
|
+
{
|
|
441
|
+
pathname: _navigator.location.pathname
|
|
442
|
+
}
|
|
443
|
+
];
|
|
444
|
+
var stack = _navigator.stack = initialStack;
|
|
445
|
+
_navigator.listen(function(param) {
|
|
446
|
+
var location = param.location, action = param.action;
|
|
447
|
+
var pathname = location.pathname;
|
|
448
|
+
if (action === "PUSH") {
|
|
449
|
+
stack.push({
|
|
450
|
+
pathname: pathname
|
|
451
|
+
});
|
|
452
|
+
} else if (action === "REPLACE") {
|
|
453
|
+
stack.pop();
|
|
454
|
+
stack.push({
|
|
455
|
+
pathname: pathname
|
|
456
|
+
});
|
|
457
|
+
} else if (action === "POP") {
|
|
458
|
+
stack.pop();
|
|
459
|
+
}
|
|
460
|
+
console.log("router", action, pathname, stack);
|
|
461
|
+
});
|
|
462
|
+
_navigator.reLaunch = function(path, state) {
|
|
463
|
+
var _stack_;
|
|
464
|
+
var shouldBlack = -stack.length + 1;
|
|
465
|
+
if (shouldBlack < 0) _navigator.go(shouldBlack);
|
|
466
|
+
if (((_stack_ = stack[0]) === null || _stack_ === void 0 ? void 0 : _stack_.pathname) === path) return;
|
|
467
|
+
setTimeout(function() {
|
|
468
|
+
_navigator.replace(path, state);
|
|
469
|
+
}, 1);
|
|
470
|
+
};
|
|
471
|
+
_navigator.preload = function() {
|
|
472
|
+
var _ref = _async_to_generator(function(path, isReplace, query) {
|
|
473
|
+
var _, _this, _path_split, tmp, originPath, tmp1, search, matchPath, currentRoute, _pageModule_default_emitOnLoad, _pageModule_default, pageModule, _currentRoute_Component_emitOnLoad, _currentRoute_Component;
|
|
474
|
+
return _ts_generator(this, function(_state) {
|
|
475
|
+
switch(_state.label){
|
|
476
|
+
case 0:
|
|
477
|
+
_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;
|
|
478
|
+
matchPath = originPath.toLowerCase();
|
|
479
|
+
currentRoute = (_this = (0, import_react_router_dom3.matchRoutes)(routes, matchPath)) === null || _this === void 0 ? void 0 : (_ = _this[0]) === null || _ === void 0 ? void 0 : _.route;
|
|
480
|
+
query = query || import_qs.default.parse(search);
|
|
481
|
+
if (!!(currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.config)) return [
|
|
482
|
+
3,
|
|
483
|
+
3
|
|
484
|
+
];
|
|
485
|
+
return [
|
|
486
|
+
4,
|
|
487
|
+
currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.load()
|
|
488
|
+
];
|
|
489
|
+
case 1:
|
|
490
|
+
pageModule = _state.sent();
|
|
491
|
+
return [
|
|
492
|
+
4,
|
|
493
|
+
(_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, {
|
|
494
|
+
isPreload: true,
|
|
495
|
+
query: query
|
|
496
|
+
})
|
|
497
|
+
];
|
|
498
|
+
case 2:
|
|
499
|
+
_state.sent();
|
|
500
|
+
return [
|
|
501
|
+
3,
|
|
502
|
+
5
|
|
503
|
+
];
|
|
504
|
+
case 3:
|
|
505
|
+
return [
|
|
506
|
+
4,
|
|
507
|
+
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, {
|
|
508
|
+
isPreload: true,
|
|
509
|
+
query: query
|
|
510
|
+
})
|
|
511
|
+
];
|
|
512
|
+
case 4:
|
|
513
|
+
_state.sent();
|
|
514
|
+
_state.label = 5;
|
|
515
|
+
case 5:
|
|
516
|
+
if (isReplace) {
|
|
517
|
+
_navigator.replace(path);
|
|
518
|
+
} else {
|
|
519
|
+
_navigator.push(path);
|
|
520
|
+
}
|
|
521
|
+
return [
|
|
522
|
+
2
|
|
523
|
+
];
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
return function(path, isReplace, query) {
|
|
528
|
+
return _ref.apply(this, arguments);
|
|
529
|
+
};
|
|
530
|
+
}();
|
|
531
|
+
return _navigator;
|
|
532
|
+
}
|
|
533
|
+
var getClientNavigator = function() {
|
|
534
|
+
return _navigator;
|
|
535
|
+
};
|
|
536
|
+
var routerReady = import_component2.loadableReady;
|
|
537
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
538
|
+
0 && (module.exports = _object_spread({
|
|
539
|
+
Link: Link,
|
|
540
|
+
Router: Router,
|
|
541
|
+
createNavigator: createNavigator,
|
|
542
|
+
getClientNavigator: getClientNavigator,
|
|
543
|
+
getRoutes: getRoutes,
|
|
544
|
+
routerReady: routerReady
|
|
545
|
+
}, require("react-router-dom")));
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHashHistory, createBrowserHistory, createMemoryHistory } from "history";
|
|
3
|
+
import { loadableReady } from "@loadable/component";
|
|
4
|
+
import { matchRoutes } from "react-router-dom";
|
|
5
|
+
import qs from "qs";
|
|
6
|
+
export * from "react-router-dom";
|
|
7
|
+
|
|
8
|
+
// src/routes.ts
|
|
9
|
+
import { createElement } from "react";
|
|
10
|
+
import loadable from "@loadable/component";
|
|
11
|
+
import { Loading } from "@xfe-repo/web-components";
|
|
12
|
+
var getRoutes = (pagesIndexPath, getPageComponents) => {
|
|
13
|
+
const routes = [];
|
|
14
|
+
pagesIndexPath.forEach((pageIndexPath) => {
|
|
15
|
+
if (pageIndexPath.startsWith("./"))
|
|
16
|
+
return;
|
|
17
|
+
const regExpResult = pageIndexPath.match(/(?<=pages\/).*(?=\/index.(?:tsx|js))/);
|
|
18
|
+
const pageRelativePath = regExpResult ? regExpResult[0].toLowerCase() : "";
|
|
19
|
+
const pageComponent = loadable(() => getPageComponents(pageRelativePath), {
|
|
20
|
+
fallback: createElement(Loading, { loading: true })
|
|
21
|
+
});
|
|
22
|
+
let routerPath = `/${pageRelativePath}`;
|
|
23
|
+
if (routerPath === "/home" || routerPath === "/index")
|
|
24
|
+
routerPath = "/";
|
|
25
|
+
const config = {
|
|
26
|
+
path: routerPath,
|
|
27
|
+
Component: pageComponent
|
|
28
|
+
};
|
|
29
|
+
routes.push(config);
|
|
30
|
+
});
|
|
31
|
+
return routes;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/Router.tsx
|
|
35
|
+
import { memo, useState, useEffect } from "react";
|
|
36
|
+
import { Router as OriginalRouter, Routes, Route } from "react-router-dom";
|
|
37
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
38
|
+
var Router = memo((props) => {
|
|
39
|
+
const { navigator, routes, basename = "/", children, NotFoundPageComponent } = props;
|
|
40
|
+
const [navigatorState, setNavigatorState] = useState({
|
|
41
|
+
action: navigator.action,
|
|
42
|
+
location: navigator.location
|
|
43
|
+
});
|
|
44
|
+
useEffect(() => navigator.listen(setNavigatorState), [navigator]);
|
|
45
|
+
return /* @__PURE__ */ jsx(OriginalRouter, { navigator, navigationType: navigatorState.action, location: navigatorState.location, basename, children: /* @__PURE__ */ jsx(Routes, { children: /* @__PURE__ */ jsxs(Route, { path: "/", element: children, children: [
|
|
46
|
+
routes.map(({ path, Component }) => /* @__PURE__ */ jsx(Route, { path, Component }, path)),
|
|
47
|
+
NotFoundPageComponent && /* @__PURE__ */ jsx(Route, { path: "*", Component: NotFoundPageComponent })
|
|
48
|
+
] }) }) });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// src/Link.tsx
|
|
52
|
+
import { useMemo } from "react";
|
|
53
|
+
import { Link as OriginalLink } from "react-router-dom";
|
|
54
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
55
|
+
var Link = (props) => {
|
|
56
|
+
const { to, children, reLaunch, preload, replace, className } = props;
|
|
57
|
+
const _navigator2 = useMemo(() => getClientNavigator(), []);
|
|
58
|
+
if (/http(s)?:\/{2}/.test(to)) {
|
|
59
|
+
const handleLink = (e) => {
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
window.location.href = to;
|
|
62
|
+
};
|
|
63
|
+
return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
|
|
64
|
+
}
|
|
65
|
+
if (reLaunch) {
|
|
66
|
+
const handleLink = (e) => {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
_navigator2.reLaunch(to);
|
|
69
|
+
};
|
|
70
|
+
return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
|
|
71
|
+
}
|
|
72
|
+
if (preload) {
|
|
73
|
+
const handleLink = async (e) => {
|
|
74
|
+
e.preventDefault();
|
|
75
|
+
await _navigator2.preload(to, replace);
|
|
76
|
+
};
|
|
77
|
+
return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
|
|
78
|
+
}
|
|
79
|
+
return /* @__PURE__ */ jsx2(OriginalLink, { ...props });
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/index.ts
|
|
83
|
+
var _navigator;
|
|
84
|
+
function createNavigator(options) {
|
|
85
|
+
const { mode, initPathname = "/", routes } = options;
|
|
86
|
+
let originHistory;
|
|
87
|
+
if (mode === "static") {
|
|
88
|
+
originHistory = createMemoryHistory({
|
|
89
|
+
initialEntries: [initPathname]
|
|
90
|
+
});
|
|
91
|
+
} else if (mode === "browser") {
|
|
92
|
+
originHistory = createBrowserHistory();
|
|
93
|
+
} else {
|
|
94
|
+
originHistory = createHashHistory();
|
|
95
|
+
}
|
|
96
|
+
_navigator = originHistory;
|
|
97
|
+
const initialStack = [{ pathname: _navigator.location.pathname }];
|
|
98
|
+
let stack = _navigator.stack = initialStack;
|
|
99
|
+
_navigator.listen(({ location, action }) => {
|
|
100
|
+
const { pathname } = location;
|
|
101
|
+
if (action === "PUSH") {
|
|
102
|
+
stack.push({ pathname });
|
|
103
|
+
} else if (action === "REPLACE") {
|
|
104
|
+
stack.pop();
|
|
105
|
+
stack.push({ pathname });
|
|
106
|
+
} else if (action === "POP") {
|
|
107
|
+
stack.pop();
|
|
108
|
+
}
|
|
109
|
+
console.log("router", action, pathname, stack);
|
|
110
|
+
});
|
|
111
|
+
_navigator.reLaunch = (path, state) => {
|
|
112
|
+
let shouldBlack = -stack.length + 1;
|
|
113
|
+
if (shouldBlack < 0)
|
|
114
|
+
_navigator.go(shouldBlack);
|
|
115
|
+
if (stack[0]?.pathname === path)
|
|
116
|
+
return;
|
|
117
|
+
setTimeout(() => {
|
|
118
|
+
_navigator.replace(path, state);
|
|
119
|
+
}, 1);
|
|
120
|
+
};
|
|
121
|
+
_navigator.preload = async (path, isReplace, query) => {
|
|
122
|
+
const [originPath = "", search = ""] = path.split("?");
|
|
123
|
+
let matchPath = originPath.toLowerCase();
|
|
124
|
+
const currentRoute = matchRoutes(routes, matchPath)?.[0]?.route;
|
|
125
|
+
query = query || qs.parse(search);
|
|
126
|
+
if (!currentRoute?.Component.config) {
|
|
127
|
+
const pageModule = await currentRoute?.Component.load();
|
|
128
|
+
await pageModule.default?.emitOnLoad?.({ isPreload: true, query });
|
|
129
|
+
} else {
|
|
130
|
+
await currentRoute?.Component?.emitOnLoad?.({ isPreload: true, query });
|
|
131
|
+
}
|
|
132
|
+
if (isReplace) {
|
|
133
|
+
_navigator.replace(path);
|
|
134
|
+
} else {
|
|
135
|
+
_navigator.push(path);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
return _navigator;
|
|
139
|
+
}
|
|
140
|
+
var getClientNavigator = () => _navigator;
|
|
141
|
+
var routerReady = loadableReady;
|
|
142
|
+
export {
|
|
143
|
+
Link,
|
|
144
|
+
Router,
|
|
145
|
+
createNavigator,
|
|
146
|
+
getClientNavigator,
|
|
147
|
+
getRoutes,
|
|
148
|
+
routerReady
|
|
149
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xfe-repo/web-router",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": "./dist/index.mjs",
|
|
8
|
+
"require": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@loadable/component": "^5.15.3",
|
|
17
|
+
"history": "^5.3.0",
|
|
18
|
+
"qs": "^6.11.2",
|
|
19
|
+
"react-router-dom": "^6.11.1",
|
|
20
|
+
"@xfe-repo/web-components": "1.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/loadable__component": "^5.13.8",
|
|
24
|
+
"@types/node": "^20.10.4",
|
|
25
|
+
"@types/qs": "^6.9.10",
|
|
26
|
+
"@types/react": "^18.2.0",
|
|
27
|
+
"eslint": "^8.53.0",
|
|
28
|
+
"react": "^18.2.0",
|
|
29
|
+
"tsup": "^8.0.1",
|
|
30
|
+
"typescript": "^5.2.2",
|
|
31
|
+
"@xfe-repo/typescript-config": "0.0.0",
|
|
32
|
+
"@xfe-repo/web-register": "1.0.0",
|
|
33
|
+
"@xfe-repo/eslint-config": "0.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"lint": "eslint \"src/**/*.ts*\"",
|
|
39
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
40
|
+
}
|
|
41
|
+
}
|