@modern-js/plugin-router-v5 2.56.3-alpha.0 → 2.57.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.
@@ -28,21 +28,11 @@ var import_react_router_dom = require("react-router-dom");
28
28
  var import_runtime = require("@meta/runtime");
29
29
  var import_parsed = require("@modern-js/runtime-utils/parsed");
30
30
  var import_context = require("@meta/runtime/context");
31
+ var import_merge = require("@modern-js/runtime-utils/merge");
31
32
  var import_utils = require("./utils");
32
33
  var import_hooks = require("./hooks");
33
- const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
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;
40
- const isBrow = (0, import_runtime.isBrowser)();
41
- const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
42
- let routes = [];
43
- if (isBrow) {
44
- window._SERVER_DATA = (0, import_parsed.parsedJSONFromElement)("__MODERN_SERVER_DATA__");
45
- }
34
+ let routes = [];
35
+ const routerPlugin = (userConfig = {}) => {
46
36
  return {
47
37
  name: "@modern-js/plugin-router",
48
38
  registerHook: {
@@ -50,7 +40,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
50
40
  },
51
41
  setup: (api) => {
52
42
  return {
53
- init({ context }, next) {
43
+ beforeRender(context) {
54
44
  context.router = {
55
45
  useRouteMatch: import_react_router_dom.useRouteMatch,
56
46
  useLocation: import_react_router_dom.useLocation,
@@ -61,19 +51,30 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
61
51
  return routes;
62
52
  }
63
53
  });
64
- return next({
65
- context
66
- });
67
54
  },
68
- hoc: ({ App, config }, next) => {
55
+ wrapRoot: (App) => {
56
+ const pluginConfig = api.useRuntimeConfigContext();
57
+ const { serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} } = (0, import_merge.merge)(pluginConfig.router || {}, userConfig);
58
+ const finalRouteConfig = {
59
+ routes: (0, import_context.getGlobalRoutes)(),
60
+ globalApp: (0, import_context.getGlobalLayoutApp)(),
61
+ ...routesConfig
62
+ };
63
+ const originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
64
+ const isBrow = (0, import_runtime.isBrowser)();
65
+ const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
66
+ if (isBrow) {
67
+ window._SERVER_DATA = (0, import_parsed.parsedJSONFromElement)("__MODERN_SERVER_DATA__");
68
+ }
69
69
  const getRouteApp = () => {
70
70
  if (isBrow) {
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;
75
- const history = customHistory || (supportHtml5History ? (0, import_history.createBrowserHistory)(historyOptions) : (0, import_history.createHashHistory)(historyOptions));
76
71
  return (props) => {
72
+ var _window__SERVER_DATA;
73
+ const runtimeContext = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
74
+ const baseUrl = (runtimeContext._internalRouterBaseName || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
75
+ const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
76
+ historyOptions.basename = basename;
77
+ const history = customHistory || (supportHtml5History ? (0, import_history.createBrowserHistory)(historyOptions) : (0, import_history.createHashHistory)(historyOptions));
77
78
  const runner = api.useHookRunners();
78
79
  routes = runner.modifyRoutes(originRoutes);
79
80
  finalRouteConfig && (finalRouteConfig.routes = routes);
@@ -88,13 +89,13 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
88
89
  };
89
90
  }
90
91
  return (props) => {
91
- var _this, _config_router2;
92
+ var _this;
92
93
  const runtimeContext = (0, import_react.useContext)(import_runtime.RuntimeReactContext);
93
94
  const { ssrContext } = runtimeContext;
94
95
  const location1 = (0, import_utils.getLocation)(ssrContext);
95
96
  const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
96
97
  const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
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(/^\/*/, "/");
98
+ const baseUrl = (_this = runtimeContext._internalRouterBaseName || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
98
99
  const basename = baseUrl === "/" ? (0, import_utils.urlJoin)(baseUrl, historyOptions.basename) : baseUrl;
99
100
  const runner = api.useHookRunners();
100
101
  const routes2 = runner.modifyRoutes(originRoutes);
@@ -111,11 +112,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
111
112
  });
112
113
  };
113
114
  };
114
- const RouteApp = getRouteApp();
115
- return next({
116
- App: RouteApp,
117
- config
118
- });
115
+ return getRouteApp();
119
116
  }
120
117
  };
121
118
  }
@@ -6,25 +6,12 @@ import { Router, StaticRouter, useRouteMatch, useLocation, useHistory } from "re
6
6
  import { RuntimeReactContext, isBrowser } from "@meta/runtime";
7
7
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
8
8
  import { getGlobalLayoutApp, getGlobalRoutes } from "@meta/runtime/context";
9
+ import { merge } from "@modern-js/runtime-utils/merge";
9
10
  import { renderRoutes, getLocation, urlJoin } from "./utils";
10
11
  import { modifyRoutesHook } from "./hooks";
11
- var routerPlugin = function(param) {
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;
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;
18
- var isBrow = isBrowser();
19
- var select = function(pathname) {
20
- return serverBase.find(function(baseUrl) {
21
- return pathname.search(baseUrl) === 0;
22
- }) || "/";
23
- };
24
- var routes = [];
25
- if (isBrow) {
26
- window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
27
- }
12
+ var routes = [];
13
+ var routerPlugin = function() {
14
+ var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
28
15
  return {
29
16
  name: "@modern-js/plugin-router",
30
17
  registerHook: {
@@ -32,8 +19,7 @@ var routerPlugin = function(param) {
32
19
  },
33
20
  setup: function(api) {
34
21
  return {
35
- init: function init(param2, next) {
36
- var context = param2.context;
22
+ beforeRender: function beforeRender(context) {
37
23
  context.router = {
38
24
  useRouteMatch,
39
25
  useLocation,
@@ -44,20 +30,33 @@ var routerPlugin = function(param) {
44
30
  return routes;
45
31
  }
46
32
  });
47
- return next({
48
- context
49
- });
50
33
  },
51
- hoc: function(param2, next) {
52
- var App = param2.App, config = param2.config;
34
+ wrapRoot: function(App) {
35
+ var pluginConfig = api.useRuntimeConfigContext();
36
+ var _merge = merge(pluginConfig.router || {}, userConfig), _merge_serverBase = _merge.serverBase, serverBase = _merge_serverBase === void 0 ? [] : _merge_serverBase, customHistory = _merge.history, _merge_supportHtml5History = _merge.supportHtml5History, supportHtml5History = _merge_supportHtml5History === void 0 ? true : _merge_supportHtml5History, routesConfig = _merge.routesConfig, createRoutes = _merge.createRoutes, _merge_historyOptions = _merge.historyOptions, historyOptions = _merge_historyOptions === void 0 ? {} : _merge_historyOptions;
37
+ var finalRouteConfig = _object_spread({
38
+ routes: getGlobalRoutes(),
39
+ globalApp: getGlobalLayoutApp()
40
+ }, routesConfig);
41
+ var originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
42
+ var isBrow = isBrowser();
43
+ var select = function(pathname) {
44
+ return serverBase.find(function(baseUrl) {
45
+ return pathname.search(baseUrl) === 0;
46
+ }) || "/";
47
+ };
48
+ if (isBrow) {
49
+ window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
50
+ }
53
51
  var getRouteApp = function() {
54
52
  if (isBrow) {
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;
59
- var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
60
53
  return function(props) {
54
+ var _window__SERVER_DATA;
55
+ var runtimeContext = useContext(RuntimeReactContext);
56
+ var baseUrl = (runtimeContext._internalRouterBaseName || ((_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;
59
+ var history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
61
60
  var runner = api.useHookRunners();
62
61
  routes = runner.modifyRoutes(originRoutes);
63
62
  finalRouteConfig && (finalRouteConfig.routes = routes);
@@ -70,20 +69,20 @@ var routerPlugin = function(param) {
70
69
  };
71
70
  }
72
71
  return function(props) {
73
- var _this, _config_router2;
72
+ var _this;
74
73
  var runtimeContext = useContext(RuntimeReactContext);
75
74
  var ssrContext = runtimeContext.ssrContext;
76
- var _$location = getLocation(ssrContext);
75
+ var location1 = getLocation(ssrContext);
77
76
  var routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
78
77
  var request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
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
+ var baseUrl = (_this = runtimeContext._internalRouterBaseName || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
79
+ var basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
81
80
  var runner = api.useHookRunners();
82
81
  var routes2 = runner.modifyRoutes(originRoutes);
83
82
  finalRouteConfig && (finalRouteConfig.routes = routes2);
84
83
  return /* @__PURE__ */ _jsx(StaticRouter, {
85
- basename: basename2 === "/" ? "" : basename2,
86
- location: _$location,
84
+ basename: basename === "/" ? "" : basename,
85
+ location: location1,
87
86
  context: routerContext,
88
87
  children: createRoutes ? /* @__PURE__ */ _jsx(App, {
89
88
  Component: createRoutes()
@@ -91,11 +90,7 @@ var routerPlugin = function(param) {
91
90
  });
92
91
  };
93
92
  };
94
- var RouteApp = getRouteApp();
95
- return next({
96
- App: RouteApp,
97
- config
98
- });
93
+ return getRouteApp();
99
94
  }
100
95
  };
101
96
  }
@@ -5,21 +5,11 @@ import { Router, StaticRouter, useRouteMatch, useLocation, useHistory } from "re
5
5
  import { RuntimeReactContext, isBrowser } from "@meta/runtime";
6
6
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
7
7
  import { getGlobalLayoutApp, getGlobalRoutes } from "@meta/runtime/context";
8
+ import { merge } from "@modern-js/runtime-utils/merge";
8
9
  import { renderRoutes, getLocation, urlJoin } from "./utils";
9
10
  import { modifyRoutesHook } from "./hooks";
10
- const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} }) => {
11
- const finalRouteConfig = {
12
- routes: getGlobalRoutes(),
13
- globalApp: getGlobalLayoutApp(),
14
- ...routesConfig
15
- };
16
- const originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
17
- const isBrow = isBrowser();
18
- const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
19
- let routes = [];
20
- if (isBrow) {
21
- window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
22
- }
11
+ let routes = [];
12
+ const routerPlugin = (userConfig = {}) => {
23
13
  return {
24
14
  name: "@modern-js/plugin-router",
25
15
  registerHook: {
@@ -27,7 +17,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
27
17
  },
28
18
  setup: (api) => {
29
19
  return {
30
- init({ context }, next) {
20
+ beforeRender(context) {
31
21
  context.router = {
32
22
  useRouteMatch,
33
23
  useLocation,
@@ -38,19 +28,30 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
38
28
  return routes;
39
29
  }
40
30
  });
41
- return next({
42
- context
43
- });
44
31
  },
45
- hoc: ({ App, config }, next) => {
32
+ wrapRoot: (App) => {
33
+ const pluginConfig = api.useRuntimeConfigContext();
34
+ const { serverBase = [], history: customHistory, supportHtml5History = true, routesConfig, createRoutes, historyOptions = {} } = merge(pluginConfig.router || {}, userConfig);
35
+ const finalRouteConfig = {
36
+ routes: getGlobalRoutes(),
37
+ globalApp: getGlobalLayoutApp(),
38
+ ...routesConfig
39
+ };
40
+ const originRoutes = finalRouteConfig === null || finalRouteConfig === void 0 ? void 0 : finalRouteConfig.routes;
41
+ const isBrow = isBrowser();
42
+ const select = (pathname) => serverBase.find((baseUrl) => pathname.search(baseUrl) === 0) || "/";
43
+ if (isBrow) {
44
+ window._SERVER_DATA = parsedJSONFromElement("__MODERN_SERVER_DATA__");
45
+ }
46
46
  const getRouteApp = () => {
47
47
  if (isBrow) {
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;
52
- const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
53
48
  return (props) => {
49
+ var _window__SERVER_DATA;
50
+ const runtimeContext = useContext(RuntimeReactContext);
51
+ const baseUrl = (runtimeContext._internalRouterBaseName || ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname)).replace(/^\/*/, "/");
52
+ const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
53
+ historyOptions.basename = basename;
54
+ const history = customHistory || (supportHtml5History ? createBrowserHistory(historyOptions) : createHashHistory(historyOptions));
54
55
  const runner = api.useHookRunners();
55
56
  routes = runner.modifyRoutes(originRoutes);
56
57
  finalRouteConfig && (finalRouteConfig.routes = routes);
@@ -65,13 +66,13 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
65
66
  };
66
67
  }
67
68
  return (props) => {
68
- var _this, _config_router2;
69
+ var _this;
69
70
  const runtimeContext = useContext(RuntimeReactContext);
70
71
  const { ssrContext } = runtimeContext;
71
72
  const location1 = getLocation(ssrContext);
72
73
  const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
73
74
  const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
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(/^\/*/, "/");
75
+ const baseUrl = (_this = runtimeContext._internalRouterBaseName || (request === null || request === void 0 ? void 0 : request.baseUrl)) === null || _this === void 0 ? void 0 : _this.replace(/^\/*/, "/");
75
76
  const basename = baseUrl === "/" ? urlJoin(baseUrl, historyOptions.basename) : baseUrl;
76
77
  const runner = api.useHookRunners();
77
78
  const routes2 = runner.modifyRoutes(originRoutes);
@@ -88,11 +89,7 @@ const routerPlugin = ({ serverBase = [], history: customHistory, supportHtml5His
88
89
  });
89
90
  };
90
91
  };
91
- const RouteApp = getRouteApp();
92
- return next({
93
- App: RouteApp,
94
- config
95
- });
92
+ return getRouteApp();
96
93
  }
97
94
  };
98
95
  }
@@ -42,4 +42,4 @@ export type RouterConfig = Partial<HistoryConfig> & {
42
42
  history?: History;
43
43
  serverBase?: string[];
44
44
  };
45
- export declare const routerPlugin: ({ serverBase, history: customHistory, supportHtml5History, routesConfig, createRoutes, historyOptions, }: RouterConfig) => Plugin;
45
+ export declare const routerPlugin: (userConfig?: RouterConfig) => Plugin;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.56.3-alpha.0",
18
+ "version": "2.57.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",
@@ -59,15 +59,15 @@
59
59
  "history": "^4.7.9",
60
60
  "react-router-dom": "^5.3.4",
61
61
  "@swc/helpers": "0.5.3",
62
- "@modern-js/types": "2.56.2",
63
- "@modern-js/runtime-utils": "2.56.2",
64
- "@modern-js/utils": "2.56.2",
65
- "@modern-js/plugin": "2.56.2"
62
+ "@modern-js/plugin": "2.57.0",
63
+ "@modern-js/types": "2.57.0",
64
+ "@modern-js/runtime-utils": "2.57.0",
65
+ "@modern-js/utils": "2.57.0"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "react": ">=17",
69
69
  "react-dom": ">=17",
70
- "@modern-js/runtime": "^2.56.2"
70
+ "@modern-js/runtime": "^2.57.0"
71
71
  },
72
72
  "devDependencies": {
73
73
  "@testing-library/react": "^13.4.0",
@@ -79,16 +79,17 @@
79
79
  "react-dom": "^18",
80
80
  "ts-jest": "^29.1.0",
81
81
  "typescript": "^5",
82
- "@modern-js/app-tools": "2.56.2",
83
- "@modern-js/core": "2.56.2",
84
- "@scripts/build": "2.56.2",
85
- "@scripts/jest-config": "2.56.2",
86
- "@modern-js/runtime": "2.56.3-alpha.0"
82
+ "@modern-js/app-tools": "2.57.0",
83
+ "@modern-js/core": "2.57.0",
84
+ "@modern-js/runtime": "2.57.0",
85
+ "@scripts/build": "2.57.0",
86
+ "@scripts/jest-config": "2.57.0"
87
87
  },
88
88
  "sideEffects": false,
89
89
  "publishConfig": {
90
90
  "registry": "https://registry.npmjs.org/",
91
- "access": "public"
91
+ "access": "public",
92
+ "provenance": true
92
93
  },
93
94
  "scripts": {
94
95
  "dev": "modern-lib build --watch",