@modern-js/plugin-router-v5 2.4.1-beta.0 → 2.6.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/{js/node → cjs}/cli/index.js +7 -28
- package/dist/{js/node → cjs}/index.js +4 -0
- package/dist/{js/node → cjs}/runtime/DefaultNotFound.js +13 -10
- package/dist/{js/node → cjs}/runtime/plugin.js +14 -31
- package/dist/{js/node → cjs}/runtime/utils.js +29 -47
- package/dist/{js/treeshaking → esm}/cli/index.js +1 -5
- package/dist/{js/treeshaking → esm}/runtime/plugin.js +8 -4
- package/dist/{js/modern → esm-node}/cli/index.js +6 -28
- package/dist/esm-node/runtime/DefaultNotFound.js +17 -0
- package/dist/{js/modern → esm-node}/runtime/plugin.js +10 -33
- package/dist/esm-node/runtime/utils.js +91 -0
- package/dist/types/runtime/plugin.d.ts +1 -1
- package/package.json +14 -15
- package/dist/js/modern/runtime/DefaultNotFound.js +0 -14
- package/dist/js/modern/runtime/utils.js +0 -111
- /package/dist/{js/modern → cjs}/cli/types.js +0 -0
- /package/dist/{js/node → cjs}/runtime/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/cli/types.js +0 -0
- /package/dist/{js/treeshaking → esm}/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime/DefaultNotFound.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime/utils.js +0 -0
- /package/dist/{js/node → esm-node}/cli/types.js +0 -0
- /package/dist/{js/modern → esm-node}/index.js +0 -0
- /package/dist/{js/modern → esm-node}/runtime/index.js +0 -0
@@ -1,24 +1,7 @@
|
|
1
1
|
var __defProp = Object.defineProperty;
|
2
|
-
var __defProps = Object.defineProperties;
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
5
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
7
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
9
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
10
|
-
var __spreadValues = (a, b) => {
|
11
|
-
for (var prop in b || (b = {}))
|
12
|
-
if (__hasOwnProp.call(b, prop))
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
14
|
-
if (__getOwnPropSymbols)
|
15
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
16
|
-
if (__propIsEnum.call(b, prop))
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
18
|
-
}
|
19
|
-
return a;
|
20
|
-
};
|
21
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
22
5
|
var __export = (target, all) => {
|
23
6
|
for (var name in all)
|
24
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -41,10 +24,6 @@ var import_utils = require("@modern-js/utils");
|
|
41
24
|
var import_types2 = require("./types");
|
42
25
|
const PLUGIN_IDENTIFIER = "router";
|
43
26
|
const ROUTES_IDENTIFIER = "routes";
|
44
|
-
const isV5 = (config) => {
|
45
|
-
var _a, _b, _c;
|
46
|
-
return typeof ((_a = config.runtime) == null ? void 0 : _a.router) !== "boolean" && ((_c = (_b = config == null ? void 0 : config.runtime) == null ? void 0 : _b.router) == null ? void 0 : _c.mode) === "react-router-5";
|
47
|
-
};
|
48
27
|
var cli_default = () => ({
|
49
28
|
name: "@modern-js/plugin-router-v5",
|
50
29
|
required: ["@modern-js/runtime"],
|
@@ -86,7 +65,7 @@ var cli_default = () => ({
|
|
86
65
|
packageName
|
87
66
|
);
|
88
67
|
runtimeConfigMap.set(entryName, runtimeConfig);
|
89
|
-
if (
|
68
|
+
if ((0, import_utils.isRouterV5)(userConfig)) {
|
90
69
|
imports.push({
|
91
70
|
value: "@modern-js/runtime/plugins",
|
92
71
|
specifiers: [{ imported: PLUGIN_IDENTIFIER }]
|
@@ -106,15 +85,15 @@ var cli_default = () => ({
|
|
106
85
|
const { serverRoutes } = api.useAppContext();
|
107
86
|
const runtimeConfig = runtimeConfigMap.get(entryName);
|
108
87
|
const userConfig = api.useResolvedConfigContext();
|
109
|
-
if (
|
88
|
+
if ((0, import_utils.isRouterV5)(userConfig)) {
|
110
89
|
const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
|
111
90
|
plugins.push({
|
112
91
|
name: PLUGIN_IDENTIFIER,
|
113
|
-
options: JSON.stringify(
|
114
|
-
serverBase
|
115
|
-
|
92
|
+
options: JSON.stringify({
|
93
|
+
serverBase,
|
94
|
+
...runtimeConfig.router,
|
116
95
|
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
117
|
-
})
|
96
|
+
}).replace(
|
118
97
|
/"routesConfig"\s*:\s*"((\S|\s)+)"/g,
|
119
98
|
'"routesConfig": $1,'
|
120
99
|
)
|
@@ -127,7 +106,7 @@ var cli_default = () => ({
|
|
127
106
|
},
|
128
107
|
addRuntimeExports() {
|
129
108
|
const userConfig = api.useResolvedConfigContext();
|
130
|
-
if (
|
109
|
+
if ((0, import_utils.isRouterV5)(userConfig)) {
|
131
110
|
pluginsExportsUtils.addExport(
|
132
111
|
`export { default as router } from '@modern-js/plugin-router-v5/runtime'`
|
133
112
|
);
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
18
18
|
};
|
19
19
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
22
26
|
mod
|
23
27
|
));
|
@@ -21,16 +21,19 @@ __export(DefaultNotFound_exports, {
|
|
21
21
|
});
|
22
22
|
module.exports = __toCommonJS(DefaultNotFound_exports);
|
23
23
|
var import_jsx_runtime = require("react/jsx-runtime");
|
24
|
-
const DefaultNotFound = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
}
|
24
|
+
const DefaultNotFound = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
25
|
+
"div",
|
26
|
+
{
|
27
|
+
style: {
|
28
|
+
margin: "150px auto",
|
29
|
+
textAlign: "center",
|
30
|
+
display: "flex",
|
31
|
+
alignItems: "center",
|
32
|
+
justifyContent: "center"
|
33
|
+
},
|
34
|
+
children: "404"
|
35
|
+
}
|
36
|
+
);
|
34
37
|
// Annotate the CommonJS export names for ESM import in node:
|
35
38
|
0 && (module.exports = {
|
36
39
|
DefaultNotFound
|
@@ -1,26 +1,9 @@
|
|
1
1
|
var __create = Object.create;
|
2
2
|
var __defProp = Object.defineProperty;
|
3
|
-
var __defProps = Object.defineProperties;
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
6
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
8
5
|
var __getProtoOf = Object.getPrototypeOf;
|
9
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
12
|
-
var __spreadValues = (a, b) => {
|
13
|
-
for (var prop in b || (b = {}))
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
if (__getOwnPropSymbols)
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
18
|
-
if (__propIsEnum.call(b, prop))
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
20
|
-
}
|
21
|
-
return a;
|
22
|
-
};
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
24
7
|
var __export = (target, all) => {
|
25
8
|
for (var name in all)
|
26
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -34,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
34
17
|
return to;
|
35
18
|
};
|
36
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
37
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
38
25
|
mod
|
39
26
|
));
|
@@ -78,12 +65,7 @@ const routerPlugin = ({
|
|
78
65
|
const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
|
79
66
|
historyOptions.basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
|
80
67
|
const history = customHistory || (supportHtml5History ? (0, import_history.createBrowserHistory)(historyOptions) : (0, import_history.createHashHistory)(historyOptions));
|
81
|
-
return (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Router, {
|
82
|
-
history,
|
83
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, __spreadProps(__spreadValues({}, props), {
|
84
|
-
children: createRoutes ? createRoutes() : (0, import_utils.renderRoutes)(routesConfig, props)
|
85
|
-
}))
|
86
|
-
});
|
68
|
+
return (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Router, { history, children: createRoutes ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, { ...props, children: (0, import_utils.renderRoutes)(routesConfig, props) }) });
|
87
69
|
}
|
88
70
|
return (props) => {
|
89
71
|
const runtimeContext = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
|
@@ -93,14 +75,15 @@ const routerPlugin = ({
|
|
93
75
|
const request = ssrContext == null ? void 0 : ssrContext.request;
|
94
76
|
const baseUrl = request == null ? void 0 : request.baseUrl;
|
95
77
|
const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
|
96
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
78
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
79
|
+
import_react_router_dom.StaticRouter,
|
80
|
+
{
|
81
|
+
basename: basename === "/" ? "" : basename,
|
82
|
+
location: location2,
|
83
|
+
context: routerContext,
|
84
|
+
children: createRoutes ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, { ...props, children: (0, import_utils.renderRoutes)(routesConfig, props) })
|
85
|
+
}
|
86
|
+
);
|
104
87
|
};
|
105
88
|
};
|
106
89
|
let RouteApp = getRouteApp();
|
@@ -1,33 +1,7 @@
|
|
1
1
|
var __defProp = Object.defineProperty;
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
-
var __spreadValues = (a, b) => {
|
9
|
-
for (var prop in b || (b = {}))
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
12
|
-
if (__getOwnPropSymbols)
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
-
if (__propIsEnum.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
}
|
17
|
-
return a;
|
18
|
-
};
|
19
|
-
var __objRest = (source, exclude) => {
|
20
|
-
var target = {};
|
21
|
-
for (var prop in source)
|
22
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
23
|
-
target[prop] = source[prop];
|
24
|
-
if (source != null && __getOwnPropSymbols)
|
25
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
26
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
27
|
-
target[prop] = source[prop];
|
28
|
-
}
|
29
|
-
return target;
|
30
|
-
};
|
31
5
|
var __export = (target, all) => {
|
32
6
|
for (var name in all)
|
33
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -56,15 +30,12 @@ function renderRoutes(routesConfig, extraProps = {}) {
|
|
56
30
|
if (!routesConfig) {
|
57
31
|
return null;
|
58
32
|
}
|
59
|
-
const Layout = (
|
60
|
-
var _b = _a, { Component } = _b, props = __objRest(_b, ["Component"]);
|
33
|
+
const Layout = ({ Component, ...props }) => {
|
61
34
|
const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
|
62
35
|
if (!GlobalLayout) {
|
63
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component,
|
36
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props });
|
64
37
|
}
|
65
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GlobalLayout,
|
66
|
-
Component
|
67
|
-
}, props));
|
38
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GlobalLayout, { Component, ...props });
|
68
39
|
};
|
69
40
|
const findMatchedRoute = (pathname) => {
|
70
41
|
var _a;
|
@@ -77,23 +48,34 @@ function renderRoutes(routesConfig, extraProps = {}) {
|
|
77
48
|
return Boolean(info);
|
78
49
|
});
|
79
50
|
};
|
80
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
51
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
52
|
+
import_react_router_dom.Route,
|
53
|
+
{
|
54
|
+
path: "/",
|
55
|
+
render: (props) => {
|
56
|
+
const matchedRoute = findMatchedRoute(props.location.pathname);
|
57
|
+
if (!matchedRoute) {
|
58
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DefaultNotFound.DefaultNotFound, {});
|
59
|
+
}
|
60
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
61
|
+
import_react_router_dom.Route,
|
62
|
+
{
|
63
|
+
path: matchedRoute.path,
|
64
|
+
exact: matchedRoute.exact,
|
65
|
+
sensitive: matchedRoute.sensitive,
|
66
|
+
render: (routeProps) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
67
|
+
Layout,
|
68
|
+
{
|
69
|
+
Component: matchedRoute.component,
|
70
|
+
...routeProps,
|
71
|
+
...extraProps
|
72
|
+
}
|
73
|
+
)
|
74
|
+
}
|
75
|
+
);
|
86
76
|
}
|
87
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
88
|
-
path: matchedRoute.path,
|
89
|
-
exact: matchedRoute.exact,
|
90
|
-
sensitive: matchedRoute.sensitive,
|
91
|
-
render: (routeProps) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Layout, __spreadValues(__spreadValues({
|
92
|
-
Component: matchedRoute.component
|
93
|
-
}, routeProps), extraProps))
|
94
|
-
});
|
95
77
|
}
|
96
|
-
|
78
|
+
);
|
97
79
|
}
|
98
80
|
function getLocation(serverContext) {
|
99
81
|
var _a;
|
@@ -50,14 +50,10 @@ function _objectSpreadProps(target, source) {
|
|
50
50
|
}
|
51
51
|
return target;
|
52
52
|
}
|
53
|
-
import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from "@modern-js/utils";
|
53
|
+
import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS, isRouterV5 as isV5 } from "@modern-js/utils";
|
54
54
|
import "./types";
|
55
55
|
var PLUGIN_IDENTIFIER = "router";
|
56
56
|
var ROUTES_IDENTIFIER = "routes";
|
57
|
-
var isV5 = function(config) {
|
58
|
-
var _config_runtime, _config_runtime1, _config_runtime_router;
|
59
|
-
return typeof ((_config_runtime = config.runtime) === null || _config_runtime === void 0 ? void 0 : _config_runtime.router) !== "boolean" && (config === null || config === void 0 ? void 0 : (_config_runtime1 = config.runtime) === null || _config_runtime1 === void 0 ? void 0 : (_config_runtime_router = _config_runtime1.router) === null || _config_runtime_router === void 0 ? void 0 : _config_runtime_router.mode) === "react-router-5";
|
60
|
-
};
|
61
57
|
var cli_default = function() {
|
62
58
|
return {
|
63
59
|
name: "@modern-js/plugin-router-v5",
|
@@ -90,8 +90,10 @@ var routerPlugin = function(param) {
|
|
90
90
|
return function(props) {
|
91
91
|
return /* @__PURE__ */ jsx(Router, {
|
92
92
|
history: history,
|
93
|
-
children: /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
94
|
-
|
93
|
+
children: createRoutes ? /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
94
|
+
Component: createRoutes()
|
95
|
+
})) : /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
96
|
+
children: renderRoutes(routesConfig, props)
|
95
97
|
}))
|
96
98
|
});
|
97
99
|
};
|
@@ -108,8 +110,10 @@ var routerPlugin = function(param) {
|
|
108
110
|
basename: basename === "/" ? "" : basename,
|
109
111
|
location: location2,
|
110
112
|
context: routerContext,
|
111
|
-
children: /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
112
|
-
|
113
|
+
children: createRoutes ? /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
114
|
+
Component: createRoutes()
|
115
|
+
})) : /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
116
|
+
children: renderRoutes(routesConfig, props)
|
113
117
|
}))
|
114
118
|
});
|
115
119
|
};
|
@@ -1,34 +1,12 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __defProps = Object.defineProperties;
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
-
var __spreadValues = (a, b) => {
|
9
|
-
for (var prop in b || (b = {}))
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
12
|
-
if (__getOwnPropSymbols)
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
-
if (__propIsEnum.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
}
|
17
|
-
return a;
|
18
|
-
};
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20
1
|
import {
|
21
2
|
getEntryOptions,
|
22
3
|
createRuntimeExportsUtils,
|
23
|
-
PLUGIN_SCHEMAS
|
4
|
+
PLUGIN_SCHEMAS,
|
5
|
+
isRouterV5 as isV5
|
24
6
|
} from "@modern-js/utils";
|
25
7
|
import "./types";
|
26
8
|
const PLUGIN_IDENTIFIER = "router";
|
27
9
|
const ROUTES_IDENTIFIER = "routes";
|
28
|
-
const isV5 = (config) => {
|
29
|
-
var _a, _b, _c;
|
30
|
-
return typeof ((_a = config.runtime) == null ? void 0 : _a.router) !== "boolean" && ((_c = (_b = config == null ? void 0 : config.runtime) == null ? void 0 : _b.router) == null ? void 0 : _c.mode) === "react-router-5";
|
31
|
-
};
|
32
10
|
var cli_default = () => ({
|
33
11
|
name: "@modern-js/plugin-router-v5",
|
34
12
|
required: ["@modern-js/runtime"],
|
@@ -94,11 +72,11 @@ var cli_default = () => ({
|
|
94
72
|
const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
|
95
73
|
plugins.push({
|
96
74
|
name: PLUGIN_IDENTIFIER,
|
97
|
-
options: JSON.stringify(
|
98
|
-
serverBase
|
99
|
-
|
75
|
+
options: JSON.stringify({
|
76
|
+
serverBase,
|
77
|
+
...runtimeConfig.router,
|
100
78
|
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
101
|
-
})
|
79
|
+
}).replace(
|
102
80
|
/"routesConfig"\s*:\s*"((\S|\s)+)"/g,
|
103
81
|
'"routesConfig": $1,'
|
104
82
|
)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
2
|
+
const DefaultNotFound = () => /* @__PURE__ */ jsx(
|
3
|
+
"div",
|
4
|
+
{
|
5
|
+
style: {
|
6
|
+
margin: "150px auto",
|
7
|
+
textAlign: "center",
|
8
|
+
display: "flex",
|
9
|
+
alignItems: "center",
|
10
|
+
justifyContent: "center"
|
11
|
+
},
|
12
|
+
children: "404"
|
13
|
+
}
|
14
|
+
);
|
15
|
+
export {
|
16
|
+
DefaultNotFound
|
17
|
+
};
|
@@ -1,22 +1,3 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __defProps = Object.defineProperties;
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
-
var __spreadValues = (a, b) => {
|
9
|
-
for (var prop in b || (b = {}))
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
12
|
-
if (__getOwnPropSymbols)
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
-
if (__propIsEnum.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
}
|
17
|
-
return a;
|
18
|
-
};
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20
1
|
import { jsx } from "react/jsx-runtime";
|
21
2
|
import { useContext } from "react";
|
22
3
|
import {
|
@@ -60,12 +41,7 @@ const routerPlugin = ({
|
|
60
41
|
const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
|
61
42
|
historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
62
43
|
const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
63
|
-
return (props) => /* @__PURE__ */ jsx(Router, {
|
64
|
-
history,
|
65
|
-
children: /* @__PURE__ */ jsx(App, __spreadProps(__spreadValues({}, props), {
|
66
|
-
children: createRoutes ? createRoutes() : renderRoutes(routesConfig, props)
|
67
|
-
}))
|
68
|
-
});
|
44
|
+
return (props) => /* @__PURE__ */ jsx(Router, { history, children: createRoutes ? /* @__PURE__ */ jsx(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ jsx(App, { ...props, children: renderRoutes(routesConfig, props) }) });
|
69
45
|
}
|
70
46
|
return (props) => {
|
71
47
|
const runtimeContext = useContext(RuntimeReactContext);
|
@@ -75,14 +51,15 @@ const routerPlugin = ({
|
|
75
51
|
const request = ssrContext == null ? void 0 : ssrContext.request;
|
76
52
|
const baseUrl = request == null ? void 0 : request.baseUrl;
|
77
53
|
const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
78
|
-
return /* @__PURE__ */ jsx(
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
54
|
+
return /* @__PURE__ */ jsx(
|
55
|
+
StaticRouter,
|
56
|
+
{
|
57
|
+
basename: basename === "/" ? "" : basename,
|
58
|
+
location: location2,
|
59
|
+
context: routerContext,
|
60
|
+
children: createRoutes ? /* @__PURE__ */ jsx(App, { ...props, Component: createRoutes() }) : /* @__PURE__ */ jsx(App, { ...props, children: renderRoutes(routesConfig, props) })
|
61
|
+
}
|
62
|
+
);
|
86
63
|
};
|
87
64
|
};
|
88
65
|
let RouteApp = getRouteApp();
|
@@ -0,0 +1,91 @@
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
2
|
+
import { Route, matchPath } from "react-router-dom";
|
3
|
+
import { DefaultNotFound } from "./DefaultNotFound";
|
4
|
+
function renderRoutes(routesConfig, extraProps = {}) {
|
5
|
+
if (!routesConfig) {
|
6
|
+
return null;
|
7
|
+
}
|
8
|
+
const Layout = ({ Component, ...props }) => {
|
9
|
+
const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
|
10
|
+
if (!GlobalLayout) {
|
11
|
+
return /* @__PURE__ */ jsx(Component, { ...props });
|
12
|
+
}
|
13
|
+
return /* @__PURE__ */ jsx(GlobalLayout, { Component, ...props });
|
14
|
+
};
|
15
|
+
const findMatchedRoute = (pathname) => {
|
16
|
+
var _a;
|
17
|
+
return (_a = routesConfig == null ? void 0 : routesConfig.routes) == null ? void 0 : _a.find((route) => {
|
18
|
+
const info = matchPath(pathname, {
|
19
|
+
path: route.path,
|
20
|
+
exact: route.exact,
|
21
|
+
sensitive: route.sensitive
|
22
|
+
});
|
23
|
+
return Boolean(info);
|
24
|
+
});
|
25
|
+
};
|
26
|
+
return /* @__PURE__ */ jsx(
|
27
|
+
Route,
|
28
|
+
{
|
29
|
+
path: "/",
|
30
|
+
render: (props) => {
|
31
|
+
const matchedRoute = findMatchedRoute(props.location.pathname);
|
32
|
+
if (!matchedRoute) {
|
33
|
+
return /* @__PURE__ */ jsx(DefaultNotFound, {});
|
34
|
+
}
|
35
|
+
return /* @__PURE__ */ jsx(
|
36
|
+
Route,
|
37
|
+
{
|
38
|
+
path: matchedRoute.path,
|
39
|
+
exact: matchedRoute.exact,
|
40
|
+
sensitive: matchedRoute.sensitive,
|
41
|
+
render: (routeProps) => /* @__PURE__ */ jsx(
|
42
|
+
Layout,
|
43
|
+
{
|
44
|
+
Component: matchedRoute.component,
|
45
|
+
...routeProps,
|
46
|
+
...extraProps
|
47
|
+
}
|
48
|
+
)
|
49
|
+
}
|
50
|
+
);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
);
|
54
|
+
}
|
55
|
+
function getLocation(serverContext) {
|
56
|
+
var _a;
|
57
|
+
const { pathname, url } = (serverContext == null ? void 0 : serverContext.request) || {};
|
58
|
+
const cleanUrl = (_a = url == null ? void 0 : url.replace("http://", "")) == null ? void 0 : _a.replace("https://", "");
|
59
|
+
const index = (cleanUrl || "").indexOf(pathname);
|
60
|
+
if (index === -1) {
|
61
|
+
return pathname;
|
62
|
+
}
|
63
|
+
return cleanUrl.substring(index);
|
64
|
+
}
|
65
|
+
const urlJoin = (...parts) => {
|
66
|
+
const separator = "/";
|
67
|
+
const replace = new RegExp(`${separator}{1,}`, "g");
|
68
|
+
return standardSlash(parts.join(separator).replace(replace, separator));
|
69
|
+
};
|
70
|
+
function standardSlash(str) {
|
71
|
+
let addr = str;
|
72
|
+
if (!addr || typeof addr !== "string") {
|
73
|
+
return addr;
|
74
|
+
}
|
75
|
+
if (addr.startsWith(".")) {
|
76
|
+
addr = addr.slice(1);
|
77
|
+
}
|
78
|
+
if (!addr.startsWith("/")) {
|
79
|
+
addr = `/${addr}`;
|
80
|
+
}
|
81
|
+
if (addr.endsWith("/") && addr !== "/") {
|
82
|
+
addr = addr.slice(0, addr.length - 1);
|
83
|
+
}
|
84
|
+
return addr;
|
85
|
+
}
|
86
|
+
export {
|
87
|
+
getLocation,
|
88
|
+
renderRoutes,
|
89
|
+
standardSlash,
|
90
|
+
urlJoin
|
91
|
+
};
|
@@ -40,7 +40,7 @@ export type RouterConfig = Partial<HistoryConfig> & {
|
|
40
40
|
globalApp?: React.ComponentType<any>;
|
41
41
|
routes?: SingleRouteConfig[];
|
42
42
|
};
|
43
|
-
createRoutes?: () =>
|
43
|
+
createRoutes?: () => React.ComponentType<any> | null;
|
44
44
|
history?: History;
|
45
45
|
serverBase?: string[];
|
46
46
|
};
|
package/package.json
CHANGED
@@ -11,24 +11,23 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "2.
|
14
|
+
"version": "2.6.0",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/cli/index.d.ts",
|
17
|
-
"main": "./dist/
|
18
|
-
"module": "./dist/
|
19
|
-
"jsnext:modern": "./dist/js/modern/cli/index.js",
|
17
|
+
"main": "./dist/cjs/cli/index.js",
|
18
|
+
"module": "./dist/esm/index.js",
|
20
19
|
"exports": {
|
21
20
|
".": {
|
22
21
|
"jsnext:source": "./src/cli/index.ts",
|
23
|
-
"default": "./dist/
|
22
|
+
"default": "./dist/cjs/cli/index.js"
|
24
23
|
},
|
25
24
|
"./runtime": {
|
26
25
|
"jsnext:source": "./src/runtime/index.ts",
|
27
|
-
"default": "./dist/
|
26
|
+
"default": "./dist/esm/runtime/index.js"
|
28
27
|
},
|
29
28
|
"./cli": {
|
30
29
|
"jsnext:source": "./src/cli/index.ts",
|
31
|
-
"default": "./dist/
|
30
|
+
"default": "./dist/cjs/cli/index.js"
|
32
31
|
}
|
33
32
|
},
|
34
33
|
"typesVersions": {
|
@@ -53,8 +52,8 @@
|
|
53
52
|
"hoist-non-react-statics": "^3.3.2",
|
54
53
|
"@types/history": "^4.7.9",
|
55
54
|
"@types/react-router-dom": "^5.1.8",
|
56
|
-
"@modern-js/utils": "2.
|
57
|
-
"@modern-js/types": "2.
|
55
|
+
"@modern-js/utils": "2.6.0",
|
56
|
+
"@modern-js/types": "2.6.0"
|
58
57
|
},
|
59
58
|
"peerDependencies": {
|
60
59
|
"react": ">=17",
|
@@ -73,12 +72,12 @@
|
|
73
72
|
"@testing-library/react": "^13.4.0",
|
74
73
|
"@testing-library/react-hooks": "^8.0.1",
|
75
74
|
"@types/invariant": "^2.2.30",
|
76
|
-
"@modern-js/runtime": "2.
|
77
|
-
"@modern-js/core": "2.
|
78
|
-
"@modern-js/utils": "2.
|
79
|
-
"@scripts/build": "2.
|
80
|
-
"@modern-js/app-tools": "
|
81
|
-
"@scripts/jest-config": "2.
|
75
|
+
"@modern-js/runtime": "2.6.0",
|
76
|
+
"@modern-js/core": "2.6.0",
|
77
|
+
"@modern-js/utils": "2.6.0",
|
78
|
+
"@scripts/build": "2.6.0",
|
79
|
+
"@modern-js/app-tools": "2.6.0",
|
80
|
+
"@scripts/jest-config": "2.6.0"
|
82
81
|
},
|
83
82
|
"sideEffects": false,
|
84
83
|
"modernConfig": {},
|
@@ -1,14 +0,0 @@
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
2
|
-
const DefaultNotFound = () => /* @__PURE__ */ jsx("div", {
|
3
|
-
style: {
|
4
|
-
margin: "150px auto",
|
5
|
-
textAlign: "center",
|
6
|
-
display: "flex",
|
7
|
-
alignItems: "center",
|
8
|
-
justifyContent: "center"
|
9
|
-
},
|
10
|
-
children: "404"
|
11
|
-
});
|
12
|
-
export {
|
13
|
-
DefaultNotFound
|
14
|
-
};
|
@@ -1,111 +0,0 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
3
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6
|
-
var __spreadValues = (a, b) => {
|
7
|
-
for (var prop in b || (b = {}))
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
10
|
-
if (__getOwnPropSymbols)
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
12
|
-
if (__propIsEnum.call(b, prop))
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
14
|
-
}
|
15
|
-
return a;
|
16
|
-
};
|
17
|
-
var __objRest = (source, exclude) => {
|
18
|
-
var target = {};
|
19
|
-
for (var prop in source)
|
20
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
21
|
-
target[prop] = source[prop];
|
22
|
-
if (source != null && __getOwnPropSymbols)
|
23
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
24
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
25
|
-
target[prop] = source[prop];
|
26
|
-
}
|
27
|
-
return target;
|
28
|
-
};
|
29
|
-
import { jsx } from "react/jsx-runtime";
|
30
|
-
import { Route, matchPath } from "react-router-dom";
|
31
|
-
import { DefaultNotFound } from "./DefaultNotFound";
|
32
|
-
function renderRoutes(routesConfig, extraProps = {}) {
|
33
|
-
if (!routesConfig) {
|
34
|
-
return null;
|
35
|
-
}
|
36
|
-
const Layout = (_a) => {
|
37
|
-
var _b = _a, { Component } = _b, props = __objRest(_b, ["Component"]);
|
38
|
-
const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
|
39
|
-
if (!GlobalLayout) {
|
40
|
-
return /* @__PURE__ */ jsx(Component, __spreadValues({}, props));
|
41
|
-
}
|
42
|
-
return /* @__PURE__ */ jsx(GlobalLayout, __spreadValues({
|
43
|
-
Component
|
44
|
-
}, props));
|
45
|
-
};
|
46
|
-
const findMatchedRoute = (pathname) => {
|
47
|
-
var _a;
|
48
|
-
return (_a = routesConfig == null ? void 0 : routesConfig.routes) == null ? void 0 : _a.find((route) => {
|
49
|
-
const info = matchPath(pathname, {
|
50
|
-
path: route.path,
|
51
|
-
exact: route.exact,
|
52
|
-
sensitive: route.sensitive
|
53
|
-
});
|
54
|
-
return Boolean(info);
|
55
|
-
});
|
56
|
-
};
|
57
|
-
return /* @__PURE__ */ jsx(Route, {
|
58
|
-
path: "/",
|
59
|
-
render: (props) => {
|
60
|
-
const matchedRoute = findMatchedRoute(props.location.pathname);
|
61
|
-
if (!matchedRoute) {
|
62
|
-
return /* @__PURE__ */ jsx(DefaultNotFound, {});
|
63
|
-
}
|
64
|
-
return /* @__PURE__ */ jsx(Route, {
|
65
|
-
path: matchedRoute.path,
|
66
|
-
exact: matchedRoute.exact,
|
67
|
-
sensitive: matchedRoute.sensitive,
|
68
|
-
render: (routeProps) => /* @__PURE__ */ jsx(Layout, __spreadValues(__spreadValues({
|
69
|
-
Component: matchedRoute.component
|
70
|
-
}, routeProps), extraProps))
|
71
|
-
});
|
72
|
-
}
|
73
|
-
});
|
74
|
-
}
|
75
|
-
function getLocation(serverContext) {
|
76
|
-
var _a;
|
77
|
-
const { pathname, url } = (serverContext == null ? void 0 : serverContext.request) || {};
|
78
|
-
const cleanUrl = (_a = url == null ? void 0 : url.replace("http://", "")) == null ? void 0 : _a.replace("https://", "");
|
79
|
-
const index = (cleanUrl || "").indexOf(pathname);
|
80
|
-
if (index === -1) {
|
81
|
-
return pathname;
|
82
|
-
}
|
83
|
-
return cleanUrl.substring(index);
|
84
|
-
}
|
85
|
-
const urlJoin = (...parts) => {
|
86
|
-
const separator = "/";
|
87
|
-
const replace = new RegExp(`${separator}{1,}`, "g");
|
88
|
-
return standardSlash(parts.join(separator).replace(replace, separator));
|
89
|
-
};
|
90
|
-
function standardSlash(str) {
|
91
|
-
let addr = str;
|
92
|
-
if (!addr || typeof addr !== "string") {
|
93
|
-
return addr;
|
94
|
-
}
|
95
|
-
if (addr.startsWith(".")) {
|
96
|
-
addr = addr.slice(1);
|
97
|
-
}
|
98
|
-
if (!addr.startsWith("/")) {
|
99
|
-
addr = `/${addr}`;
|
100
|
-
}
|
101
|
-
if (addr.endsWith("/") && addr !== "/") {
|
102
|
-
addr = addr.slice(0, addr.length - 1);
|
103
|
-
}
|
104
|
-
return addr;
|
105
|
-
}
|
106
|
-
export {
|
107
|
-
getLocation,
|
108
|
-
renderRoutes,
|
109
|
-
standardSlash,
|
110
|
-
urlJoin
|
111
|
-
};
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|