@modern-js/plugin-router-v5 2.17.1 → 2.17.2-alpha.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/cjs/runtime/hooks.js +10 -0
- package/dist/cjs/runtime/plugin.js +9 -1
- package/dist/esm/runtime/hooks.js +3 -0
- package/dist/esm/runtime/plugin.js +9 -1
- package/dist/esm-node/runtime/hooks.js +3 -0
- package/dist/esm-node/runtime/plugin.js +9 -1
- package/dist/js/modern/cli/index.js +128 -0
- package/dist/js/modern/cli/types.js +0 -0
- package/dist/js/modern/index.js +5 -0
- package/dist/js/modern/runtime/DefaultNotFound.js +14 -0
- package/dist/js/modern/runtime/index.js +7 -0
- package/dist/js/modern/runtime/plugin.js +106 -0
- package/dist/js/modern/runtime/utils.js +108 -0
- package/dist/js/node/cli/index.js +143 -0
- package/dist/js/node/cli/types.js +0 -0
- package/dist/js/node/index.js +33 -0
- package/dist/js/node/runtime/DefaultNotFound.js +37 -0
- package/dist/js/node/runtime/index.js +29 -0
- package/dist/js/node/runtime/plugin.js +125 -0
- package/dist/js/node/runtime/utils.js +132 -0
- package/dist/js/treeshaking/cli/index.js +151 -0
- package/dist/js/treeshaking/cli/types.js +1 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/dist/js/treeshaking/runtime/DefaultNotFound.js +14 -0
- package/dist/js/treeshaking/runtime/index.js +5 -0
- package/dist/js/treeshaking/runtime/plugin.js +134 -0
- package/dist/js/treeshaking/runtime/utils.js +138 -0
- package/dist/types/runtime/DefaultNotFound.d.ts +0 -1
- package/dist/types/runtime/hooks.d.ts +5 -0
- package/dist/types/runtime/plugin.d.ts +1 -1
- package/package.json +2 -1
@@ -0,0 +1,37 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
5
|
+
var __export = (target, all) => {
|
6
|
+
for (var name in all)
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
8
|
+
};
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
18
|
+
var DefaultNotFound_exports = {};
|
19
|
+
__export(DefaultNotFound_exports, {
|
20
|
+
DefaultNotFound: () => DefaultNotFound
|
21
|
+
});
|
22
|
+
module.exports = __toCommonJS(DefaultNotFound_exports);
|
23
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
24
|
+
const DefaultNotFound = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
25
|
+
style: {
|
26
|
+
margin: "150px auto",
|
27
|
+
textAlign: "center",
|
28
|
+
display: "flex",
|
29
|
+
alignItems: "center",
|
30
|
+
justifyContent: "center"
|
31
|
+
},
|
32
|
+
children: "404"
|
33
|
+
});
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
35
|
+
0 && (module.exports = {
|
36
|
+
DefaultNotFound
|
37
|
+
});
|
@@ -0,0 +1,29 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
5
|
+
var __export = (target, all) => {
|
6
|
+
for (var name in all)
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
8
|
+
};
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var runtime_exports = {};
|
20
|
+
__export(runtime_exports, {
|
21
|
+
default: () => runtime_default
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(runtime_exports);
|
24
|
+
var import_plugin = require("./plugin");
|
25
|
+
__reExport(runtime_exports, require("react-router-dom"), module.exports);
|
26
|
+
__reExport(runtime_exports, require("history"), module.exports);
|
27
|
+
var runtime_default = import_plugin.routerPlugin;
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
29
|
+
0 && (module.exports = {});
|
@@ -0,0 +1,125 @@
|
|
1
|
+
var __create = Object.create;
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __defProps = Object.defineProperties;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
9
|
+
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
|
+
var __export = (target, all) => {
|
25
|
+
for (var name in all)
|
26
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
27
|
+
};
|
28
|
+
var __copyProps = (to, from, except, desc) => {
|
29
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
30
|
+
for (let key of __getOwnPropNames(from))
|
31
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
32
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
33
|
+
}
|
34
|
+
return to;
|
35
|
+
};
|
36
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
37
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
38
|
+
mod
|
39
|
+
));
|
40
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
41
|
+
var plugin_exports = {};
|
42
|
+
__export(plugin_exports, {
|
43
|
+
routerPlugin: () => routerPlugin
|
44
|
+
});
|
45
|
+
module.exports = __toCommonJS(plugin_exports);
|
46
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
47
|
+
var import_react = require("react");
|
48
|
+
var import_history = require("history");
|
49
|
+
var import_react_router_dom = require("react-router-dom");
|
50
|
+
var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
|
51
|
+
var import_runtime = require("@modern-js/runtime");
|
52
|
+
var import_utils = require("./utils");
|
53
|
+
const routerPlugin = ({
|
54
|
+
serverBase = [],
|
55
|
+
history: customHistory,
|
56
|
+
supportHtml5History = true,
|
57
|
+
routesConfig,
|
58
|
+
historyOptions = {}
|
59
|
+
}) => {
|
60
|
+
const isBrow = (0, import_runtime.isBrowser)();
|
61
|
+
const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
|
62
|
+
return {
|
63
|
+
name: "@modern-js/plugin-router",
|
64
|
+
setup: () => {
|
65
|
+
return {
|
66
|
+
init({ context }, next) {
|
67
|
+
context.router = {
|
68
|
+
useRouteMatch: import_react_router_dom.useRouteMatch,
|
69
|
+
useLocation: import_react_router_dom.useLocation
|
70
|
+
};
|
71
|
+
return next({ context });
|
72
|
+
},
|
73
|
+
hoc: ({ App }, next) => {
|
74
|
+
const getRouteApp = () => {
|
75
|
+
var _a;
|
76
|
+
if (isBrow) {
|
77
|
+
const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
|
78
|
+
historyOptions.basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
|
79
|
+
const history = customHistory || (supportHtml5History ? (0, import_history.createBrowserHistory)(historyOptions) : (0, import_history.createHashHistory)(historyOptions));
|
80
|
+
return (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Router, {
|
81
|
+
history,
|
82
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, __spreadProps(__spreadValues({}, props), {
|
83
|
+
children: routesConfig ? (0, import_utils.renderRoutes)(routesConfig, props) : null
|
84
|
+
}))
|
85
|
+
});
|
86
|
+
}
|
87
|
+
return (props) => {
|
88
|
+
const runtimeContext = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
|
89
|
+
const { ssrContext } = runtimeContext;
|
90
|
+
const location2 = (0, import_utils.getLocation)(ssrContext);
|
91
|
+
const routerContext = (ssrContext == null ? void 0 : ssrContext.redirection) || {};
|
92
|
+
const request = ssrContext == null ? void 0 : ssrContext.request;
|
93
|
+
const baseUrl = request == null ? void 0 : request.baseUrl;
|
94
|
+
const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
|
95
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.StaticRouter, {
|
96
|
+
basename: basename === "/" ? "" : basename,
|
97
|
+
location: location2,
|
98
|
+
context: routerContext,
|
99
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, __spreadProps(__spreadValues({}, props), {
|
100
|
+
children: routesConfig ? (0, import_utils.renderRoutes)(routesConfig, props) : null
|
101
|
+
}))
|
102
|
+
});
|
103
|
+
};
|
104
|
+
};
|
105
|
+
let RouteApp = getRouteApp();
|
106
|
+
if (App) {
|
107
|
+
RouteApp = (0, import_hoist_non_react_statics.default)(RouteApp, App);
|
108
|
+
}
|
109
|
+
if (routesConfig == null ? void 0 : routesConfig.globalApp) {
|
110
|
+
return next({
|
111
|
+
App: (0, import_hoist_non_react_statics.default)(RouteApp, routesConfig.globalApp)
|
112
|
+
});
|
113
|
+
}
|
114
|
+
return next({
|
115
|
+
App: RouteApp
|
116
|
+
});
|
117
|
+
}
|
118
|
+
};
|
119
|
+
}
|
120
|
+
};
|
121
|
+
};
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
123
|
+
0 && (module.exports = {
|
124
|
+
routerPlugin
|
125
|
+
});
|
@@ -0,0 +1,132 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
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 __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
|
+
var __export = (target, all) => {
|
32
|
+
for (var name in all)
|
33
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
34
|
+
};
|
35
|
+
var __copyProps = (to, from, except, desc) => {
|
36
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
37
|
+
for (let key of __getOwnPropNames(from))
|
38
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
39
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
40
|
+
}
|
41
|
+
return to;
|
42
|
+
};
|
43
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
44
|
+
var utils_exports = {};
|
45
|
+
__export(utils_exports, {
|
46
|
+
getLocation: () => getLocation,
|
47
|
+
renderRoutes: () => renderRoutes,
|
48
|
+
standardSlash: () => standardSlash,
|
49
|
+
urlJoin: () => urlJoin
|
50
|
+
});
|
51
|
+
module.exports = __toCommonJS(utils_exports);
|
52
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
53
|
+
var import_react_router_dom = require("react-router-dom");
|
54
|
+
var import_DefaultNotFound = require("./DefaultNotFound");
|
55
|
+
function renderRoutes(routesConfig, extraProps = {}) {
|
56
|
+
const Layout = (_a) => {
|
57
|
+
var _b = _a, { Component } = _b, props = __objRest(_b, ["Component"]);
|
58
|
+
const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
|
59
|
+
if (!GlobalLayout) {
|
60
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, __spreadValues({}, props));
|
61
|
+
}
|
62
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GlobalLayout, __spreadValues({
|
63
|
+
Component
|
64
|
+
}, props));
|
65
|
+
};
|
66
|
+
const findMatchedRoute = (pathname) => {
|
67
|
+
var _a;
|
68
|
+
return (_a = routesConfig == null ? void 0 : routesConfig.routes) == null ? void 0 : _a.find((route) => {
|
69
|
+
const info = (0, import_react_router_dom.matchPath)(pathname, {
|
70
|
+
path: route.path,
|
71
|
+
exact: route.exact,
|
72
|
+
sensitive: route.sensitive
|
73
|
+
});
|
74
|
+
return Boolean(info);
|
75
|
+
});
|
76
|
+
};
|
77
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
78
|
+
path: "/",
|
79
|
+
render: (props) => {
|
80
|
+
const matchedRoute = findMatchedRoute(props.location.pathname);
|
81
|
+
if (!matchedRoute) {
|
82
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_DefaultNotFound.DefaultNotFound, {});
|
83
|
+
}
|
84
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
85
|
+
path: matchedRoute.path,
|
86
|
+
exact: matchedRoute.exact,
|
87
|
+
sensitive: matchedRoute.sensitive,
|
88
|
+
render: (routeProps) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Layout, __spreadValues(__spreadValues({
|
89
|
+
Component: matchedRoute.component
|
90
|
+
}, routeProps), extraProps))
|
91
|
+
});
|
92
|
+
}
|
93
|
+
});
|
94
|
+
}
|
95
|
+
function getLocation(serverContext) {
|
96
|
+
var _a;
|
97
|
+
const { pathname, url } = (serverContext == null ? void 0 : serverContext.request) || {};
|
98
|
+
const cleanUrl = (_a = url == null ? void 0 : url.replace("http://", "")) == null ? void 0 : _a.replace("https://", "");
|
99
|
+
const index = (cleanUrl || "").indexOf(pathname);
|
100
|
+
if (index === -1) {
|
101
|
+
return pathname;
|
102
|
+
}
|
103
|
+
return cleanUrl.substring(index);
|
104
|
+
}
|
105
|
+
const urlJoin = (...parts) => {
|
106
|
+
const separator = "/";
|
107
|
+
const replace = new RegExp(`${separator}{1,}`, "g");
|
108
|
+
return standardSlash(parts.join(separator).replace(replace, separator));
|
109
|
+
};
|
110
|
+
function standardSlash(str) {
|
111
|
+
let addr = str;
|
112
|
+
if (!addr || typeof addr !== "string") {
|
113
|
+
return addr;
|
114
|
+
}
|
115
|
+
if (addr.startsWith(".")) {
|
116
|
+
addr = addr.slice(1);
|
117
|
+
}
|
118
|
+
if (!addr.startsWith("/")) {
|
119
|
+
addr = `/${addr}`;
|
120
|
+
}
|
121
|
+
if (addr.endsWith("/") && addr !== "/") {
|
122
|
+
addr = addr.slice(0, addr.length - 1);
|
123
|
+
}
|
124
|
+
return addr;
|
125
|
+
}
|
126
|
+
// Annotate the CommonJS export names for ESM import in node:
|
127
|
+
0 && (module.exports = {
|
128
|
+
getLocation,
|
129
|
+
renderRoutes,
|
130
|
+
standardSlash,
|
131
|
+
urlJoin
|
132
|
+
});
|
@@ -0,0 +1,151 @@
|
|
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;
|
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
|
+
});
|
26
|
+
}
|
27
|
+
return target;
|
28
|
+
}
|
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);
|
39
|
+
}
|
40
|
+
return keys;
|
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;
|
52
|
+
}
|
53
|
+
import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from "@modern-js/utils";
|
54
|
+
import "./types";
|
55
|
+
var PLUGIN_IDENTIFIER = "router";
|
56
|
+
var ROUTES_IDENTIFIER = "routes";
|
57
|
+
var isV5 = function(config) {
|
58
|
+
var ref, ref1, ref2;
|
59
|
+
return typeof ((ref = config.runtime) === null || ref === void 0 ? void 0 : ref.router) !== "boolean" && (config === null || config === void 0 ? void 0 : (ref1 = config.runtime) === null || ref1 === void 0 ? void 0 : (ref2 = ref1.router) === null || ref2 === void 0 ? void 0 : ref2.mode) === "react-router-5";
|
60
|
+
};
|
61
|
+
var cli_default = function() {
|
62
|
+
return {
|
63
|
+
name: "@modern-js/plugin-router-v5",
|
64
|
+
required: [
|
65
|
+
"@modern-js/runtime"
|
66
|
+
],
|
67
|
+
setup: function(api) {
|
68
|
+
var runtimeConfigMap = /* @__PURE__ */ new Map();
|
69
|
+
var pluginsExportsUtils;
|
70
|
+
var routerExportsUtils;
|
71
|
+
return {
|
72
|
+
config: function config() {
|
73
|
+
var appContext = api.useAppContext();
|
74
|
+
pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
|
75
|
+
routerExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "router");
|
76
|
+
return {
|
77
|
+
source: {
|
78
|
+
alias: {
|
79
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
|
80
|
+
"@modern-js/runtime/router-v5": routerExportsUtils.getPath()
|
81
|
+
}
|
82
|
+
}
|
83
|
+
};
|
84
|
+
},
|
85
|
+
validateSchema: function validateSchema() {
|
86
|
+
return PLUGIN_SCHEMAS["@modern-js/plugin-router"];
|
87
|
+
},
|
88
|
+
modifyEntryImports: function modifyEntryImports(param) {
|
89
|
+
var entrypoint = param.entrypoint, imports = param.imports;
|
90
|
+
var entryName = entrypoint.entryName;
|
91
|
+
var userConfig = api.useResolvedConfigContext();
|
92
|
+
var packageName = api.useAppContext().packageName;
|
93
|
+
var runtimeConfig = getEntryOptions(entryName, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
94
|
+
runtimeConfigMap.set(entryName, runtimeConfig);
|
95
|
+
if (isV5(userConfig)) {
|
96
|
+
imports.push({
|
97
|
+
value: "@modern-js/runtime/plugins",
|
98
|
+
specifiers: [
|
99
|
+
{
|
100
|
+
imported: PLUGIN_IDENTIFIER
|
101
|
+
}
|
102
|
+
]
|
103
|
+
});
|
104
|
+
} else {
|
105
|
+
throw new Error("should enable runtime.router.mode for entry ".concat(entryName));
|
106
|
+
}
|
107
|
+
return {
|
108
|
+
entrypoint: entrypoint,
|
109
|
+
imports: imports
|
110
|
+
};
|
111
|
+
},
|
112
|
+
modifyEntryRuntimePlugins: function modifyEntryRuntimePlugins(param) {
|
113
|
+
var entrypoint = param.entrypoint, plugins = param.plugins;
|
114
|
+
var entryName = entrypoint.entryName, fileSystemRoutes = entrypoint.fileSystemRoutes;
|
115
|
+
var serverRoutes = api.useAppContext().serverRoutes;
|
116
|
+
var runtimeConfig = runtimeConfigMap.get(entryName);
|
117
|
+
var userConfig = api.useResolvedConfigContext();
|
118
|
+
if (isV5(userConfig)) {
|
119
|
+
var serverBase = serverRoutes.filter(function(route) {
|
120
|
+
return route.entryName === entryName;
|
121
|
+
}).map(function(route) {
|
122
|
+
return route.urlPath;
|
123
|
+
}).sort(function(a, b) {
|
124
|
+
return a.length - b.length > 0 ? -1 : 1;
|
125
|
+
});
|
126
|
+
plugins.push({
|
127
|
+
name: PLUGIN_IDENTIFIER,
|
128
|
+
options: JSON.stringify(_objectSpreadProps(_objectSpread({
|
129
|
+
serverBase: serverBase
|
130
|
+
}, runtimeConfig.router), {
|
131
|
+
routesConfig: fileSystemRoutes ? "{ ".concat(ROUTES_IDENTIFIER, ", globalApp: App }") : void 0
|
132
|
+
})).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
|
133
|
+
});
|
134
|
+
}
|
135
|
+
return {
|
136
|
+
entrypoint: entrypoint,
|
137
|
+
plugins: plugins
|
138
|
+
};
|
139
|
+
},
|
140
|
+
addRuntimeExports: function addRuntimeExports() {
|
141
|
+
var userConfig = api.useResolvedConfigContext();
|
142
|
+
if (isV5(userConfig)) {
|
143
|
+
pluginsExportsUtils.addExport("export { default as router } from '@modern-js/plugin-router-v5/runtime'");
|
144
|
+
routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport("export * from '@modern-js/plugin-router-v5/runtime'");
|
145
|
+
}
|
146
|
+
}
|
147
|
+
};
|
148
|
+
}
|
149
|
+
};
|
150
|
+
};
|
151
|
+
export { cli_default as default };
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
2
|
+
var DefaultNotFound = function() {
|
3
|
+
return /* @__PURE__ */ jsx("div", {
|
4
|
+
style: {
|
5
|
+
margin: "150px auto",
|
6
|
+
textAlign: "center",
|
7
|
+
display: "flex",
|
8
|
+
alignItems: "center",
|
9
|
+
justifyContent: "center"
|
10
|
+
},
|
11
|
+
children: "404"
|
12
|
+
});
|
13
|
+
};
|
14
|
+
export { DefaultNotFound };
|
@@ -0,0 +1,134 @@
|
|
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;
|
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
|
+
});
|
26
|
+
}
|
27
|
+
return target;
|
28
|
+
}
|
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);
|
39
|
+
}
|
40
|
+
return keys;
|
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;
|
52
|
+
}
|
53
|
+
import { jsx } from "react/jsx-runtime";
|
54
|
+
import { useContext } from "react";
|
55
|
+
import { createBrowserHistory, createHashHistory } from "history";
|
56
|
+
import { Router, StaticRouter, useRouteMatch, useLocation } from "react-router-dom";
|
57
|
+
import hoistNonReactStatics from "hoist-non-react-statics";
|
58
|
+
import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
|
59
|
+
import { renderRoutes, getLocation, urlJoin } from "./utils";
|
60
|
+
var routerPlugin = function(param) {
|
61
|
+
var _serverBase = param.serverBase, serverBase = _serverBase === void 0 ? [] : _serverBase, customHistory = param.history, _supportHtml5History = param.supportHtml5History, supportHtml5History = _supportHtml5History === void 0 ? true : _supportHtml5History, routesConfig = param.routesConfig, _historyOptions = param.historyOptions, historyOptions = _historyOptions === void 0 ? {} : _historyOptions;
|
62
|
+
var isBrow = isBrowser();
|
63
|
+
var select = function(pathname) {
|
64
|
+
return serverBase.find(function(baseUrl) {
|
65
|
+
return pathname.search(baseUrl) === 0;
|
66
|
+
}) || "/";
|
67
|
+
};
|
68
|
+
return {
|
69
|
+
name: "@modern-js/plugin-router",
|
70
|
+
setup: function() {
|
71
|
+
return {
|
72
|
+
init: function init(param, next) {
|
73
|
+
var context = param.context;
|
74
|
+
context.router = {
|
75
|
+
useRouteMatch: useRouteMatch,
|
76
|
+
useLocation: useLocation
|
77
|
+
};
|
78
|
+
return next({
|
79
|
+
context: context
|
80
|
+
});
|
81
|
+
},
|
82
|
+
hoc: function(param, next) {
|
83
|
+
var App = param.App;
|
84
|
+
var getRouteApp = function() {
|
85
|
+
if (isBrow) {
|
86
|
+
var ref;
|
87
|
+
var baseUrl = ((ref = window._SERVER_DATA) === null || ref === void 0 ? void 0 : ref.router.baseUrl) || select(location.pathname);
|
88
|
+
historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
89
|
+
var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
90
|
+
return function(props) {
|
91
|
+
return /* @__PURE__ */ jsx(Router, {
|
92
|
+
history: history,
|
93
|
+
children: /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
94
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
95
|
+
}))
|
96
|
+
});
|
97
|
+
};
|
98
|
+
}
|
99
|
+
return function(props) {
|
100
|
+
var runtimeContext = useContext(RuntimeReactContext);
|
101
|
+
var ssrContext = runtimeContext.ssrContext;
|
102
|
+
var location2 = getLocation(ssrContext);
|
103
|
+
var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
|
104
|
+
var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
|
105
|
+
var baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
|
106
|
+
var basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
107
|
+
return /* @__PURE__ */ jsx(StaticRouter, {
|
108
|
+
basename: basename === "/" ? "" : basename,
|
109
|
+
location: location2,
|
110
|
+
context: routerContext,
|
111
|
+
children: /* @__PURE__ */ jsx(App, _objectSpreadProps(_objectSpread({}, props), {
|
112
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
113
|
+
}))
|
114
|
+
});
|
115
|
+
};
|
116
|
+
};
|
117
|
+
var RouteApp = getRouteApp();
|
118
|
+
if (App) {
|
119
|
+
RouteApp = hoistNonReactStatics(RouteApp, App);
|
120
|
+
}
|
121
|
+
if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
|
122
|
+
return next({
|
123
|
+
App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
|
124
|
+
});
|
125
|
+
}
|
126
|
+
return next({
|
127
|
+
App: RouteApp
|
128
|
+
});
|
129
|
+
}
|
130
|
+
};
|
131
|
+
}
|
132
|
+
};
|
133
|
+
};
|
134
|
+
export { routerPlugin };
|