@modern-js/plugin-router-v5 2.17.0 → 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/CHANGELOG.md +7 -0
- 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 +10 -9
package/CHANGELOG.md
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
3
|
+
value: true
|
4
|
+
});
|
5
|
+
Object.defineProperty(exports, "modifyRoutesHook", {
|
6
|
+
enumerable: true,
|
7
|
+
get: () => modifyRoutesHook
|
8
|
+
});
|
9
|
+
const _plugin = require("@modern-js/plugin");
|
10
|
+
const modifyRoutesHook = (0, _plugin.createWaterfall)();
|
@@ -14,6 +14,7 @@ const _hoistnonreactstatics = /* @__PURE__ */ _interop_require_default(require("
|
|
14
14
|
const _runtime = require("@modern-js/runtime");
|
15
15
|
const _runtime1 = require("@modern-js/utils/runtime");
|
16
16
|
const _utils = require("./utils");
|
17
|
+
const _hooks = require("./hooks");
|
17
18
|
function _interop_require_default(obj) {
|
18
19
|
return obj && obj.__esModule ? obj : {
|
19
20
|
default: obj
|
@@ -60,6 +61,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
|
|
60
61
|
return newObj;
|
61
62
|
}
|
62
63
|
const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
|
64
|
+
const { routes: originRoutes } = routesConfig;
|
63
65
|
const isBrow = (0, _runtime.isBrowser)();
|
64
66
|
const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
|
65
67
|
if (isBrow) {
|
@@ -67,7 +69,10 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
|
|
67
69
|
}
|
68
70
|
return {
|
69
71
|
name: "@modern-js/plugin-router",
|
70
|
-
|
72
|
+
registerHook: {
|
73
|
+
modifyRoutes: _hooks.modifyRoutesHook
|
74
|
+
},
|
75
|
+
setup: (api) => {
|
71
76
|
return {
|
72
77
|
init({ context }, next) {
|
73
78
|
context.router = {
|
@@ -84,6 +89,9 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
|
|
84
89
|
var _window__SERVER_DATA;
|
85
90
|
const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
|
86
91
|
historyOptions.basename = baseUrl === "/" ? (0, _utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
|
92
|
+
const runner = api.useHookRunners();
|
93
|
+
const routes = runner.modifyRoutes(originRoutes);
|
94
|
+
routesConfig.routes = routes;
|
87
95
|
const history = customHistory || (supportHtml5History ? (0, _history.createBrowserHistory)(historyOptions) : (0, _history.createHashHistory)(historyOptions));
|
88
96
|
return (props) => /* @__PURE__ */ (0, _jsxruntime.jsx)(_reactrouterdom.Router, {
|
89
97
|
history,
|
@@ -58,8 +58,10 @@ 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
|
+
import { modifyRoutesHook } from "./hooks";
|
61
62
|
export var routerPlugin = function(param) {
|
62
63
|
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;
|
64
|
+
var originRoutes = routesConfig.routes;
|
63
65
|
var isBrow = isBrowser();
|
64
66
|
var select = function(pathname) {
|
65
67
|
return serverBase.find(function(baseUrl) {
|
@@ -71,7 +73,10 @@ export var routerPlugin = function(param) {
|
|
71
73
|
}
|
72
74
|
return {
|
73
75
|
name: "@modern-js/plugin-router",
|
74
|
-
|
76
|
+
registerHook: {
|
77
|
+
modifyRoutes: modifyRoutesHook
|
78
|
+
},
|
79
|
+
setup: function(api) {
|
75
80
|
return {
|
76
81
|
init: function init(param2, next) {
|
77
82
|
var context = param2.context;
|
@@ -90,6 +95,9 @@ export var routerPlugin = function(param) {
|
|
90
95
|
var _window__SERVER_DATA;
|
91
96
|
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
97
|
historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
98
|
+
var runner = api.useHookRunners();
|
99
|
+
var routes = runner.modifyRoutes(originRoutes);
|
100
|
+
routesConfig.routes = routes;
|
93
101
|
var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
94
102
|
return function(props) {
|
95
103
|
return /* @__PURE__ */ _jsx(Router, {
|
@@ -6,7 +6,9 @@ import hoistNonReactStatics from "hoist-non-react-statics";
|
|
6
6
|
import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
|
7
7
|
import { parsedJSONFromElement } from "@modern-js/utils/runtime";
|
8
8
|
import { renderRoutes, getLocation, urlJoin } from "./utils";
|
9
|
+
import { modifyRoutesHook } from "./hooks";
|
9
10
|
export const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
|
11
|
+
const { routes: originRoutes } = routesConfig;
|
10
12
|
const isBrow = isBrowser();
|
11
13
|
const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
|
12
14
|
if (isBrow) {
|
@@ -14,7 +16,10 @@ export const routerPlugin = ({ serverBase = [], history: customHistory, supportH
|
|
14
16
|
}
|
15
17
|
return {
|
16
18
|
name: "@modern-js/plugin-router",
|
17
|
-
|
19
|
+
registerHook: {
|
20
|
+
modifyRoutes: modifyRoutesHook
|
21
|
+
},
|
22
|
+
setup: (api) => {
|
18
23
|
return {
|
19
24
|
init({ context }, next) {
|
20
25
|
context.router = {
|
@@ -31,6 +36,9 @@ export const routerPlugin = ({ serverBase = [], history: customHistory, supportH
|
|
31
36
|
var _window__SERVER_DATA;
|
32
37
|
const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
|
33
38
|
historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
39
|
+
const runner = api.useHookRunners();
|
40
|
+
const routes = runner.modifyRoutes(originRoutes);
|
41
|
+
routesConfig.routes = routes;
|
34
42
|
const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
35
43
|
return (props) => /* @__PURE__ */ _jsx(Router, {
|
36
44
|
history,
|
@@ -0,0 +1,128 @@
|
|
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
|
+
import {
|
21
|
+
getEntryOptions,
|
22
|
+
createRuntimeExportsUtils,
|
23
|
+
PLUGIN_SCHEMAS
|
24
|
+
} from "@modern-js/utils";
|
25
|
+
import "./types";
|
26
|
+
const PLUGIN_IDENTIFIER = "router";
|
27
|
+
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
|
+
var cli_default = () => ({
|
33
|
+
name: "@modern-js/plugin-router-v5",
|
34
|
+
required: ["@modern-js/runtime"],
|
35
|
+
setup: (api) => {
|
36
|
+
const runtimeConfigMap = /* @__PURE__ */ new Map();
|
37
|
+
let pluginsExportsUtils;
|
38
|
+
let routerExportsUtils;
|
39
|
+
return {
|
40
|
+
config() {
|
41
|
+
const appContext = api.useAppContext();
|
42
|
+
pluginsExportsUtils = createRuntimeExportsUtils(
|
43
|
+
appContext.internalDirectory,
|
44
|
+
"plugins"
|
45
|
+
);
|
46
|
+
routerExportsUtils = createRuntimeExportsUtils(
|
47
|
+
appContext.internalDirectory,
|
48
|
+
"router"
|
49
|
+
);
|
50
|
+
return {
|
51
|
+
source: {
|
52
|
+
alias: {
|
53
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
|
54
|
+
"@modern-js/runtime/router-v5": routerExportsUtils.getPath()
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
58
|
+
},
|
59
|
+
validateSchema() {
|
60
|
+
return PLUGIN_SCHEMAS["@modern-js/plugin-router"];
|
61
|
+
},
|
62
|
+
modifyEntryImports({ entrypoint, imports }) {
|
63
|
+
const { entryName } = entrypoint;
|
64
|
+
const userConfig = api.useResolvedConfigContext();
|
65
|
+
const { packageName } = api.useAppContext();
|
66
|
+
const runtimeConfig = getEntryOptions(
|
67
|
+
entryName,
|
68
|
+
userConfig.runtime,
|
69
|
+
userConfig.runtimeByEntries,
|
70
|
+
packageName
|
71
|
+
);
|
72
|
+
runtimeConfigMap.set(entryName, runtimeConfig);
|
73
|
+
if (isV5(userConfig)) {
|
74
|
+
imports.push({
|
75
|
+
value: "@modern-js/runtime/plugins",
|
76
|
+
specifiers: [{ imported: PLUGIN_IDENTIFIER }]
|
77
|
+
});
|
78
|
+
} else {
|
79
|
+
throw new Error(
|
80
|
+
`should enable runtime.router.mode for entry ${entryName}`
|
81
|
+
);
|
82
|
+
}
|
83
|
+
return {
|
84
|
+
entrypoint,
|
85
|
+
imports
|
86
|
+
};
|
87
|
+
},
|
88
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
89
|
+
const { entryName, fileSystemRoutes } = entrypoint;
|
90
|
+
const { serverRoutes } = api.useAppContext();
|
91
|
+
const runtimeConfig = runtimeConfigMap.get(entryName);
|
92
|
+
const userConfig = api.useResolvedConfigContext();
|
93
|
+
if (isV5(userConfig)) {
|
94
|
+
const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
|
95
|
+
plugins.push({
|
96
|
+
name: PLUGIN_IDENTIFIER,
|
97
|
+
options: JSON.stringify(__spreadProps(__spreadValues({
|
98
|
+
serverBase
|
99
|
+
}, runtimeConfig.router), {
|
100
|
+
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
101
|
+
})).replace(
|
102
|
+
/"routesConfig"\s*:\s*"((\S|\s)+)"/g,
|
103
|
+
'"routesConfig": $1,'
|
104
|
+
)
|
105
|
+
});
|
106
|
+
}
|
107
|
+
return {
|
108
|
+
entrypoint,
|
109
|
+
plugins
|
110
|
+
};
|
111
|
+
},
|
112
|
+
addRuntimeExports() {
|
113
|
+
const userConfig = api.useResolvedConfigContext();
|
114
|
+
if (isV5(userConfig)) {
|
115
|
+
pluginsExportsUtils.addExport(
|
116
|
+
`export { default as router } from '@modern-js/plugin-router-v5/runtime'`
|
117
|
+
);
|
118
|
+
routerExportsUtils == null ? void 0 : routerExportsUtils.addExport(
|
119
|
+
`export * from '@modern-js/plugin-router-v5/runtime'`
|
120
|
+
);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
};
|
124
|
+
}
|
125
|
+
});
|
126
|
+
export {
|
127
|
+
cli_default as default
|
128
|
+
};
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
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
|
+
};
|
@@ -0,0 +1,106 @@
|
|
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
|
+
import { jsx } from "react/jsx-runtime";
|
21
|
+
import { useContext } from "react";
|
22
|
+
import {
|
23
|
+
createBrowserHistory,
|
24
|
+
createHashHistory
|
25
|
+
} from "history";
|
26
|
+
import {
|
27
|
+
Router,
|
28
|
+
StaticRouter,
|
29
|
+
useRouteMatch,
|
30
|
+
useLocation
|
31
|
+
} from "react-router-dom";
|
32
|
+
import hoistNonReactStatics from "hoist-non-react-statics";
|
33
|
+
import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
|
34
|
+
import { renderRoutes, getLocation, urlJoin } from "./utils";
|
35
|
+
const routerPlugin = ({
|
36
|
+
serverBase = [],
|
37
|
+
history: customHistory,
|
38
|
+
supportHtml5History = true,
|
39
|
+
routesConfig,
|
40
|
+
historyOptions = {}
|
41
|
+
}) => {
|
42
|
+
const isBrow = isBrowser();
|
43
|
+
const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
|
44
|
+
return {
|
45
|
+
name: "@modern-js/plugin-router",
|
46
|
+
setup: () => {
|
47
|
+
return {
|
48
|
+
init({ context }, next) {
|
49
|
+
context.router = {
|
50
|
+
useRouteMatch,
|
51
|
+
useLocation
|
52
|
+
};
|
53
|
+
return next({ context });
|
54
|
+
},
|
55
|
+
hoc: ({ App }, next) => {
|
56
|
+
const getRouteApp = () => {
|
57
|
+
var _a;
|
58
|
+
if (isBrow) {
|
59
|
+
const baseUrl = ((_a = window._SERVER_DATA) == null ? void 0 : _a.router.baseUrl) || select(location.pathname);
|
60
|
+
historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
61
|
+
const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
|
62
|
+
return (props) => /* @__PURE__ */ jsx(Router, {
|
63
|
+
history,
|
64
|
+
children: /* @__PURE__ */ jsx(App, __spreadProps(__spreadValues({}, props), {
|
65
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
66
|
+
}))
|
67
|
+
});
|
68
|
+
}
|
69
|
+
return (props) => {
|
70
|
+
const runtimeContext = useContext(RuntimeReactContext);
|
71
|
+
const { ssrContext } = runtimeContext;
|
72
|
+
const location2 = getLocation(ssrContext);
|
73
|
+
const routerContext = (ssrContext == null ? void 0 : ssrContext.redirection) || {};
|
74
|
+
const request = ssrContext == null ? void 0 : ssrContext.request;
|
75
|
+
const baseUrl = request == null ? void 0 : request.baseUrl;
|
76
|
+
const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
|
77
|
+
return /* @__PURE__ */ jsx(StaticRouter, {
|
78
|
+
basename: basename === "/" ? "" : basename,
|
79
|
+
location: location2,
|
80
|
+
context: routerContext,
|
81
|
+
children: /* @__PURE__ */ jsx(App, __spreadProps(__spreadValues({}, props), {
|
82
|
+
children: routesConfig ? renderRoutes(routesConfig, props) : null
|
83
|
+
}))
|
84
|
+
});
|
85
|
+
};
|
86
|
+
};
|
87
|
+
let RouteApp = getRouteApp();
|
88
|
+
if (App) {
|
89
|
+
RouteApp = hoistNonReactStatics(RouteApp, App);
|
90
|
+
}
|
91
|
+
if (routesConfig == null ? void 0 : routesConfig.globalApp) {
|
92
|
+
return next({
|
93
|
+
App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
|
94
|
+
});
|
95
|
+
}
|
96
|
+
return next({
|
97
|
+
App: RouteApp
|
98
|
+
});
|
99
|
+
}
|
100
|
+
};
|
101
|
+
}
|
102
|
+
};
|
103
|
+
};
|
104
|
+
export {
|
105
|
+
routerPlugin
|
106
|
+
};
|
@@ -0,0 +1,108 @@
|
|
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
|
+
const Layout = (_a) => {
|
34
|
+
var _b = _a, { Component } = _b, props = __objRest(_b, ["Component"]);
|
35
|
+
const GlobalLayout = routesConfig == null ? void 0 : routesConfig.globalApp;
|
36
|
+
if (!GlobalLayout) {
|
37
|
+
return /* @__PURE__ */ jsx(Component, __spreadValues({}, props));
|
38
|
+
}
|
39
|
+
return /* @__PURE__ */ jsx(GlobalLayout, __spreadValues({
|
40
|
+
Component
|
41
|
+
}, props));
|
42
|
+
};
|
43
|
+
const findMatchedRoute = (pathname) => {
|
44
|
+
var _a;
|
45
|
+
return (_a = routesConfig == null ? void 0 : routesConfig.routes) == null ? void 0 : _a.find((route) => {
|
46
|
+
const info = matchPath(pathname, {
|
47
|
+
path: route.path,
|
48
|
+
exact: route.exact,
|
49
|
+
sensitive: route.sensitive
|
50
|
+
});
|
51
|
+
return Boolean(info);
|
52
|
+
});
|
53
|
+
};
|
54
|
+
return /* @__PURE__ */ jsx(Route, {
|
55
|
+
path: "/",
|
56
|
+
render: (props) => {
|
57
|
+
const matchedRoute = findMatchedRoute(props.location.pathname);
|
58
|
+
if (!matchedRoute) {
|
59
|
+
return /* @__PURE__ */ jsx(DefaultNotFound, {});
|
60
|
+
}
|
61
|
+
return /* @__PURE__ */ jsx(Route, {
|
62
|
+
path: matchedRoute.path,
|
63
|
+
exact: matchedRoute.exact,
|
64
|
+
sensitive: matchedRoute.sensitive,
|
65
|
+
render: (routeProps) => /* @__PURE__ */ jsx(Layout, __spreadValues(__spreadValues({
|
66
|
+
Component: matchedRoute.component
|
67
|
+
}, routeProps), extraProps))
|
68
|
+
});
|
69
|
+
}
|
70
|
+
});
|
71
|
+
}
|
72
|
+
function getLocation(serverContext) {
|
73
|
+
var _a;
|
74
|
+
const { pathname, url } = (serverContext == null ? void 0 : serverContext.request) || {};
|
75
|
+
const cleanUrl = (_a = url == null ? void 0 : url.replace("http://", "")) == null ? void 0 : _a.replace("https://", "");
|
76
|
+
const index = (cleanUrl || "").indexOf(pathname);
|
77
|
+
if (index === -1) {
|
78
|
+
return pathname;
|
79
|
+
}
|
80
|
+
return cleanUrl.substring(index);
|
81
|
+
}
|
82
|
+
const urlJoin = (...parts) => {
|
83
|
+
const separator = "/";
|
84
|
+
const replace = new RegExp(`${separator}{1,}`, "g");
|
85
|
+
return standardSlash(parts.join(separator).replace(replace, separator));
|
86
|
+
};
|
87
|
+
function standardSlash(str) {
|
88
|
+
let addr = str;
|
89
|
+
if (!addr || typeof addr !== "string") {
|
90
|
+
return addr;
|
91
|
+
}
|
92
|
+
if (addr.startsWith(".")) {
|
93
|
+
addr = addr.slice(1);
|
94
|
+
}
|
95
|
+
if (!addr.startsWith("/")) {
|
96
|
+
addr = `/${addr}`;
|
97
|
+
}
|
98
|
+
if (addr.endsWith("/") && addr !== "/") {
|
99
|
+
addr = addr.slice(0, addr.length - 1);
|
100
|
+
}
|
101
|
+
return addr;
|
102
|
+
}
|
103
|
+
export {
|
104
|
+
getLocation,
|
105
|
+
renderRoutes,
|
106
|
+
standardSlash,
|
107
|
+
urlJoin
|
108
|
+
};
|
@@ -0,0 +1,143 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __defProps = Object.defineProperties;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
7
|
+
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
|
+
var __export = (target, all) => {
|
23
|
+
for (var name in all)
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
25
|
+
};
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
28
|
+
for (let key of __getOwnPropNames(from))
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
31
|
+
}
|
32
|
+
return to;
|
33
|
+
};
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
35
|
+
var cli_exports = {};
|
36
|
+
__export(cli_exports, {
|
37
|
+
default: () => cli_default
|
38
|
+
});
|
39
|
+
module.exports = __toCommonJS(cli_exports);
|
40
|
+
var import_utils = require("@modern-js/utils");
|
41
|
+
var import_types2 = require("./types");
|
42
|
+
const PLUGIN_IDENTIFIER = "router";
|
43
|
+
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
|
+
var cli_default = () => ({
|
49
|
+
name: "@modern-js/plugin-router-v5",
|
50
|
+
required: ["@modern-js/runtime"],
|
51
|
+
setup: (api) => {
|
52
|
+
const runtimeConfigMap = /* @__PURE__ */ new Map();
|
53
|
+
let pluginsExportsUtils;
|
54
|
+
let routerExportsUtils;
|
55
|
+
return {
|
56
|
+
config() {
|
57
|
+
const appContext = api.useAppContext();
|
58
|
+
pluginsExportsUtils = (0, import_utils.createRuntimeExportsUtils)(
|
59
|
+
appContext.internalDirectory,
|
60
|
+
"plugins"
|
61
|
+
);
|
62
|
+
routerExportsUtils = (0, import_utils.createRuntimeExportsUtils)(
|
63
|
+
appContext.internalDirectory,
|
64
|
+
"router"
|
65
|
+
);
|
66
|
+
return {
|
67
|
+
source: {
|
68
|
+
alias: {
|
69
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
|
70
|
+
"@modern-js/runtime/router-v5": routerExportsUtils.getPath()
|
71
|
+
}
|
72
|
+
}
|
73
|
+
};
|
74
|
+
},
|
75
|
+
validateSchema() {
|
76
|
+
return import_utils.PLUGIN_SCHEMAS["@modern-js/plugin-router"];
|
77
|
+
},
|
78
|
+
modifyEntryImports({ entrypoint, imports }) {
|
79
|
+
const { entryName } = entrypoint;
|
80
|
+
const userConfig = api.useResolvedConfigContext();
|
81
|
+
const { packageName } = api.useAppContext();
|
82
|
+
const runtimeConfig = (0, import_utils.getEntryOptions)(
|
83
|
+
entryName,
|
84
|
+
userConfig.runtime,
|
85
|
+
userConfig.runtimeByEntries,
|
86
|
+
packageName
|
87
|
+
);
|
88
|
+
runtimeConfigMap.set(entryName, runtimeConfig);
|
89
|
+
if (isV5(userConfig)) {
|
90
|
+
imports.push({
|
91
|
+
value: "@modern-js/runtime/plugins",
|
92
|
+
specifiers: [{ imported: PLUGIN_IDENTIFIER }]
|
93
|
+
});
|
94
|
+
} else {
|
95
|
+
throw new Error(
|
96
|
+
`should enable runtime.router.mode for entry ${entryName}`
|
97
|
+
);
|
98
|
+
}
|
99
|
+
return {
|
100
|
+
entrypoint,
|
101
|
+
imports
|
102
|
+
};
|
103
|
+
},
|
104
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
105
|
+
const { entryName, fileSystemRoutes } = entrypoint;
|
106
|
+
const { serverRoutes } = api.useAppContext();
|
107
|
+
const runtimeConfig = runtimeConfigMap.get(entryName);
|
108
|
+
const userConfig = api.useResolvedConfigContext();
|
109
|
+
if (isV5(userConfig)) {
|
110
|
+
const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
|
111
|
+
plugins.push({
|
112
|
+
name: PLUGIN_IDENTIFIER,
|
113
|
+
options: JSON.stringify(__spreadProps(__spreadValues({
|
114
|
+
serverBase
|
115
|
+
}, runtimeConfig.router), {
|
116
|
+
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
117
|
+
})).replace(
|
118
|
+
/"routesConfig"\s*:\s*"((\S|\s)+)"/g,
|
119
|
+
'"routesConfig": $1,'
|
120
|
+
)
|
121
|
+
});
|
122
|
+
}
|
123
|
+
return {
|
124
|
+
entrypoint,
|
125
|
+
plugins
|
126
|
+
};
|
127
|
+
},
|
128
|
+
addRuntimeExports() {
|
129
|
+
const userConfig = api.useResolvedConfigContext();
|
130
|
+
if (isV5(userConfig)) {
|
131
|
+
pluginsExportsUtils.addExport(
|
132
|
+
`export { default as router } from '@modern-js/plugin-router-v5/runtime'`
|
133
|
+
);
|
134
|
+
routerExportsUtils == null ? void 0 : routerExportsUtils.addExport(
|
135
|
+
`export * from '@modern-js/plugin-router-v5/runtime'`
|
136
|
+
);
|
137
|
+
}
|
138
|
+
}
|
139
|
+
};
|
140
|
+
}
|
141
|
+
});
|
142
|
+
// Annotate the CommonJS export names for ESM import in node:
|
143
|
+
0 && (module.exports = {});
|
File without changes
|