@modern-js/runtime 2.26.0 → 2.28.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 +30 -0
- package/dist/cjs/core/compatible.js +16 -17
- package/dist/cjs/core/loader/useLoader.js +10 -12
- package/dist/cjs/core/plugin.js +3 -2
- package/dist/cjs/document/Html.js +4 -3
- package/dist/cjs/document/cli/index.js +138 -138
- package/dist/cjs/router/cli/index.js +91 -92
- package/dist/cjs/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/cjs/router/runtime/PrefetchLink.js +8 -4
- package/dist/cjs/router/runtime/plugin.js +4 -3
- package/dist/cjs/router/runtime/plugin.node.js +11 -4
- package/dist/cjs/router/runtime/utils.js +3 -3
- package/dist/cjs/ssr/cli/index.js +122 -123
- package/dist/cjs/ssr/index.js +105 -107
- package/dist/cjs/ssr/index.node.js +37 -38
- package/dist/cjs/ssr/prefetch.js +37 -39
- package/dist/cjs/ssr/react/prerender/util.js +4 -2
- package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
- package/dist/cjs/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/cjs/state/cli/index.js +70 -72
- package/dist/cjs/state/runtime/plugin.js +40 -42
- package/dist/esm/core/compatible.js +10 -9
- package/dist/esm/core/loader/useLoader.js +9 -9
- package/dist/esm/core/plugin.js +3 -2
- package/dist/esm/document/Html.js +4 -3
- package/dist/esm/document/cli/index.js +4 -3
- package/dist/esm/router/cli/index.js +2 -1
- package/dist/esm/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/esm/router/runtime/PrefetchLink.js +8 -4
- package/dist/esm/router/runtime/plugin.js +4 -3
- package/dist/esm/router/runtime/plugin.node.js +11 -4
- package/dist/esm/router/runtime/utils.js +3 -3
- package/dist/esm/ssr/cli/index.js +7 -6
- package/dist/esm/ssr/index.js +6 -6
- package/dist/esm/ssr/index.node.js +2 -1
- package/dist/esm/ssr/prefetch.js +2 -2
- package/dist/esm/ssr/react/prerender/util.js +4 -2
- package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +7 -7
- package/dist/esm/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/esm/state/runtime/plugin.js +2 -2
- package/dist/esm-node/core/compatible.js +16 -17
- package/dist/esm-node/core/loader/useLoader.js +10 -12
- package/dist/esm-node/core/plugin.js +3 -2
- package/dist/esm-node/document/Html.js +4 -3
- package/dist/esm-node/document/cli/index.js +138 -138
- package/dist/esm-node/router/cli/index.js +91 -92
- package/dist/esm-node/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/esm-node/router/runtime/PrefetchLink.js +8 -4
- package/dist/esm-node/router/runtime/plugin.js +4 -3
- package/dist/esm-node/router/runtime/plugin.node.js +11 -4
- package/dist/esm-node/router/runtime/utils.js +3 -3
- package/dist/esm-node/ssr/cli/index.js +122 -123
- package/dist/esm-node/ssr/cli/loadable-bundler-plugin.js +2 -2
- package/dist/esm-node/ssr/index.js +105 -107
- package/dist/esm-node/ssr/index.node.js +37 -38
- package/dist/esm-node/ssr/prefetch.js +37 -39
- package/dist/esm-node/ssr/react/prerender/util.js +4 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
- package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/esm-node/state/cli/index.js +70 -72
- package/dist/esm-node/state/runtime/plugin.js +40 -42
- package/package.json +13 -10
|
@@ -2,12 +2,13 @@ import path from "path";
|
|
|
2
2
|
import { getEntryOptions, SERVER_RENDER_FUNCTION_NAME, LOADABLE_STATS_FILE, isUseSSRBundle, createRuntimeExportsUtils, isSSGEntry } from "@modern-js/utils";
|
|
3
3
|
const PLUGIN_IDENTIFIER = "ssr";
|
|
4
4
|
const hasStringSSREntry = (userConfig) => {
|
|
5
|
+
var _server, _server1;
|
|
5
6
|
const isStreaming = (ssr) => ssr && typeof ssr === "object" && ssr.mode === "stream";
|
|
6
7
|
const { server } = userConfig;
|
|
7
|
-
if ((server === null ||
|
|
8
|
+
if (((_server = server) === null || _server === void 0 ? void 0 : _server.ssr) && !isStreaming(server.ssr)) {
|
|
8
9
|
return true;
|
|
9
10
|
}
|
|
10
|
-
if ((server === null ||
|
|
11
|
+
if (((_server1 = server) === null || _server1 === void 0 ? void 0 : _server1.ssrByEntries) && typeof server.ssrByEntries === "object") {
|
|
11
12
|
for (const name of Object.keys(server.ssrByEntries)) {
|
|
12
13
|
if (!isStreaming(server.ssrByEntries[name])) {
|
|
13
14
|
return true;
|
|
@@ -16,144 +17,142 @@ const hasStringSSREntry = (userConfig) => {
|
|
|
16
17
|
}
|
|
17
18
|
return false;
|
|
18
19
|
};
|
|
19
|
-
export const ssrPlugin = () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
20
|
+
export const ssrPlugin = () => ({
|
|
21
|
+
name: "@modern-js/plugin-ssr",
|
|
22
|
+
required: [
|
|
23
|
+
"@modern-js/runtime"
|
|
24
|
+
],
|
|
25
|
+
setup: (api) => {
|
|
26
|
+
const ssrConfigMap = /* @__PURE__ */ new Map();
|
|
27
|
+
let pluginsExportsUtils;
|
|
28
|
+
return {
|
|
29
|
+
config() {
|
|
30
|
+
const appContext = api.useAppContext();
|
|
31
|
+
pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
|
|
32
|
+
const userConfig = api.useConfigContext();
|
|
33
|
+
const { bundlerType = "webpack" } = api.useAppContext();
|
|
34
|
+
const babelConfig = (() => {
|
|
35
|
+
if (bundlerType === "webpack") {
|
|
36
|
+
return (config) => {
|
|
37
|
+
var _config_plugins;
|
|
38
|
+
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
39
|
+
if (isUseSSRBundle(userConfig) && hasStringSSREntry(userConfig)) {
|
|
40
|
+
var _config_plugins1;
|
|
41
|
+
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
} else if (bundlerType === "rspack") {
|
|
45
|
+
if (isUseSSRBundle(userConfig)) {
|
|
36
46
|
return (config) => {
|
|
37
47
|
var _config_plugins;
|
|
38
48
|
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
39
|
-
if (
|
|
49
|
+
if (hasStringSSREntry(userConfig)) {
|
|
40
50
|
var _config_plugins1;
|
|
41
51
|
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
42
52
|
}
|
|
43
53
|
};
|
|
44
|
-
} else if (bundlerType === "rspack") {
|
|
45
|
-
if (isUseSSRBundle(userConfig)) {
|
|
46
|
-
return (config) => {
|
|
47
|
-
var _config_plugins;
|
|
48
|
-
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(path.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
49
|
-
if (hasStringSSREntry(userConfig)) {
|
|
50
|
-
var _config_plugins1;
|
|
51
|
-
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
54
|
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
globalVars: (values, { target }) => {
|
|
65
|
-
values["process.env.MODERN_TARGET"] = target === "node" || target === "service-worker" ? "node" : "browser";
|
|
66
|
-
}
|
|
55
|
+
}
|
|
56
|
+
})();
|
|
57
|
+
return {
|
|
58
|
+
source: {
|
|
59
|
+
alias: {
|
|
60
|
+
// ensure that all packages use the same storage in @modern-js/utils/runtime-node
|
|
61
|
+
"@modern-js/utils/runtime-node$": require.resolve("@modern-js/utils/runtime-node"),
|
|
62
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
|
|
67
63
|
},
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const userConfig2 = api.useResolvedConfigContext();
|
|
71
|
-
if (isUseSSRBundle(userConfig2) && !isServer && !isServiceWorker && hasStringSSREntry(userConfig2)) {
|
|
72
|
-
const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
|
|
73
|
-
chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableBundlerPlugin, [
|
|
74
|
-
{
|
|
75
|
-
filename: LOADABLE_STATS_FILE
|
|
76
|
-
}
|
|
77
|
-
]);
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
babel: babelConfig
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
modifyEntryImports({ entrypoint, imports }) {
|
|
85
|
-
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
86
|
-
const userConfig = api.useResolvedConfigContext();
|
|
87
|
-
const { packageName, entrypoints } = api.useAppContext();
|
|
88
|
-
pluginsExportsUtils.addExport(`export { default as ssr } from '@modern-js/runtime/ssr'`);
|
|
89
|
-
const ssrConfig = getEntryOptions(entryName, isMainEntry, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
|
|
90
|
-
if (typeof ssrConfig === "object" && ssrConfig.mode === "stream") {
|
|
91
|
-
var _ref;
|
|
92
|
-
const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
93
|
-
if (((_ref = runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.router) === null || _ref === void 0 ? void 0 : _ref.mode) === "react-router-5") {
|
|
94
|
-
throw new Error(`router v5 plugin doesn't support streaming SSR, check your config 'runtime.router'`);
|
|
95
|
-
}
|
|
96
|
-
if (fileSystemRoutes && !entrypoint.nestedRoutesEntry) {
|
|
97
|
-
throw new Error("You should switch to file-system based router to support streaming SSR.");
|
|
64
|
+
globalVars: (values, { target }) => {
|
|
65
|
+
values["process.env.MODERN_TARGET"] = target === "node" || target === "service-worker" ? "node" : "browser";
|
|
98
66
|
}
|
|
67
|
+
},
|
|
68
|
+
tools: {
|
|
69
|
+
bundlerChain(chain, { isServer, isServiceWorker, CHAIN_ID }) {
|
|
70
|
+
const userConfig2 = api.useResolvedConfigContext();
|
|
71
|
+
if (isUseSSRBundle(userConfig2) && !isServer && !isServiceWorker && hasStringSSREntry(userConfig2)) {
|
|
72
|
+
const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
|
|
73
|
+
chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableBundlerPlugin, [
|
|
74
|
+
{
|
|
75
|
+
filename: LOADABLE_STATS_FILE
|
|
76
|
+
}
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
babel: babelConfig
|
|
99
81
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
modifyEntryImports({ entrypoint, imports }) {
|
|
85
|
+
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
86
|
+
const userConfig = api.useResolvedConfigContext();
|
|
87
|
+
const { packageName, entrypoints } = api.useAppContext();
|
|
88
|
+
pluginsExportsUtils.addExport(`export { default as ssr } from '@modern-js/runtime/ssr'`);
|
|
89
|
+
const ssrConfig = getEntryOptions(entryName, isMainEntry, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
|
|
90
|
+
if (typeof ssrConfig === "object" && ssrConfig.mode === "stream") {
|
|
91
|
+
var _this, _runtimeConfig;
|
|
92
|
+
const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
93
|
+
if (((_this = (_runtimeConfig = runtimeConfig) === null || _runtimeConfig === void 0 ? void 0 : _runtimeConfig.router) === null || _this === void 0 ? void 0 : _this.mode) === "react-router-5") {
|
|
94
|
+
throw new Error(`router v5 plugin doesn't support streaming SSR, check your config 'runtime.router'`);
|
|
111
95
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
imports
|
|
115
|
-
};
|
|
116
|
-
},
|
|
117
|
-
modifyEntryRuntimePlugins({ entrypoint, plugins, bundlerConfigs }) {
|
|
118
|
-
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
119
|
-
var _bundlerConfigs_find, _bundlerConfigs_find_output;
|
|
120
|
-
const chunkLoadingGlobal = (_bundlerConfigs_find = bundlerConfigs === null || bundlerConfigs === void 0 ? void 0 : bundlerConfigs.find((config2) => config2.name === "client")) === null || _bundlerConfigs_find === void 0 ? void 0 : (_bundlerConfigs_find_output = _bundlerConfigs_find.output) === null || _bundlerConfigs_find_output === void 0 ? void 0 : _bundlerConfigs_find_output.chunkLoadingGlobal;
|
|
121
|
-
const config = api.useResolvedConfigContext();
|
|
122
|
-
const { crossorigin, scriptLoading } = config.html;
|
|
123
|
-
plugins.push({
|
|
124
|
-
name: PLUGIN_IDENTIFIER,
|
|
125
|
-
options: JSON.stringify({
|
|
126
|
-
...ssrConfigMap.get(entrypoint.entryName) || {},
|
|
127
|
-
crossorigin,
|
|
128
|
-
scriptLoading,
|
|
129
|
-
chunkLoadingGlobal
|
|
130
|
-
})
|
|
131
|
-
});
|
|
96
|
+
if (fileSystemRoutes && !entrypoint.nestedRoutesEntry) {
|
|
97
|
+
throw new Error("You should switch to file-system based router to support streaming SSR.");
|
|
132
98
|
}
|
|
99
|
+
}
|
|
100
|
+
const useSSG = isSSGEntry(userConfig, entryName, entrypoints);
|
|
101
|
+
ssrConfigMap.set(entryName, ssrConfig || useSSG);
|
|
102
|
+
if (ssrConfig || useSSG) {
|
|
103
|
+
imports.push({
|
|
104
|
+
value: "@modern-js/runtime/plugins",
|
|
105
|
+
specifiers: [
|
|
106
|
+
{
|
|
107
|
+
imported: PLUGIN_IDENTIFIER
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
entrypoint,
|
|
114
|
+
imports
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins, bundlerConfigs }) {
|
|
118
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
119
|
+
var _bundlerConfigs_find_output, _bundlerConfigs_find, _bundlerConfigs;
|
|
120
|
+
const chunkLoadingGlobal = (_bundlerConfigs = bundlerConfigs) === null || _bundlerConfigs === void 0 ? void 0 : (_bundlerConfigs_find = _bundlerConfigs.find((config2) => config2.name === "client")) === null || _bundlerConfigs_find === void 0 ? void 0 : (_bundlerConfigs_find_output = _bundlerConfigs_find.output) === null || _bundlerConfigs_find_output === void 0 ? void 0 : _bundlerConfigs_find_output.chunkLoadingGlobal;
|
|
121
|
+
const config = api.useResolvedConfigContext();
|
|
122
|
+
const { crossorigin, scriptLoading } = config.html;
|
|
123
|
+
plugins.push({
|
|
124
|
+
name: PLUGIN_IDENTIFIER,
|
|
125
|
+
options: JSON.stringify({
|
|
126
|
+
...ssrConfigMap.get(entrypoint.entryName) || {},
|
|
127
|
+
crossorigin,
|
|
128
|
+
scriptLoading,
|
|
129
|
+
chunkLoadingGlobal
|
|
130
|
+
})
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
entrypoint,
|
|
135
|
+
plugins
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
modifyEntryExport({ entrypoint, exportStatement }) {
|
|
139
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
133
140
|
return {
|
|
134
141
|
entrypoint,
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
},
|
|
138
|
-
modifyEntryExport({ entrypoint, exportStatement }) {
|
|
139
|
-
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
140
|
-
return {
|
|
141
|
-
entrypoint,
|
|
142
|
-
exportStatement: [
|
|
143
|
-
`export function ${SERVER_RENDER_FUNCTION_NAME}(context) {
|
|
142
|
+
exportStatement: [
|
|
143
|
+
`export function ${SERVER_RENDER_FUNCTION_NAME}(context) {
|
|
144
144
|
return bootstrap(AppWrapper, context)
|
|
145
145
|
}`,
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
entrypoint,
|
|
152
|
-
exportStatement
|
|
146
|
+
exportStatement
|
|
147
|
+
].join("\n")
|
|
153
148
|
};
|
|
154
149
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
};
|
|
150
|
+
return {
|
|
151
|
+
entrypoint,
|
|
152
|
+
exportStatement
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
});
|
|
159
158
|
export default ssrPlugin;
|
|
@@ -71,7 +71,7 @@ class LoadablePlugin {
|
|
|
71
71
|
const outputFile = path.resolve(outputFolder || "", this.opts.filename);
|
|
72
72
|
fs.outputFileSync(outputFile, manifest);
|
|
73
73
|
}
|
|
74
|
-
constructor({ filename = "loadable-stats.json", path:
|
|
74
|
+
constructor({ filename = "loadable-stats.json", path: path1, writeToDisk, outputAsset = true, chunkLoadingGlobal = "__LOADABLE_LOADED_CHUNKS__" } = {
|
|
75
75
|
filename: "loadable-stats.json",
|
|
76
76
|
outputAsset: true,
|
|
77
77
|
chunkLoadingGlobal: "__LOADABLE_LOADED_CHUNKS__"
|
|
@@ -80,7 +80,7 @@ class LoadablePlugin {
|
|
|
80
80
|
_define_property(this, "compiler", void 0);
|
|
81
81
|
this.opts = {
|
|
82
82
|
filename,
|
|
83
|
-
path:
|
|
83
|
+
path: path1,
|
|
84
84
|
writeToDisk,
|
|
85
85
|
outputAsset,
|
|
86
86
|
chunkLoadingGlobal
|
|
@@ -6,129 +6,127 @@ import { RenderLevel } from "./serverRender/types";
|
|
|
6
6
|
import { WithCallback } from "./react/withCallback";
|
|
7
7
|
import { formatClient, mockResponse, isReact18 } from "./utils";
|
|
8
8
|
import { ROUTER_DATA_JSON_ID, SSR_DATA_JSON_ID } from "./serverRender/utils";
|
|
9
|
-
export const ssr = (config) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
children: /* @__PURE__ */ _jsx(App, {
|
|
50
|
-
context: hydrateContext
|
|
51
|
-
})
|
|
52
|
-
});
|
|
53
|
-
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
54
|
-
ModernHydrate(/* @__PURE__ */ _jsx(SSRApp, {}));
|
|
55
|
-
}, loadableReadyOptions);
|
|
56
|
-
} else {
|
|
57
|
-
loadableReady(() => {
|
|
58
|
-
ModernHydrate(/* @__PURE__ */ _jsx(App, {
|
|
9
|
+
export const ssr = (config) => ({
|
|
10
|
+
name: "@modern-js/plugin-ssr",
|
|
11
|
+
setup: () => {
|
|
12
|
+
const mockResp = mockResponse();
|
|
13
|
+
if (config.inlineScript === false) {
|
|
14
|
+
window._SSR_DATA = parsedJSONFromElement(SSR_DATA_JSON_ID);
|
|
15
|
+
window._ROUTER_DATA = parsedJSONFromElement(ROUTER_DATA_JSON_ID);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
client: async ({ App, context, ModernRender, ModernHydrate }) => {
|
|
19
|
+
var _window__SSR_DATA, _window;
|
|
20
|
+
const hydrateContext = {
|
|
21
|
+
...context,
|
|
22
|
+
get routes() {
|
|
23
|
+
return context.routes;
|
|
24
|
+
},
|
|
25
|
+
_hydration: true
|
|
26
|
+
};
|
|
27
|
+
const callback = () => {
|
|
28
|
+
delete hydrateContext._hydration;
|
|
29
|
+
};
|
|
30
|
+
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) || RenderLevel.CLIENT_RENDER;
|
|
31
|
+
if (isReact18() && config.mode === "stream") {
|
|
32
|
+
return streamSSRHydrate();
|
|
33
|
+
}
|
|
34
|
+
return stringSSRHydrate();
|
|
35
|
+
function stringSSRHydrate() {
|
|
36
|
+
if (renderLevel === RenderLevel.CLIENT_RENDER || renderLevel === RenderLevel.SERVER_PREFETCH) {
|
|
37
|
+
ModernRender(/* @__PURE__ */ _jsx(App, {
|
|
38
|
+
context
|
|
39
|
+
}));
|
|
40
|
+
} else if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
41
|
+
const loadableReadyOptions = {
|
|
42
|
+
chunkLoadingGlobal: config.chunkLoadingGlobal
|
|
43
|
+
};
|
|
44
|
+
if (isReact18()) {
|
|
45
|
+
loadableReady(() => {
|
|
46
|
+
let SSRApp = () => /* @__PURE__ */ _jsx(WithCallback, {
|
|
47
|
+
callback,
|
|
48
|
+
children: /* @__PURE__ */ _jsx(App, {
|
|
59
49
|
context: hydrateContext
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
53
|
+
ModernHydrate(/* @__PURE__ */ _jsx(SSRApp, {}));
|
|
54
|
+
}, loadableReadyOptions);
|
|
63
55
|
} else {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
context
|
|
67
|
-
}));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
function streamSSRHydrate() {
|
|
71
|
-
if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
72
|
-
let SSRApp = () => /* @__PURE__ */ _jsx(WithCallback, {
|
|
73
|
-
callback,
|
|
74
|
-
children: /* @__PURE__ */ _jsx(App, {
|
|
56
|
+
loadableReady(() => {
|
|
57
|
+
ModernHydrate(/* @__PURE__ */ _jsx(App, {
|
|
75
58
|
context: hydrateContext
|
|
76
|
-
})
|
|
77
|
-
});
|
|
78
|
-
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
79
|
-
ModernHydrate(/* @__PURE__ */ _jsx(SSRApp, {}));
|
|
80
|
-
} else {
|
|
81
|
-
ModernRender(/* @__PURE__ */ _jsx(App, {
|
|
82
|
-
context
|
|
83
|
-
}));
|
|
59
|
+
}), callback);
|
|
60
|
+
}, loadableReadyOptions);
|
|
84
61
|
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
var _window__SSR_DATA, _window__SSR_DATA_context;
|
|
89
|
-
const request = window === null || window === void 0 ? void 0 : (_window__SSR_DATA = window._SSR_DATA) === null || _window__SSR_DATA === void 0 ? void 0 : (_window__SSR_DATA_context = _window__SSR_DATA.context) === null || _window__SSR_DATA_context === void 0 ? void 0 : _window__SSR_DATA_context.request;
|
|
90
|
-
if (!request) {
|
|
91
|
-
context.ssrContext = {
|
|
92
|
-
...context.ssrContext,
|
|
93
|
-
response: mockResp,
|
|
94
|
-
request: formatClient({})
|
|
95
|
-
};
|
|
96
|
-
return next({
|
|
62
|
+
} else {
|
|
63
|
+
console.warn(`unknow render level: ${renderLevel}, execute render()`);
|
|
64
|
+
ModernRender(/* @__PURE__ */ _jsx(App, {
|
|
97
65
|
context
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function streamSSRHydrate() {
|
|
70
|
+
if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
71
|
+
let SSRApp = () => /* @__PURE__ */ _jsx(WithCallback, {
|
|
72
|
+
callback,
|
|
73
|
+
children: /* @__PURE__ */ _jsx(App, {
|
|
74
|
+
context: hydrateContext
|
|
75
|
+
})
|
|
98
76
|
});
|
|
77
|
+
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
78
|
+
ModernHydrate(/* @__PURE__ */ _jsx(SSRApp, {}));
|
|
79
|
+
} else {
|
|
80
|
+
ModernRender(/* @__PURE__ */ _jsx(App, {
|
|
81
|
+
context
|
|
82
|
+
}));
|
|
99
83
|
}
|
|
100
|
-
|
|
101
|
-
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
init({ context }, next) {
|
|
87
|
+
var _window__SSR_DATA_context, _window__SSR_DATA, _window;
|
|
88
|
+
const request = (_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_context = _window__SSR_DATA.context) === null || _window__SSR_DATA_context === void 0 ? void 0 : _window__SSR_DATA_context.request;
|
|
89
|
+
if (!request) {
|
|
90
|
+
context.ssrContext = {
|
|
91
|
+
...context.ssrContext,
|
|
92
|
+
response: mockResp,
|
|
93
|
+
request: formatClient({})
|
|
94
|
+
};
|
|
102
95
|
return next({
|
|
103
96
|
context
|
|
104
97
|
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
});
|
|
118
|
-
}
|
|
98
|
+
}
|
|
99
|
+
context.ssrContext.response = mockResp;
|
|
100
|
+
context.ssrContext.request = formatClient(request);
|
|
101
|
+
return next({
|
|
102
|
+
context
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
pickContext: ({ context, pickedContext }, next) => {
|
|
106
|
+
var _window__SSR_DATA_context, _window__SSR_DATA, _window;
|
|
107
|
+
const request = (_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_context = _window__SSR_DATA.context) === null || _window__SSR_DATA_context === void 0 ? void 0 : _window__SSR_DATA_context.request;
|
|
108
|
+
const { initialData } = context;
|
|
109
|
+
if (!request) {
|
|
119
110
|
return next({
|
|
120
111
|
context,
|
|
121
112
|
pickedContext: {
|
|
122
113
|
...pickedContext,
|
|
123
|
-
initialData
|
|
124
|
-
request,
|
|
125
|
-
response: mockResp
|
|
114
|
+
initialData
|
|
126
115
|
}
|
|
127
116
|
});
|
|
128
117
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
118
|
+
return next({
|
|
119
|
+
context,
|
|
120
|
+
pickedContext: {
|
|
121
|
+
...pickedContext,
|
|
122
|
+
initialData,
|
|
123
|
+
request,
|
|
124
|
+
response: mockResp
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
});
|
|
133
131
|
export default ssr;
|
|
134
132
|
export * from "./react";
|
|
@@ -1,42 +1,41 @@
|
|
|
1
1
|
import { formatServer } from "./utils";
|
|
2
2
|
import render from "./serverRender";
|
|
3
|
-
export const ssr = (config = {}) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
3
|
+
export const ssr = (config = {}) => ({
|
|
4
|
+
name: "@modern-js/plugin-ssr",
|
|
5
|
+
setup: () => {
|
|
6
|
+
return {
|
|
7
|
+
server: async ({ App, context }) => {
|
|
8
|
+
const html = await render({
|
|
9
|
+
context,
|
|
10
|
+
App,
|
|
11
|
+
config
|
|
12
|
+
});
|
|
13
|
+
return html;
|
|
14
|
+
},
|
|
15
|
+
init({ context }, next) {
|
|
16
|
+
const { request } = context.ssrContext;
|
|
17
|
+
context.ssrContext.request = formatServer(request);
|
|
18
|
+
context.ssrContext.mode = config.mode;
|
|
19
|
+
return next({
|
|
20
|
+
context
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
pickContext: ({ context, pickedContext }, next) => {
|
|
24
|
+
var _context;
|
|
25
|
+
const { request, response } = (_context = context) === null || _context === void 0 ? void 0 : _context.ssrContext;
|
|
26
|
+
const { initialData } = context;
|
|
27
|
+
return next({
|
|
28
|
+
context,
|
|
29
|
+
pickedContext: {
|
|
30
|
+
...pickedContext,
|
|
31
|
+
initialData,
|
|
32
|
+
request,
|
|
33
|
+
response
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
});
|
|
41
40
|
export default ssr;
|
|
42
41
|
export * from "./react";
|