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