@modern-js/plugin-router-v5 2.15.0 → 2.16.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.
@@ -1,56 +1,56 @@
1
- function _defineProperty(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
13
  }
14
- function _objectSpread(target) {
15
- for(var i = 1; i < arguments.length; i++){
16
- var source = arguments[i] != null ? arguments[i] : {};
17
- var ownKeys = Object.keys(source);
18
- if (typeof Object.getOwnPropertySymbols === "function") {
19
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
- }));
22
- }
23
- ownKeys.forEach(function(key) {
24
- _defineProperty(target, key, source[key]);
25
- });
14
+ function _object_spread(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys2 = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys2 = ownKeys2.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
26
22
  }
27
- return target;
23
+ ownKeys2.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
28
  }
29
29
  function ownKeys(object, enumerableOnly) {
30
- var keys = Object.keys(object);
31
- if (Object.getOwnPropertySymbols) {
32
- var symbols = Object.getOwnPropertySymbols(object);
33
- if (enumerableOnly) {
34
- symbols = symbols.filter(function(sym) {
35
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
- });
37
- }
38
- keys.push.apply(keys, symbols);
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
39
37
  }
40
- return keys;
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
41
  }
42
- function _objectSpreadProps(target, source) {
43
- source = source != null ? source : {};
44
- if (Object.getOwnPropertyDescriptors) {
45
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
- } else {
47
- ownKeys(Object(source)).forEach(function(key) {
48
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
- });
50
- }
51
- return target;
42
+ function _object_spread_props(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
52
  }
53
- import { jsx } from "react/jsx-runtime";
53
+ import { jsx as _jsx } from "react/jsx-runtime";
54
54
  import { useContext } from "react";
55
55
  import { createBrowserHistory, createHashHistory } from "history";
56
56
  import { Router, StaticRouter, useRouteMatch, useLocation } from "react-router-dom";
@@ -58,85 +58,84 @@ import hoistNonReactStatics from "hoist-non-react-statics";
58
58
  import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
59
59
  import { parsedJSONFromElement } from "@modern-js/utils/runtime";
60
60
  import { renderRoutes, getLocation, urlJoin } from "./utils";
61
- var routerPlugin = function(param) {
62
- var _param_serverBase = param.serverBase, serverBase = _param_serverBase === void 0 ? [] : _param_serverBase, customHistory = param.history, _param_supportHtml5History = param.supportHtml5History, supportHtml5History = _param_supportHtml5History === void 0 ? true : _param_supportHtml5History, routesConfig = param.routesConfig, createRoutes = param.createRoutes, _param_historyOptions = param.historyOptions, historyOptions = _param_historyOptions === void 0 ? {} : _param_historyOptions;
63
- var isBrow = isBrowser();
64
- var select = function(pathname) {
65
- return serverBase.find(function(baseUrl) {
66
- return pathname.search(baseUrl) === 0;
67
- }) || "/";
68
- };
69
- if (isBrow) {
70
- window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
71
- }
72
- return {
73
- name: "@modern-js/plugin-router",
74
- setup: function() {
75
- return {
76
- init: function init(param, next) {
77
- var context = param.context;
78
- context.router = {
79
- useRouteMatch: useRouteMatch,
80
- useLocation: useLocation
81
- };
82
- return next({
83
- context: context
84
- });
85
- },
86
- hoc: function(param, next) {
87
- var App = param.App;
88
- var getRouteApp = function() {
89
- if (isBrow) {
90
- var _window__SERVER_DATA;
91
- var baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
92
- historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
93
- var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
94
- return function(props) {
95
- return /* @__PURE__ */ jsx(Router, {
96
- history: history,
97
- children: createRoutes ? /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
98
- Component: createRoutes()
99
- })) : /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
100
- children: renderRoutes(routesConfig, props)
101
- }))
102
- });
103
- };
104
- }
105
- return function(props) {
106
- var runtimeContext = useContext(RuntimeReactContext);
107
- var ssrContext = runtimeContext.ssrContext;
108
- var location2 = getLocation(ssrContext);
109
- var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
110
- var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
111
- var baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
112
- var basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
113
- return /* @__PURE__ */ jsx(StaticRouter, {
114
- basename: basename === "/" ? "" : basename,
115
- location: location2,
116
- context: routerContext,
117
- children: createRoutes ? /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
118
- Component: createRoutes()
119
- })) : /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
120
- children: renderRoutes(routesConfig, props)
121
- }))
122
- });
123
- };
124
- };
125
- var RouteApp = getRouteApp();
126
- if (App) {
127
- RouteApp = hoistNonReactStatics(RouteApp, App);
128
- }
129
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
130
- return next({
131
- App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
132
- });
133
- }
134
- return next({
135
- App: RouteApp
136
- });
137
- }
61
+ export var routerPlugin = function(param) {
62
+ var _param_serverBase = param.serverBase, serverBase = _param_serverBase === void 0 ? [] : _param_serverBase, customHistory = param.history, _param_supportHtml5History = param.supportHtml5History, supportHtml5History = _param_supportHtml5History === void 0 ? true : _param_supportHtml5History, routesConfig = param.routesConfig, createRoutes = param.createRoutes, _param_historyOptions = param.historyOptions, historyOptions = _param_historyOptions === void 0 ? {} : _param_historyOptions;
63
+ var isBrow = isBrowser();
64
+ var select = function(pathname) {
65
+ return serverBase.find(function(baseUrl) {
66
+ return pathname.search(baseUrl) === 0;
67
+ }) || "/";
68
+ };
69
+ if (isBrow) {
70
+ window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
71
+ }
72
+ return {
73
+ name: "@modern-js/plugin-router",
74
+ setup: function() {
75
+ return {
76
+ init: function init(param2, next) {
77
+ var context = param2.context;
78
+ context.router = {
79
+ useRouteMatch,
80
+ useLocation
81
+ };
82
+ return next({
83
+ context
84
+ });
85
+ },
86
+ hoc: function(param2, next) {
87
+ var App = param2.App;
88
+ var getRouteApp = function() {
89
+ if (isBrow) {
90
+ var _window__SERVER_DATA;
91
+ var baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
92
+ historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
93
+ var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
94
+ return function(props) {
95
+ return /* @__PURE__ */ _jsx(Router, {
96
+ history,
97
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
98
+ Component: createRoutes()
99
+ })) : /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
100
+ children: renderRoutes(routesConfig, props)
101
+ }))
102
+ });
103
+ };
104
+ }
105
+ return function(props) {
106
+ var runtimeContext = useContext(RuntimeReactContext);
107
+ var ssrContext = runtimeContext.ssrContext;
108
+ var _$location = getLocation(ssrContext);
109
+ var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
110
+ var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
111
+ var baseUrl2 = request === null || request === void 0 ? void 0 : request.baseUrl;
112
+ var basename = baseUrl2 === "/" ? urlJoin(baseUrl2, historyOptions.basename) : baseUrl2;
113
+ return /* @__PURE__ */ _jsx(StaticRouter, {
114
+ basename: basename === "/" ? "" : basename,
115
+ location: _$location,
116
+ context: routerContext,
117
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
118
+ Component: createRoutes()
119
+ })) : /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
120
+ children: renderRoutes(routesConfig, props)
121
+ }))
122
+ });
138
123
  };
124
+ };
125
+ var RouteApp = getRouteApp();
126
+ if (App) {
127
+ RouteApp = hoistNonReactStatics(RouteApp, App);
128
+ }
129
+ if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
130
+ return next({
131
+ App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
132
+ });
133
+ }
134
+ return next({
135
+ App: RouteApp
136
+ });
139
137
  }
140
- };
138
+ };
139
+ }
140
+ };
141
141
  };
142
- export { routerPlugin };
@@ -1,141 +1,145 @@
1
- function _defineProperty(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
13
  }
14
- function _objectSpread(target) {
15
- for(var i = 1; i < arguments.length; i++){
16
- var source = arguments[i] != null ? arguments[i] : {};
17
- var ownKeys = Object.keys(source);
18
- if (typeof Object.getOwnPropertySymbols === "function") {
19
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
- }));
22
- }
23
- ownKeys.forEach(function(key) {
24
- _defineProperty(target, key, source[key]);
25
- });
14
+ function _object_spread(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
26
22
  }
27
- return target;
23
+ ownKeys.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
28
  }
29
- function _objectWithoutProperties(source, excluded) {
30
- if (source == null) return {};
31
- var target = _objectWithoutPropertiesLoose(source, excluded);
32
- var key, i;
33
- if (Object.getOwnPropertySymbols) {
34
- var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
35
- for(i = 0; i < sourceSymbolKeys.length; i++){
36
- key = sourceSymbolKeys[i];
37
- if (excluded.indexOf(key) >= 0) continue;
38
- if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
39
- target[key] = source[key];
40
- }
29
+ function _object_without_properties(source, excluded) {
30
+ if (source == null)
31
+ return {};
32
+ var target = _object_without_properties_loose(source, excluded);
33
+ var key, i;
34
+ if (Object.getOwnPropertySymbols) {
35
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
36
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
37
+ key = sourceSymbolKeys[i];
38
+ if (excluded.indexOf(key) >= 0)
39
+ continue;
40
+ if (!Object.prototype.propertyIsEnumerable.call(source, key))
41
+ continue;
42
+ target[key] = source[key];
41
43
  }
42
- return target;
44
+ }
45
+ return target;
43
46
  }
44
- function _objectWithoutPropertiesLoose(source, excluded) {
45
- if (source == null) return {};
46
- var target = {};
47
- var sourceKeys = Object.keys(source);
48
- var key, i;
49
- for(i = 0; i < sourceKeys.length; i++){
50
- key = sourceKeys[i];
51
- if (excluded.indexOf(key) >= 0) continue;
52
- target[key] = source[key];
53
- }
54
- return target;
47
+ function _object_without_properties_loose(source, excluded) {
48
+ if (source == null)
49
+ return {};
50
+ var target = {};
51
+ var sourceKeys = Object.keys(source);
52
+ var key, i;
53
+ for (i = 0; i < sourceKeys.length; i++) {
54
+ key = sourceKeys[i];
55
+ if (excluded.indexOf(key) >= 0)
56
+ continue;
57
+ target[key] = source[key];
58
+ }
59
+ return target;
55
60
  }
56
- import { jsx } from "react/jsx-runtime";
61
+ import { jsx as _jsx } from "react/jsx-runtime";
57
62
  import { Route, matchPath } from "react-router-dom";
58
63
  import { DefaultNotFound } from "./DefaultNotFound";
59
- function renderRoutes(routesConfig) {
60
- var extraProps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
61
- if (!routesConfig) {
62
- return null;
64
+ export function renderRoutes(routesConfig) {
65
+ var extraProps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
66
+ if (!routesConfig) {
67
+ return null;
68
+ }
69
+ var Layout = function(_param) {
70
+ var Component = _param.Component, props = _object_without_properties(_param, [
71
+ "Component"
72
+ ]);
73
+ var GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
74
+ if (!GlobalLayout) {
75
+ return /* @__PURE__ */ _jsx(Component, _object_spread({}, props));
63
76
  }
64
- var Layout = function(_param) {
65
- var Component = _param.Component, props = _objectWithoutProperties(_param, [
66
- "Component"
67
- ]);
68
- var GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
69
- if (!GlobalLayout) {
70
- return /* @__PURE__ */ jsx(Component, _objectSpread({}, props));
71
- }
72
- return /* @__PURE__ */ jsx(GlobalLayout, _objectSpread({
73
- Component: Component
74
- }, props));
75
- };
76
- var findMatchedRoute = function(pathname) {
77
- var _routesConfig_routes;
78
- return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find(function(route) {
79
- var info = matchPath(pathname, {
80
- path: route.path,
81
- exact: route.exact,
82
- sensitive: route.sensitive
83
- });
84
- return Boolean(info);
85
- });
86
- };
87
- return /* @__PURE__ */ jsx(Route, {
88
- path: "/",
89
- render: function(props) {
90
- var matchedRoute = findMatchedRoute(props.location.pathname);
91
- if (!matchedRoute) {
92
- return /* @__PURE__ */ jsx(DefaultNotFound, {});
93
- }
94
- return /* @__PURE__ */ jsx(Route, {
95
- path: matchedRoute.path,
96
- exact: matchedRoute.exact,
97
- sensitive: matchedRoute.sensitive,
98
- render: function(routeProps) {
99
- return /* @__PURE__ */ jsx(Layout, _objectSpread({
100
- Component: matchedRoute.component
101
- }, routeProps, extraProps));
102
- }
103
- });
104
- }
77
+ return /* @__PURE__ */ _jsx(GlobalLayout, _object_spread({
78
+ Component
79
+ }, props));
80
+ };
81
+ var findMatchedRoute = function(pathname) {
82
+ var _routesConfig_routes;
83
+ return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find(function(route) {
84
+ var info = matchPath(pathname, {
85
+ path: route.path,
86
+ exact: route.exact,
87
+ sensitive: route.sensitive
88
+ });
89
+ return Boolean(info);
105
90
  });
106
- }
107
- function getLocation(serverContext) {
108
- var _url_replace;
109
- var _ref = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {}, pathname = _ref.pathname, url = _ref.url;
110
- var cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
111
- var index = (cleanUrl || "").indexOf(pathname);
112
- if (index === -1) {
113
- return pathname;
91
+ };
92
+ return /* @__PURE__ */ _jsx(Route, {
93
+ path: "/",
94
+ render: function(props) {
95
+ var matchedRoute = findMatchedRoute(props.location.pathname);
96
+ if (!matchedRoute) {
97
+ return /* @__PURE__ */ _jsx(DefaultNotFound, {});
98
+ }
99
+ return /* @__PURE__ */ _jsx(Route, {
100
+ path: matchedRoute.path,
101
+ exact: matchedRoute.exact,
102
+ sensitive: matchedRoute.sensitive,
103
+ render: function(routeProps) {
104
+ return /* @__PURE__ */ _jsx(Layout, _object_spread({
105
+ Component: matchedRoute.component
106
+ }, routeProps, extraProps));
107
+ }
108
+ });
114
109
  }
115
- return cleanUrl.substring(index);
110
+ });
116
111
  }
117
- var urlJoin = function() {
118
- for(var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++){
119
- parts[_key] = arguments[_key];
120
- }
121
- var separator = "/";
122
- var replace = new RegExp("".concat(separator, "{1,}"), "g");
123
- return standardSlash(parts.join(separator).replace(replace, separator));
112
+ export function getLocation(serverContext) {
113
+ var _url_replace;
114
+ var _ref = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {}, pathname = _ref.pathname, url = _ref.url;
115
+ var cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
116
+ var index = (cleanUrl || "").indexOf(pathname);
117
+ if (index === -1) {
118
+ return pathname;
119
+ }
120
+ return cleanUrl.substring(index);
121
+ }
122
+ export var urlJoin = function() {
123
+ for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
124
+ parts[_key] = arguments[_key];
125
+ }
126
+ var separator = "/";
127
+ var replace = new RegExp("".concat(separator, "{1,}"), "g");
128
+ return standardSlash(parts.join(separator).replace(replace, separator));
124
129
  };
125
- function standardSlash(str) {
126
- var addr = str;
127
- if (!addr || typeof addr !== "string") {
128
- return addr;
129
- }
130
- if (addr.startsWith(".")) {
131
- addr = addr.slice(1);
132
- }
133
- if (!addr.startsWith("/")) {
134
- addr = "/".concat(addr);
135
- }
136
- if (addr.endsWith("/") && addr !== "/") {
137
- addr = addr.slice(0, addr.length - 1);
138
- }
130
+ export function standardSlash(str) {
131
+ var addr = str;
132
+ if (!addr || typeof addr !== "string") {
139
133
  return addr;
134
+ }
135
+ if (addr.startsWith(".")) {
136
+ addr = addr.slice(1);
137
+ }
138
+ if (!addr.startsWith("/")) {
139
+ addr = "/".concat(addr);
140
+ }
141
+ if (addr.endsWith("/") && addr !== "/") {
142
+ addr = addr.slice(0, addr.length - 1);
143
+ }
144
+ return addr;
140
145
  }
141
- export { getLocation, renderRoutes, standardSlash, urlJoin };