@xfe-repo/web-router 1.0.3 → 1.1.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/dist/index.d.mts CHANGED
@@ -1,35 +1,39 @@
1
1
  import { History } from 'history';
2
2
  import { LoadableComponent, loadableReady } from '@loadable/component';
3
3
  import { ParsedQuery } from 'query-string';
4
- import React, { ReactElement, ComponentType } from 'react';
4
+ import React, { ReactElement, PropsWithChildren, ComponentType } from 'react';
5
5
  import { RegisterComponentStatic } from '@xfe-repo/web-register';
6
6
  export * from 'react-router-dom';
7
+ import * as react_jsx_runtime from 'react/jsx-runtime';
7
8
 
8
9
  type PageComponent = LoadableComponent<any> & RegisterComponentStatic;
9
10
  type RouteConfig = {
10
11
  path: string;
11
12
  Component: PageComponent;
12
13
  };
13
- declare const getRoutes: (pagesIndexPath: string[], getPageComponents: (path: string) => Promise<any>, fallback?: ReactElement) => RouteConfig[];
14
+ type GetRoutesConfig = {
15
+ pagesIndexPath: string[];
16
+ getPageComponents: (path: string) => Promise<any>;
17
+ fallback?: ReactElement;
18
+ };
19
+ declare const getRoutes: (config: GetRoutesConfig) => RouteConfig[];
14
20
 
15
- type RouterProps = {
21
+ type RouterProps = PropsWithChildren<{
16
22
  navigator: NavigatorType;
17
23
  routes: RouteConfig[];
18
24
  basename?: string;
19
- children: ReactElement;
20
25
  NotFoundPageComponent?: ComponentType;
21
- };
22
- declare const Router: React.MemoExoticComponent<(props: RouterProps) => JSX.Element>;
26
+ }>;
27
+ declare const Router: React.MemoExoticComponent<(props: RouterProps) => react_jsx_runtime.JSX.Element>;
23
28
 
24
- type LinkProps = {
29
+ type LinkProps = PropsWithChildren<{
25
30
  to: string;
26
- children?: ReactElement | string | ReactElement[];
27
31
  reLaunch?: boolean;
28
32
  replace?: boolean;
29
33
  preload?: boolean;
30
34
  className?: string;
31
- };
32
- declare const Link: (props: LinkProps) => JSX.Element;
35
+ }>;
36
+ declare const Link: React.MemoExoticComponent<(props: LinkProps) => react_jsx_runtime.JSX.Element>;
33
37
 
34
38
  type NavigatorStack = {
35
39
  pathname: string;
@@ -50,4 +54,4 @@ declare function createNavigator(options: CreateNavigatorOptions): NavigatorType
50
54
  declare const getClientNavigator: () => NavigatorType;
51
55
  declare const routerReady: typeof loadableReady;
52
56
 
53
- export { type CreateNavigatorOptions, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
57
+ export { type CreateNavigatorOptions, type GetRoutesConfig, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
package/dist/index.d.ts CHANGED
@@ -1,35 +1,39 @@
1
1
  import { History } from 'history';
2
2
  import { LoadableComponent, loadableReady } from '@loadable/component';
3
3
  import { ParsedQuery } from 'query-string';
4
- import React, { ReactElement, ComponentType } from 'react';
4
+ import React, { ReactElement, PropsWithChildren, ComponentType } from 'react';
5
5
  import { RegisterComponentStatic } from '@xfe-repo/web-register';
6
6
  export * from 'react-router-dom';
7
+ import * as react_jsx_runtime from 'react/jsx-runtime';
7
8
 
8
9
  type PageComponent = LoadableComponent<any> & RegisterComponentStatic;
9
10
  type RouteConfig = {
10
11
  path: string;
11
12
  Component: PageComponent;
12
13
  };
13
- declare const getRoutes: (pagesIndexPath: string[], getPageComponents: (path: string) => Promise<any>, fallback?: ReactElement) => RouteConfig[];
14
+ type GetRoutesConfig = {
15
+ pagesIndexPath: string[];
16
+ getPageComponents: (path: string) => Promise<any>;
17
+ fallback?: ReactElement;
18
+ };
19
+ declare const getRoutes: (config: GetRoutesConfig) => RouteConfig[];
14
20
 
15
- type RouterProps = {
21
+ type RouterProps = PropsWithChildren<{
16
22
  navigator: NavigatorType;
17
23
  routes: RouteConfig[];
18
24
  basename?: string;
19
- children: ReactElement;
20
25
  NotFoundPageComponent?: ComponentType;
21
- };
22
- declare const Router: React.MemoExoticComponent<(props: RouterProps) => JSX.Element>;
26
+ }>;
27
+ declare const Router: React.MemoExoticComponent<(props: RouterProps) => react_jsx_runtime.JSX.Element>;
23
28
 
24
- type LinkProps = {
29
+ type LinkProps = PropsWithChildren<{
25
30
  to: string;
26
- children?: ReactElement | string | ReactElement[];
27
31
  reLaunch?: boolean;
28
32
  replace?: boolean;
29
33
  preload?: boolean;
30
34
  className?: string;
31
- };
32
- declare const Link: (props: LinkProps) => JSX.Element;
35
+ }>;
36
+ declare const Link: React.MemoExoticComponent<(props: LinkProps) => react_jsx_runtime.JSX.Element>;
33
37
 
34
38
  type NavigatorStack = {
35
39
  pathname: string;
@@ -50,4 +54,4 @@ declare function createNavigator(options: CreateNavigatorOptions): NavigatorType
50
54
  declare const getClientNavigator: () => NavigatorType;
51
55
  declare const routerReady: typeof loadableReady;
52
56
 
53
- export { type CreateNavigatorOptions, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
57
+ export { type CreateNavigatorOptions, type GetRoutesConfig, Link, type NavigatorStack, type NavigatorType, type PageComponent, type RouteConfig, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
package/dist/index.js CHANGED
@@ -288,7 +288,8 @@ var import_query_string = __toESM(require("query-string"));
288
288
  __reExport(src_exports, require("react-router-dom"), module.exports);
289
289
  // src/routes.ts
290
290
  var import_component = __toESM(require("@loadable/component"));
291
- var getRoutes = function(pagesIndexPath, getPageComponents, fallback) {
291
+ var getRoutes = function(config) {
292
+ var pagesIndexPath = config.pagesIndexPath, getPageComponents = config.getPageComponents, fallback = config.fallback;
292
293
  var routes = [];
293
294
  pagesIndexPath.forEach(function(pageIndexPath) {
294
295
  if (pageIndexPath.startsWith("./")) return;
@@ -301,11 +302,11 @@ var getRoutes = function(pagesIndexPath, getPageComponents, fallback) {
301
302
  });
302
303
  var routerPath = "/".concat(pageRelativePath);
303
304
  if (routerPath === "/home" || routerPath === "/index") routerPath = "/";
304
- var config = {
305
+ var config2 = {
305
306
  path: routerPath,
306
307
  Component: pageComponent
307
308
  };
308
- routes.push(config);
309
+ routes.push(config2);
309
310
  });
310
311
  return routes;
311
312
  };
@@ -354,7 +355,7 @@ var Router = (0, import_react.memo)(function(props) {
354
355
  var import_react2 = require("react");
355
356
  var import_react_router_dom2 = require("react-router-dom");
356
357
  var import_jsx_runtime2 = require("react/jsx-runtime");
357
- var Link = function(props) {
358
+ var Link = (0, import_react2.memo)(function(props) {
358
359
  var to = props.to, children = props.children, reLaunch = props.reLaunch, preload = props.preload, replace = props.replace, className = props.className;
359
360
  var _navigator2 = (0, import_react2.useMemo)(function() {
360
361
  return getClientNavigator();
@@ -414,7 +415,7 @@ var Link = function(props) {
414
415
  });
415
416
  }
416
417
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_router_dom2.Link, _object_spread({}, props));
417
- };
418
+ });
418
419
  // src/index.ts
419
420
  var _navigator;
420
421
  function createNavigator(options) {
package/dist/index.mjs CHANGED
@@ -1,145 +1,453 @@
1
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
+ }
2
200
  import { createHashHistory, createBrowserHistory, createMemoryHistory } from "history";
3
201
  import { loadableReady } from "@loadable/component";
4
202
  import { matchRoutes } from "react-router-dom";
5
203
  import queryString from "query-string";
6
204
  export * from "react-router-dom";
7
-
8
205
  // src/routes.ts
9
206
  import loadable from "@loadable/component";
10
- var getRoutes = (pagesIndexPath, getPageComponents, fallback) => {
11
- const routes = [];
12
- pagesIndexPath.forEach((pageIndexPath) => {
13
- if (pageIndexPath.startsWith("./"))
14
- return;
15
- const regExpResult = pageIndexPath.match(/(?<=pages\/).*(?=\/index.(?:tsx|js))/);
16
- const pageRelativePath = regExpResult ? regExpResult[0].toLowerCase() : "";
17
- const pageComponent = loadable(() => getPageComponents(pageRelativePath), { fallback });
18
- let routerPath = `/${pageRelativePath}`;
19
- if (routerPath === "/home" || routerPath === "/index")
20
- routerPath = "/";
21
- const config = {
22
- path: routerPath,
23
- Component: pageComponent
24
- };
25
- routes.push(config);
26
- });
27
- return routes;
207
+ var getRoutes = function(config) {
208
+ var pagesIndexPath = config.pagesIndexPath, getPageComponents = config.getPageComponents, fallback = config.fallback;
209
+ var routes = [];
210
+ pagesIndexPath.forEach(function(pageIndexPath) {
211
+ if (pageIndexPath.startsWith("./")) return;
212
+ var regExpResult = pageIndexPath.match(RegExp("(?<=pages\\/).*(?=\\/index.(?:tsx|js))"));
213
+ var pageRelativePath = regExpResult ? regExpResult[0].toLowerCase() : "";
214
+ var pageComponent = loadable(function() {
215
+ return getPageComponents(pageRelativePath);
216
+ }, {
217
+ fallback: fallback
218
+ });
219
+ var routerPath = "/".concat(pageRelativePath);
220
+ if (routerPath === "/home" || routerPath === "/index") routerPath = "/";
221
+ var config2 = {
222
+ path: routerPath,
223
+ Component: pageComponent
224
+ };
225
+ routes.push(config2);
226
+ });
227
+ return routes;
28
228
  };
29
-
30
229
  // src/Router.tsx
31
230
  import { memo, useState, useEffect } from "react";
32
231
  import { Router as OriginalRouter, Routes, Route } from "react-router-dom";
33
232
  import { jsx, jsxs } from "react/jsx-runtime";
34
- var Router = memo((props) => {
35
- const { navigator, routes, basename = "/", children, NotFoundPageComponent } = props;
36
- const [navigatorState, setNavigatorState] = useState({
37
- action: navigator.action,
38
- location: navigator.location
39
- });
40
- useEffect(() => navigator.listen(setNavigatorState), [navigator]);
41
- return /* @__PURE__ */ jsx(OriginalRouter, { navigator, navigationType: navigatorState.action, location: navigatorState.location, basename, children: /* @__PURE__ */ jsx(Routes, { children: /* @__PURE__ */ jsxs(Route, { path: "/", element: children, children: [
42
- routes.map(({ path, Component }) => /* @__PURE__ */ jsx(Route, { path, Component }, path)),
43
- NotFoundPageComponent && /* @__PURE__ */ jsx(Route, { path: "*", Component: NotFoundPageComponent })
44
- ] }) }) });
233
+ var Router = memo(function(props) {
234
+ var navigator = props.navigator, routes = props.routes, _props_basename = props.basename, basename = _props_basename === void 0 ? "/" : _props_basename, children = props.children, NotFoundPageComponent = props.NotFoundPageComponent;
235
+ var _useState = _sliced_to_array(useState({
236
+ action: navigator.action,
237
+ location: navigator.location
238
+ }), 2), navigatorState = _useState[0], setNavigatorState = _useState[1];
239
+ useEffect(function() {
240
+ return navigator.listen(setNavigatorState);
241
+ }, [
242
+ navigator
243
+ ]);
244
+ return /* @__PURE__ */ jsx(OriginalRouter, {
245
+ navigator: navigator,
246
+ navigationType: navigatorState.action,
247
+ location: navigatorState.location,
248
+ basename: basename,
249
+ children: /* @__PURE__ */ jsx(Routes, {
250
+ children: /* @__PURE__ */ jsxs(Route, {
251
+ path: "/",
252
+ element: children,
253
+ children: [
254
+ routes.map(function(param) {
255
+ var path = param.path, Component = param.Component;
256
+ return /* @__PURE__ */ jsx(Route, {
257
+ path: path,
258
+ Component: Component
259
+ }, path);
260
+ }),
261
+ NotFoundPageComponent && /* @__PURE__ */ jsx(Route, {
262
+ path: "*",
263
+ Component: NotFoundPageComponent
264
+ })
265
+ ]
266
+ })
267
+ })
268
+ });
45
269
  });
46
-
47
270
  // src/Link.tsx
48
- import { useMemo } from "react";
271
+ import { memo as memo2, useMemo } from "react";
49
272
  import { Link as OriginalLink } from "react-router-dom";
50
273
  import { jsx as jsx2 } from "react/jsx-runtime";
51
- var Link = (props) => {
52
- const { to, children, reLaunch, preload, replace, className } = props;
53
- const _navigator2 = useMemo(() => getClientNavigator(), []);
54
- if (/http(s)?:\/{2}/.test(to)) {
55
- const handleLink = (e) => {
56
- e.preventDefault();
57
- window.location.href = to;
58
- };
59
- return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
60
- }
61
- if (reLaunch) {
62
- const handleLink = (e) => {
63
- e.preventDefault();
64
- _navigator2.reLaunch(to);
65
- };
66
- return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
67
- }
68
- if (preload) {
69
- const handleLink = async (e) => {
70
- e.preventDefault();
71
- await _navigator2.preload(to, replace);
72
- };
73
- return /* @__PURE__ */ jsx2("a", { href: to, onClick: handleLink, className, children });
74
- }
75
- return /* @__PURE__ */ jsx2(OriginalLink, { ...props });
76
- };
77
-
274
+ var Link = memo2(function(props) {
275
+ var to = props.to, children = props.children, reLaunch = props.reLaunch, preload = props.preload, replace = props.replace, className = props.className;
276
+ var _navigator2 = useMemo(function() {
277
+ return getClientNavigator();
278
+ }, []);
279
+ if (/http(s)?:\/{2}/.test(to)) {
280
+ var handleLink = function(e) {
281
+ e.preventDefault();
282
+ window.location.href = to;
283
+ };
284
+ return /* @__PURE__ */ jsx2("a", {
285
+ href: to,
286
+ onClick: handleLink,
287
+ className: className,
288
+ children: children
289
+ });
290
+ }
291
+ if (reLaunch) {
292
+ var handleLink1 = function(e) {
293
+ e.preventDefault();
294
+ _navigator2.reLaunch(to);
295
+ };
296
+ return /* @__PURE__ */ jsx2("a", {
297
+ href: to,
298
+ onClick: handleLink1,
299
+ className: className,
300
+ children: children
301
+ });
302
+ }
303
+ if (preload) {
304
+ var handleLink2 = function() {
305
+ var _ref = _async_to_generator(function(e) {
306
+ return _ts_generator(this, function(_state) {
307
+ switch(_state.label){
308
+ case 0:
309
+ e.preventDefault();
310
+ return [
311
+ 4,
312
+ _navigator2.preload(to, replace)
313
+ ];
314
+ case 1:
315
+ _state.sent();
316
+ return [
317
+ 2
318
+ ];
319
+ }
320
+ });
321
+ });
322
+ return function handleLink2(e) {
323
+ return _ref.apply(this, arguments);
324
+ };
325
+ }();
326
+ return /* @__PURE__ */ jsx2("a", {
327
+ href: to,
328
+ onClick: handleLink2,
329
+ className: className,
330
+ children: children
331
+ });
332
+ }
333
+ return /* @__PURE__ */ jsx2(OriginalLink, _object_spread({}, props));
334
+ });
78
335
  // src/index.ts
79
336
  var _navigator;
80
337
  function createNavigator(options) {
81
- const { mode, initPathname = "/", routes } = options;
82
- let originHistory;
83
- if (mode === "static") {
84
- originHistory = createMemoryHistory({
85
- initialEntries: [initPathname]
86
- });
87
- } else if (mode === "browser") {
88
- originHistory = createBrowserHistory();
89
- } else {
90
- originHistory = createHashHistory();
91
- }
92
- _navigator = originHistory;
93
- const initialStack = [{ pathname: _navigator.location.pathname }];
94
- let stack = _navigator.stack = initialStack;
95
- _navigator.listen(({ location, action }) => {
96
- const { pathname } = location;
97
- if (action === "PUSH") {
98
- stack.push({ pathname });
99
- } else if (action === "REPLACE") {
100
- stack.pop();
101
- stack.push({ pathname });
102
- } else if (action === "POP") {
103
- stack.pop();
104
- }
105
- console.log("router", action, pathname, stack);
106
- });
107
- _navigator.reLaunch = (path, state) => {
108
- let shouldBlack = -stack.length + 1;
109
- if (shouldBlack < 0)
110
- _navigator.go(shouldBlack);
111
- if (stack[0]?.pathname === path)
112
- return;
113
- setTimeout(() => {
114
- _navigator.replace(path, state);
115
- }, 1);
116
- };
117
- _navigator.preload = async (path, isReplace, query) => {
118
- const [originPath = "", search = ""] = path.split("?");
119
- let matchPath = originPath.toLowerCase();
120
- const currentRoute = matchRoutes(routes, matchPath)?.[0]?.route;
121
- query = query || queryString.parse(search, { parseBooleans: true, parseNumbers: true });
122
- if (!currentRoute?.Component.config) {
123
- const pageModule = await currentRoute?.Component.load();
124
- await pageModule.default?.emitOnLoad?.({ isPreload: true, query });
125
- } else {
126
- await currentRoute?.Component?.emitOnLoad?.({ isPreload: true, query });
127
- }
128
- if (isReplace) {
129
- _navigator.replace(path);
338
+ var mode = options.mode, _options_initPathname = options.initPathname, initPathname = _options_initPathname === void 0 ? "/" : _options_initPathname, routes = options.routes;
339
+ var originHistory;
340
+ if (mode === "static") {
341
+ originHistory = createMemoryHistory({
342
+ initialEntries: [
343
+ initPathname
344
+ ]
345
+ });
346
+ } else if (mode === "browser") {
347
+ originHistory = createBrowserHistory();
130
348
  } else {
131
- _navigator.push(path);
349
+ originHistory = createHashHistory();
132
350
  }
133
- };
134
- return _navigator;
351
+ _navigator = originHistory;
352
+ var initialStack = [
353
+ {
354
+ pathname: _navigator.location.pathname
355
+ }
356
+ ];
357
+ var stack = _navigator.stack = initialStack;
358
+ _navigator.listen(function(param) {
359
+ var location = param.location, action = param.action;
360
+ var pathname = location.pathname;
361
+ if (action === "PUSH") {
362
+ stack.push({
363
+ pathname: pathname
364
+ });
365
+ } else if (action === "REPLACE") {
366
+ stack.pop();
367
+ stack.push({
368
+ pathname: pathname
369
+ });
370
+ } else if (action === "POP") {
371
+ stack.pop();
372
+ }
373
+ console.log("router", action, pathname, stack);
374
+ });
375
+ _navigator.reLaunch = function(path, state) {
376
+ var _stack_;
377
+ var shouldBlack = -stack.length + 1;
378
+ if (shouldBlack < 0) _navigator.go(shouldBlack);
379
+ if (((_stack_ = stack[0]) === null || _stack_ === void 0 ? void 0 : _stack_.pathname) === path) return;
380
+ setTimeout(function() {
381
+ _navigator.replace(path, state);
382
+ }, 1);
383
+ };
384
+ _navigator.preload = function() {
385
+ var _ref = _async_to_generator(function(path, isReplace, query) {
386
+ var _matchRoutes_, _matchRoutes, _path_split, tmp, originPath, tmp1, search, matchPath, currentRoute, _pageModule_default_emitOnLoad, _pageModule_default, pageModule, _currentRoute_Component_emitOnLoad, _currentRoute_Component;
387
+ return _ts_generator(this, function(_state) {
388
+ switch(_state.label){
389
+ case 0:
390
+ _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;
391
+ matchPath = originPath.toLowerCase();
392
+ currentRoute = (_matchRoutes = matchRoutes(routes, matchPath)) === null || _matchRoutes === void 0 ? void 0 : (_matchRoutes_ = _matchRoutes[0]) === null || _matchRoutes_ === void 0 ? void 0 : _matchRoutes_.route;
393
+ query = query || queryString.parse(search, {
394
+ parseBooleans: true,
395
+ parseNumbers: true
396
+ });
397
+ if (!!(currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.config)) return [
398
+ 3,
399
+ 3
400
+ ];
401
+ return [
402
+ 4,
403
+ currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.Component.load()
404
+ ];
405
+ case 1:
406
+ pageModule = _state.sent();
407
+ return [
408
+ 4,
409
+ (_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, {
410
+ isPreload: true,
411
+ query: query
412
+ })
413
+ ];
414
+ case 2:
415
+ _state.sent();
416
+ return [
417
+ 3,
418
+ 5
419
+ ];
420
+ case 3:
421
+ return [
422
+ 4,
423
+ 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, {
424
+ isPreload: true,
425
+ query: query
426
+ })
427
+ ];
428
+ case 4:
429
+ _state.sent();
430
+ _state.label = 5;
431
+ case 5:
432
+ if (isReplace) {
433
+ _navigator.replace(path);
434
+ } else {
435
+ _navigator.push(path);
436
+ }
437
+ return [
438
+ 2
439
+ ];
440
+ }
441
+ });
442
+ });
443
+ return function(path, isReplace, query) {
444
+ return _ref.apply(this, arguments);
445
+ };
446
+ }();
447
+ return _navigator;
135
448
  }
136
- var getClientNavigator = () => _navigator;
137
- var routerReady = loadableReady;
138
- export {
139
- Link,
140
- Router,
141
- createNavigator,
142
- getClientNavigator,
143
- getRoutes,
144
- routerReady
449
+ var getClientNavigator = function() {
450
+ return _navigator;
145
451
  };
452
+ var routerReady = loadableReady;
453
+ export { Link, Router, createNavigator, getClientNavigator, getRoutes, routerReady };
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@xfe-repo/web-router",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "sideEffects": false,
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
5
7
  "exports": {
6
8
  ".": {
7
9
  "import": {
@@ -11,10 +13,6 @@
11
13
  "require": {
12
14
  "types": "./dist/index.d.ts",
13
15
  "default": "./dist/index.js"
14
- },
15
- "default": {
16
- "types": "./dist/index.d.ts",
17
- "default": "./dist/index.js"
18
16
  }
19
17
  }
20
18
  },
@@ -22,18 +20,18 @@
22
20
  "dist"
23
21
  ],
24
22
  "dependencies": {
25
- "@loadable/component": "^5.15.3",
23
+ "@loadable/component": "^5.16.4",
26
24
  "history": "^5.3.0",
27
25
  "query-string": "^7.1.3",
28
- "react-router-dom": "^6.11.1"
26
+ "react-router-dom": "^6.26.2"
29
27
  },
30
28
  "devDependencies": {
31
- "@types/loadable__component": "^5.13.8",
32
- "@types/node": "^20.10.4",
33
- "@types/react": "^18.2.0",
34
- "@xfe-repo/web-register": "1.0.3",
29
+ "@types/loadable__component": "^5.13.9",
30
+ "@types/node": "^20.16.5",
31
+ "@types/react": "^18",
35
32
  "@xfe-repo/eslint-config": "0.0.2",
36
- "@xfe-repo/typescript-config": "0.0.3"
33
+ "@xfe-repo/typescript-config": "0.0.3",
34
+ "@xfe-repo/web-register": "1.1.0"
37
35
  },
38
36
  "scripts": {
39
37
  "build": "tsup",