@modern-js/plugin-router-v5 2.26.0 → 2.28.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 CHANGED
@@ -1,5 +1,29 @@
1
1
  # @modern-js/plugin-router-v5
2
2
 
3
+ ## 2.28.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [4e3ce96]
8
+ - Updated dependencies [00b58a7]
9
+ - @modern-js/types@2.28.0
10
+ - @modern-js/utils@2.28.0
11
+ - @modern-js/plugin@2.28.0
12
+
13
+ ## 2.27.0
14
+
15
+ ### Patch Changes
16
+
17
+ - 8322a51: chore: migrate packages from tsc to module-lib build
18
+
19
+ chore: 将使用 tsc 的包迁移到 module-lib 构建
20
+
21
+ - Updated dependencies [91d14b8]
22
+ - Updated dependencies [6d7104d]
23
+ - @modern-js/utils@2.27.0
24
+ - @modern-js/plugin@2.27.0
25
+ - @modern-js/types@2.27.0
26
+
3
27
  ## 2.26.0
4
28
 
5
29
  ### Patch Changes
@@ -21,96 +21,95 @@ const _utils = require("@modern-js/utils");
21
21
  require("./types");
22
22
  const PLUGIN_IDENTIFIER = "router";
23
23
  const ROUTES_IDENTIFIER = "routes";
24
- const routerPlugin = () => {
25
- return {
26
- name: "@modern-js/plugin-router-v5",
27
- required: [
28
- "@modern-js/runtime"
29
- ],
30
- setup: (api) => {
31
- const runtimeConfigMap = /* @__PURE__ */ new Map();
32
- let pluginsExportsUtils;
33
- let routerExportsUtils;
34
- return {
35
- config() {
36
- const appContext = api.useAppContext();
37
- pluginsExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
38
- routerExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "router");
39
- return {
40
- source: {
41
- alias: {
42
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
43
- "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
44
- }
45
- }
46
- };
47
- },
48
- validateSchema() {
49
- return [
50
- {
51
- target: "runtime.router",
52
- schema: {
53
- type: [
54
- "boolean",
55
- "object"
56
- ]
57
- }
24
+ const routerPlugin = () => ({
25
+ name: "@modern-js/plugin-router-v5",
26
+ required: [
27
+ "@modern-js/runtime"
28
+ ],
29
+ setup: (api) => {
30
+ const runtimeConfigMap = /* @__PURE__ */ new Map();
31
+ let pluginsExportsUtils;
32
+ let routerExportsUtils;
33
+ return {
34
+ config() {
35
+ const appContext = api.useAppContext();
36
+ pluginsExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
37
+ routerExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "router");
38
+ return {
39
+ source: {
40
+ alias: {
41
+ "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
42
+ "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
58
43
  }
59
- ];
60
- },
61
- modifyEntryImports({ entrypoint, imports }) {
62
- const { entryName, isMainEntry } = entrypoint;
63
- const userConfig = api.useResolvedConfigContext();
64
- const { packageName } = api.useAppContext();
65
- const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
66
- runtimeConfigMap.set(entryName, runtimeConfig);
67
- if ((0, _utils.isRouterV5)(userConfig)) {
68
- imports.push({
69
- value: "@modern-js/runtime/plugins",
70
- specifiers: [
71
- {
72
- imported: PLUGIN_IDENTIFIER
73
- }
74
- ]
75
- });
76
- } else {
77
- throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
78
- }
79
- return {
80
- entrypoint,
81
- imports
82
- };
83
- },
84
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
85
- const { entryName, fileSystemRoutes } = entrypoint;
86
- const { serverRoutes } = api.useAppContext();
87
- const runtimeConfig = runtimeConfigMap.get(entryName);
88
- const userConfig = api.useResolvedConfigContext();
89
- if ((0, _utils.isRouterV5)(userConfig)) {
90
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
91
- plugins.push({
92
- name: PLUGIN_IDENTIFIER,
93
- options: JSON.stringify({
94
- serverBase,
95
- ...runtimeConfig.router,
96
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
97
- }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
98
- });
99
44
  }
100
- return {
101
- entrypoint,
102
- plugins
103
- };
104
- },
105
- addRuntimeExports() {
106
- const userConfig = api.useResolvedConfigContext();
107
- if ((0, _utils.isRouterV5)(userConfig)) {
108
- pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
109
- routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
45
+ };
46
+ },
47
+ validateSchema() {
48
+ return [
49
+ {
50
+ target: "runtime.router",
51
+ schema: {
52
+ type: [
53
+ "boolean",
54
+ "object"
55
+ ]
56
+ }
110
57
  }
58
+ ];
59
+ },
60
+ modifyEntryImports({ entrypoint, imports }) {
61
+ const { entryName, isMainEntry } = entrypoint;
62
+ const userConfig = api.useResolvedConfigContext();
63
+ const { packageName } = api.useAppContext();
64
+ const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
65
+ runtimeConfigMap.set(entryName, runtimeConfig);
66
+ if ((0, _utils.isRouterV5)(userConfig)) {
67
+ imports.push({
68
+ value: "@modern-js/runtime/plugins",
69
+ specifiers: [
70
+ {
71
+ imported: PLUGIN_IDENTIFIER
72
+ }
73
+ ]
74
+ });
75
+ } else {
76
+ throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
77
+ }
78
+ return {
79
+ entrypoint,
80
+ imports
81
+ };
82
+ },
83
+ modifyEntryRuntimePlugins({ entrypoint, plugins }) {
84
+ const { entryName, fileSystemRoutes } = entrypoint;
85
+ const { serverRoutes } = api.useAppContext();
86
+ const runtimeConfig = runtimeConfigMap.get(entryName);
87
+ const userConfig = api.useResolvedConfigContext();
88
+ if ((0, _utils.isRouterV5)(userConfig)) {
89
+ const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
90
+ plugins.push({
91
+ name: PLUGIN_IDENTIFIER,
92
+ options: JSON.stringify({
93
+ serverBase,
94
+ ...runtimeConfig.router,
95
+ routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
96
+ }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
97
+ });
111
98
  }
112
- };
113
- }
114
- };
115
- };
99
+ return {
100
+ entrypoint,
101
+ plugins
102
+ };
103
+ },
104
+ addRuntimeExports() {
105
+ const userConfig = api.useResolvedConfigContext();
106
+ if ((0, _utils.isRouterV5)(userConfig)) {
107
+ var _routerExportsUtils;
108
+ pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
109
+ (_routerExportsUtils = routerExportsUtils) === null || _routerExportsUtils === void 0 ? void 0 : _routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
110
+ }
111
+ }
112
+ };
113
+ }
114
+ });
116
115
  const _default = routerPlugin;
@@ -20,7 +20,8 @@ const _runtimebrowser = require("@modern-js/utils/runtime-browser");
20
20
  const _utils = require("./utils");
21
21
  const _hooks = require("./hooks");
22
22
  const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
23
- const originRoutes = (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.routes) || [];
23
+ var _routesConfig;
24
+ const originRoutes = ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.routes) || [];
24
25
  const isBrow = (0, _runtime.isBrowser)();
25
26
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
26
27
  if (isBrow) {
@@ -43,6 +44,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
43
44
  });
44
45
  },
45
46
  hoc: ({ App }, next) => {
47
+ var _routesConfig2;
46
48
  const getRouteApp = () => {
47
49
  if (isBrow) {
48
50
  var _window__SERVER_DATA;
@@ -66,12 +68,13 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
66
68
  };
67
69
  }
68
70
  return (props) => {
71
+ var _ssrContext, _ssrContext1, _request;
69
72
  const runtimeContext = (0, _react.useContext)(_runtime.RuntimeReactContext);
70
73
  const { ssrContext } = runtimeContext;
71
74
  const location1 = (0, _utils.getLocation)(ssrContext);
72
- const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
73
- const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
74
- const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
75
+ const routerContext = ((_ssrContext = ssrContext) === null || _ssrContext === void 0 ? void 0 : _ssrContext.redirection) || {};
76
+ const request = (_ssrContext1 = ssrContext) === null || _ssrContext1 === void 0 ? void 0 : _ssrContext1.request;
77
+ const baseUrl = (_request = request) === null || _request === void 0 ? void 0 : _request.baseUrl;
75
78
  const basename = baseUrl === "/" ? (0, _utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
76
79
  const runner = api.useHookRunners();
77
80
  const routes = runner.modifyRoutes(originRoutes);
@@ -94,7 +97,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
94
97
  if (App) {
95
98
  RouteApp = (0, _hoistnonreactstatics.default)(RouteApp, App);
96
99
  }
97
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
100
+ if ((_routesConfig2 = routesConfig) === null || _routesConfig2 === void 0 ? void 0 : _routesConfig2.globalApp) {
98
101
  return next({
99
102
  App: (0, _hoistnonreactstatics.default)(RouteApp, routesConfig.globalApp)
100
103
  });
@@ -33,7 +33,8 @@ function renderRoutes(routesConfig, extraProps = {}) {
33
33
  return null;
34
34
  }
35
35
  const Layout = ({ Component, ...props }) => {
36
- const GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
36
+ var _routesConfig;
37
+ const GlobalLayout = (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp;
37
38
  if (!GlobalLayout) {
38
39
  return /* @__PURE__ */ (0, _jsxruntime.jsx)(Component, {
39
40
  ...props
@@ -45,8 +46,8 @@ function renderRoutes(routesConfig, extraProps = {}) {
45
46
  });
46
47
  };
47
48
  const findMatchedRoute = (pathname) => {
48
- var _routesConfig_routes;
49
- return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find((route) => {
49
+ var _routesConfig_routes, _routesConfig;
50
+ return (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : (_routesConfig_routes = _routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find((route) => {
50
51
  const info = (0, _reactrouterdom.matchPath)(pathname, {
51
52
  path: route.path,
52
53
  exact: route.exact,
@@ -76,9 +77,9 @@ function renderRoutes(routesConfig, extraProps = {}) {
76
77
  });
77
78
  }
78
79
  function getLocation(serverContext) {
79
- var _url_replace;
80
- const { pathname, url } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
81
- const cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
80
+ var _serverContext, _url_replace, _url;
81
+ const { pathname, url } = ((_serverContext = serverContext) === null || _serverContext === void 0 ? void 0 : _serverContext.request) || {};
82
+ const cleanUrl = (_url = url) === null || _url === void 0 ? void 0 : (_url_replace = _url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
82
83
  const index = (cleanUrl || "").indexOf(pathname);
83
84
  if (index === -1) {
84
85
  return pathname;
@@ -96,8 +96,9 @@ export var routerPlugin = function() {
96
96
  addRuntimeExports: function addRuntimeExports() {
97
97
  var userConfig = api.useResolvedConfigContext();
98
98
  if (isV5(userConfig)) {
99
+ var _routerExportsUtils;
99
100
  pluginsExportsUtils.addExport("export { default as router } from '@modern-js/plugin-router-v5/runtime'");
100
- routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport("export * from '@modern-js/plugin-router-v5/runtime'");
101
+ (_routerExportsUtils = routerExportsUtils) === null || _routerExportsUtils === void 0 ? void 0 : _routerExportsUtils.addExport("export * from '@modern-js/plugin-router-v5/runtime'");
101
102
  }
102
103
  }
103
104
  };
@@ -11,7 +11,8 @@ import { renderRoutes, getLocation, urlJoin } from "./utils";
11
11
  import { modifyRoutesHook } from "./hooks";
12
12
  export var routerPlugin = function(param) {
13
13
  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;
14
- var originRoutes = (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.routes) || [];
14
+ var _routesConfig;
15
+ var originRoutes = ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.routes) || [];
15
16
  var isBrow = isBrowser();
16
17
  var select = function(pathname) {
17
18
  return serverBase.find(function(baseUrl) {
@@ -40,6 +41,7 @@ export var routerPlugin = function(param) {
40
41
  },
41
42
  hoc: function(param2, next) {
42
43
  var App = param2.App;
44
+ var _routesConfig2;
43
45
  var getRouteApp = function() {
44
46
  if (isBrow) {
45
47
  var _window__SERVER_DATA;
@@ -61,12 +63,13 @@ export var routerPlugin = function(param) {
61
63
  };
62
64
  }
63
65
  return function(props) {
66
+ var _ssrContext, _ssrContext1, _request;
64
67
  var runtimeContext = useContext(RuntimeReactContext);
65
68
  var ssrContext = runtimeContext.ssrContext;
66
69
  var _$location = getLocation(ssrContext);
67
- var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
68
- var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
69
- var baseUrl2 = request === null || request === void 0 ? void 0 : request.baseUrl;
70
+ var routerContext = ((_ssrContext = ssrContext) === null || _ssrContext === void 0 ? void 0 : _ssrContext.redirection) || {};
71
+ var request = (_ssrContext1 = ssrContext) === null || _ssrContext1 === void 0 ? void 0 : _ssrContext1.request;
72
+ var baseUrl2 = (_request = request) === null || _request === void 0 ? void 0 : _request.baseUrl;
70
73
  var basename = baseUrl2 === "/" ? urlJoin(baseUrl2, historyOptions.basename) : baseUrl2;
71
74
  var runner = api.useHookRunners();
72
75
  var routes = runner.modifyRoutes(originRoutes);
@@ -87,7 +90,7 @@ export var routerPlugin = function(param) {
87
90
  if (App) {
88
91
  RouteApp = hoistNonReactStatics(RouteApp, App);
89
92
  }
90
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
93
+ if ((_routesConfig2 = routesConfig) === null || _routesConfig2 === void 0 ? void 0 : _routesConfig2.globalApp) {
91
94
  return next({
92
95
  App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
93
96
  });
@@ -12,7 +12,8 @@ export function renderRoutes(routesConfig) {
12
12
  var Component = _param.Component, props = _object_without_properties(_param, [
13
13
  "Component"
14
14
  ]);
15
- var GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
15
+ var _routesConfig;
16
+ var GlobalLayout = (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp;
16
17
  if (!GlobalLayout) {
17
18
  return /* @__PURE__ */ _jsx(Component, _object_spread({}, props));
18
19
  }
@@ -21,8 +22,8 @@ export function renderRoutes(routesConfig) {
21
22
  }, props));
22
23
  };
23
24
  var findMatchedRoute = function(pathname) {
24
- var _routesConfig_routes;
25
- return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find(function(route) {
25
+ var _routesConfig_routes, _routesConfig;
26
+ return (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : (_routesConfig_routes = _routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find(function(route) {
26
27
  var info = matchPath(pathname, {
27
28
  path: route.path,
28
29
  exact: route.exact,
@@ -52,9 +53,9 @@ export function renderRoutes(routesConfig) {
52
53
  });
53
54
  }
54
55
  export function getLocation(serverContext) {
55
- var _url_replace;
56
- var _ref = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {}, pathname = _ref.pathname, url = _ref.url;
57
- 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://", "");
56
+ var _serverContext, _url_replace, _url;
57
+ var _ref = ((_serverContext = serverContext) === null || _serverContext === void 0 ? void 0 : _serverContext.request) || {}, pathname = _ref.pathname, url = _ref.url;
58
+ var cleanUrl = (_url = url) === null || _url === void 0 ? void 0 : (_url_replace = _url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
58
59
  var index = (cleanUrl || "").indexOf(pathname);
59
60
  if (index === -1) {
60
61
  return pathname;
@@ -2,96 +2,95 @@ import { getEntryOptions, createRuntimeExportsUtils, isRouterV5 as isV5 } from "
2
2
  import "./types";
3
3
  const PLUGIN_IDENTIFIER = "router";
4
4
  const ROUTES_IDENTIFIER = "routes";
5
- export const routerPlugin = () => {
6
- return {
7
- name: "@modern-js/plugin-router-v5",
8
- required: [
9
- "@modern-js/runtime"
10
- ],
11
- setup: (api) => {
12
- const runtimeConfigMap = /* @__PURE__ */ new Map();
13
- let pluginsExportsUtils;
14
- let routerExportsUtils;
15
- return {
16
- config() {
17
- const appContext = api.useAppContext();
18
- pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
19
- routerExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "router");
20
- return {
21
- source: {
22
- alias: {
23
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
24
- "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
25
- }
26
- }
27
- };
28
- },
29
- validateSchema() {
30
- return [
31
- {
32
- target: "runtime.router",
33
- schema: {
34
- type: [
35
- "boolean",
36
- "object"
37
- ]
38
- }
5
+ export const routerPlugin = () => ({
6
+ name: "@modern-js/plugin-router-v5",
7
+ required: [
8
+ "@modern-js/runtime"
9
+ ],
10
+ setup: (api) => {
11
+ const runtimeConfigMap = /* @__PURE__ */ new Map();
12
+ let pluginsExportsUtils;
13
+ let routerExportsUtils;
14
+ return {
15
+ config() {
16
+ const appContext = api.useAppContext();
17
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
18
+ routerExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "router");
19
+ return {
20
+ source: {
21
+ alias: {
22
+ "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
23
+ "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
39
24
  }
40
- ];
41
- },
42
- modifyEntryImports({ entrypoint, imports }) {
43
- const { entryName, isMainEntry } = entrypoint;
44
- const userConfig = api.useResolvedConfigContext();
45
- const { packageName } = api.useAppContext();
46
- const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
47
- runtimeConfigMap.set(entryName, runtimeConfig);
48
- if (isV5(userConfig)) {
49
- imports.push({
50
- value: "@modern-js/runtime/plugins",
51
- specifiers: [
52
- {
53
- imported: PLUGIN_IDENTIFIER
54
- }
55
- ]
56
- });
57
- } else {
58
- throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
59
- }
60
- return {
61
- entrypoint,
62
- imports
63
- };
64
- },
65
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
66
- const { entryName, fileSystemRoutes } = entrypoint;
67
- const { serverRoutes } = api.useAppContext();
68
- const runtimeConfig = runtimeConfigMap.get(entryName);
69
- const userConfig = api.useResolvedConfigContext();
70
- if (isV5(userConfig)) {
71
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
72
- plugins.push({
73
- name: PLUGIN_IDENTIFIER,
74
- options: JSON.stringify({
75
- serverBase,
76
- ...runtimeConfig.router,
77
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
78
- }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
79
- });
80
25
  }
81
- return {
82
- entrypoint,
83
- plugins
84
- };
85
- },
86
- addRuntimeExports() {
87
- const userConfig = api.useResolvedConfigContext();
88
- if (isV5(userConfig)) {
89
- pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
90
- routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
26
+ };
27
+ },
28
+ validateSchema() {
29
+ return [
30
+ {
31
+ target: "runtime.router",
32
+ schema: {
33
+ type: [
34
+ "boolean",
35
+ "object"
36
+ ]
37
+ }
91
38
  }
39
+ ];
40
+ },
41
+ modifyEntryImports({ entrypoint, imports }) {
42
+ const { entryName, isMainEntry } = entrypoint;
43
+ const userConfig = api.useResolvedConfigContext();
44
+ const { packageName } = api.useAppContext();
45
+ const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
46
+ runtimeConfigMap.set(entryName, runtimeConfig);
47
+ if (isV5(userConfig)) {
48
+ imports.push({
49
+ value: "@modern-js/runtime/plugins",
50
+ specifiers: [
51
+ {
52
+ imported: PLUGIN_IDENTIFIER
53
+ }
54
+ ]
55
+ });
56
+ } else {
57
+ throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
58
+ }
59
+ return {
60
+ entrypoint,
61
+ imports
62
+ };
63
+ },
64
+ modifyEntryRuntimePlugins({ entrypoint, plugins }) {
65
+ const { entryName, fileSystemRoutes } = entrypoint;
66
+ const { serverRoutes } = api.useAppContext();
67
+ const runtimeConfig = runtimeConfigMap.get(entryName);
68
+ const userConfig = api.useResolvedConfigContext();
69
+ if (isV5(userConfig)) {
70
+ const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
71
+ plugins.push({
72
+ name: PLUGIN_IDENTIFIER,
73
+ options: JSON.stringify({
74
+ serverBase,
75
+ ...runtimeConfig.router,
76
+ routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
77
+ }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
78
+ });
79
+ }
80
+ return {
81
+ entrypoint,
82
+ plugins
83
+ };
84
+ },
85
+ addRuntimeExports() {
86
+ const userConfig = api.useResolvedConfigContext();
87
+ if (isV5(userConfig)) {
88
+ var _routerExportsUtils;
89
+ pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
90
+ (_routerExportsUtils = routerExportsUtils) === null || _routerExportsUtils === void 0 ? void 0 : _routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
92
91
  }
93
- };
94
- }
95
- };
96
- };
92
+ }
93
+ };
94
+ }
95
+ });
97
96
  export default routerPlugin;
@@ -8,7 +8,8 @@ import { parsedJSONFromElement } from "@modern-js/utils/runtime-browser";
8
8
  import { renderRoutes, getLocation, urlJoin } from "./utils";
9
9
  import { modifyRoutesHook } from "./hooks";
10
10
  export const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
11
- const originRoutes = (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.routes) || [];
11
+ var _routesConfig;
12
+ const originRoutes = ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.routes) || [];
12
13
  const isBrow = isBrowser();
13
14
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
14
15
  if (isBrow) {
@@ -31,6 +32,7 @@ export const routerPlugin = ({ serverBase = [], history: customHistory, supportH
31
32
  });
32
33
  },
33
34
  hoc: ({ App }, next) => {
35
+ var _routesConfig2;
34
36
  const getRouteApp = () => {
35
37
  if (isBrow) {
36
38
  var _window__SERVER_DATA;
@@ -54,12 +56,13 @@ export const routerPlugin = ({ serverBase = [], history: customHistory, supportH
54
56
  };
55
57
  }
56
58
  return (props) => {
59
+ var _ssrContext, _ssrContext1, _request;
57
60
  const runtimeContext = useContext(RuntimeReactContext);
58
61
  const { ssrContext } = runtimeContext;
59
62
  const location1 = getLocation(ssrContext);
60
- const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
61
- const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
62
- const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
63
+ const routerContext = ((_ssrContext = ssrContext) === null || _ssrContext === void 0 ? void 0 : _ssrContext.redirection) || {};
64
+ const request = (_ssrContext1 = ssrContext) === null || _ssrContext1 === void 0 ? void 0 : _ssrContext1.request;
65
+ const baseUrl = (_request = request) === null || _request === void 0 ? void 0 : _request.baseUrl;
63
66
  const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
64
67
  const runner = api.useHookRunners();
65
68
  const routes = runner.modifyRoutes(originRoutes);
@@ -82,7 +85,7 @@ export const routerPlugin = ({ serverBase = [], history: customHistory, supportH
82
85
  if (App) {
83
86
  RouteApp = hoistNonReactStatics(RouteApp, App);
84
87
  }
85
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
88
+ if ((_routesConfig2 = routesConfig) === null || _routesConfig2 === void 0 ? void 0 : _routesConfig2.globalApp) {
86
89
  return next({
87
90
  App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
88
91
  });
@@ -6,7 +6,8 @@ export function renderRoutes(routesConfig, extraProps = {}) {
6
6
  return null;
7
7
  }
8
8
  const Layout = ({ Component, ...props }) => {
9
- const GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
9
+ var _routesConfig;
10
+ const GlobalLayout = (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp;
10
11
  if (!GlobalLayout) {
11
12
  return /* @__PURE__ */ _jsx(Component, {
12
13
  ...props
@@ -18,8 +19,8 @@ export function renderRoutes(routesConfig, extraProps = {}) {
18
19
  });
19
20
  };
20
21
  const findMatchedRoute = (pathname) => {
21
- var _routesConfig_routes;
22
- return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig_routes = routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find((route) => {
22
+ var _routesConfig_routes, _routesConfig;
23
+ return (_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : (_routesConfig_routes = _routesConfig.routes) === null || _routesConfig_routes === void 0 ? void 0 : _routesConfig_routes.find((route) => {
23
24
  const info = matchPath(pathname, {
24
25
  path: route.path,
25
26
  exact: route.exact,
@@ -49,9 +50,9 @@ export function renderRoutes(routesConfig, extraProps = {}) {
49
50
  });
50
51
  }
51
52
  export function getLocation(serverContext) {
52
- var _url_replace;
53
- const { pathname, url } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
54
- const cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
53
+ var _serverContext, _url_replace, _url;
54
+ const { pathname, url } = ((_serverContext = serverContext) === null || _serverContext === void 0 ? void 0 : _serverContext.request) || {};
55
+ const cleanUrl = (_url = url) === null || _url === void 0 ? void 0 : (_url_replace = _url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
55
56
  const index = (cleanUrl || "").indexOf(pathname);
56
57
  if (index === -1) {
57
58
  return pathname;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.26.0",
18
+ "version": "2.28.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/cli/index.d.ts",
21
21
  "main": "./dist/cjs/cli/index.js",
@@ -60,9 +60,9 @@
60
60
  "hoist-non-react-statics": "^3.3.2",
61
61
  "react-router-dom": "^5.3.4",
62
62
  "@swc/helpers": "0.5.1",
63
- "@modern-js/plugin": "2.26.0",
64
- "@modern-js/types": "2.26.0",
65
- "@modern-js/utils": "2.26.0"
63
+ "@modern-js/plugin": "2.28.0",
64
+ "@modern-js/types": "2.28.0",
65
+ "@modern-js/utils": "2.28.0"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "react": ">=17",
@@ -78,15 +78,14 @@
78
78
  "react-dom": "^18",
79
79
  "ts-jest": "^29.1.0",
80
80
  "typescript": "^5",
81
- "@modern-js/app-tools": "2.26.0",
82
- "@modern-js/core": "2.26.0",
83
- "@modern-js/runtime": "2.26.0",
84
- "@modern-js/utils": "2.26.0",
85
- "@scripts/build": "2.26.0",
86
- "@scripts/jest-config": "2.26.0"
81
+ "@modern-js/app-tools": "2.28.0",
82
+ "@modern-js/core": "2.28.0",
83
+ "@modern-js/runtime": "2.28.0",
84
+ "@modern-js/utils": "2.28.0",
85
+ "@scripts/build": "2.28.0",
86
+ "@scripts/jest-config": "2.28.0"
87
87
  },
88
88
  "sideEffects": false,
89
- "modernConfig": {},
90
89
  "publishConfig": {
91
90
  "registry": "https://registry.npmjs.org/",
92
91
  "access": "public",