swico 0.9.0-beta6 → 0.9.0-beta8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swico",
3
- "version": "0.9.0-beta6",
3
+ "version": "0.9.0-beta8",
4
4
  "description": "Simple, efficient, and practical front-end framework",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -11,7 +11,7 @@ export declare const Link: FC<{
11
11
  }>;
12
12
  export { Outlet } from 'react-router-dom';
13
13
  export { useLocation, useNav } from './template/react/react-hooks';
14
- export { history, Router } from './mock-history';
14
+ export { history } from './mock-history';
15
15
  export interface DefineSwicoConfigType {
16
16
  (env: 'base', config: CustomConfigType['base']): any;
17
17
  (env: 'dev', config: CustomConfigType['dev']): any;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.defineGlobal = exports.defineConfig = exports.Router = exports.history = exports.useNav = exports.useLocation = exports.Outlet = exports.Link = void 0;
6
+ exports.defineGlobal = exports.defineConfig = exports.history = exports.useNav = exports.useLocation = exports.Outlet = exports.Link = void 0;
7
7
  var react_router_dom_1 = require("react-router-dom");
8
8
  var react_1 = __importDefault(require("react"));
9
9
  var Link = function (_a) {
@@ -18,7 +18,6 @@ Object.defineProperty(exports, "useLocation", { enumerable: true, get: function
18
18
  Object.defineProperty(exports, "useNav", { enumerable: true, get: function () { return react_hooks_1.useNav; } });
19
19
  var mock_history_1 = require("./mock-history");
20
20
  Object.defineProperty(exports, "history", { enumerable: true, get: function () { return mock_history_1.history; } });
21
- Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return mock_history_1.Router; } });
22
21
  var defineConfig = function (env, config) { return config; };
23
22
  exports.defineConfig = defineConfig;
24
23
  var defineGlobal = function (config) {
@@ -1,6 +1,2 @@
1
1
  import { SwicoHistoryType } from './typings/global-type';
2
- import React, { ReactNode } from 'react';
3
2
  export declare const history: SwicoHistoryType;
4
- export declare const Router: React.FC<{
5
- children: ReactNode;
6
- }>;
@@ -1,6 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Router = exports.history = void 0;
3
+ exports.history = void 0;
4
4
  exports.history = {};
5
- var Router = function () { return null; };
6
- exports.Router = Router;
@@ -1,8 +1,5 @@
1
+ import { ConfigRouterType } from '../../../utils/config';
1
2
  import { SwicoHistoryType } from '../../../typings/global-type';
2
- import React, { ReactNode } from 'react';
3
- export declare const Router: React.FC<{
4
- children: ReactNode;
5
- }>;
6
3
  export declare const compareURLPatterns: (urlPatternWithValues: string, urlPatternWithParams: string) => boolean;
7
4
  export declare const interpolatePath: (pathTemplate: string, params: Record<string, any>) => string;
8
5
  export declare const formatAncPath: (ancPath: string) => string;
@@ -11,3 +8,4 @@ export declare const pathNameList: {
11
8
  name: string;
12
9
  }[];
13
10
  export declare let history: SwicoHistoryType;
11
+ export declare const getRouter: (routerType: ConfigRouterType['type']) => import("@remix-run/router").Router;
@@ -3,18 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.history = exports.pathNameList = exports.formatAncPath = exports.interpolatePath = exports.compareURLPatterns = exports.Router = void 0;
6
+ exports.getRouter = exports.history = exports.pathNameList = exports.formatAncPath = exports.interpolatePath = exports.compareURLPatterns = void 0;
7
+ var index_1 = require("./index");
7
8
  var qs_1 = __importDefault(require("qs"));
8
9
  var history_1 = require("history");
9
10
  var routes_1 = __importDefault(require("./routes"));
10
11
  var config_1 = require("./config");
11
12
  var react_router_dom_1 = require("react-router-dom");
12
- var react_1 = __importDefault(require("react"));
13
- var Router = function (_a) {
14
- var children = _a.children;
15
- return react_1.default.createElement(react_router_dom_1.unstable_HistoryRouter, { basename: config_1.routerBase, history: getOriHistory(config_1.routerType) }, children);
16
- };
17
- exports.Router = Router;
18
13
  var lastIndexBase = config_1.routerBase[config_1.routerBase.length - 1];
19
14
  var formatRouterBase = lastIndexBase === '/' ? config_1.routerBase.slice(0, config_1.routerBase.length - 1) : config_1.routerBase;
20
15
  var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
@@ -108,22 +103,21 @@ var getLocation = function (originalHistory) {
108
103
  path: path
109
104
  };
110
105
  };
111
- var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
106
+ var getFormatHistoryOption = function (to, pathList, type) {
112
107
  var _a;
113
- var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query, state = to.state;
108
+ var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query;
114
109
  var obj = {
115
110
  hash: hash,
116
- state: state,
117
111
  search: query ? qs_1.default.stringify(query) : undefined
118
112
  };
119
113
  if (path) {
120
- obj.pathname = "".concat(formatRouterBase).concat(path);
114
+ obj.pathname = path;
121
115
  }
122
116
  if (name) {
123
117
  var fullPath = (_a = pathList.find(function (item) { return item.name === name; })) === null || _a === void 0 ? void 0 : _a.path;
124
118
  var targetPath = fullPath ? (0, exports.interpolatePath)(fullPath, params) : null;
125
119
  if (targetPath) {
126
- obj.pathname = "".concat(formatRouterBase).concat(targetPath);
120
+ obj.pathname = targetPath;
127
121
  }
128
122
  else {
129
123
  throw "An error occurred while executing 'history.".concat(type, "' operation: The path for the name \"").concat(name, "\" could not be found");
@@ -131,21 +125,29 @@ var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
131
125
  }
132
126
  return obj;
133
127
  };
134
- var getOriHistory = function (routerType) {
128
+ var getRouter = function (routerType) {
135
129
  var _a;
136
- var originalHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
130
+ var routeList = (0, index_1.getChildrenRouteList)(routes_1.default, '');
131
+ var router = (routerType === 'hash' ? react_router_dom_1.createHashRouter : react_router_dom_1.createBrowserRouter)(routeList);
132
+ var oriHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
137
133
  exports.history = {
138
- go: originalHistory.go,
139
- forward: originalHistory.forward,
140
- back: originalHistory.back,
134
+ go: function (delta) {
135
+ router.navigate(delta);
136
+ },
137
+ forward: function () {
138
+ router.navigate(1);
139
+ },
140
+ back: function () {
141
+ router.navigate(-1);
142
+ },
141
143
  push: function (to) {
142
144
  switch (typeof to) {
143
145
  case 'string':
144
- originalHistory.push("".concat(formatRouterBase).concat(to));
146
+ router.navigate(to);
145
147
  break;
146
148
  case 'object':
147
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'push');
148
- originalHistory.push(formatOption);
149
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'push');
150
+ router.navigate(formatOption, { state: to.state });
149
151
  break;
150
152
  default:
151
153
  throw "An error occurred while executing history.push operation: unexpected type of 'to':".concat(typeof to);
@@ -154,19 +156,20 @@ var getOriHistory = function (routerType) {
154
156
  replace: function (to) {
155
157
  switch (typeof to) {
156
158
  case 'string':
157
- originalHistory.replace("".concat(formatRouterBase).concat(to));
159
+ router.navigate(to, { replace: true });
158
160
  break;
159
161
  case 'object':
160
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'replace');
161
- originalHistory.replace(formatOption);
162
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'replace');
163
+ router.navigate(formatOption, { state: to.state, replace: true });
162
164
  break;
163
165
  default:
164
166
  throw "An error occurred while executing history.replace operation: unexpected type of 'to':".concat(typeof to);
165
167
  }
166
168
  },
167
169
  get location() {
168
- return getLocation(originalHistory);
170
+ return getLocation(oriHistory);
169
171
  }
170
172
  };
171
- return originalHistory;
173
+ return router;
172
174
  };
175
+ exports.getRouter = getRouter;
@@ -9,3 +9,4 @@ export type RoutesItemType = {
9
9
  redirect?: string;
10
10
  name?: string;
11
11
  };
12
+ export declare const getChildrenRouteList: (childrenRoutes: RoutesItemType[], ancPathKey: string) => any;
@@ -26,31 +26,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getChildrenRouteList = void 0;
29
30
  var client_1 = require("react-dom/client");
30
31
  var react_1 = __importStar(require("react"));
31
32
  var react_router_dom_1 = require("react-router-dom");
32
- var routes_1 = __importDefault(require("./routes"));
33
33
  var loading_1 = __importDefault(require("../loading"));
34
34
  var history_1 = require("./history");
35
35
  var config_1 = require("./config");
36
36
  require("../../global");
37
- var layout_1 = __importDefault(require("../../layout"));
38
- var renderChildrenRouteList = function (childrenRoutes, ancPathKey) {
37
+ var getChildrenRouteList = function (childrenRoutes, ancPathKey) {
39
38
  return childrenRoutes === null || childrenRoutes === void 0 ? void 0 : childrenRoutes.map(function (item) {
40
39
  var component = item.component, path = item.path, children = item.children, redirect = item.redirect;
41
40
  var newAncPathKey = "".concat(ancPathKey, "-").concat(path);
42
- return (react_1.default.createElement(react_router_dom_1.Route, { element: redirect ? (react_1.default.createElement(react_router_dom_1.Navigate, { to: redirect, replace: true })) : component ? (react_1.default.createElement(react_1.Suspense, { fallback: (0, react_1.createElement)(loading_1.default) }, (0, react_1.createElement)((0, react_1.lazy)(component)))) : (react_1.default.createElement(react_router_dom_1.Outlet, null)), key: newAncPathKey, path: path }, renderChildrenRouteList(children, newAncPathKey)));
41
+ return {
42
+ element: redirect ? (react_1.default.createElement(react_router_dom_1.Navigate, { to: redirect, replace: true })) : component ? (react_1.default.createElement(react_1.Suspense, { fallback: (0, react_1.createElement)(loading_1.default) }, (0, react_1.createElement)((0, react_1.lazy)(component)))) : (react_1.default.createElement(react_router_dom_1.Outlet, null)),
43
+ key: newAncPathKey,
44
+ path: path,
45
+ children: (0, exports.getChildrenRouteList)(children, newAncPathKey)
46
+ };
43
47
  });
44
48
  };
49
+ exports.getChildrenRouteList = getChildrenRouteList;
45
50
  var App = function () {
51
+ var router = (0, history_1.getRouter)(config_1.routerType);
46
52
  if (config_1.routerBase && config_1.routerBase !== '/') {
47
53
  if (config_1.routerType === 'browser' && window.location.pathname === '/') {
48
54
  window.location.replace("".concat(config_1.routerBase));
49
55
  }
50
56
  }
51
- return (react_1.default.createElement(history_1.Router, null,
52
- react_1.default.createElement(react_router_dom_1.Routes, null,
53
- react_1.default.createElement(react_router_dom_1.Route, { element: react_1.default.createElement(layout_1.default, null), path: '' }, renderChildrenRouteList(routes_1.default, '')))));
57
+ return react_1.default.createElement(react_router_dom_1.RouterProvider, { router: router, basename: config_1.routerBase });
54
58
  };
55
59
  var root = (0, client_1.createRoot)(document.getElementById('root'));
56
60
  root.render(react_1.default.createElement(App, null));