@modern-js/runtime 2.52.0 → 2.54.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.
Files changed (123) hide show
  1. package/dist/cjs/{runtimeContext.js → cli/constants.js} +6 -10
  2. package/dist/cjs/cli/entry.js +42 -0
  3. package/dist/cjs/cli/index.js +10 -0
  4. package/dist/cjs/core/compatible.js +13 -29
  5. package/dist/cjs/core/{appConfig.js → config.js} +6 -4
  6. package/dist/cjs/core/context/index.js +28 -0
  7. package/dist/cjs/core/context/runtime.js +42 -0
  8. package/dist/cjs/core/index.js +8 -12
  9. package/dist/cjs/core/loader/useLoader.js +2 -2
  10. package/dist/cjs/core/{plugin.js → plugin/base.js} +5 -25
  11. package/dist/cjs/core/plugin/index.js +39 -0
  12. package/dist/cjs/core/plugin/runner.js +42 -0
  13. package/dist/cjs/index.js +2 -4
  14. package/dist/cjs/router/cli/code/getClientRoutes/getRoutes.js +197 -0
  15. package/dist/cjs/router/cli/code/getClientRoutes/getRoutesLegacy.js +195 -0
  16. package/dist/cjs/router/cli/code/getClientRoutes/index.js +31 -0
  17. package/dist/cjs/router/cli/code/getClientRoutes/utils.js +59 -0
  18. package/dist/cjs/router/cli/code/index.js +134 -0
  19. package/dist/cjs/router/cli/code/makeLegalIdentifier.js +37 -0
  20. package/dist/cjs/router/cli/code/nestedRoutes.js +294 -0
  21. package/dist/cjs/router/cli/code/templates.js +371 -0
  22. package/dist/cjs/router/cli/code/utils.js +143 -0
  23. package/dist/cjs/router/cli/constants.js +83 -0
  24. package/dist/cjs/router/cli/entry.js +87 -0
  25. package/dist/cjs/router/cli/handler.js +71 -0
  26. package/dist/cjs/router/cli/index.js +25 -0
  27. package/dist/cjs/router/runtime/plugin.js +7 -4
  28. package/dist/cjs/router/runtime/plugin.node.js +12 -5
  29. package/dist/cjs/ssr/index.node.js +1 -0
  30. package/dist/cjs/state/runtime/plugin.js +3 -2
  31. package/dist/esm/cli/constants.js +4 -0
  32. package/dist/esm/cli/entry.js +14 -0
  33. package/dist/esm/cli/index.js +10 -0
  34. package/dist/esm/core/compatible.js +11 -30
  35. package/dist/esm/core/{appConfig.js → config.js} +4 -1
  36. package/dist/esm/core/context/index.js +4 -0
  37. package/dist/esm/core/context/runtime.js +18 -0
  38. package/dist/esm/core/index.js +4 -6
  39. package/dist/esm/core/loader/useLoader.js +1 -1
  40. package/dist/esm/core/plugin/base.js +21 -0
  41. package/dist/esm/core/plugin/index.js +15 -0
  42. package/dist/esm/core/plugin/runner.js +17 -0
  43. package/dist/esm/index.js +2 -3
  44. package/dist/esm/router/cli/code/getClientRoutes/getRoutes.js +185 -0
  45. package/dist/esm/router/cli/code/getClientRoutes/getRoutesLegacy.js +183 -0
  46. package/dist/esm/router/cli/code/getClientRoutes/index.js +6 -0
  47. package/dist/esm/router/cli/code/getClientRoutes/utils.js +28 -0
  48. package/dist/esm/router/cli/code/index.js +214 -0
  49. package/dist/esm/router/cli/code/makeLegalIdentifier.js +15 -0
  50. package/dist/esm/router/cli/code/nestedRoutes.js +397 -0
  51. package/dist/esm/router/cli/code/templates.js +417 -0
  52. package/dist/esm/router/cli/code/utils.js +212 -0
  53. package/dist/esm/router/cli/constants.js +47 -0
  54. package/dist/esm/router/cli/entry.js +57 -0
  55. package/dist/esm/router/cli/handler.js +103 -0
  56. package/dist/esm/router/cli/index.js +57 -0
  57. package/dist/esm/router/runtime/plugin.js +7 -4
  58. package/dist/esm/router/runtime/plugin.node.js +13 -6
  59. package/dist/esm/ssr/index.node.js +1 -0
  60. package/dist/esm/state/runtime/plugin.js +3 -2
  61. package/dist/esm-node/cli/constants.js +4 -0
  62. package/dist/esm-node/cli/entry.js +8 -0
  63. package/dist/esm-node/cli/index.js +9 -0
  64. package/dist/esm-node/core/compatible.js +12 -28
  65. package/dist/esm-node/core/{appConfig.js → config.js} +2 -1
  66. package/dist/esm-node/core/context/index.js +4 -0
  67. package/dist/esm-node/core/context/runtime.js +16 -0
  68. package/dist/esm-node/core/index.js +4 -6
  69. package/dist/esm-node/core/loader/useLoader.js +1 -1
  70. package/dist/esm-node/core/plugin/base.js +19 -0
  71. package/dist/esm-node/core/plugin/index.js +13 -0
  72. package/dist/esm-node/core/plugin/runner.js +17 -0
  73. package/dist/esm-node/index.js +2 -3
  74. package/dist/esm-node/router/cli/code/getClientRoutes/getRoutes.js +163 -0
  75. package/dist/esm-node/router/cli/code/getClientRoutes/getRoutesLegacy.js +161 -0
  76. package/dist/esm-node/router/cli/code/getClientRoutes/index.js +6 -0
  77. package/dist/esm-node/router/cli/code/getClientRoutes/utils.js +22 -0
  78. package/dist/esm-node/router/cli/code/index.js +100 -0
  79. package/dist/esm-node/router/cli/code/makeLegalIdentifier.js +13 -0
  80. package/dist/esm-node/router/cli/code/nestedRoutes.js +258 -0
  81. package/dist/esm-node/router/cli/code/templates.js +335 -0
  82. package/dist/esm-node/router/cli/code/utils.js +101 -0
  83. package/dist/esm-node/router/cli/constants.js +47 -0
  84. package/dist/esm-node/router/cli/entry.js +50 -0
  85. package/dist/esm-node/router/cli/handler.js +36 -0
  86. package/dist/esm-node/router/cli/index.js +22 -0
  87. package/dist/esm-node/router/runtime/plugin.js +7 -4
  88. package/dist/esm-node/router/runtime/plugin.node.js +12 -5
  89. package/dist/esm-node/ssr/index.node.js +1 -0
  90. package/dist/esm-node/state/runtime/plugin.js +3 -2
  91. package/dist/types/cli/constants.d.ts +1 -0
  92. package/dist/types/cli/entry.d.ts +1 -0
  93. package/dist/types/cli/index.d.ts +1 -0
  94. package/dist/types/core/compatible.d.ts +7 -3
  95. package/dist/types/core/config.d.ts +14 -0
  96. package/dist/types/core/context/index.d.ts +1 -0
  97. package/dist/types/{runtimeContext.d.ts → core/context/runtime.d.ts} +5 -4
  98. package/dist/types/core/index.d.ts +4 -4
  99. package/dist/types/core/plugin/base.d.ts +80 -0
  100. package/dist/types/core/plugin/index.d.ts +29 -0
  101. package/dist/types/core/plugin/runner.d.ts +26 -0
  102. package/dist/types/index.d.ts +2 -2
  103. package/dist/types/router/cli/code/getClientRoutes/getRoutes.d.ts +8 -0
  104. package/dist/types/router/cli/code/getClientRoutes/getRoutesLegacy.d.ts +9 -0
  105. package/dist/types/router/cli/code/getClientRoutes/index.d.ts +2 -0
  106. package/dist/types/router/cli/code/getClientRoutes/utils.d.ts +5 -0
  107. package/dist/types/router/cli/code/index.d.ts +6 -0
  108. package/dist/types/router/cli/code/makeLegalIdentifier.d.ts +1 -0
  109. package/dist/types/router/cli/code/nestedRoutes.d.ts +7 -0
  110. package/dist/types/router/cli/code/templates.d.ts +14 -0
  111. package/dist/types/router/cli/code/utils.d.ts +12 -0
  112. package/dist/types/router/cli/constants.d.ts +32 -0
  113. package/dist/types/router/cli/entry.d.ts +5 -0
  114. package/dist/types/router/cli/handler.d.ts +5 -0
  115. package/dist/types/router/cli/index.d.ts +3 -1
  116. package/dist/types/ssr/serverRender/types.d.ts +3 -0
  117. package/package.json +37 -11
  118. package/dist/esm/core/plugin.js +0 -59
  119. package/dist/esm/runtimeContext.js +0 -7
  120. package/dist/esm-node/core/plugin.js +0 -37
  121. package/dist/esm-node/runtimeContext.js +0 -7
  122. package/dist/types/core/appConfig.d.ts +0 -4
  123. package/dist/types/core/plugin.d.ts +0 -204
@@ -0,0 +1,57 @@
1
+ import path from "path";
2
+ import { JS_EXTENSIONS, findExists, fs, isRouterV5 } from "@modern-js/utils";
3
+ import { FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, NESTED_ROUTES_DIR, PAGES_DIR_NAME } from "./constants";
4
+ var hasPages = function(dir) {
5
+ return fs.existsSync(path.join(dir, PAGES_DIR_NAME));
6
+ };
7
+ var hasNestedRoutes = function(dir) {
8
+ return fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
9
+ };
10
+ var isRouteEntry = function(dir) {
11
+ if (hasNestedRoutes(dir)) {
12
+ return path.join(dir, NESTED_ROUTES_DIR);
13
+ }
14
+ if (hasPages(dir)) {
15
+ return path.join(dir, PAGES_DIR_NAME);
16
+ }
17
+ return false;
18
+ };
19
+ var modifyEntrypoints = function(entrypoints) {
20
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
21
+ return entrypoints.map(function(entrypoint) {
22
+ if (!entrypoint.isAutoMount) {
23
+ return entrypoint;
24
+ }
25
+ var isHasNestedRoutes = hasNestedRoutes(entrypoint.absoluteEntryDir);
26
+ var isHasPages = hasPages(entrypoint.absoluteEntryDir);
27
+ if (!isHasNestedRoutes && !isHasPages) {
28
+ return entrypoint;
29
+ }
30
+ if (entrypoint.fileSystemRoutes && !isRouterV5(config)) {
31
+ entrypoint.nestedRoutesEntry = entrypoint.entry;
32
+ } else if (!entrypoint.fileSystemRoutes) {
33
+ entrypoint.fileSystemRoutes = {
34
+ globalApp: findExists(JS_EXTENSIONS.map(function(ext) {
35
+ return path.resolve(entrypoint.absoluteEntryDir, "./".concat(PAGES_DIR_NAME, "/").concat(FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT).concat(ext));
36
+ }))
37
+ };
38
+ if (isHasPages) {
39
+ entrypoint.entry = path.join(entrypoint.absoluteEntryDir, PAGES_DIR_NAME);
40
+ entrypoint.pageRoutesEntry = entrypoint.entry;
41
+ }
42
+ if (isHasNestedRoutes) {
43
+ entrypoint.entry = path.join(entrypoint.absoluteEntryDir, NESTED_ROUTES_DIR);
44
+ entrypoint.nestedRoutesEntry = entrypoint.entry;
45
+ }
46
+ } else {
47
+ throw Error("Custom entries with conventional routing not support use react router v5!");
48
+ }
49
+ return entrypoint;
50
+ });
51
+ };
52
+ export {
53
+ hasNestedRoutes,
54
+ hasPages,
55
+ isRouteEntry,
56
+ modifyEntrypoints
57
+ };
@@ -0,0 +1,103 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import path from "path";
4
+ import { cloneDeep } from "@modern-js/utils/lodash";
5
+ import { modifyEntrypoints } from "./entry";
6
+ import { isPageComponentFile } from "./code/utils";
7
+ var originEntrypoints = [];
8
+ function handleModifyEntrypoints(api, entrypoints) {
9
+ return _handleModifyEntrypoints.apply(this, arguments);
10
+ }
11
+ function _handleModifyEntrypoints() {
12
+ _handleModifyEntrypoints = _async_to_generator(function(api, entrypoints) {
13
+ var config, newEntryPoints, appContext, resolvedConfig, generateCode;
14
+ return _ts_generator(this, function(_state) {
15
+ switch (_state.label) {
16
+ case 0:
17
+ config = api.useResolvedConfigContext();
18
+ newEntryPoints = modifyEntrypoints(entrypoints, config);
19
+ appContext = api.useAppContext();
20
+ resolvedConfig = api.useResolvedConfigContext();
21
+ appContext.entrypoints = newEntryPoints;
22
+ originEntrypoints = cloneDeep(newEntryPoints);
23
+ return [
24
+ 4,
25
+ import("./code")
26
+ ];
27
+ case 1:
28
+ generateCode = _state.sent().generateCode;
29
+ return [
30
+ 4,
31
+ generateCode(appContext, resolvedConfig, entrypoints, api)
32
+ ];
33
+ case 2:
34
+ _state.sent();
35
+ return [
36
+ 2,
37
+ newEntryPoints
38
+ ];
39
+ }
40
+ });
41
+ });
42
+ return _handleModifyEntrypoints.apply(this, arguments);
43
+ }
44
+ function handleFileChange(api, e) {
45
+ return _handleFileChange.apply(this, arguments);
46
+ }
47
+ function _handleFileChange() {
48
+ _handleFileChange = _async_to_generator(function(api, e) {
49
+ var appContext, appDirectory, entrypoints, filename, eventType, nestedRouteEntries, pagesDir, isPageFile, absoluteFilePath, isRouteComponent, resolvedConfig, generateCode, entrypoints1;
50
+ return _ts_generator(this, function(_state) {
51
+ switch (_state.label) {
52
+ case 0:
53
+ appContext = api.useAppContext();
54
+ appDirectory = appContext.appDirectory, entrypoints = appContext.entrypoints;
55
+ filename = e.filename, eventType = e.eventType;
56
+ nestedRouteEntries = entrypoints.map(function(point) {
57
+ return point.nestedRoutesEntry;
58
+ }).filter(Boolean);
59
+ pagesDir = entrypoints.map(function(point) {
60
+ return point.entry;
61
+ }).filter(function(entry) {
62
+ return entry && !path.extname(entry);
63
+ }).concat(nestedRouteEntries);
64
+ isPageFile = function(name) {
65
+ return pagesDir.some(function(pageDir) {
66
+ return name.includes(pageDir);
67
+ });
68
+ };
69
+ absoluteFilePath = path.resolve(appDirectory, filename);
70
+ isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
71
+ if (!(isRouteComponent && (eventType === "add" || eventType === "unlink")))
72
+ return [
73
+ 3,
74
+ 3
75
+ ];
76
+ resolvedConfig = api.useResolvedConfigContext();
77
+ return [
78
+ 4,
79
+ import("./code")
80
+ ];
81
+ case 1:
82
+ generateCode = _state.sent().generateCode;
83
+ entrypoints1 = cloneDeep(originEntrypoints);
84
+ return [
85
+ 4,
86
+ generateCode(appContext, resolvedConfig, entrypoints1, api)
87
+ ];
88
+ case 2:
89
+ _state.sent();
90
+ _state.label = 3;
91
+ case 3:
92
+ return [
93
+ 2
94
+ ];
95
+ }
96
+ });
97
+ });
98
+ return _handleFileChange.apply(this, arguments);
99
+ }
100
+ export {
101
+ handleFileChange,
102
+ handleModifyEntrypoints
103
+ };
@@ -1,6 +1,12 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
1
2
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
5
  import { getEntryOptions, createRuntimeExportsUtils, isRouterV5 as isV5 } from "@modern-js/utils";
6
+ import { isRouteEntry } from "./entry";
7
+ import { handleFileChange, handleModifyEntrypoints } from "./handler";
8
+ import { isRouteEntry as isRouteEntry2 } from "./entry";
9
+ import { handleFileChange as handleFileChange2, handleModifyEntrypoints as handleModifyEntrypoints2 } from "./handler";
4
10
  var PLUGIN_IDENTIFIER = "router";
5
11
  var ROUTES_IDENTIFIER = "routes";
6
12
  var routerPlugin = function() {
@@ -13,6 +19,13 @@ var routerPlugin = function() {
13
19
  var runtimeConfigMap = /* @__PURE__ */ new Map();
14
20
  var pluginsExportsUtils;
15
21
  return {
22
+ checkEntryPoint: function checkEntryPoint(param) {
23
+ var path = param.path, entry = param.entry;
24
+ return {
25
+ path,
26
+ entry: entry || isRouteEntry(path)
27
+ };
28
+ },
16
29
  config: function config() {
17
30
  var appContext = api.useAppContext();
18
31
  pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
@@ -32,6 +45,29 @@ var routerPlugin = function() {
32
45
  }
33
46
  };
34
47
  },
48
+ modifyEntrypoints: function modifyEntrypoints(param) {
49
+ var entrypoints = param.entrypoints;
50
+ return _async_to_generator(function() {
51
+ var newEntryPoints;
52
+ return _ts_generator(this, function(_state) {
53
+ switch (_state.label) {
54
+ case 0:
55
+ return [
56
+ 4,
57
+ handleModifyEntrypoints(api, entrypoints)
58
+ ];
59
+ case 1:
60
+ newEntryPoints = _state.sent();
61
+ return [
62
+ 2,
63
+ {
64
+ entrypoints: newEntryPoints
65
+ }
66
+ ];
67
+ }
68
+ });
69
+ })();
70
+ },
35
71
  modifyEntryImports: function modifyEntryImports(param) {
36
72
  var entrypoint = param.entrypoint, imports = param.imports;
37
73
  var entryName = entrypoint.entryName, isMainEntry = entrypoint.isMainEntry, fileSystemRoutes = entrypoint.fileSystemRoutes;
@@ -91,6 +127,24 @@ var routerPlugin = function() {
91
127
  if (!isV5(userConfig)) {
92
128
  pluginsExportsUtils.addExport("export { default as router } from '@modern-js/runtime/router'");
93
129
  }
130
+ },
131
+ fileChange: function fileChange(e) {
132
+ return _async_to_generator(function() {
133
+ return _ts_generator(this, function(_state) {
134
+ switch (_state.label) {
135
+ case 0:
136
+ return [
137
+ 4,
138
+ handleFileChange(api, e)
139
+ ];
140
+ case 1:
141
+ _state.sent();
142
+ return [
143
+ 2
144
+ ];
145
+ }
146
+ });
147
+ })();
94
148
  }
95
149
  };
96
150
  }
@@ -99,5 +153,8 @@ var routerPlugin = function() {
99
153
  var cli_default = routerPlugin;
100
154
  export {
101
155
  cli_default as default,
156
+ handleFileChange2 as handleFileChange,
157
+ handleModifyEntrypoints2 as handleModifyEntrypoints,
158
+ isRouteEntry2 as isRouteEntry,
102
159
  routerPlugin
103
160
  };
@@ -56,10 +56,11 @@ var routerPlugin = function(param) {
56
56
  });
57
57
  },
58
58
  hoc: function(param2, next) {
59
- var App = param2.App;
59
+ var App = param2.App, config = param2.config;
60
60
  if (!finalRouteConfig && !createRoutes) {
61
61
  return next({
62
- App
62
+ App,
63
+ config
63
64
  });
64
65
  }
65
66
  var getRouteApp = function() {
@@ -134,11 +135,13 @@ var routerPlugin = function(param) {
134
135
  }
135
136
  if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
136
137
  return next({
137
- App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
138
+ App: hoistNonReactStatics(RouteApp, routesConfig.globalApp),
139
+ config
138
140
  });
139
141
  }
140
142
  return next({
141
- App: RouteApp
143
+ App: RouteApp,
144
+ config
142
145
  });
143
146
  },
144
147
  pickContext: function(param2, next) {
@@ -90,7 +90,7 @@ var routerPlugin = function(param) {
90
90
  init: function init(param2, next) {
91
91
  var context = param2.context;
92
92
  return _async_to_generator(function() {
93
- var _context_ssrContext, _context_ssrContext1, request, ssrMode, nonce, baseUrl, _basename, _context_ssrContext2, reporter, serverTiming, requestContext, routes, runner, query, remixRequest, end, routerContext, cost, router;
93
+ var _context_ssrContext, _context_ssrContext1, request, ssrMode, nonce, _context_ssrContext_loaderFailureMode, loaderFailureMode, baseUrl, _basename, _context_ssrContext2, reporter, serverTiming, requestContext, routes, runner, query, remixRequest, end, routerContext, cost, router;
94
94
  return _ts_generator(this, function(_state) {
95
95
  switch (_state.label) {
96
96
  case 0:
@@ -102,7 +102,7 @@ var routerPlugin = function(param) {
102
102
  })
103
103
  ];
104
104
  }
105
- _context_ssrContext1 = context.ssrContext, request = _context_ssrContext1.request, ssrMode = _context_ssrContext1.mode, nonce = _context_ssrContext1.nonce;
105
+ _context_ssrContext1 = context.ssrContext, request = _context_ssrContext1.request, ssrMode = _context_ssrContext1.mode, nonce = _context_ssrContext1.nonce, _context_ssrContext_loaderFailureMode = _context_ssrContext1.loaderFailureMode, loaderFailureMode = _context_ssrContext_loaderFailureMode === void 0 ? "errorBoundary" : _context_ssrContext_loaderFailureMode;
106
106
  baseUrl = originalBaseUrl || request.baseUrl;
107
107
  _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
108
108
  _context_ssrContext2 = context.ssrContext, reporter = _context_ssrContext2.reporter, serverTiming = _context_ssrContext2.serverTiming;
@@ -140,6 +140,10 @@ var routerPlugin = function(param) {
140
140
  routerContext
141
141
  ];
142
142
  }
143
+ if (routerContext.statusCode >= 500 && routerContext.statusCode < 600 && loaderFailureMode === "clientRender") {
144
+ routerContext.statusCode = 200;
145
+ throw routerContext.errors[0];
146
+ }
143
147
  router = createStaticRouter(routes, routerContext);
144
148
  context.remixRouter = router;
145
149
  context.routerContext = routerContext;
@@ -156,10 +160,11 @@ var routerPlugin = function(param) {
156
160
  })();
157
161
  },
158
162
  hoc: function(param2, next) {
159
- var App = param2.App;
163
+ var App = param2.App, config = param2.config;
160
164
  if (!routesConfig) {
161
165
  return next({
162
- App
166
+ App,
167
+ config
163
168
  });
164
169
  }
165
170
  var getRouteApp = function() {
@@ -185,11 +190,13 @@ var routerPlugin = function(param) {
185
190
  var RouteApp = getRouteApp();
186
191
  if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
187
192
  return next({
188
- App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
193
+ App: hoistNonReactStatics(RouteApp, routesConfig.globalApp),
194
+ config
189
195
  });
190
196
  }
191
197
  return next({
192
- App: RouteApp
198
+ App: RouteApp,
199
+ config
193
200
  });
194
201
  },
195
202
  pickContext: function(param2, next) {
@@ -45,6 +45,7 @@ var ssr = function() {
45
45
  context.ssrContext.request = formatServer(request);
46
46
  context.ssrContext.mode = config.mode;
47
47
  context.ssrContext.tracker = createSSRTracker(context.ssrContext);
48
+ context.ssrContext.loaderFailureMode = config.loaderFailureMode;
48
49
  if (!context.ssrContext.htmlModifiers) {
49
50
  context.ssrContext.htmlModifiers = [];
50
51
  }
@@ -61,7 +61,7 @@ var state = function(config) {
61
61
  var storeConfig = getStoreConfig(config);
62
62
  return {
63
63
  hoc: function hoc(param, next) {
64
- var App = param.App;
64
+ var App = param.App, _$config = param.config;
65
65
  var getStateApp = function(props) {
66
66
  var context = useContext(RuntimeReactContext);
67
67
  return /* @__PURE__ */ _jsx(Provider, {
@@ -71,7 +71,8 @@ var state = function(config) {
71
71
  });
72
72
  };
73
73
  return next({
74
- App: hoistNonReactStatics(getStateApp, App)
74
+ App: hoistNonReactStatics(getStateApp, App),
75
+ config: _$config
75
76
  });
76
77
  },
77
78
  init: function init(param, next) {
@@ -0,0 +1,4 @@
1
+ const APP_FILE_NAME = "App";
2
+ export {
3
+ APP_FILE_NAME
4
+ };
@@ -0,0 +1,8 @@
1
+ import path from "path";
2
+ import { JS_EXTENSIONS, findExists } from "@modern-js/utils";
3
+ import { APP_FILE_NAME } from "./constants";
4
+ const hasApp = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${APP_FILE_NAME}${ext}`)));
5
+ const isRuntimeEntry = (dir) => hasApp(dir);
6
+ export {
7
+ isRuntimeEntry
8
+ };
@@ -3,6 +3,8 @@ import { statePlugin } from "../state/cli";
3
3
  import { ssrPlugin } from "../ssr/cli";
4
4
  import { routerPlugin } from "../router/cli";
5
5
  import { documentPlugin } from "../document/cli";
6
+ import { isRuntimeEntry } from "./entry";
7
+ import { isRuntimeEntry as isRuntimeEntry2 } from "./entry";
6
8
  const runtimePlugin = () => ({
7
9
  name: "@modern-js/runtime",
8
10
  post: [
@@ -21,6 +23,12 @@ const runtimePlugin = () => ({
21
23
  ],
22
24
  setup: (api) => {
23
25
  return {
26
+ checkEntryPoint({ path, entry }) {
27
+ return {
28
+ path,
29
+ entry: entry || isRuntimeEntry(path)
30
+ };
31
+ },
24
32
  config() {
25
33
  const appDir = api.useAppContext().appDirectory;
26
34
  process.env.IS_REACT18 = isReact18(appDir).toString();
@@ -63,5 +71,6 @@ const runtimePlugin = () => ({
63
71
  var cli_default = runtimePlugin;
64
72
  export {
65
73
  cli_default as default,
74
+ isRuntimeEntry2 as isRuntimeEntry,
66
75
  runtimePlugin
67
76
  };
@@ -2,48 +2,39 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import React, { useContext, useMemo } from "react";
3
3
  import hoistNonReactStatics from "hoist-non-react-statics";
4
4
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
5
- import { RuntimeReactContext } from "../runtimeContext";
6
- import { runtime } from "./plugin";
5
+ import { RuntimeReactContext } from "./context/runtime";
6
+ import { registerPlugin } from "./plugin";
7
7
  import { createLoaderManager } from "./loader/loaderManager";
8
+ import { getGlobalRunner } from "./plugin/runner";
8
9
  const IS_REACT18 = process.env.IS_REACT18 === "true";
9
10
  function isClientArgs(id) {
10
11
  return typeof id === "string" || typeof HTMLElement !== "undefined" && id instanceof HTMLElement;
11
12
  }
12
- const runnerMap = /* @__PURE__ */ new WeakMap();
13
13
  const getInitialContext = (runner) => ({
14
14
  loaderManager: createLoaderManager({}),
15
15
  runner,
16
16
  isBrowser: true,
17
17
  routeManifest: typeof window !== "undefined" && window[ROUTE_MANIFEST]
18
18
  });
19
- const createApp = ({ plugins, props: globalProps }) => {
20
- const appRuntime = runtime.clone();
21
- appRuntime.usePlugin(...plugins);
19
+ const createApp = ({ plugins, runtime, props: globalProps }) => {
20
+ const runner = registerPlugin(plugins, {
21
+ plugins: []
22
+ }, runtime);
22
23
  return (App) => {
23
- const runner = appRuntime.init();
24
24
  const WrapperComponent = (props) => {
25
- const element = /* @__PURE__ */ React.createElement(App || React.Fragment, App ? {
25
+ return /* @__PURE__ */ React.createElement(App || React.Fragment, App ? {
26
26
  ...props
27
27
  } : null, App ? props.children : React.Children.map(props.children, (child) => /* @__PURE__ */ React.isValidElement(child) ? /* @__PURE__ */ React.cloneElement(child, {
28
28
  ...child.props,
29
29
  ...props
30
30
  }) : child));
31
- const context = useContext(RuntimeReactContext);
32
- return runner.provide({
33
- element,
34
- props: {
35
- ...props
36
- },
37
- context
38
- }, {
39
- onLast: ({ element: element2 }) => element2
40
- });
41
31
  };
42
32
  if (App) {
43
33
  hoistNonReactStatics(WrapperComponent, App);
44
34
  }
45
35
  const HOCApp = runner.hoc({
46
- App: WrapperComponent
36
+ App: WrapperComponent,
37
+ config: globalProps || {}
47
38
  }, {
48
39
  onLast: ({ App: App2 }) => {
49
40
  const WrapComponent = ({ context, ...props }) => {
@@ -73,19 +64,12 @@ const createApp = ({ plugins, props: globalProps }) => {
73
64
  return hoistNonReactStatics(WrapComponent, App2);
74
65
  }
75
66
  });
76
- runnerMap.set(HOCApp, runner);
77
67
  return HOCApp;
78
68
  };
79
69
  };
80
70
  const bootstrap = async (BootApp, id, root, ReactDOM) => {
81
- let App = BootApp;
82
- let runner = runnerMap.get(App);
83
- if (!runner) {
84
- App = createApp({
85
- plugins: []
86
- })(App);
87
- runner = runnerMap.get(App);
88
- }
71
+ const App = BootApp;
72
+ const runner = getGlobalRunner();
89
73
  const context = getInitialContext(runner);
90
74
  const runInit = (_context) => runner.init({
91
75
  context: _context
@@ -1,6 +1,5 @@
1
1
  const APP_CONFIG_SYMBOL = "config";
2
2
  const getConfig = (Component) => (
3
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
3
  // @ts-expect-error
5
4
  Component[APP_CONFIG_SYMBOL]
6
5
  );
@@ -8,7 +7,9 @@ const defineConfig = (Component, config) => {
8
7
  Component[APP_CONFIG_SYMBOL] = config;
9
8
  return Component;
10
9
  };
10
+ const defineRuntimeConfig = (config) => config;
11
11
  export {
12
12
  defineConfig,
13
+ defineRuntimeConfig,
13
14
  getConfig
14
15
  };
@@ -0,0 +1,4 @@
1
+ import { RuntimeReactContext } from "./runtime";
2
+ export {
3
+ RuntimeReactContext
4
+ };
@@ -0,0 +1,16 @@
1
+ import { createContext } from "react";
2
+ import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
3
+ import { createLoaderManager } from "../loader/loaderManager";
4
+ const RuntimeReactContext = createContext({});
5
+ const ServerRouterContext = createContext({});
6
+ const getInitialContext = (runner) => ({
7
+ loaderManager: createLoaderManager({}),
8
+ runner,
9
+ isBrowser: true,
10
+ routeManifest: typeof window !== "undefined" && window[ROUTE_MANIFEST]
11
+ });
12
+ export {
13
+ RuntimeReactContext,
14
+ ServerRouterContext,
15
+ getInitialContext
16
+ };
@@ -1,16 +1,14 @@
1
- import { createPlugin, createRuntime, runtime, registerInit } from "./plugin";
2
- import { defineConfig, getConfig } from "./appConfig";
1
+ import { runtime } from "./plugin";
2
+ import { defineConfig, getConfig, defineRuntimeConfig } from "./config";
3
3
  export * from "./compatible";
4
- import { RuntimeReactContext, ServerRouterContext } from "../runtimeContext";
4
+ import { RuntimeReactContext, ServerRouterContext } from "./context/runtime";
5
5
  export * from "./loader";
6
6
  export * from "@modern-js/plugin";
7
7
  export {
8
8
  RuntimeReactContext,
9
9
  ServerRouterContext,
10
- createPlugin,
11
- createRuntime,
12
10
  defineConfig,
11
+ defineRuntimeConfig,
13
12
  getConfig,
14
- registerInit,
15
13
  runtime
16
14
  };
@@ -1,6 +1,6 @@
1
1
  import { useContext, useRef, useMemo, useState, useCallback, useEffect } from "react";
2
2
  import invariant from "invariant";
3
- import { RuntimeReactContext } from "../../runtimeContext";
3
+ import { RuntimeReactContext } from "../context/runtime";
4
4
  import { LoaderStatus } from "./loaderManager";
5
5
  const useLoader = (loaderFn, options = {
6
6
  params: void 0
@@ -0,0 +1,19 @@
1
+ import { createManager, createPipeline, createAsyncPipeline } from "@modern-js/plugin";
2
+ const client = createAsyncPipeline();
3
+ const server = createAsyncPipeline();
4
+ const hoc = createPipeline();
5
+ const init = createAsyncPipeline();
6
+ const pickContext = createPipeline();
7
+ const runtimeHooks = {
8
+ hoc,
9
+ init,
10
+ client,
11
+ server,
12
+ pickContext
13
+ };
14
+ const createRuntime = () => createManager(runtimeHooks);
15
+ const runtime = createRuntime();
16
+ export {
17
+ createRuntime,
18
+ runtime
19
+ };
@@ -0,0 +1,13 @@
1
+ import { runtime } from "./base";
2
+ import { setGlobalRunner } from "./runner";
3
+ export * from "./base";
4
+ function registerPlugin(internalPlugins, runtimeConfig, customRuntime) {
5
+ const { plugins = [] } = runtimeConfig || {};
6
+ (customRuntime || runtime).usePlugin(...internalPlugins, ...plugins);
7
+ const runner = (customRuntime || runtime).init();
8
+ setGlobalRunner(runner);
9
+ return runner;
10
+ }
11
+ export {
12
+ registerPlugin
13
+ };
@@ -0,0 +1,17 @@
1
+ import { runtime } from "./base";
2
+ let globalRunner;
3
+ function setGlobalRunner(runner) {
4
+ globalRunner = runner;
5
+ }
6
+ function getGlobalRunner() {
7
+ if (globalRunner) {
8
+ return globalRunner;
9
+ }
10
+ const runner = runtime.init();
11
+ setGlobalRunner(runner);
12
+ return runner;
13
+ }
14
+ export {
15
+ getGlobalRunner,
16
+ setGlobalRunner
17
+ };
@@ -1,13 +1,12 @@
1
1
  import { isBrowser } from "./common";
2
- import { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, defineConfig, registerInit, useRuntimeContext } from "./core";
2
+ import { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext } from "./core";
3
3
  export {
4
4
  RuntimeReactContext,
5
5
  bootstrap,
6
6
  createApp,
7
- createPlugin,
8
7
  defineConfig,
8
+ defineRuntimeConfig,
9
9
  isBrowser,
10
- registerInit,
11
10
  useLoader,
12
11
  useRuntimeContext
13
12
  };