@modern-js/runtime 2.63.7 → 2.64.1

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.
Files changed (69) hide show
  1. package/dist/cjs/core/browser/index.js +16 -6
  2. package/dist/cjs/core/compat/hooks.js +80 -0
  3. package/dist/cjs/core/{plugin/runner.js → compat/index.js} +19 -19
  4. package/dist/cjs/core/compatible.js +15 -11
  5. package/dist/cjs/core/context/index.js +12 -2
  6. package/dist/cjs/core/context/runtime.js +1 -2
  7. package/dist/cjs/core/index.js +1 -4
  8. package/dist/cjs/core/plugin/base.js +0 -35
  9. package/dist/cjs/core/plugin/index.js +17 -17
  10. package/dist/cjs/core/plugin/types.js +16 -0
  11. package/dist/cjs/core/react/index.js +3 -3
  12. package/dist/cjs/core/server/requestHandler.js +4 -4
  13. package/dist/cjs/router/runtime/hooks.js +7 -7
  14. package/dist/cjs/router/runtime/plugin.js +107 -108
  15. package/dist/cjs/router/runtime/plugin.node.js +89 -91
  16. package/dist/cjs/state/cli/index.js +2 -2
  17. package/dist/esm/core/browser/index.js +14 -8
  18. package/dist/esm/core/compat/hooks.js +88 -0
  19. package/dist/esm/core/compat/index.js +20 -0
  20. package/dist/esm/core/compatible.js +17 -13
  21. package/dist/esm/core/context/index.js +9 -1
  22. package/dist/esm/core/context/runtime.js +2 -3
  23. package/dist/esm/core/index.js +1 -3
  24. package/dist/esm/core/plugin/base.js +0 -28
  25. package/dist/esm/core/plugin/index.js +14 -17
  26. package/dist/esm/core/plugin/types.js +0 -0
  27. package/dist/esm/core/react/index.js +4 -4
  28. package/dist/esm/core/server/requestHandler.js +6 -6
  29. package/dist/esm/router/runtime/hooks.js +5 -5
  30. package/dist/esm/router/runtime/plugin.js +114 -115
  31. package/dist/esm/router/runtime/plugin.node.js +112 -111
  32. package/dist/esm/state/cli/index.js +2 -2
  33. package/dist/esm-node/core/browser/index.js +12 -6
  34. package/dist/esm-node/core/compat/hooks.js +54 -0
  35. package/dist/esm-node/core/compat/index.js +18 -0
  36. package/dist/esm-node/core/compatible.js +16 -12
  37. package/dist/esm-node/core/context/index.js +9 -1
  38. package/dist/esm-node/core/context/runtime.js +1 -2
  39. package/dist/esm-node/core/index.js +1 -3
  40. package/dist/esm-node/core/plugin/base.js +0 -24
  41. package/dist/esm-node/core/plugin/index.js +16 -14
  42. package/dist/esm-node/core/plugin/types.js +0 -0
  43. package/dist/esm-node/core/react/index.js +4 -4
  44. package/dist/esm-node/core/server/requestHandler.js +5 -5
  45. package/dist/esm-node/router/runtime/hooks.js +5 -5
  46. package/dist/esm-node/router/runtime/plugin.js +108 -109
  47. package/dist/esm-node/router/runtime/plugin.node.js +90 -92
  48. package/dist/esm-node/state/cli/index.js +2 -2
  49. package/dist/types/cli/template.d.ts +2 -2
  50. package/dist/types/core/browser/hydrate.d.ts +1 -1
  51. package/dist/types/core/browser/index.d.ts +4 -0
  52. package/dist/types/core/compat/hooks.d.ts +4 -0
  53. package/dist/types/core/compat/index.d.ts +2 -0
  54. package/dist/types/core/compatible.d.ts +7 -6
  55. package/dist/types/core/context/index.d.ts +7 -2
  56. package/dist/types/core/context/runtime.d.ts +1 -3
  57. package/dist/types/core/index.d.ts +1 -2
  58. package/dist/types/core/plugin/base.d.ts +9 -33
  59. package/dist/types/core/plugin/index.d.ts +19 -12
  60. package/dist/types/core/plugin/types.d.ts +10 -0
  61. package/dist/types/index.d.ts +1 -1
  62. package/dist/types/router/runtime/hooks.d.ts +7 -3
  63. package/dist/types/router/runtime/index.d.ts +1 -0
  64. package/dist/types/router/runtime/plugin.d.ts +5 -2
  65. package/dist/types/router/runtime/plugin.node.d.ts +5 -2
  66. package/package.json +10 -10
  67. package/dist/esm/core/plugin/runner.js +0 -17
  68. package/dist/esm-node/core/plugin/runner.js +0 -17
  69. package/dist/types/core/plugin/runner.d.ts +0 -9
@@ -7,36 +7,35 @@ import { jsx as _jsx } from "react/jsx-runtime";
7
7
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
8
8
  import React, { useContext, useMemo } from "react";
9
9
  import { hydrateRoot as ModernHydrateRoot } from "./browser/hydrate";
10
- import { getGlobalAppInit } from "./context";
10
+ import { getGlobalAppInit, getGlobalInternalRuntimeContext } from "./context";
11
11
  import { RuntimeReactContext } from "./context/runtime";
12
12
  import { createLoaderManager } from "./loader/loaderManager";
13
13
  import { registerPlugin } from "./plugin";
14
- import { getGlobalRunner } from "./plugin/runner";
15
14
  import { wrapRuntimeContextProvider } from "./react/wrapper";
16
15
  var IS_REACT18 = process.env.IS_REACT18 === "true";
17
16
  function isClientArgs(id) {
18
17
  return typeof id === "string" || typeof HTMLElement !== "undefined" && _instanceof(id, HTMLElement);
19
18
  }
20
- var getInitialContext = function(runner) {
19
+ var getInitialContext = function() {
21
20
  return {
22
21
  loaderManager: createLoaderManager({}),
23
- runner,
24
22
  isBrowser: true,
25
23
  routeManifest: typeof window !== "undefined" && window[ROUTE_MANIFEST]
26
24
  };
27
25
  };
28
26
  var createApp = function(param) {
29
- var plugins = param.plugins, runtime = param.runtime, globalProps = param.props;
30
- var runner = registerPlugin(plugins, {
27
+ var plugins = param.plugins, globalProps = param.props;
28
+ var context = registerPlugin(plugins, {
31
29
  plugins: []
32
- }, runtime);
30
+ });
31
+ var hooks = context.hooks;
33
32
  return function(App) {
34
33
  var WrapperComponent = function(props) {
35
34
  return /* @__PURE__ */ React.createElement(App || React.Fragment, App ? _object_spread({}, props) : null, App ? props.children : React.Children.map(props.children, function(child) {
36
35
  return /* @__PURE__ */ React.isValidElement(child) ? /* @__PURE__ */ React.cloneElement(child, _object_spread({}, child.props, props)) : child;
37
36
  }));
38
37
  };
39
- var WrapperApp = runner.wrapRoot(WrapperComponent);
38
+ var WrapperApp = hooks.wrapRoot.call(WrapperComponent);
40
39
  var WrapComponent = function(props) {
41
40
  var mergedProps = _object_spread({}, props, globalProps);
42
41
  return /* @__PURE__ */ _jsx(WrapperApp, _object_spread({}, mergedProps));
@@ -46,13 +45,16 @@ var createApp = function(param) {
46
45
  };
47
46
  var bootstrap = function() {
48
47
  var _ref = _async_to_generator(function(BootApp, id, root, ReactDOM) {
49
- var App, runner, context, runBeforeRender, isBrowser, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate;
48
+ var App, internalRuntimeContext, api, hooks, context, runBeforeRender, isBrowser, _ssrData_data, _ssrData_data1, ssrData, loadersData, initialLoadersState, initialData, rootElement, ModernRender, ModernHydrate;
50
49
  return _ts_generator(this, function(_state) {
51
50
  switch (_state.label) {
52
51
  case 0:
53
52
  App = BootApp;
54
- runner = getGlobalRunner();
55
- context = getInitialContext(runner);
53
+ internalRuntimeContext = getGlobalInternalRuntimeContext();
54
+ api = internalRuntimeContext.pluginAPI;
55
+ hooks = internalRuntimeContext.hooks;
56
+ context = getInitialContext();
57
+ api.updateRuntimeContext(context);
56
58
  runBeforeRender = function() {
57
59
  var _ref2 = _async_to_generator(function(context2) {
58
60
  var init;
@@ -61,7 +63,7 @@ var bootstrap = function() {
61
63
  case 0:
62
64
  return [
63
65
  4,
64
- runner.beforeRender(context2)
66
+ hooks.onBeforeRender.call(context2)
65
67
  ];
66
68
  case 1:
67
69
  _state2.sent();
@@ -208,8 +210,10 @@ var useRuntimeContext = function() {
208
210
  request: (_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.request,
209
211
  response: (_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : _context_ssrContext1.response
210
212
  });
213
+ var internalRuntimeContext = getGlobalInternalRuntimeContext();
214
+ var hooks = internalRuntimeContext.hooks;
211
215
  var memoizedContext = useMemo(function() {
212
- return context.runner.pickContext(pickedContext);
216
+ return hooks.pickContext.call(pickedContext);
213
217
  }, [
214
218
  context
215
219
  ]);
@@ -7,6 +7,12 @@ function setGlobalContext(context) {
7
7
  globalContext.appConfig = typeof context.appConfig === "function" ? context.appConfig() : context.appConfig;
8
8
  globalContext.layoutApp = context.layoutApp;
9
9
  }
10
+ function setGlobalInternalRuntimeContext(context) {
11
+ globalContext.internalRuntimeContext = context;
12
+ }
13
+ function getGlobalInternalRuntimeContext() {
14
+ return globalContext.internalRuntimeContext;
15
+ }
10
16
  function getGlobalApp() {
11
17
  return globalContext.App;
12
18
  }
@@ -29,8 +35,10 @@ export {
29
35
  getGlobalApp,
30
36
  getGlobalAppConfig,
31
37
  getGlobalAppInit,
38
+ getGlobalInternalRuntimeContext,
32
39
  getGlobalLayoutApp,
33
40
  getGlobalRoutes,
34
41
  getInitialContext,
35
- setGlobalContext
42
+ setGlobalContext,
43
+ setGlobalInternalRuntimeContext
36
44
  };
@@ -3,11 +3,10 @@ import { createContext } from "react";
3
3
  import { createLoaderManager } from "../loader/loaderManager";
4
4
  var RuntimeReactContext = createContext({});
5
5
  var ServerRouterContext = createContext({});
6
- var getInitialContext = function(runner) {
7
- var isBrowser = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true, routeManifest = arguments.length > 2 ? arguments[2] : void 0;
6
+ var getInitialContext = function() {
7
+ var isBrowser = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true, routeManifest = arguments.length > 1 ? arguments[1] : void 0;
8
8
  return {
9
9
  loaderManager: createLoaderManager({}),
10
- runner,
11
10
  isBrowser,
12
11
  routeManifest: routeManifest || typeof window !== "undefined" && window[ROUTE_MANIFEST]
13
12
  };
@@ -1,4 +1,3 @@
1
- import { runtime } from "./plugin";
2
1
  import { defineConfig, getConfig, defineRuntimeConfig } from "./config";
3
2
  export * from "./compatible";
4
3
  import { RuntimeReactContext, ServerRouterContext } from "./context/runtime";
@@ -9,6 +8,5 @@ export {
9
8
  ServerRouterContext,
10
9
  defineConfig,
11
10
  defineRuntimeConfig,
12
- getConfig,
13
- runtime
11
+ getConfig
14
12
  };
@@ -1,28 +0,0 @@
1
- import { createAsyncInterruptWorkflow, createContext, createManager, createSyncParallelWorkflow, createWaterfall } from "@modern-js/plugin";
2
- var RuntimeConfigContext = createContext({});
3
- var useRuntimeConfigContext = function() {
4
- return RuntimeConfigContext.use().value;
5
- };
6
- var wrapRoot = createWaterfall();
7
- var beforeRender = createAsyncInterruptWorkflow();
8
- var pickContext = createWaterfall();
9
- var modifyRuntimeConfig = createSyncParallelWorkflow();
10
- var runtimeHooks = {
11
- beforeRender,
12
- wrapRoot,
13
- pickContext,
14
- modifyRuntimeConfig
15
- };
16
- var runtimePluginAPI = {
17
- useRuntimeConfigContext
18
- };
19
- var createRuntime = function() {
20
- return createManager(runtimeHooks, runtimePluginAPI);
21
- };
22
- var runtime = createRuntime();
23
- export {
24
- RuntimeConfigContext,
25
- createRuntime,
26
- runtime,
27
- useRuntimeConfigContext
28
- };
@@ -1,23 +1,20 @@
1
1
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import { runtime } from "@modern-js/plugin-v2/runtime";
2
3
  import { merge } from "@modern-js/runtime-utils/merge";
3
- import { RuntimeConfigContext, runtime } from "./base";
4
- import { getGlobalRunner, setGlobalRunner } from "./runner";
5
- export * from "./base";
6
- function setupConfigContext() {
7
- var runner = getGlobalRunner();
8
- var configs = runner.modifyRuntimeConfig();
9
- RuntimeConfigContext.set(merge.apply(void 0, [
10
- {}
11
- ].concat(_to_consumable_array(configs))));
12
- }
13
- function registerPlugin(internalPlugins, runtimeConfig, customRuntime) {
14
- var _instance;
4
+ import { compatPlugin } from "../compat";
5
+ import { handleSetupResult } from "../compat/hooks";
6
+ import { setGlobalInternalRuntimeContext } from "../context";
7
+ function registerPlugin(internalPlugins, runtimeConfig) {
15
8
  var _ref = runtimeConfig || {}, _ref_plugins = _ref.plugins, plugins = _ref_plugins === void 0 ? [] : _ref_plugins;
16
- (_instance = customRuntime || runtime).usePlugin.apply(_instance, _to_consumable_array(internalPlugins).concat(_to_consumable_array(plugins)));
17
- var runner = (customRuntime || runtime).init();
18
- setGlobalRunner(runner);
19
- setupConfigContext();
20
- return runner;
9
+ var runtimeContext = runtime.run({
10
+ plugins: [
11
+ compatPlugin()
12
+ ].concat(_to_consumable_array(internalPlugins), _to_consumable_array(plugins)),
13
+ config: runtimeConfig || {},
14
+ handleSetupResult
15
+ }).runtimeContext;
16
+ setGlobalInternalRuntimeContext(runtimeContext);
17
+ return runtimeContext;
21
18
  }
22
19
  function mergeConfig(config) {
23
20
  for (var _len = arguments.length, otherConfig = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
File without changes
@@ -1,16 +1,16 @@
1
1
  import { parsedJSONFromElement } from "@modern-js/runtime-utils/parsed";
2
2
  import { isBrowser } from "../../common";
3
3
  import { ROUTER_DATA_JSON_ID, SSR_DATA_JSON_ID } from "../constants";
4
- import { getGlobalApp } from "../context";
5
- import { getGlobalRunner } from "../plugin/runner";
4
+ import { getGlobalApp, getGlobalInternalRuntimeContext } from "../context";
6
5
  function createRoot(UserApp) {
7
6
  var App = UserApp || getGlobalApp();
8
7
  if (isBrowser()) {
9
8
  window._SSR_DATA = window._SSR_DATA || parsedJSONFromElement(SSR_DATA_JSON_ID);
10
9
  window._ROUTER_DATA = window._ROUTER_DATA || parsedJSONFromElement(ROUTER_DATA_JSON_ID);
11
10
  }
12
- var runner = getGlobalRunner();
13
- var WrapperApp = runner.wrapRoot(App);
11
+ var internalRuntimeContext = getGlobalInternalRuntimeContext();
12
+ var hooks = internalRuntimeContext.hooks;
13
+ var WrapperApp = hooks.wrapRoot.call(App);
14
14
  return WrapperApp;
15
15
  }
16
16
  export {
@@ -6,10 +6,9 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
7
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
8
  import { getPathname, parseCookie, parseHeaders, parseQuery } from "@modern-js/runtime-utils/universal/request";
9
- import { getGlobalAppInit } from "../context";
9
+ import { getGlobalAppInit, getGlobalInternalRuntimeContext } from "../context";
10
10
  import { getInitialContext } from "../context/runtime";
11
11
  import { createLoaderManager } from "../loader/loaderManager";
12
- import { getGlobalRunner } from "../plugin/runner";
13
12
  import { createRoot } from "../react";
14
13
  import { CHUNK_CSS_PLACEHOLDER } from "./constants";
15
14
  import { getSSRConfigByEntry, getSSRMode } from "./utils";
@@ -75,14 +74,15 @@ var createRequestHandler = function() {
75
74
  return _ts_generator(this, function(_state) {
76
75
  requestHandler = function() {
77
76
  var _ref2 = _async_to_generator(function(request, options) {
78
- var _context_routerContext, _context_routerContext1, Root, runner, routeManifest, context, runBeforeRender, responseProxy, ssrContext, getRedirectResponse, initialData, _context_routerContext2, _context_ssrContext, redirectResponse, htmlTemplate, response;
77
+ var _context_routerContext, _context_routerContext1, Root, internalRuntimeContext, hooks, routeManifest, context, runBeforeRender, responseProxy, ssrContext, getRedirectResponse, initialData, _context_routerContext2, _context_ssrContext, redirectResponse, htmlTemplate, response;
79
78
  return _ts_generator(this, function(_state2) {
80
79
  switch (_state2.label) {
81
80
  case 0:
82
81
  Root = createRoot();
83
- runner = getGlobalRunner();
82
+ internalRuntimeContext = getGlobalInternalRuntimeContext();
83
+ hooks = internalRuntimeContext.hooks;
84
84
  routeManifest = options.resource.routeManifest;
85
- context = getInitialContext(runner, false, routeManifest);
85
+ context = getInitialContext(false, routeManifest);
86
86
  runBeforeRender = function() {
87
87
  var _ref3 = _async_to_generator(function(context2) {
88
88
  var result, init;
@@ -91,7 +91,7 @@ var createRequestHandler = function() {
91
91
  case 0:
92
92
  return [
93
93
  4,
94
- runner.beforeRender(context2)
94
+ hooks.onBeforeRender.call(context2)
95
95
  ];
96
96
  case 1:
97
97
  result = _state3.sent();
@@ -1,7 +1,7 @@
1
- import { createAsyncInterruptWorkflow, createWaterfall } from "@modern-js/plugin";
2
- var modifyRoutes = createWaterfall();
3
- var beforeCreateRoutes = createAsyncInterruptWorkflow();
1
+ import { createSyncHook } from "@modern-js/plugin-v2";
2
+ var modifyRoutes = createSyncHook();
3
+ var onBeforeCreateRoutes = createSyncHook();
4
4
  export {
5
- beforeCreateRoutes,
6
- modifyRoutes
5
+ modifyRoutes,
6
+ onBeforeCreateRoutes
7
7
  };
@@ -8,7 +8,7 @@ import { normalizePathname } from "@modern-js/runtime-utils/url";
8
8
  import { useContext, useMemo } from "react";
9
9
  import { RuntimeReactContext } from "../../core";
10
10
  import { getGlobalLayoutApp, getGlobalRoutes } from "../../core/context";
11
- import { modifyRoutes as modifyRoutesHook } from "./hooks";
11
+ import { modifyRoutes as modifyRoutesHook, onBeforeCreateRoutes as onBeforeCreateRoutesHook } from "./hooks";
12
12
  import { deserializeErrors, renderRoutes, urlJoin } from "./utils";
13
13
  var finalRouteConfig = {
14
14
  routes: []
@@ -27,128 +27,127 @@ var routerPlugin = function() {
27
27
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
28
28
  return {
29
29
  name: "@modern-js/plugin-router",
30
- registerHook: {
31
- modifyRoutes: modifyRoutesHook
30
+ registryHooks: {
31
+ modifyRoutes: modifyRoutesHook,
32
+ onBeforeCreateRoutes: onBeforeCreateRoutesHook
32
33
  },
33
34
  setup: function(api) {
34
35
  var routes = [];
35
- return {
36
- beforeRender: function beforeRender(context) {
37
- if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
38
- var _ssrContext_request;
39
- var ssrContext = context.ssrContext;
40
- var currentPathname = normalizePathname(window.location.pathname);
41
- var initialPathname = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_request = ssrContext.request) === null || _ssrContext_request === void 0 ? void 0 : _ssrContext_request.pathname) && normalizePathname(ssrContext.request.pathname);
42
- if (initialPathname && initialPathname !== currentPathname) {
43
- var errorMsg = "The initial URL ".concat(initialPathname, " and the URL ").concat(currentPathname, " to be hydrated do not match, reload.");
44
- console.error(errorMsg);
45
- window.location.reload();
46
- }
36
+ api.onBeforeRender(function(context) {
37
+ if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
38
+ var _ssrContext_request;
39
+ var ssrContext = context.ssrContext;
40
+ var currentPathname = normalizePathname(window.location.pathname);
41
+ var initialPathname = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_request = ssrContext.request) === null || _ssrContext_request === void 0 ? void 0 : _ssrContext_request.pathname) && normalizePathname(ssrContext.request.pathname);
42
+ if (initialPathname && initialPathname !== currentPathname) {
43
+ var errorMsg = "The initial URL ".concat(initialPathname, " and the URL ").concat(currentPathname, " to be hydrated do not match, reload.");
44
+ console.error(errorMsg);
45
+ window.location.reload();
47
46
  }
48
- context.router = {
49
- useMatches,
50
- useLocation,
51
- useHref
52
- };
53
- Object.defineProperty(context, "routes", {
54
- get: function get() {
55
- return routes;
56
- }
57
- });
58
- },
59
- wrapRoot: function(App) {
60
- var pluginConfig = api.useRuntimeConfigContext();
61
- var _merge = merge(pluginConfig.router || {}, userConfig), _merge_serverBase = _merge.serverBase, serverBase = _merge_serverBase === void 0 ? [] : _merge_serverBase, _merge_supportHtml5History = _merge.supportHtml5History, supportHtml5History = _merge_supportHtml5History === void 0 ? true : _merge_supportHtml5History, _merge_basename = _merge.basename, basename = _merge_basename === void 0 ? "" : _merge_basename, routesConfig = _merge.routesConfig, createRoutes = _merge.createRoutes, future = _merge.future;
62
- var select = function(pathname) {
63
- return serverBase.find(function(baseUrl) {
64
- return pathname.search(baseUrl) === 0;
65
- }) || "/";
66
- };
67
- finalRouteConfig = _object_spread({
68
- routes: getGlobalRoutes(),
69
- globalApp: getGlobalLayoutApp()
70
- }, routesConfig);
71
- if (!finalRouteConfig.routes && !createRoutes) {
72
- return App;
47
+ }
48
+ context.router = {
49
+ useMatches,
50
+ useLocation,
51
+ useHref
52
+ };
53
+ Object.defineProperty(context, "routes", {
54
+ get: function get() {
55
+ return routes;
73
56
  }
74
- var getRouteApp = function() {
75
- var useCreateRouter = function(props) {
76
- var runtimeContext = useContext(RuntimeReactContext);
77
- var baseUrl = select(location.pathname).replace(/^\/*/, "/");
78
- var _basename = baseUrl === "/" ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
79
- var hydrationData = window._ROUTER_DATA;
80
- var getBlockNavState = runtimeContext.unstable_getBlockNavState;
81
- return useMemo(function() {
82
- if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
83
- hydrationData = _object_spread_props(_object_spread({}, hydrationData), {
84
- errors: deserializeErrors(hydrationData.errors)
85
- });
86
- }
87
- routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
88
- routesConfig: finalRouteConfig,
89
- props
90
- }));
91
- var runner = api.useHookRunners();
92
- routes = runner.modifyRoutes(routes);
93
- var router = supportHtml5History ? createBrowserRouter(routes, {
94
- basename: _basename,
95
- hydrationData
96
- }) : createHashRouter(routes, {
97
- basename: _basename,
98
- hydrationData
57
+ });
58
+ });
59
+ api.wrapRoot(function(App) {
60
+ var pluginConfig = api.getRuntimeConfig();
61
+ var _merge = merge(pluginConfig.router || {}, userConfig), _merge_serverBase = _merge.serverBase, serverBase = _merge_serverBase === void 0 ? [] : _merge_serverBase, _merge_supportHtml5History = _merge.supportHtml5History, supportHtml5History = _merge_supportHtml5History === void 0 ? true : _merge_supportHtml5History, _merge_basename = _merge.basename, basename = _merge_basename === void 0 ? "" : _merge_basename, routesConfig = _merge.routesConfig, createRoutes = _merge.createRoutes, future = _merge.future;
62
+ var select = function(pathname) {
63
+ return serverBase.find(function(baseUrl) {
64
+ return pathname.search(baseUrl) === 0;
65
+ }) || "/";
66
+ };
67
+ finalRouteConfig = _object_spread({
68
+ routes: getGlobalRoutes(),
69
+ globalApp: getGlobalLayoutApp()
70
+ }, routesConfig);
71
+ if (!finalRouteConfig.routes && !createRoutes) {
72
+ return App;
73
+ }
74
+ var getRouteApp = function() {
75
+ var useCreateRouter = function(props) {
76
+ var runtimeContext = useContext(RuntimeReactContext);
77
+ var baseUrl = select(location.pathname).replace(/^\/*/, "/");
78
+ var _basename = baseUrl === "/" ? urlJoin(baseUrl, runtimeContext._internalRouterBaseName || basename) : baseUrl;
79
+ var hydrationData = window._ROUTER_DATA;
80
+ var getBlockNavState = runtimeContext.unstable_getBlockNavState;
81
+ return useMemo(function() {
82
+ if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
83
+ hydrationData = _object_spread_props(_object_spread({}, hydrationData), {
84
+ errors: deserializeErrors(hydrationData.errors)
99
85
  });
100
- var originSubscribe = router.subscribe;
101
- router.subscribe = function(listener) {
102
- var wrapedListener = function() {
103
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
104
- args[_key] = arguments[_key];
105
- }
106
- var blockRoute = getBlockNavState ? getBlockNavState() : false;
107
- if (blockRoute) {
108
- return;
109
- }
110
- return listener.apply(void 0, _to_consumable_array(args));
111
- };
112
- return originSubscribe(wrapedListener);
86
+ }
87
+ routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
88
+ routesConfig: finalRouteConfig,
89
+ props
90
+ }));
91
+ var hooks = api.getHooks();
92
+ routes = hooks.modifyRoutes.call(routes);
93
+ var router = supportHtml5History ? createBrowserRouter(routes, {
94
+ basename: _basename,
95
+ hydrationData
96
+ }) : createHashRouter(routes, {
97
+ basename: _basename,
98
+ hydrationData
99
+ });
100
+ var originSubscribe = router.subscribe;
101
+ router.subscribe = function(listener) {
102
+ var wrapedListener = function() {
103
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
104
+ args[_key] = arguments[_key];
105
+ }
106
+ var blockRoute = getBlockNavState ? getBlockNavState() : false;
107
+ if (blockRoute) {
108
+ return;
109
+ }
110
+ return listener.apply(void 0, _to_consumable_array(args));
113
111
  };
114
- return router;
115
- }, [
116
- finalRouteConfig,
117
- props,
118
- _basename,
119
- hydrationData,
120
- getBlockNavState
121
- ]);
122
- };
123
- var Null = function() {
124
- return null;
125
- };
126
- return function(props) {
127
- beforeCreateRouter = false;
128
- var router = useCreateRouter(props);
129
- var routerWrapper = (
130
- // To match the node tree about https://github.com/web-infra-dev/modern.js/blob/v2.59.0/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx#L150-L168
131
- // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
132
- // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
133
- /* @__PURE__ */ _jsxs(_Fragment, {
134
- children: [
135
- /* @__PURE__ */ _jsx(RouterProvider, {
136
- router,
137
- future
138
- }),
139
- /* @__PURE__ */ _jsx(Null, {}),
140
- /* @__PURE__ */ _jsx(Null, {})
141
- ]
142
- })
143
- );
144
- return App ? /* @__PURE__ */ _jsx(App, {
145
- children: routerWrapper
146
- }) : routerWrapper;
147
- };
112
+ return originSubscribe(wrapedListener);
113
+ };
114
+ return router;
115
+ }, [
116
+ finalRouteConfig,
117
+ props,
118
+ _basename,
119
+ hydrationData,
120
+ getBlockNavState
121
+ ]);
148
122
  };
149
- return getRouteApp();
150
- }
151
- };
123
+ var Null = function() {
124
+ return null;
125
+ };
126
+ return function(props) {
127
+ beforeCreateRouter = false;
128
+ var router = useCreateRouter(props);
129
+ var routerWrapper = (
130
+ // To match the node tree about https://github.com/web-infra-dev/modern.js/blob/v2.59.0/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx#L150-L168
131
+ // According to react [useId generation algorithm](https://github.com/facebook/react/pull/22644), `useId` will generate id with the react node react struct.
132
+ // To void hydration failed, we must guarantee that the node tree when browser hydrate must have same struct with node tree when ssr render.
133
+ /* @__PURE__ */ _jsxs(_Fragment, {
134
+ children: [
135
+ /* @__PURE__ */ _jsx(RouterProvider, {
136
+ router,
137
+ future
138
+ }),
139
+ /* @__PURE__ */ _jsx(Null, {}),
140
+ /* @__PURE__ */ _jsx(Null, {})
141
+ ]
142
+ })
143
+ );
144
+ return App ? /* @__PURE__ */ _jsx(App, {
145
+ children: routerWrapper
146
+ }) : routerWrapper;
147
+ };
148
+ };
149
+ return getRouteApp();
150
+ });
152
151
  }
153
152
  };
154
153
  };