@modern-js/plugin-router-v5 2.54.6 → 2.55.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.
@@ -24,80 +24,55 @@ __export(cli_exports, {
24
24
  module.exports = __toCommonJS(cli_exports);
25
25
  var import_utils = require("@modern-js/utils");
26
26
  var import_types = require("./types");
27
- const PLUGIN_IDENTIFIER = "router";
28
- const ROUTES_IDENTIFIER = "routes";
29
27
  const routerPlugin = () => ({
30
28
  name: "@modern-js/plugin-router-v5",
31
29
  required: [
32
30
  "@modern-js/runtime"
33
31
  ],
34
32
  setup: (api) => {
35
- const runtimeConfigMap = /* @__PURE__ */ new Map();
36
- let pluginsExportsUtils;
37
33
  let routerExportsUtils;
38
34
  return {
39
- config() {
40
- const appContext = api.useAppContext();
41
- pluginsExportsUtils = (0, import_utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
42
- routerExportsUtils = (0, import_utils.createRuntimeExportsUtils)(appContext.internalDirectory, "router");
43
- return {
44
- source: {
45
- alias: {
46
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
47
- "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
48
- }
49
- }
50
- };
51
- },
52
- modifyEntryImports({ entrypoint, imports }) {
53
- const { entryName, isMainEntry } = entrypoint;
35
+ _internalRuntimePlugins({ entrypoint, plugins }) {
54
36
  const userConfig = api.useResolvedConfigContext();
55
- const { packageName } = api.useAppContext();
56
- const runtimeConfig = (0, import_utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
57
- runtimeConfigMap.set(entryName, runtimeConfig);
37
+ const { serverRoutes, metaName, packageName } = api.useAppContext();
58
38
  if ((0, import_utils.isRouterV5)(userConfig)) {
59
- imports.push({
60
- value: "@modern-js/runtime/plugins",
61
- specifiers: [
62
- {
63
- imported: PLUGIN_IDENTIFIER
64
- }
65
- ]
39
+ var _getEntryOptions;
40
+ const routerConfig = (_getEntryOptions = (0, import_utils.getEntryOptions)(entrypoint.entryName, entrypoint.isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName)) === null || _getEntryOptions === void 0 ? void 0 : _getEntryOptions.router;
41
+ const serverBase = serverRoutes.filter((route) => route.entryName === entrypoint.entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
42
+ plugins.push({
43
+ name: "router",
44
+ path: `@${metaName}/plugin-router-v5/runtime`,
45
+ config: typeof routerConfig === "boolean" ? {
46
+ serverBase
47
+ } : {
48
+ ...routerConfig,
49
+ serverBase
50
+ }
66
51
  });
67
- } else {
68
- throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
69
52
  }
70
53
  return {
71
54
  entrypoint,
72
- imports
55
+ plugins
73
56
  };
74
57
  },
75
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
76
- const { entryName, fileSystemRoutes } = entrypoint;
77
- const { serverRoutes } = api.useAppContext();
78
- const runtimeConfig = runtimeConfigMap.get(entryName);
79
- const userConfig = api.useResolvedConfigContext();
80
- if ((0, import_utils.isRouterV5)(userConfig)) {
81
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
82
- plugins.push({
83
- name: PLUGIN_IDENTIFIER,
84
- options: JSON.stringify({
85
- serverBase,
86
- ...runtimeConfig.router,
87
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
88
- }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
89
- });
90
- }
58
+ config() {
59
+ const { internalDirectory, metaName } = api.useAppContext();
60
+ routerExportsUtils = (0, import_utils.createRuntimeExportsUtils)(internalDirectory, "router");
91
61
  return {
92
- entrypoint,
93
- plugins
62
+ source: {
63
+ alias: {
64
+ [`@${metaName}/runtime/router-v5`]: routerExportsUtils.getPath()
65
+ }
66
+ }
94
67
  };
95
68
  },
96
69
  addRuntimeExports() {
97
70
  const userConfig = api.useResolvedConfigContext();
71
+ const { internalDirectory, metaName } = api.useAppContext();
72
+ const pluginsExportsUtils = (0, import_utils.createRuntimeExportsUtils)(internalDirectory, "plugins");
98
73
  if ((0, import_utils.isRouterV5)(userConfig)) {
99
- 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'`);
74
+ pluginsExportsUtils.addExport(`export { default as router } from '@${metaName}/plugin-router-v5/runtime'`);
75
+ routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@${metaName}/plugin-router-v5/runtime'`);
101
76
  }
102
77
  }
103
78
  };
@@ -19,15 +19,18 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
  var runtime_exports = {};
21
21
  __export(runtime_exports, {
22
- default: () => runtime_default
22
+ default: () => runtime_default,
23
+ routerPlugin: () => import_plugin2.routerPlugin
23
24
  });
24
25
  module.exports = __toCommonJS(runtime_exports);
25
26
  var import_plugin = require("./plugin");
27
+ var import_plugin2 = require("./plugin");
26
28
  __reExport(runtime_exports, require("react-router-dom"), module.exports);
27
29
  __reExport(runtime_exports, require("history"), module.exports);
28
30
  var runtime_default = import_plugin.routerPlugin;
29
31
  // Annotate the CommonJS export names for ESM import in node:
30
32
  0 && (module.exports = {
33
+ routerPlugin,
31
34
  ...require("react-router-dom"),
32
35
  ...require("history")
33
36
  });
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
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.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var plugin_exports = {};
30
20
  __export(plugin_exports, {
@@ -35,13 +25,18 @@ var import_jsx_runtime = require("react/jsx-runtime");
35
25
  var import_react = require("react");
36
26
  var import_history = require("history");
37
27
  var import_react_router_dom = require("react-router-dom");
38
- var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
39
28
  var import_runtime = require("@modern-js/runtime");
40
29
  var import_parsed = require("@modern-js/runtime-utils/parsed");
30
+ var import_context = require("@modern-js/runtime/context");
41
31
  var import_utils = require("./utils");
42
32
  var import_hooks = require("./hooks");
43
33
  const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
44
- const originRoutes = (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.routes) || [];
34
+ const finalRouteConfig = {
35
+ routes: (0, import_context.getGlobalRoutes)(),
36
+ globalApp: (0, import_context.getGlobalLayoutApp)(),
37
+ ...routesConfig
38
+ };
39
+ const originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
45
40
  const isBrow = (0, import_runtime.isBrowser)();
46
41
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
47
42
  let routes = [];
@@ -73,61 +68,50 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
73
68
  hoc: ({ App, config }, next) => {
74
69
  const getRouteApp = () => {
75
70
  if (isBrow) {
76
- var _window__SERVER_DATA;
77
- const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
78
- historyOptions.basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
71
+ var _config_router, _window__SERVER_DATA;
72
+ const baseUrl = (((_config_router = config.router) === null || _config_router === void 0 ? void 0 : _config_router.basename) || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
73
+ const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
74
+ historyOptions.basename = basename;
79
75
  const history = customHistory || (supportHtml5History ? (0, import_history.createBrowserHistory)(historyOptions) : (0, import_history.createHashHistory)(historyOptions));
80
76
  return (props) => {
81
77
  const runner = api.useHookRunners();
82
78
  routes = runner.modifyRoutes(originRoutes);
83
- routesConfig && (routesConfig.routes = routes);
79
+ finalRouteConfig && (finalRouteConfig.routes = routes);
84
80
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Router, {
85
81
  history,
86
82
  children: createRoutes ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
87
- ...props,
88
83
  Component: createRoutes()
89
- }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
90
- ...props,
91
- children: (0, import_utils.renderRoutes)(routesConfig, props)
92
- })
84
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
85
+ ...props
86
+ }) : (0, import_utils.renderRoutes)(finalRouteConfig, props)
93
87
  });
94
88
  };
95
89
  }
96
90
  return (props) => {
91
+ var _this, _config_router2;
97
92
  const runtimeContext = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
98
93
  const { ssrContext } = runtimeContext;
99
94
  const location1 = (0, import_utils.getLocation)(ssrContext);
100
95
  const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
101
96
  const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
102
- const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
97
+ const baseUrl = (_this = ((_config_router2 = config.router) === null || _config_router2 === void 0 ? void 0 : _config_router2.basename) || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
103
98
  const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
104
99
  const runner = api.useHookRunners();
105
100
  const routes2 = runner.modifyRoutes(originRoutes);
106
- routesConfig && (routesConfig.routes = routes2);
101
+ finalRouteConfig && (finalRouteConfig.routes = routes2);
107
102
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.StaticRouter, {
108
103
  basename: basename === "/" ? "" : basename,
109
104
  location: location1,
110
105
  context: routerContext,
111
106
  children: createRoutes ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
112
- ...props,
113
107
  Component: createRoutes()
114
- }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
115
- ...props,
116
- children: (0, import_utils.renderRoutes)(routesConfig, props)
117
- })
108
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
109
+ ...props
110
+ }) : (0, import_utils.renderRoutes)(finalRouteConfig, props)
118
111
  });
119
112
  };
120
113
  };
121
- let RouteApp = getRouteApp();
122
- if (App) {
123
- RouteApp = (0, import_hoist_non_react_statics.default)(RouteApp, App);
124
- }
125
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
126
- return next({
127
- App: (0, import_hoist_non_react_statics.default)(RouteApp, routesConfig.globalApp),
128
- config
129
- });
130
- }
114
+ const RouteApp = getRouteApp();
131
115
  return next({
132
116
  App: RouteApp,
133
117
  config
@@ -1,9 +1,8 @@
1
+ import { _ as _define_property } from "@swc/helpers/_/_define_property";
1
2
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
- import { getEntryOptions, createRuntimeExportsUtils, isRouterV5 as isV5 } from "@modern-js/utils";
4
+ import { createRuntimeExportsUtils, getEntryOptions, isRouterV5 as isV5 } from "@modern-js/utils";
4
5
  import "./types";
5
- var PLUGIN_IDENTIFIER = "router";
6
- var ROUTES_IDENTIFIER = "routes";
7
6
  var routerPlugin = function() {
8
7
  return {
9
8
  name: "@modern-js/plugin-router-v5",
@@ -11,68 +10,30 @@ var routerPlugin = function() {
11
10
  "@modern-js/runtime"
12
11
  ],
13
12
  setup: function(api) {
14
- var runtimeConfigMap = /* @__PURE__ */ new Map();
15
- var pluginsExportsUtils;
16
13
  var routerExportsUtils;
17
14
  return {
18
- config: function config() {
19
- var appContext = api.useAppContext();
20
- pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
21
- routerExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "router");
22
- return {
23
- source: {
24
- alias: {
25
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath(),
26
- "@modern-js/runtime/router-v5": routerExportsUtils.getPath()
27
- }
28
- }
29
- };
30
- },
31
- modifyEntryImports: function modifyEntryImports(param) {
32
- var entrypoint = param.entrypoint, imports = param.imports;
33
- var entryName = entrypoint.entryName, isMainEntry = entrypoint.isMainEntry;
34
- var userConfig = api.useResolvedConfigContext();
35
- var packageName = api.useAppContext().packageName;
36
- var runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
37
- runtimeConfigMap.set(entryName, runtimeConfig);
38
- if (isV5(userConfig)) {
39
- imports.push({
40
- value: "@modern-js/runtime/plugins",
41
- specifiers: [
42
- {
43
- imported: PLUGIN_IDENTIFIER
44
- }
45
- ]
46
- });
47
- } else {
48
- throw new Error("should enable runtime.router.mode for entry ".concat(entryName));
49
- }
50
- return {
51
- entrypoint,
52
- imports
53
- };
54
- },
55
- modifyEntryRuntimePlugins: function modifyEntryRuntimePlugins(param) {
15
+ _internalRuntimePlugins: function _internalRuntimePlugins(param) {
56
16
  var entrypoint = param.entrypoint, plugins = param.plugins;
57
- var entryName = entrypoint.entryName, fileSystemRoutes = entrypoint.fileSystemRoutes;
58
- var serverRoutes = api.useAppContext().serverRoutes;
59
- var runtimeConfig = runtimeConfigMap.get(entryName);
60
17
  var userConfig = api.useResolvedConfigContext();
18
+ var _api_useAppContext = api.useAppContext(), serverRoutes = _api_useAppContext.serverRoutes, metaName = _api_useAppContext.metaName, packageName = _api_useAppContext.packageName;
61
19
  if (isV5(userConfig)) {
20
+ var _getEntryOptions;
21
+ var routerConfig = (_getEntryOptions = getEntryOptions(entrypoint.entryName, entrypoint.isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName)) === null || _getEntryOptions === void 0 ? void 0 : _getEntryOptions.router;
62
22
  var serverBase = serverRoutes.filter(function(route) {
63
- return route.entryName === entryName;
23
+ return route.entryName === entrypoint.entryName;
64
24
  }).map(function(route) {
65
25
  return route.urlPath;
66
26
  }).sort(function(a, b) {
67
27
  return a.length - b.length > 0 ? -1 : 1;
68
28
  });
69
29
  plugins.push({
70
- name: PLUGIN_IDENTIFIER,
71
- options: JSON.stringify(_object_spread_props(_object_spread({
30
+ name: "router",
31
+ path: "@".concat(metaName, "/plugin-router-v5/runtime"),
32
+ config: typeof routerConfig === "boolean" ? {
72
33
  serverBase
73
- }, runtimeConfig.router), {
74
- routesConfig: fileSystemRoutes ? "{ ".concat(ROUTES_IDENTIFIER, ", globalApp: App }") : void 0
75
- })).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
34
+ } : _object_spread_props(_object_spread({}, routerConfig), {
35
+ serverBase
36
+ })
76
37
  });
77
38
  }
78
39
  return {
@@ -80,11 +41,22 @@ var routerPlugin = function() {
80
41
  plugins
81
42
  };
82
43
  },
44
+ config: function config() {
45
+ var _api_useAppContext = api.useAppContext(), internalDirectory = _api_useAppContext.internalDirectory, metaName = _api_useAppContext.metaName;
46
+ routerExportsUtils = createRuntimeExportsUtils(internalDirectory, "router");
47
+ return {
48
+ source: {
49
+ alias: _define_property({}, "@".concat(metaName, "/runtime/router-v5"), routerExportsUtils.getPath())
50
+ }
51
+ };
52
+ },
83
53
  addRuntimeExports: function addRuntimeExports() {
84
54
  var userConfig = api.useResolvedConfigContext();
55
+ var _api_useAppContext = api.useAppContext(), internalDirectory = _api_useAppContext.internalDirectory, metaName = _api_useAppContext.metaName;
56
+ var pluginsExportsUtils = createRuntimeExportsUtils(internalDirectory, "plugins");
85
57
  if (isV5(userConfig)) {
86
- pluginsExportsUtils.addExport("export { default as router } from '@modern-js/plugin-router-v5/runtime'");
87
- routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport("export * from '@modern-js/plugin-router-v5/runtime'");
58
+ pluginsExportsUtils.addExport("export { default as router } from '@".concat(metaName, "/plugin-router-v5/runtime'"));
59
+ routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport("export * from '@".concat(metaName, "/plugin-router-v5/runtime'"));
88
60
  }
89
61
  }
90
62
  };
@@ -1,7 +1,9 @@
1
1
  import { routerPlugin } from "./plugin";
2
+ import { routerPlugin as routerPlugin2 } from "./plugin";
2
3
  var runtime_default = routerPlugin;
3
4
  export * from "react-router-dom";
4
5
  export * from "history";
5
6
  export {
6
- runtime_default as default
7
+ runtime_default as default,
8
+ routerPlugin2 as routerPlugin
7
9
  };
@@ -1,17 +1,20 @@
1
1
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
2
  import { jsx as _jsx } from "react/jsx-runtime";
4
3
  import { useContext } from "react";
5
4
  import { createBrowserHistory, createHashHistory } from "history";
6
5
  import { Router, StaticRouter, useRouteMatch, useLocation, useHistory } from "react-router-dom";
7
- import hoistNonReactStatics from "hoist-non-react-statics";
8
6
  import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
9
7
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
8
+ import { getGlobalLayoutApp, getGlobalRoutes } from "@modern-js/runtime/context";
10
9
  import { renderRoutes, getLocation, urlJoin } from "./utils";
11
10
  import { modifyRoutesHook } from "./hooks";
12
11
  var routerPlugin = function(param) {
13
12
  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) || [];
13
+ var finalRouteConfig = _object_spread({
14
+ routes: getGlobalRoutes(),
15
+ globalApp: getGlobalLayoutApp()
16
+ }, routesConfig);
17
+ var originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
15
18
  var isBrow = isBrowser();
16
19
  var select = function(pathname) {
17
20
  return serverBase.find(function(baseUrl) {
@@ -49,57 +52,46 @@ var routerPlugin = function(param) {
49
52
  var App = param2.App, config = param2.config;
50
53
  var getRouteApp = function() {
51
54
  if (isBrow) {
52
- var _window__SERVER_DATA;
53
- var baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
54
- historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
55
+ var _config_router, _window__SERVER_DATA;
56
+ var baseUrl = (((_config_router = config.router) === null || _config_router === void 0 ? void 0 : _config_router.basename) || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
57
+ var basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
58
+ historyOptions.basename = basename;
55
59
  var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
56
60
  return function(props) {
57
61
  var runner = api.useHookRunners();
58
62
  routes = runner.modifyRoutes(originRoutes);
59
- routesConfig && (routesConfig.routes = routes);
63
+ finalRouteConfig && (finalRouteConfig.routes = routes);
60
64
  return /* @__PURE__ */ _jsx(Router, {
61
65
  history,
62
- children: createRoutes ? /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
66
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, {
63
67
  Component: createRoutes()
64
- })) : /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
65
- children: renderRoutes(routesConfig, props)
66
- }))
68
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ _jsx(App, _object_spread({}, props)) : renderRoutes(finalRouteConfig, props)
67
69
  });
68
70
  };
69
71
  }
70
72
  return function(props) {
73
+ var _this, _config_router2;
71
74
  var runtimeContext = useContext(RuntimeReactContext);
72
75
  var ssrContext = runtimeContext.ssrContext;
73
76
  var _$location = getLocation(ssrContext);
74
77
  var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
75
78
  var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
76
- var baseUrl2 = request === null || request === void 0 ? void 0 : request.baseUrl;
77
- var basename = baseUrl2 === "/" ? urlJoin(baseUrl2, historyOptions.basename) : baseUrl2;
79
+ var baseUrl2 = (_this = ((_config_router2 = config.router) === null || _config_router2 === void 0 ? void 0 : _config_router2.basename) || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
80
+ var basename2 = baseUrl2 === "/" ? urlJoin(baseUrl2, historyOptions.basename) : baseUrl2;
78
81
  var runner = api.useHookRunners();
79
82
  var routes2 = runner.modifyRoutes(originRoutes);
80
- routesConfig && (routesConfig.routes = routes2);
83
+ finalRouteConfig && (finalRouteConfig.routes = routes2);
81
84
  return /* @__PURE__ */ _jsx(StaticRouter, {
82
- basename: basename === "/" ? "" : basename,
85
+ basename: basename2 === "/" ? "" : basename2,
83
86
  location: _$location,
84
87
  context: routerContext,
85
- children: createRoutes ? /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
88
+ children: createRoutes ? /* @__PURE__ */ _jsx(App, {
86
89
  Component: createRoutes()
87
- })) : /* @__PURE__ */ _jsx(App, _object_spread_props(_object_spread({}, props), {
88
- children: renderRoutes(routesConfig, props)
89
- }))
90
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ _jsx(App, _object_spread({}, props)) : renderRoutes(finalRouteConfig, props)
90
91
  });
91
92
  };
92
93
  };
93
94
  var RouteApp = getRouteApp();
94
- if (App) {
95
- RouteApp = hoistNonReactStatics(RouteApp, App);
96
- }
97
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
98
- return next({
99
- App: hoistNonReactStatics(RouteApp, routesConfig.globalApp),
100
- config
101
- });
102
- }
103
95
  return next({
104
96
  App: RouteApp,
105
97
  config
@@ -1,79 +1,54 @@
1
- import { getEntryOptions, createRuntimeExportsUtils, isRouterV5 as isV5 } from "@modern-js/utils";
1
+ import { createRuntimeExportsUtils, getEntryOptions, isRouterV5 as isV5 } from "@modern-js/utils";
2
2
  import "./types";
3
- const PLUGIN_IDENTIFIER = "router";
4
- const ROUTES_IDENTIFIER = "routes";
5
3
  const routerPlugin = () => ({
6
4
  name: "@modern-js/plugin-router-v5",
7
5
  required: [
8
6
  "@modern-js/runtime"
9
7
  ],
10
8
  setup: (api) => {
11
- const runtimeConfigMap = /* @__PURE__ */ new Map();
12
- let pluginsExportsUtils;
13
9
  let routerExportsUtils;
14
10
  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()
24
- }
25
- }
26
- };
27
- },
28
- modifyEntryImports({ entrypoint, imports }) {
29
- const { entryName, isMainEntry } = entrypoint;
11
+ _internalRuntimePlugins({ entrypoint, plugins }) {
30
12
  const userConfig = api.useResolvedConfigContext();
31
- const { packageName } = api.useAppContext();
32
- const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
33
- runtimeConfigMap.set(entryName, runtimeConfig);
13
+ const { serverRoutes, metaName, packageName } = api.useAppContext();
34
14
  if (isV5(userConfig)) {
35
- imports.push({
36
- value: "@modern-js/runtime/plugins",
37
- specifiers: [
38
- {
39
- imported: PLUGIN_IDENTIFIER
40
- }
41
- ]
15
+ var _getEntryOptions;
16
+ const routerConfig = (_getEntryOptions = getEntryOptions(entrypoint.entryName, entrypoint.isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName)) === null || _getEntryOptions === void 0 ? void 0 : _getEntryOptions.router;
17
+ const serverBase = serverRoutes.filter((route) => route.entryName === entrypoint.entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
18
+ plugins.push({
19
+ name: "router",
20
+ path: `@${metaName}/plugin-router-v5/runtime`,
21
+ config: typeof routerConfig === "boolean" ? {
22
+ serverBase
23
+ } : {
24
+ ...routerConfig,
25
+ serverBase
26
+ }
42
27
  });
43
- } else {
44
- throw new Error(`should enable runtime.router.mode for entry ${entryName}`);
45
28
  }
46
29
  return {
47
30
  entrypoint,
48
- imports
31
+ plugins
49
32
  };
50
33
  },
51
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
52
- const { entryName, fileSystemRoutes } = entrypoint;
53
- const { serverRoutes } = api.useAppContext();
54
- const runtimeConfig = runtimeConfigMap.get(entryName);
55
- const userConfig = api.useResolvedConfigContext();
56
- if (isV5(userConfig)) {
57
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
58
- plugins.push({
59
- name: PLUGIN_IDENTIFIER,
60
- options: JSON.stringify({
61
- serverBase,
62
- ...runtimeConfig.router,
63
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
64
- }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
65
- });
66
- }
34
+ config() {
35
+ const { internalDirectory, metaName } = api.useAppContext();
36
+ routerExportsUtils = createRuntimeExportsUtils(internalDirectory, "router");
67
37
  return {
68
- entrypoint,
69
- plugins
38
+ source: {
39
+ alias: {
40
+ [`@${metaName}/runtime/router-v5`]: routerExportsUtils.getPath()
41
+ }
42
+ }
70
43
  };
71
44
  },
72
45
  addRuntimeExports() {
73
46
  const userConfig = api.useResolvedConfigContext();
47
+ const { internalDirectory, metaName } = api.useAppContext();
48
+ const pluginsExportsUtils = createRuntimeExportsUtils(internalDirectory, "plugins");
74
49
  if (isV5(userConfig)) {
75
- pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/plugin-router-v5/runtime'`);
76
- routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@modern-js/plugin-router-v5/runtime'`);
50
+ pluginsExportsUtils.addExport(`export { default as router } from '@${metaName}/plugin-router-v5/runtime'`);
51
+ routerExportsUtils === null || routerExportsUtils === void 0 ? void 0 : routerExportsUtils.addExport(`export * from '@${metaName}/plugin-router-v5/runtime'`);
77
52
  }
78
53
  }
79
54
  };
@@ -1,7 +1,9 @@
1
1
  import { routerPlugin } from "./plugin";
2
+ import { routerPlugin as routerPlugin2 } from "./plugin";
2
3
  var runtime_default = routerPlugin;
3
4
  export * from "react-router-dom";
4
5
  export * from "history";
5
6
  export {
6
- runtime_default as default
7
+ runtime_default as default,
8
+ routerPlugin2 as routerPlugin
7
9
  };
@@ -2,13 +2,18 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { createBrowserHistory, createHashHistory } from "history";
4
4
  import { Router, StaticRouter, useRouteMatch, useLocation, useHistory } from "react-router-dom";
5
- import hoistNonReactStatics from "hoist-non-react-statics";
6
5
  import { RuntimeReactContext, isBrowser } from "@modern-js/runtime";
7
6
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
7
+ import { getGlobalLayoutApp, getGlobalRoutes } from "@modern-js/runtime/context";
8
8
  import { renderRoutes, getLocation, urlJoin } from "./utils";
9
9
  import { modifyRoutesHook } from "./hooks";
10
10
  const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
11
- const originRoutes = (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.routes) || [];
11
+ const finalRouteConfig = {
12
+ routes: getGlobalRoutes(),
13
+ globalApp: getGlobalLayoutApp(),
14
+ ...routesConfig
15
+ };
16
+ const originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
12
17
  const isBrow = isBrowser();
13
18
  const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
14
19
  let routes = [];
@@ -40,61 +45,50 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
40
45
  hoc: ({ App, config }, next) => {
41
46
  const getRouteApp = () => {
42
47
  if (isBrow) {
43
- var _window__SERVER_DATA;
44
- const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
45
- historyOptions.basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
48
+ var _config_router, _window__SERVER_DATA;
49
+ const baseUrl = (((_config_router = config.router) === null || _config_router === void 0 ? void 0 : _config_router.basename) || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
50
+ const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
51
+ historyOptions.basename = basename;
46
52
  const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
47
53
  return (props) => {
48
54
  const runner = api.useHookRunners();
49
55
  routes = runner.modifyRoutes(originRoutes);
50
- routesConfig && (routesConfig.routes = routes);
56
+ finalRouteConfig && (finalRouteConfig.routes = routes);
51
57
  return /* @__PURE__ */ _jsx(Router, {
52
58
  history,
53
59
  children: createRoutes ? /* @__PURE__ */ _jsx(App, {
54
- ...props,
55
60
  Component: createRoutes()
56
- }) : /* @__PURE__ */ _jsx(App, {
57
- ...props,
58
- children: renderRoutes(routesConfig, props)
59
- })
61
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ _jsx(App, {
62
+ ...props
63
+ }) : renderRoutes(finalRouteConfig, props)
60
64
  });
61
65
  };
62
66
  }
63
67
  return (props) => {
68
+ var _this, _config_router2;
64
69
  const runtimeContext = useContext(RuntimeReactContext);
65
70
  const { ssrContext } = runtimeContext;
66
71
  const location1 = getLocation(ssrContext);
67
72
  const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
68
73
  const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
69
- const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
74
+ const baseUrl = (_this = ((_config_router2 = config.router) === null || _config_router2 === void 0 ? void 0 : _config_router2.basename) || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
70
75
  const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
71
76
  const runner = api.useHookRunners();
72
77
  const routes2 = runner.modifyRoutes(originRoutes);
73
- routesConfig && (routesConfig.routes = routes2);
78
+ finalRouteConfig && (finalRouteConfig.routes = routes2);
74
79
  return /* @__PURE__ */ _jsx(StaticRouter, {
75
80
  basename: basename === "/" ? "" : basename,
76
81
  location: location1,
77
82
  context: routerContext,
78
83
  children: createRoutes ? /* @__PURE__ */ _jsx(App, {
79
- ...props,
80
84
  Component: createRoutes()
81
- }) : /* @__PURE__ */ _jsx(App, {
82
- ...props,
83
- children: renderRoutes(routesConfig, props)
84
- })
85
+ }) : App && !(finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes) ? /* @__PURE__ */ _jsx(App, {
86
+ ...props
87
+ }) : renderRoutes(finalRouteConfig, props)
85
88
  });
86
89
  };
87
90
  };
88
- let RouteApp = getRouteApp();
89
- if (App) {
90
- RouteApp = hoistNonReactStatics(RouteApp, App);
91
- }
92
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
93
- return next({
94
- App: hoistNonReactStatics(RouteApp, routesConfig.globalApp),
95
- config
96
- });
97
- }
91
+ const RouteApp = getRouteApp();
98
92
  return next({
99
93
  App: RouteApp,
100
94
  config
@@ -1,6 +1,6 @@
1
1
  import { routerPlugin } from './plugin';
2
- import type { SingleRouteConfig, HistoryConfig, RouterConfig } from './plugin';
3
- export type { SingleRouteConfig, HistoryConfig, RouterConfig };
2
+ export type { SingleRouteConfig, HistoryConfig, RouterConfig } from './plugin';
3
+ export { routerPlugin } from './plugin';
4
4
  export default routerPlugin;
5
5
  export * from 'react-router-dom';
6
6
  export * from 'history';
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.54.6",
18
+ "version": "2.55.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",
@@ -57,13 +57,12 @@
57
57
  "@types/history": "^4.7.9",
58
58
  "@types/react-router-dom": "^5.1.8",
59
59
  "history": "^4.7.9",
60
- "hoist-non-react-statics": "^3.3.2",
61
60
  "react-router-dom": "^5.3.4",
62
61
  "@swc/helpers": "0.5.3",
63
- "@modern-js/plugin": "2.54.6",
64
- "@modern-js/runtime-utils": "2.54.6",
65
- "@modern-js/utils": "2.54.6",
66
- "@modern-js/types": "2.54.6"
62
+ "@modern-js/plugin": "2.55.0",
63
+ "@modern-js/utils": "2.55.0",
64
+ "@modern-js/types": "2.55.0",
65
+ "@modern-js/runtime-utils": "2.55.0"
67
66
  },
68
67
  "peerDependencies": {
69
68
  "react": ">=17",
@@ -79,11 +78,11 @@
79
78
  "react-dom": "^18",
80
79
  "ts-jest": "^29.1.0",
81
80
  "typescript": "^5",
82
- "@modern-js/app-tools": "2.54.6",
83
- "@modern-js/core": "2.54.6",
84
- "@modern-js/runtime": "2.54.6",
85
- "@scripts/build": "2.54.6",
86
- "@scripts/jest-config": "2.54.6"
81
+ "@modern-js/app-tools": "2.55.0",
82
+ "@modern-js/core": "2.55.0",
83
+ "@modern-js/runtime": "2.55.0",
84
+ "@scripts/build": "2.55.0",
85
+ "@scripts/jest-config": "2.55.0"
87
86
  },
88
87
  "sideEffects": false,
89
88
  "publishConfig": {
package/types/index.d.ts CHANGED
@@ -9,4 +9,11 @@ declare module '@modern-js/runtime' {
9
9
  interface AppConfig {
10
10
  router?: RouterConfig | boolean;
11
11
  }
12
+
13
+ interface RuntimeConfig {
14
+ router?: RouterConfig;
15
+ routerEntries?: {
16
+ [name: string]: RouterConfig;
17
+ };
18
+ }
12
19
  }