@modern-js/runtime 1.3.5 → 1.4.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.
- package/CHANGELOG.md +46 -0
- package/dist/js/modern/cli/index.js +4 -4
- package/dist/js/modern/common.js +1 -0
- package/dist/js/modern/exports/model.js +2 -1
- package/dist/js/modern/exports/router.js +2 -1
- package/dist/js/modern/exports/server.js +0 -1
- package/dist/js/modern/exports/ssr.js +2 -1
- package/dist/js/modern/router/cli/index.js +106 -0
- package/dist/js/modern/router/index.js +2 -0
- package/dist/js/modern/router/runtime/DefaultNotFound.js +13 -0
- package/dist/js/modern/router/runtime/index.js +4 -0
- package/dist/js/modern/router/runtime/plugin.js +88 -0
- package/dist/js/modern/router/runtime/utils.js +108 -0
- package/dist/js/modern/ssr/cli/index.js +120 -0
- package/dist/js/modern/ssr/index.js +109 -0
- package/dist/js/modern/ssr/index.node.js +78 -0
- package/dist/js/modern/ssr/prefetch.js +58 -0
- package/dist/js/modern/ssr/react/index.js +2 -0
- package/dist/js/modern/ssr/react/nossr/index.js +13 -0
- package/dist/js/modern/ssr/react/prerender/index.js +104 -0
- package/dist/js/modern/ssr/react/prerender/type.js +0 -0
- package/dist/js/modern/ssr/react/prerender/util.js +99 -0
- package/dist/js/modern/ssr/serverRender/entry.js +179 -0
- package/dist/js/modern/ssr/serverRender/helmet.js +46 -0
- package/dist/js/modern/ssr/serverRender/index.js +29 -0
- package/dist/js/modern/ssr/serverRender/loadable.js +48 -0
- package/dist/js/modern/ssr/serverRender/measure.js +11 -0
- package/dist/js/modern/ssr/serverRender/reduce.js +7 -0
- package/dist/js/modern/ssr/serverRender/styledComponent.js +8 -0
- package/dist/js/modern/ssr/serverRender/template.js +90 -0
- package/dist/js/modern/ssr/serverRender/type.js +8 -0
- package/dist/js/modern/ssr/utils.js +51 -0
- package/dist/js/modern/state/cli/index.js +110 -0
- package/dist/js/modern/state/index.js +2 -0
- package/dist/js/modern/state/plugins.js +7 -0
- package/dist/js/modern/state/runtime/index.js +4 -0
- package/dist/js/modern/state/runtime/plugin.js +71 -0
- package/dist/js/modern/state/types.js +1 -0
- package/dist/js/node/cli/index.js +5 -5
- package/dist/js/node/common.js +10 -0
- package/dist/js/node/exports/model.js +19 -5
- package/dist/js/node/exports/router.js +19 -5
- package/dist/js/node/exports/server.js +0 -17
- package/dist/js/node/exports/ssr.js +19 -5
- package/dist/js/node/router/cli/index.js +122 -0
- package/dist/js/node/router/index.js +30 -0
- package/dist/js/node/router/runtime/DefaultNotFound.js +26 -0
- package/dist/js/node/router/runtime/index.js +39 -0
- package/dist/js/node/router/runtime/plugin.js +111 -0
- package/dist/js/node/router/runtime/utils.js +128 -0
- package/dist/js/node/ssr/cli/index.js +136 -0
- package/dist/js/node/ssr/index.js +138 -0
- package/dist/js/node/ssr/index.node.js +105 -0
- package/dist/js/node/ssr/prefetch.js +75 -0
- package/dist/js/node/ssr/react/index.js +21 -0
- package/dist/js/node/ssr/react/nossr/index.js +28 -0
- package/dist/js/node/ssr/react/prerender/index.js +121 -0
- package/dist/js/node/ssr/react/prerender/type.js +0 -0
- package/dist/js/node/ssr/react/prerender/util.js +119 -0
- package/dist/js/node/ssr/serverRender/entry.js +208 -0
- package/dist/js/node/ssr/serverRender/helmet.js +52 -0
- package/dist/js/node/ssr/serverRender/index.js +46 -0
- package/dist/js/node/ssr/serverRender/loadable.js +60 -0
- package/dist/js/node/ssr/serverRender/measure.js +20 -0
- package/dist/js/node/ssr/serverRender/reduce.js +14 -0
- package/dist/js/node/ssr/serverRender/styledComponent.js +18 -0
- package/dist/js/node/ssr/serverRender/template.js +103 -0
- package/dist/js/node/ssr/serverRender/type.js +15 -0
- package/dist/js/node/ssr/utils.js +65 -0
- package/dist/js/node/state/cli/index.js +127 -0
- package/dist/js/node/state/index.js +30 -0
- package/dist/js/node/state/plugins.js +35 -0
- package/dist/js/node/state/runtime/index.js +61 -0
- package/dist/js/node/state/runtime/plugin.js +101 -0
- package/dist/js/node/state/types.js +5 -0
- package/dist/js/treeshaking/cli/index.js +4 -4
- package/dist/js/treeshaking/common.js +3 -0
- package/dist/js/treeshaking/exports/model.js +2 -1
- package/dist/js/treeshaking/exports/router.js +2 -1
- package/dist/js/treeshaking/exports/server.js +0 -1
- package/dist/js/treeshaking/exports/ssr.js +2 -1
- package/dist/js/treeshaking/router/cli/index.js +100 -0
- package/dist/js/treeshaking/router/index.js +2 -0
- package/dist/js/treeshaking/router/runtime/DefaultNotFound.js +15 -0
- package/dist/js/treeshaking/router/runtime/index.js +4 -0
- package/dist/js/treeshaking/router/runtime/plugin.js +89 -0
- package/dist/js/treeshaking/router/runtime/utils.js +106 -0
- package/dist/js/treeshaking/ssr/cli/index.js +113 -0
- package/dist/js/treeshaking/ssr/index.js +129 -0
- package/dist/js/treeshaking/ssr/index.node.js +100 -0
- package/dist/js/treeshaking/ssr/prefetch.js +97 -0
- package/dist/js/treeshaking/ssr/react/index.js +2 -0
- package/dist/js/treeshaking/ssr/react/nossr/index.js +16 -0
- package/dist/js/treeshaking/ssr/react/prerender/index.js +119 -0
- package/dist/js/treeshaking/ssr/react/prerender/type.js +0 -0
- package/dist/js/treeshaking/ssr/react/prerender/util.js +115 -0
- package/dist/js/treeshaking/ssr/serverRender/entry.js +267 -0
- package/dist/js/treeshaking/ssr/serverRender/helmet.js +37 -0
- package/dist/js/treeshaking/ssr/serverRender/index.js +69 -0
- package/dist/js/treeshaking/ssr/serverRender/loadable.js +59 -0
- package/dist/js/treeshaking/ssr/serverRender/measure.js +17 -0
- package/dist/js/treeshaking/ssr/serverRender/reduce.js +11 -0
- package/dist/js/treeshaking/ssr/serverRender/styledComponent.js +8 -0
- package/dist/js/treeshaking/ssr/serverRender/template.js +111 -0
- package/dist/js/treeshaking/ssr/serverRender/type.js +8 -0
- package/dist/js/treeshaking/ssr/utils.js +53 -0
- package/dist/js/treeshaking/state/cli/index.js +100 -0
- package/dist/js/treeshaking/state/index.js +2 -0
- package/dist/js/treeshaking/state/plugins.js +13 -0
- package/dist/js/treeshaking/state/runtime/index.js +4 -0
- package/dist/js/treeshaking/state/runtime/plugin.js +63 -0
- package/dist/js/treeshaking/state/types.js +1 -0
- package/dist/types/common.d.ts +1 -0
- package/dist/types/exports/model.d.ts +2 -1
- package/dist/types/exports/router.d.ts +2 -1
- package/dist/types/exports/server.d.ts +0 -1
- package/dist/types/exports/ssr.d.ts +2 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/router/cli/index.d.ts +5 -0
- package/dist/types/router/index.d.ts +2 -0
- package/dist/types/router/runtime/DefaultNotFound.d.ts +2 -0
- package/dist/types/router/runtime/index.d.ts +6 -0
- package/dist/types/router/runtime/plugin.d.ts +51 -0
- package/dist/types/router/runtime/utils.d.ts +6 -0
- package/dist/types/ssr/cli/index.d.ts +5 -0
- package/dist/types/ssr/index.d.ts +18 -0
- package/dist/types/ssr/index.node.d.ts +4 -0
- package/dist/types/ssr/prefetch.d.ts +13 -0
- package/dist/types/ssr/react/index.d.ts +2 -0
- package/dist/types/ssr/react/nossr/index.d.ts +2 -0
- package/dist/types/ssr/react/prerender/index.d.ts +1 -0
- package/dist/types/ssr/react/prerender/type.d.ts +29 -0
- package/dist/types/ssr/react/prerender/util.d.ts +6 -0
- package/dist/types/ssr/serverRender/entry.d.ts +20 -0
- package/dist/types/ssr/serverRender/helmet.d.ts +2 -0
- package/dist/types/ssr/serverRender/index.d.ts +8 -0
- package/dist/types/ssr/serverRender/loadable.d.ts +2 -0
- package/dist/types/ssr/serverRender/measure.d.ts +1 -0
- package/dist/types/ssr/serverRender/reduce.d.ts +3 -0
- package/dist/types/ssr/serverRender/styledComponent.d.ts +2 -0
- package/dist/types/ssr/serverRender/template.d.ts +14 -0
- package/dist/types/ssr/serverRender/type.d.ts +32 -0
- package/dist/types/ssr/utils.d.ts +8 -0
- package/dist/types/state/cli/index.d.ts +5 -0
- package/dist/types/state/index.d.ts +2 -0
- package/dist/types/state/plugins.d.ts +4 -0
- package/dist/types/state/runtime/index.d.ts +4 -0
- package/dist/types/state/runtime/plugin.d.ts +17 -0
- package/dist/types/state/types.d.ts +17 -0
- package/package.json +37 -20
- package/types/model.d.ts +1 -1
- package/types/state.d.ts +4 -0
- package/dist/js/modern/exports/request.js +0 -1
- package/dist/js/node/exports/request.js +0 -13
- package/dist/js/treeshaking/exports/request.js +0 -1
- package/dist/types/exports/request.d.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# @modern-js/runtime
|
|
2
2
|
|
|
3
|
+
## 1.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 4fc801f: chore(runtime): merge `@modern-js/plugin-state` to `@modern-js/runtime`
|
|
8
|
+
|
|
9
|
+
chore(runtime): 合并 `@modern-js/plugin-state` 到 `@modern-js/runtime`
|
|
10
|
+
|
|
11
|
+
- 4fc801f: chore(runtime): merge `@modern-js/plugin-router` to `@modern-js/runtime`
|
|
12
|
+
|
|
13
|
+
chore(runtime): 合并 `@modern-js/plugin-router` 到 `@modern-js/runtime`
|
|
14
|
+
|
|
15
|
+
- 4fc801f: chore(runtime): merge `@modern-js/plugin-ssr` to `@modern-js/runtime`
|
|
16
|
+
|
|
17
|
+
chore(runtime): 合并 `@modern-js/plugin-ssr` 到 `@modern-js/runtime`
|
|
18
|
+
|
|
19
|
+
- 4fc801f: chore(runtime): remove `@modern-js/create-request` from `@modern-js/runtime`
|
|
20
|
+
|
|
21
|
+
chore(runtime): `@modern-js/runtime` 中移除 `@modern-js/create-request` 依赖
|
|
22
|
+
|
|
23
|
+
- 16eaebd: fix: package exports field
|
|
24
|
+
|
|
25
|
+
fix: 修复包导出字段
|
|
26
|
+
|
|
27
|
+
- 8f046e8: chore(bff): remove `@modern-js/bff-runtime` from `@modern-js/runtime` dependence
|
|
28
|
+
|
|
29
|
+
chore(bff): `@modern-js/runtime` 中移除 `@modern-js/bff-runtime` 依赖
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- 1421965: fix: ssg build error when multi entries
|
|
34
|
+
fix: 修复多入口下 SSG 构建错误
|
|
35
|
+
- b8ea9cd: fix runtime ssr exports
|
|
36
|
+
- c8614b8: fix: using typeof window to determine the browser environment is not accurate
|
|
37
|
+
fix: 使用 typeof windows 判断浏览器环境不够准确
|
|
38
|
+
- Updated dependencies [1421965]
|
|
39
|
+
- Updated dependencies [02647d2]
|
|
40
|
+
- Updated dependencies [4fc801f]
|
|
41
|
+
- Updated dependencies [9d60891]
|
|
42
|
+
- Updated dependencies [e4b73b2]
|
|
43
|
+
- Updated dependencies [c8614b8]
|
|
44
|
+
- Updated dependencies [df73691]
|
|
45
|
+
- @modern-js/webpack@1.12.2
|
|
46
|
+
- @modern-js/utils@1.8.0
|
|
47
|
+
- @modern-js/runtime-core@1.5.4
|
|
48
|
+
|
|
3
49
|
## 1.3.5
|
|
4
50
|
|
|
5
51
|
### 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
|
|
4
|
-
import
|
|
5
|
-
import
|
|
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(
|
|
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
|
|
1
|
+
export { default as state } from "../state";
|
|
2
|
+
export * from "../state";
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { default as router } from "../router";
|
|
2
|
+
export * from "../router";
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
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,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,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";
|