@modern-js/runtime 1.3.4 → 1.4.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 (156) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/dist/js/modern/cli/index.js +4 -4
  3. package/dist/js/modern/common.js +1 -0
  4. package/dist/js/modern/exports/model.js +2 -1
  5. package/dist/js/modern/exports/router.js +2 -1
  6. package/dist/js/modern/exports/server.js +0 -1
  7. package/dist/js/modern/exports/ssr.js +2 -1
  8. package/dist/js/modern/router/cli/index.js +106 -0
  9. package/dist/js/modern/router/index.js +2 -0
  10. package/dist/js/modern/router/runtime/DefaultNotFound.js +13 -0
  11. package/dist/js/modern/router/runtime/index.js +4 -0
  12. package/dist/js/modern/router/runtime/plugin.js +88 -0
  13. package/dist/js/modern/router/runtime/utils.js +108 -0
  14. package/dist/js/modern/ssr/cli/index.js +120 -0
  15. package/dist/js/modern/ssr/index.js +109 -0
  16. package/dist/js/modern/ssr/index.node.js +78 -0
  17. package/dist/js/modern/ssr/prefetch.js +58 -0
  18. package/dist/js/modern/ssr/react/index.js +2 -0
  19. package/dist/js/modern/ssr/react/nossr/index.js +13 -0
  20. package/dist/js/modern/ssr/react/prerender/index.js +104 -0
  21. package/dist/js/modern/ssr/react/prerender/type.js +0 -0
  22. package/dist/js/modern/ssr/react/prerender/util.js +99 -0
  23. package/dist/js/modern/ssr/serverRender/entry.js +179 -0
  24. package/dist/js/modern/ssr/serverRender/helmet.js +46 -0
  25. package/dist/js/modern/ssr/serverRender/index.js +29 -0
  26. package/dist/js/modern/ssr/serverRender/loadable.js +48 -0
  27. package/dist/js/modern/ssr/serverRender/measure.js +11 -0
  28. package/dist/js/modern/ssr/serverRender/reduce.js +7 -0
  29. package/dist/js/modern/ssr/serverRender/styledComponent.js +8 -0
  30. package/dist/js/modern/ssr/serverRender/template.js +90 -0
  31. package/dist/js/modern/ssr/serverRender/type.js +8 -0
  32. package/dist/js/modern/ssr/utils.js +51 -0
  33. package/dist/js/modern/state/cli/index.js +110 -0
  34. package/dist/js/modern/state/index.js +2 -0
  35. package/dist/js/modern/state/plugins.js +7 -0
  36. package/dist/js/modern/state/runtime/index.js +4 -0
  37. package/dist/js/modern/state/runtime/plugin.js +71 -0
  38. package/dist/js/modern/state/types.js +1 -0
  39. package/dist/js/node/cli/index.js +5 -5
  40. package/dist/js/node/common.js +10 -0
  41. package/dist/js/node/exports/model.js +19 -5
  42. package/dist/js/node/exports/router.js +19 -5
  43. package/dist/js/node/exports/server.js +0 -17
  44. package/dist/js/node/exports/ssr.js +19 -5
  45. package/dist/js/node/router/cli/index.js +122 -0
  46. package/dist/js/node/router/index.js +30 -0
  47. package/dist/js/node/router/runtime/DefaultNotFound.js +26 -0
  48. package/dist/js/node/router/runtime/index.js +39 -0
  49. package/dist/js/node/router/runtime/plugin.js +111 -0
  50. package/dist/js/node/router/runtime/utils.js +128 -0
  51. package/dist/js/node/ssr/cli/index.js +136 -0
  52. package/dist/js/node/ssr/index.js +138 -0
  53. package/dist/js/node/ssr/index.node.js +105 -0
  54. package/dist/js/node/ssr/prefetch.js +75 -0
  55. package/dist/js/node/ssr/react/index.js +21 -0
  56. package/dist/js/node/ssr/react/nossr/index.js +28 -0
  57. package/dist/js/node/ssr/react/prerender/index.js +121 -0
  58. package/dist/js/node/ssr/react/prerender/type.js +0 -0
  59. package/dist/js/node/ssr/react/prerender/util.js +119 -0
  60. package/dist/js/node/ssr/serverRender/entry.js +208 -0
  61. package/dist/js/node/ssr/serverRender/helmet.js +52 -0
  62. package/dist/js/node/ssr/serverRender/index.js +46 -0
  63. package/dist/js/node/ssr/serverRender/loadable.js +60 -0
  64. package/dist/js/node/ssr/serverRender/measure.js +20 -0
  65. package/dist/js/node/ssr/serverRender/reduce.js +14 -0
  66. package/dist/js/node/ssr/serverRender/styledComponent.js +18 -0
  67. package/dist/js/node/ssr/serverRender/template.js +103 -0
  68. package/dist/js/node/ssr/serverRender/type.js +15 -0
  69. package/dist/js/node/ssr/utils.js +65 -0
  70. package/dist/js/node/state/cli/index.js +127 -0
  71. package/dist/js/node/state/index.js +30 -0
  72. package/dist/js/node/state/plugins.js +35 -0
  73. package/dist/js/node/state/runtime/index.js +61 -0
  74. package/dist/js/node/state/runtime/plugin.js +101 -0
  75. package/dist/js/node/state/types.js +5 -0
  76. package/dist/js/treeshaking/cli/index.js +4 -4
  77. package/dist/js/treeshaking/common.js +3 -0
  78. package/dist/js/treeshaking/exports/model.js +2 -1
  79. package/dist/js/treeshaking/exports/router.js +2 -1
  80. package/dist/js/treeshaking/exports/server.js +0 -1
  81. package/dist/js/treeshaking/exports/ssr.js +2 -1
  82. package/dist/js/treeshaking/router/cli/index.js +100 -0
  83. package/dist/js/treeshaking/router/index.js +2 -0
  84. package/dist/js/treeshaking/router/runtime/DefaultNotFound.js +15 -0
  85. package/dist/js/treeshaking/router/runtime/index.js +4 -0
  86. package/dist/js/treeshaking/router/runtime/plugin.js +89 -0
  87. package/dist/js/treeshaking/router/runtime/utils.js +106 -0
  88. package/dist/js/treeshaking/ssr/cli/index.js +113 -0
  89. package/dist/js/treeshaking/ssr/index.js +129 -0
  90. package/dist/js/treeshaking/ssr/index.node.js +100 -0
  91. package/dist/js/treeshaking/ssr/prefetch.js +97 -0
  92. package/dist/js/treeshaking/ssr/react/index.js +2 -0
  93. package/dist/js/treeshaking/ssr/react/nossr/index.js +16 -0
  94. package/dist/js/treeshaking/ssr/react/prerender/index.js +119 -0
  95. package/dist/js/treeshaking/ssr/react/prerender/type.js +0 -0
  96. package/dist/js/treeshaking/ssr/react/prerender/util.js +115 -0
  97. package/dist/js/treeshaking/ssr/serverRender/entry.js +267 -0
  98. package/dist/js/treeshaking/ssr/serverRender/helmet.js +37 -0
  99. package/dist/js/treeshaking/ssr/serverRender/index.js +69 -0
  100. package/dist/js/treeshaking/ssr/serverRender/loadable.js +59 -0
  101. package/dist/js/treeshaking/ssr/serverRender/measure.js +17 -0
  102. package/dist/js/treeshaking/ssr/serverRender/reduce.js +11 -0
  103. package/dist/js/treeshaking/ssr/serverRender/styledComponent.js +8 -0
  104. package/dist/js/treeshaking/ssr/serverRender/template.js +111 -0
  105. package/dist/js/treeshaking/ssr/serverRender/type.js +8 -0
  106. package/dist/js/treeshaking/ssr/utils.js +53 -0
  107. package/dist/js/treeshaking/state/cli/index.js +100 -0
  108. package/dist/js/treeshaking/state/index.js +2 -0
  109. package/dist/js/treeshaking/state/plugins.js +13 -0
  110. package/dist/js/treeshaking/state/runtime/index.js +4 -0
  111. package/dist/js/treeshaking/state/runtime/plugin.js +63 -0
  112. package/dist/js/treeshaking/state/types.js +1 -0
  113. package/dist/types/common.d.ts +1 -0
  114. package/dist/types/exports/model.d.ts +2 -1
  115. package/dist/types/exports/router.d.ts +2 -1
  116. package/dist/types/exports/server.d.ts +0 -1
  117. package/dist/types/exports/ssr.d.ts +2 -1
  118. package/dist/types/index.d.ts +8 -2
  119. package/dist/types/router/cli/index.d.ts +5 -0
  120. package/dist/types/router/index.d.ts +2 -0
  121. package/dist/types/router/runtime/DefaultNotFound.d.ts +2 -0
  122. package/dist/types/router/runtime/index.d.ts +6 -0
  123. package/dist/types/router/runtime/plugin.d.ts +51 -0
  124. package/dist/types/router/runtime/utils.d.ts +6 -0
  125. package/dist/types/ssr/cli/index.d.ts +5 -0
  126. package/dist/types/ssr/index.d.ts +18 -0
  127. package/dist/types/ssr/index.node.d.ts +4 -0
  128. package/dist/types/ssr/prefetch.d.ts +13 -0
  129. package/dist/types/ssr/react/index.d.ts +2 -0
  130. package/dist/types/ssr/react/nossr/index.d.ts +2 -0
  131. package/dist/types/ssr/react/prerender/index.d.ts +1 -0
  132. package/dist/types/ssr/react/prerender/type.d.ts +29 -0
  133. package/dist/types/ssr/react/prerender/util.d.ts +6 -0
  134. package/dist/types/ssr/serverRender/entry.d.ts +20 -0
  135. package/dist/types/ssr/serverRender/helmet.d.ts +2 -0
  136. package/dist/types/ssr/serverRender/index.d.ts +8 -0
  137. package/dist/types/ssr/serverRender/loadable.d.ts +2 -0
  138. package/dist/types/ssr/serverRender/measure.d.ts +1 -0
  139. package/dist/types/ssr/serverRender/reduce.d.ts +3 -0
  140. package/dist/types/ssr/serverRender/styledComponent.d.ts +2 -0
  141. package/dist/types/ssr/serverRender/template.d.ts +14 -0
  142. package/dist/types/ssr/serverRender/type.d.ts +32 -0
  143. package/dist/types/ssr/utils.d.ts +8 -0
  144. package/dist/types/state/cli/index.d.ts +5 -0
  145. package/dist/types/state/index.d.ts +2 -0
  146. package/dist/types/state/plugins.d.ts +4 -0
  147. package/dist/types/state/runtime/index.d.ts +4 -0
  148. package/dist/types/state/runtime/plugin.d.ts +17 -0
  149. package/dist/types/state/types.d.ts +17 -0
  150. package/package.json +36 -20
  151. package/types/model.d.ts +1 -1
  152. package/types/state.d.ts +4 -0
  153. package/dist/js/modern/exports/request.js +0 -1
  154. package/dist/js/node/exports/request.js +0 -13
  155. package/dist/js/treeshaking/exports/request.js +0 -1
  156. package/dist/types/exports/request.d.ts +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,72 @@
1
1
  # @modern-js/runtime
2
2
 
3
+ ## 1.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 132812542: fix: plugin runtime dependencies
8
+
9
+ fix: 修复 runtime 插件依赖
10
+
11
+ ## 1.4.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 4fc801f: chore(runtime): merge `@modern-js/plugin-state` to `@modern-js/runtime`
16
+
17
+ chore(runtime): 合并 `@modern-js/plugin-state` 到 `@modern-js/runtime`
18
+
19
+ - 4fc801f: chore(runtime): merge `@modern-js/plugin-router` to `@modern-js/runtime`
20
+
21
+ chore(runtime): 合并 `@modern-js/plugin-router` 到 `@modern-js/runtime`
22
+
23
+ - 4fc801f: chore(runtime): merge `@modern-js/plugin-ssr` to `@modern-js/runtime`
24
+
25
+ chore(runtime): 合并 `@modern-js/plugin-ssr` 到 `@modern-js/runtime`
26
+
27
+ - 4fc801f: chore(runtime): remove `@modern-js/create-request` from `@modern-js/runtime`
28
+
29
+ chore(runtime): `@modern-js/runtime` 中移除 `@modern-js/create-request` 依赖
30
+
31
+ - 16eaebd: fix: package exports field
32
+
33
+ fix: 修复包导出字段
34
+
35
+ - 8f046e8: chore(bff): remove `@modern-js/bff-runtime` from `@modern-js/runtime` dependence
36
+
37
+ chore(bff): `@modern-js/runtime` 中移除 `@modern-js/bff-runtime` 依赖
38
+
39
+ ### Patch Changes
40
+
41
+ - 1421965: fix: ssg build error when multi entries
42
+ fix: 修复多入口下 SSG 构建错误
43
+ - b8ea9cd: fix runtime ssr exports
44
+ - c8614b8: fix: using typeof window to determine the browser environment is not accurate
45
+ fix: 使用 typeof windows 判断浏览器环境不够准确
46
+ - Updated dependencies [1421965]
47
+ - Updated dependencies [02647d2]
48
+ - Updated dependencies [4fc801f]
49
+ - Updated dependencies [9d60891]
50
+ - Updated dependencies [e4b73b2]
51
+ - Updated dependencies [c8614b8]
52
+ - Updated dependencies [df73691]
53
+ - @modern-js/webpack@1.12.2
54
+ - @modern-js/utils@1.8.0
55
+ - @modern-js/runtime-core@1.5.4
56
+
57
+ ## 1.3.5
58
+
59
+ ### Patch Changes
60
+
61
+ - 8d0f1b9: feat: rumtime config types
62
+ feat: 补充 runtime 配置类型
63
+ - Updated dependencies [33cebd2]
64
+ - @modern-js/plugin-ssr@1.5.0
65
+ - @modern-js/plugin-router@1.2.16
66
+ - @modern-js/plugin-state@1.2.10
67
+ - @modern-js/runtime-core@1.5.3
68
+ - @modern-js/utils@1.7.12
69
+
3
70
  ## 1.3.4
4
71
 
5
72
  ### Patch Changes
@@ -1,8 +1,8 @@
1
1
  import path from 'path';
2
2
  import { PLUGIN_SCHEMAS, createRuntimeExportsUtils, cleanRequireCache } from '@modern-js/utils';
3
- import PluginState from '@modern-js/plugin-state/cli';
4
- import PluginRouter from '@modern-js/plugin-router/cli';
5
- import PluginSSR from '@modern-js/plugin-ssr/cli';
3
+ import PluginState from "../state/cli";
4
+ import PluginSSR from "../ssr/cli";
5
+ import PluginRouter from "../router/cli";
6
6
  export default (() => ({
7
7
  name: '@modern-js/runtime',
8
8
  post: ['@modern-js/plugin-router', '@modern-js/plugin-ssr', '@modern-js/plugin-state', '@modern-js/plugin-design-token'],
@@ -43,7 +43,7 @@ export default (() => ({
43
43
  },
44
44
 
45
45
  async beforeRestart() {
46
- cleanRequireCache([require.resolve('@modern-js/plugin-state/cli'), require.resolve('@modern-js/plugin-router/cli'), require.resolve('@modern-js/plugin-ssr/cli')]);
46
+ cleanRequireCache([require.resolve("../state/cli"), require.resolve("../router/cli"), require.resolve("../ssr/cli")]);
47
47
  }
48
48
 
49
49
  };
@@ -0,0 +1 @@
1
+ export const isBrowser = () => typeof window !== 'undefined' && window.name !== 'nodejs';
@@ -1 +1,2 @@
1
- export * from '@modern-js/plugin-state';
1
+ export { default as state } from "../state";
2
+ export * from "../state";
@@ -1 +1,2 @@
1
- export * from '@modern-js/plugin-router';
1
+ export { default as router } from "../router";
2
+ export * from "../router";
@@ -1,2 +1 @@
1
- export * from '@modern-js/bff-runtime';
2
1
  export const hook = attacher => attacher;
@@ -1 +1,2 @@
1
- export * from '@modern-js/plugin-ssr';
1
+ export { default as ssr } from "../ssr";
2
+ export * from "../ssr";
@@ -0,0 +1,106 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import path from 'path';
8
+ import { getEntryOptions, createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
9
+ const PLUGIN_IDENTIFIER = 'router';
10
+ const ROUTES_IDENTIFIER = 'routes';
11
+ export default (() => ({
12
+ name: '@modern-js/plugin-router',
13
+ required: ['@modern-js/runtime'],
14
+ setup: api => {
15
+ const runtimeConfigMap = new Map();
16
+ let pluginsExportsUtils;
17
+ const runtimeModulePath = path.resolve(__dirname, '../');
18
+ return {
19
+ config() {
20
+ const appContext = api.useAppContext();
21
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'plugins');
22
+ return {
23
+ source: {
24
+ alias: {
25
+ '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
26
+ }
27
+ }
28
+ };
29
+ },
30
+
31
+ validateSchema() {
32
+ return PLUGIN_SCHEMAS['@modern-js/plugin-router'];
33
+ },
34
+
35
+ modifyEntryImports({
36
+ entrypoint,
37
+ imports
38
+ }) {
39
+ const {
40
+ entryName,
41
+ fileSystemRoutes
42
+ } = entrypoint;
43
+ const userConfig = api.useResolvedConfigContext();
44
+ const {
45
+ packageName
46
+ } = api.useAppContext();
47
+ const runtimeConfig = getEntryOptions(entryName, userConfig.runtime, userConfig.runtimeByEntries, packageName);
48
+ runtimeConfigMap.set(entryName, runtimeConfig);
49
+
50
+ if (runtimeConfig !== null && runtimeConfig !== void 0 && runtimeConfig.router) {
51
+ imports.push({
52
+ value: '@modern-js/runtime/plugins',
53
+ specifiers: [{
54
+ imported: PLUGIN_IDENTIFIER
55
+ }]
56
+ });
57
+ } else if (fileSystemRoutes) {
58
+ throw new Error(`should enable runtime.router for entry ${entryName}`);
59
+ }
60
+
61
+ return {
62
+ entrypoint,
63
+ imports
64
+ };
65
+ },
66
+
67
+ modifyEntryRuntimePlugins({
68
+ entrypoint,
69
+ plugins
70
+ }) {
71
+ const {
72
+ entryName,
73
+ fileSystemRoutes
74
+ } = entrypoint;
75
+ const {
76
+ serverRoutes
77
+ } = api.useAppContext();
78
+ const runtimeConfig = runtimeConfigMap.get(entryName);
79
+
80
+ if (runtimeConfig.router) {
81
+ // Todo: plugin-router best to only handle manage client route.
82
+ // here support base server route usage, part for compatibility
83
+ const serverBase = serverRoutes.filter(route => route.entryName === entryName).map(route => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
84
+ plugins.push({
85
+ name: PLUGIN_IDENTIFIER,
86
+ options: JSON.stringify(_objectSpread(_objectSpread({
87
+ serverBase
88
+ }, runtimeConfig.router), {}, {
89
+ routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : undefined
90
+ })).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
91
+ });
92
+ }
93
+
94
+ return {
95
+ entrypoint,
96
+ plugins
97
+ };
98
+ },
99
+
100
+ addRuntimeExports() {
101
+ pluginsExportsUtils.addExport(`export { default as router } from '${runtimeModulePath}'`);
102
+ }
103
+
104
+ };
105
+ }
106
+ }));
@@ -0,0 +1,2 @@
1
+ export { default } from "./runtime";
2
+ export * from "./runtime";
@@ -0,0 +1,13 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
+ import React from 'react';
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ export const DefaultNotFound = () => /*#__PURE__*/_jsx("div", {
5
+ style: {
6
+ margin: '150px auto',
7
+ textAlign: 'center',
8
+ display: 'flex',
9
+ alignItems: 'center',
10
+ justifyContent: 'center'
11
+ },
12
+ children: "404"
13
+ });
@@ -0,0 +1,4 @@
1
+ import { routerPlugin } from "./plugin";
2
+ export default routerPlugin;
3
+ export * from 'react-router-dom';
4
+ export * from 'history';
@@ -0,0 +1,88 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import React, { useContext } from 'react';
8
+ import { createBrowserHistory, createHashHistory } from 'history';
9
+ import { Router, StaticRouter } from 'react-router-dom';
10
+ import { RuntimeReactContext } from '@modern-js/runtime-core';
11
+ import hoistNonReactStatics from 'hoist-non-react-statics';
12
+ import { isBrowser } from "../../common";
13
+ import { renderRoutes, getLocation, urlJoin } from "./utils";
14
+ import { jsx as _jsx } from "react/jsx-runtime";
15
+ export const routerPlugin = ({
16
+ serverBase: _serverBase = [],
17
+ history: customHistory,
18
+ supportHtml5History: _supportHtml5History = true,
19
+ routesConfig,
20
+ historyOptions: _historyOptions = {}
21
+ }) => {
22
+ const isBrow = isBrowser();
23
+
24
+ const select = pathname => _serverBase.find(baseUrl => pathname.search(baseUrl) === 0) || '/';
25
+
26
+ return {
27
+ name: '@modern-js/plugin-router',
28
+ setup: () => {
29
+ return {
30
+ hoc: ({
31
+ App
32
+ }, next) => {
33
+ const getRouteApp = () => {
34
+ if (isBrow) {
35
+ var _window$_SERVER_DATA;
36
+
37
+ const baseUrl = ((_window$_SERVER_DATA = window._SERVER_DATA) === null || _window$_SERVER_DATA === void 0 ? void 0 : _window$_SERVER_DATA.router.baseUrl) || select(location.pathname);
38
+ _historyOptions.basename = baseUrl === '/' ? urlJoin(baseUrl, _historyOptions.basename) : baseUrl;
39
+ const history = customHistory || (_supportHtml5History ? createBrowserHistory(_historyOptions) : createHashHistory(_historyOptions));
40
+ return props => /*#__PURE__*/_jsx(Router, {
41
+ history: history,
42
+ children: /*#__PURE__*/_jsx(App, _objectSpread(_objectSpread({}, props), {}, {
43
+ children: routesConfig ? renderRoutes(routesConfig, props) : null
44
+ }))
45
+ });
46
+ }
47
+
48
+ return props => {
49
+ const runtimeContext = useContext(RuntimeReactContext);
50
+ const {
51
+ ssrContext
52
+ } = runtimeContext;
53
+ const location = getLocation(ssrContext);
54
+ const routerContext = (ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.redirection) || {};
55
+ const request = ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.request;
56
+ const baseUrl = request === null || request === void 0 ? void 0 : request.baseUrl;
57
+ const basename = baseUrl === '/' ? urlJoin(baseUrl, _historyOptions.basename) : baseUrl;
58
+ return /*#__PURE__*/_jsx(StaticRouter, {
59
+ basename: basename === '/' ? '' : basename,
60
+ location: location,
61
+ context: routerContext,
62
+ children: /*#__PURE__*/_jsx(App, _objectSpread(_objectSpread({}, props), {}, {
63
+ children: routesConfig ? renderRoutes(routesConfig, props) : null
64
+ }))
65
+ });
66
+ };
67
+ };
68
+
69
+ let RouteApp = getRouteApp();
70
+
71
+ if (App) {
72
+ RouteApp = hoistNonReactStatics(RouteApp, App);
73
+ }
74
+
75
+ if (routesConfig !== null && routesConfig !== void 0 && routesConfig.globalApp) {
76
+ return next({
77
+ App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
78
+ });
79
+ }
80
+
81
+ return next({
82
+ App: RouteApp
83
+ });
84
+ }
85
+ };
86
+ }
87
+ };
88
+ };
@@ -0,0 +1,108 @@
1
+ const _excluded = ["Component"];
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
7
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
+
9
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
10
+
11
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
+ import React from 'react';
15
+ import { Route, matchPath } from 'react-router-dom';
16
+ import { DefaultNotFound } from "./DefaultNotFound";
17
+ import { jsx as _jsx } from "react/jsx-runtime";
18
+ export function renderRoutes(routesConfig, extraProps = {}) {
19
+ const Layout = _ref => {
20
+ let {
21
+ Component
22
+ } = _ref,
23
+ props = _objectWithoutProperties(_ref, _excluded);
24
+
25
+ const GlobalLayout = routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp;
26
+
27
+ if (!GlobalLayout) {
28
+ return /*#__PURE__*/_jsx(Component, _objectSpread({}, props));
29
+ }
30
+
31
+ return /*#__PURE__*/_jsx(GlobalLayout, _objectSpread({
32
+ Component: Component
33
+ }, props));
34
+ };
35
+
36
+ const findMatchedRoute = pathname => {
37
+ var _routesConfig$routes;
38
+
39
+ return routesConfig === null || routesConfig === void 0 ? void 0 : (_routesConfig$routes = routesConfig.routes) === null || _routesConfig$routes === void 0 ? void 0 : _routesConfig$routes.find(route => {
40
+ const info = matchPath(pathname, {
41
+ path: route.path,
42
+ exact: route.exact,
43
+ sensitive: route.sensitive
44
+ });
45
+ return Boolean(info);
46
+ });
47
+ };
48
+
49
+ return /*#__PURE__*/_jsx(Route, {
50
+ path: "/",
51
+ render: props => {
52
+ const matchedRoute = findMatchedRoute(props.location.pathname);
53
+
54
+ if (!matchedRoute) {
55
+ return /*#__PURE__*/_jsx(DefaultNotFound, {});
56
+ }
57
+
58
+ return /*#__PURE__*/_jsx(Route, {
59
+ path: matchedRoute.path,
60
+ exact: matchedRoute.exact,
61
+ sensitive: matchedRoute.sensitive,
62
+ render: routeProps => /*#__PURE__*/_jsx(Layout, _objectSpread(_objectSpread({
63
+ Component: matchedRoute.component
64
+ }, routeProps), extraProps))
65
+ });
66
+ }
67
+ });
68
+ }
69
+ export function getLocation(serverContext) {
70
+ const {
71
+ pathname,
72
+ url
73
+ } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
74
+ const cleanUrl = url.replace('http://', '').replace('https://', '');
75
+ const index = (cleanUrl || '').indexOf(pathname);
76
+
77
+ if (index === -1) {
78
+ return pathname;
79
+ }
80
+
81
+ return cleanUrl.substring(index);
82
+ }
83
+ export const urlJoin = (...parts) => {
84
+ const separator = '/';
85
+ const replace = new RegExp(`${separator}{1,}`, 'g');
86
+ return standardSlash(parts.join(separator).replace(replace, separator));
87
+ };
88
+ export function standardSlash(str) {
89
+ let addr = str;
90
+
91
+ if (!addr || typeof addr !== 'string') {
92
+ return addr;
93
+ }
94
+
95
+ if (addr.startsWith('.')) {
96
+ addr = addr.slice(1);
97
+ }
98
+
99
+ if (!addr.startsWith('/')) {
100
+ addr = `/${addr}`;
101
+ }
102
+
103
+ if (addr.endsWith('/') && addr !== '/') {
104
+ addr = addr.slice(0, addr.length - 1);
105
+ }
106
+
107
+ return addr;
108
+ }
@@ -0,0 +1,120 @@
1
+ import path from 'path';
2
+ import { getEntryOptions, SERVER_RENDER_FUNCTION_NAME, LOADABLE_STATS_FILE, isUseSSRBundle, createRuntimeExportsUtils, isSingleEntry } from '@modern-js/utils';
3
+ const PLUGIN_IDENTIFIER = 'ssr';
4
+ export default (() => ({
5
+ name: '@modern-js/plugin-ssr',
6
+ required: ['@modern-js/runtime'],
7
+ setup: api => {
8
+ const ssrConfigMap = new Map();
9
+ let pluginsExportsUtils;
10
+ const ssrModulePath = path.resolve(__dirname, '../');
11
+ return {
12
+ config() {
13
+ const appContext = api.useAppContext();
14
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'plugins');
15
+ return {
16
+ source: {
17
+ alias: {
18
+ '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
19
+ }
20
+ },
21
+ tools: {
22
+ webpackChain: (chain, {
23
+ name,
24
+ CHAIN_ID
25
+ }) => {
26
+ const userConfig = api.useResolvedConfigContext();
27
+
28
+ if (isUseSSRBundle(userConfig) && name !== 'server') {
29
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
30
+ const LoadableWebpackPlugin = require('@modern-js/webpack/@loadable/webpack-plugin');
31
+
32
+ chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableWebpackPlugin, [{
33
+ filename: LOADABLE_STATS_FILE
34
+ }]);
35
+ }
36
+ },
37
+ babel: config => {
38
+ const userConfig = api.useResolvedConfigContext();
39
+
40
+ if (isUseSSRBundle(userConfig)) {
41
+ config.plugins.push(require.resolve('@loadable/babel-plugin'));
42
+ }
43
+ }
44
+ }
45
+ };
46
+ },
47
+
48
+ modifyEntryImports({
49
+ entrypoint,
50
+ imports
51
+ }) {
52
+ const {
53
+ entryName
54
+ } = entrypoint;
55
+ const userConfig = api.useResolvedConfigContext();
56
+ const {
57
+ packageName,
58
+ entrypoints
59
+ } = api.useAppContext();
60
+ pluginsExportsUtils.addExport(`export { default as ssr } from '${ssrModulePath}'`); // if use ssg then set ssr config to true
61
+
62
+ const ssrConfig = getEntryOptions(entryName, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
63
+ const ssgConfig = userConfig.output.ssg;
64
+ const useSSG = isSingleEntry(entrypoints) ? Boolean(ssgConfig) : ssgConfig === true || typeof (ssgConfig === null || ssgConfig === void 0 ? void 0 : ssgConfig[0]) === 'function' || Boolean(ssgConfig === null || ssgConfig === void 0 ? void 0 : ssgConfig[entryName]);
65
+ ssrConfigMap.set(entryName, ssrConfig || useSSG);
66
+
67
+ if (ssrConfig || useSSG) {
68
+ imports.push({
69
+ value: '@modern-js/runtime/plugins',
70
+ specifiers: [{
71
+ imported: PLUGIN_IDENTIFIER
72
+ }]
73
+ });
74
+ }
75
+
76
+ return {
77
+ entrypoint,
78
+ imports
79
+ };
80
+ },
81
+
82
+ modifyEntryRuntimePlugins({
83
+ entrypoint,
84
+ plugins
85
+ }) {
86
+ if (ssrConfigMap.get(entrypoint.entryName)) {
87
+ plugins.push({
88
+ name: PLUGIN_IDENTIFIER,
89
+ options: ssrConfigMap.get(entrypoint.entryName)
90
+ });
91
+ }
92
+
93
+ return {
94
+ entrypoint,
95
+ plugins
96
+ };
97
+ },
98
+
99
+ modifyEntryExport({
100
+ entrypoint,
101
+ exportStatement
102
+ }) {
103
+ if (ssrConfigMap.get(entrypoint.entryName)) {
104
+ return {
105
+ entrypoint,
106
+ exportStatement: [`export function ${SERVER_RENDER_FUNCTION_NAME}(context) {
107
+ return bootstrap(AppWrapper, context)
108
+ }`, exportStatement].join('\n')
109
+ };
110
+ }
111
+
112
+ return {
113
+ entrypoint,
114
+ exportStatement
115
+ };
116
+ }
117
+
118
+ };
119
+ }
120
+ }));
@@ -0,0 +1,109 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import ReactDOM from 'react-dom';
8
+ import { loadableReady } from '@loadable/component';
9
+ import { RenderLevel } from "./serverRender/type";
10
+ import { formatClient, mockResponse } from "./utils";
11
+ import { jsx as _jsx } from "react/jsx-runtime";
12
+
13
+ const ssr = () => ({
14
+ name: '@modern-js/plugin-ssr',
15
+ setup: () => {
16
+ const mockResp = mockResponse();
17
+ return {
18
+ client: async ({
19
+ App,
20
+ context,
21
+ rootElement
22
+ }) => {
23
+ var _window, _window$_SSR_DATA;
24
+
25
+ const renderLevel = (_window = window) === null || _window === void 0 ? void 0 : (_window$_SSR_DATA = _window._SSR_DATA) === null || _window$_SSR_DATA === void 0 ? void 0 : _window$_SSR_DATA.renderLevel;
26
+
27
+ if (renderLevel === RenderLevel.CLIENT_RENDER) {
28
+ var _prefetch, _ref;
29
+
30
+ await (App === null || App === void 0 ? void 0 : (_prefetch = (_ref = App).prefetch) === null || _prefetch === void 0 ? void 0 : _prefetch.call(_ref, context));
31
+ ReactDOM.render( /*#__PURE__*/_jsx(App, {
32
+ context: context
33
+ }), rootElement);
34
+ } else if (renderLevel === RenderLevel.SERVER_RENDER) {
35
+ loadableReady(() => {
36
+ const hydrateContext = _objectSpread(_objectSpread({}, context), {}, {
37
+ _hydration: true
38
+ });
39
+
40
+ ReactDOM.hydrate( /*#__PURE__*/_jsx(App, {
41
+ context: hydrateContext
42
+ }), rootElement, () => {
43
+ // won't cause component re-render because context's reference identity doesn't change
44
+ delete hydrateContext._hydration;
45
+ });
46
+ });
47
+ } else {
48
+ // unknown renderlevel or renderlevel is server prefetch.
49
+ ReactDOM.render( /*#__PURE__*/_jsx(App, {
50
+ context: context
51
+ }), rootElement);
52
+ }
53
+ },
54
+
55
+ init({
56
+ context
57
+ }, next) {
58
+ var _window2, _window2$_SSR_DATA, _window2$_SSR_DATA$co;
59
+
60
+ const request = (_window2 = window) === null || _window2 === void 0 ? void 0 : (_window2$_SSR_DATA = _window2._SSR_DATA) === null || _window2$_SSR_DATA === void 0 ? void 0 : (_window2$_SSR_DATA$co = _window2$_SSR_DATA.context) === null || _window2$_SSR_DATA$co === void 0 ? void 0 : _window2$_SSR_DATA$co.request;
61
+
62
+ if (!request) {
63
+ return next({
64
+ context
65
+ });
66
+ }
67
+
68
+ context.ssrContext.response = mockResp;
69
+ context.ssrContext.request = formatClient(request);
70
+ return next({
71
+ context
72
+ });
73
+ },
74
+
75
+ pickContext: ({
76
+ context,
77
+ pickedContext
78
+ }, next) => {
79
+ var _window3, _window3$_SSR_DATA, _window3$_SSR_DATA$co;
80
+
81
+ const request = (_window3 = window) === null || _window3 === void 0 ? void 0 : (_window3$_SSR_DATA = _window3._SSR_DATA) === null || _window3$_SSR_DATA === void 0 ? void 0 : (_window3$_SSR_DATA$co = _window3$_SSR_DATA.context) === null || _window3$_SSR_DATA$co === void 0 ? void 0 : _window3$_SSR_DATA$co.request;
82
+ const {
83
+ initialData
84
+ } = context;
85
+
86
+ if (!request) {
87
+ return next({
88
+ context,
89
+ pickedContext: _objectSpread(_objectSpread({}, pickedContext), {}, {
90
+ initialData
91
+ })
92
+ });
93
+ }
94
+
95
+ return next({
96
+ context,
97
+ pickedContext: _objectSpread(_objectSpread({}, pickedContext), {}, {
98
+ initialData,
99
+ request,
100
+ response: mockResp
101
+ })
102
+ });
103
+ }
104
+ };
105
+ }
106
+ });
107
+
108
+ export default ssr;
109
+ export * from "./react";