swico 0.9.0-beta5 → 0.9.0-beta7

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-beta5",
3
+ "version": "0.9.0-beta7",
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,17 +3,15 @@ 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");
12
+ var layout_1 = __importDefault(require("../../layout"));
11
13
  var react_router_dom_1 = require("react-router-dom");
12
14
  var react_1 = __importDefault(require("react"));
13
- var Router = function (children) {
14
- return react_1.default.createElement(react_router_dom_1.unstable_HistoryRouter, { basename: config_1.routerBase, history: getOriHistory(config_1.routerType) }, children);
15
- };
16
- exports.Router = Router;
17
15
  var lastIndexBase = config_1.routerBase[config_1.routerBase.length - 1];
18
16
  var formatRouterBase = lastIndexBase === '/' ? config_1.routerBase.slice(0, config_1.routerBase.length - 1) : config_1.routerBase;
19
17
  var compareURLPatterns = function (urlPatternWithValues, urlPatternWithParams) {
@@ -107,22 +105,21 @@ var getLocation = function (originalHistory) {
107
105
  path: path
108
106
  };
109
107
  };
110
- var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
108
+ var getFormatHistoryOption = function (to, pathList, type) {
111
109
  var _a;
112
- var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query, state = to.state;
110
+ var params = to.params, path = to.path, hash = to.hash, name = to.name, query = to.query;
113
111
  var obj = {
114
112
  hash: hash,
115
- state: state,
116
113
  search: query ? qs_1.default.stringify(query) : undefined
117
114
  };
118
115
  if (path) {
119
- obj.pathname = "".concat(formatRouterBase).concat(path);
116
+ obj.pathname = path;
120
117
  }
121
118
  if (name) {
122
119
  var fullPath = (_a = pathList.find(function (item) { return item.name === name; })) === null || _a === void 0 ? void 0 : _a.path;
123
120
  var targetPath = fullPath ? (0, exports.interpolatePath)(fullPath, params) : null;
124
121
  if (targetPath) {
125
- obj.pathname = "".concat(formatRouterBase).concat(targetPath);
122
+ obj.pathname = targetPath;
126
123
  }
127
124
  else {
128
125
  throw "An error occurred while executing 'history.".concat(type, "' operation: The path for the name \"").concat(name, "\" could not be found");
@@ -130,21 +127,35 @@ var getFormatHistoryOption = function (to, formatRouterBase, pathList, type) {
130
127
  }
131
128
  return obj;
132
129
  };
133
- var getOriHistory = function (routerType) {
130
+ var getRouter = function (routerType) {
134
131
  var _a;
135
- var originalHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
132
+ var routeList = [
133
+ {
134
+ element: react_1.default.createElement(layout_1.default, null),
135
+ path: '',
136
+ children: (0, index_1.getChildrenRouteList)(routes_1.default, '')
137
+ }
138
+ ];
139
+ var router = (routerType === 'hash' ? react_router_dom_1.createHashRouter : react_router_dom_1.createBrowserRouter)(routeList);
140
+ var oriHistory = (_a = (routerType === 'hash' ? history_1.createHashHistory : history_1.createBrowserHistory)) === null || _a === void 0 ? void 0 : _a();
136
141
  exports.history = {
137
- go: originalHistory.go,
138
- forward: originalHistory.forward,
139
- back: originalHistory.back,
142
+ go: function (delta) {
143
+ router.navigate(delta);
144
+ },
145
+ forward: function () {
146
+ router.navigate(1);
147
+ },
148
+ back: function () {
149
+ router.navigate(-1);
150
+ },
140
151
  push: function (to) {
141
152
  switch (typeof to) {
142
153
  case 'string':
143
- originalHistory.push("".concat(formatRouterBase).concat(to));
154
+ router.navigate(to);
144
155
  break;
145
156
  case 'object':
146
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'push');
147
- originalHistory.push(formatOption);
157
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'push');
158
+ router.navigate(formatOption, { state: to.state });
148
159
  break;
149
160
  default:
150
161
  throw "An error occurred while executing history.push operation: unexpected type of 'to':".concat(typeof to);
@@ -153,19 +164,20 @@ var getOriHistory = function (routerType) {
153
164
  replace: function (to) {
154
165
  switch (typeof to) {
155
166
  case 'string':
156
- originalHistory.replace("".concat(formatRouterBase).concat(to));
167
+ router.navigate(to, { replace: true });
157
168
  break;
158
169
  case 'object':
159
- var formatOption = getFormatHistoryOption(to, formatRouterBase, exports.pathNameList, 'replace');
160
- originalHistory.replace(formatOption);
170
+ var formatOption = getFormatHistoryOption(to, exports.pathNameList, 'replace');
171
+ router.navigate(formatOption, { state: to.state, replace: true });
161
172
  break;
162
173
  default:
163
174
  throw "An error occurred while executing history.replace operation: unexpected type of 'to':".concat(typeof to);
164
175
  }
165
176
  },
166
177
  get location() {
167
- return getLocation(originalHistory);
178
+ return getLocation(oriHistory);
168
179
  }
169
180
  };
170
- return originalHistory;
181
+ return router;
171
182
  };
183
+ 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));