ssr-plugin-react 7.0.6 → 7.0.7
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 +305 -450
- package/README.md +60 -60
- package/cjs/entry/context.d.ts +1 -1
- package/cjs/entry/context.d.ts.map +1 -1
- package/cjs/entry/context.js +5 -5
- package/cjs/entry/context.js.map +1 -1
- package/cjs/entry/create.d.ts +1 -1
- package/cjs/entry/create.d.ts.map +1 -1
- package/cjs/entry/create.js +1 -1
- package/cjs/entry/create.js.map +1 -1
- package/cjs/entry/react17-client-entry.d.ts.map +1 -1
- package/cjs/entry/react17-client-entry.js +6 -7
- package/cjs/entry/react17-client-entry.js.map +1 -1
- package/cjs/entry/react18-client-entry.d.ts.map +1 -1
- package/cjs/entry/react18-client-entry.js +5 -7
- package/cjs/entry/react18-client-entry.js.map +1 -1
- package/cjs/entry/server-entry.d.ts +2 -2
- package/cjs/entry/server-entry.d.ts.map +1 -1
- package/cjs/entry/server-entry.js +31 -27
- package/cjs/entry/server-entry.js.map +1 -1
- package/cjs/index.js +9 -9
- package/cjs/index.js.map +1 -1
- package/esm/entry/context.d.ts +1 -1
- package/esm/entry/context.d.ts.map +1 -1
- package/esm/entry/context.js +7 -7
- package/esm/entry/context.js.map +1 -1
- package/esm/entry/create.d.ts +1 -1
- package/esm/entry/create.d.ts.map +1 -1
- package/esm/entry/create.js +7 -7
- package/esm/entry/create.js.map +1 -1
- package/esm/entry/react17-client-entry.d.ts.map +1 -1
- package/esm/entry/react17-client-entry.js +13 -14
- package/esm/entry/react17-client-entry.js.map +1 -1
- package/esm/entry/react18-client-entry.d.ts.map +1 -1
- package/esm/entry/react18-client-entry.js +12 -14
- package/esm/entry/react18-client-entry.js.map +1 -1
- package/esm/entry/server-entry.d.ts +2 -2
- package/esm/entry/server-entry.d.ts.map +1 -1
- package/esm/entry/server-entry.js +40 -36
- package/esm/entry/server-entry.js.map +1 -1
- package/esm/index.js +11 -11
- package/esm/index.js.map +1 -1
- package/package.json +31 -32
- package/src/entry/context.tsx +33 -29
- package/src/entry/create.ts +26 -26
- package/src/entry/react17-client-entry.tsx +66 -66
- package/src/entry/react18-client-entry.tsx +72 -72
- package/src/entry/server-entry.tsx +191 -181
- package/src/index.ts +38 -38
- package/tsconfig.cjs.json +2 -4
- package/tsconfig.esm.json +2 -4
|
@@ -1,195 +1,205 @@
|
|
|
1
|
-
import type { renderToPipeableStream } from
|
|
2
|
-
import { PassThrough } from
|
|
3
|
-
import * as React from
|
|
4
|
-
import { createElement } from
|
|
5
|
-
import * as ReactDOMServer from
|
|
6
|
-
import { StaticRouter } from
|
|
1
|
+
import type { renderToPipeableStream } from "react-dom18/server";
|
|
2
|
+
import { PassThrough } from "stream";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { createElement } from "react";
|
|
5
|
+
import * as ReactDOMServer from "react-dom/server";
|
|
6
|
+
import { StaticRouter } from "react-router-dom";
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} from
|
|
22
|
-
import { ISSRContext, IConfig, ReactESMPreloadFeRouteItem, DynamicFC, StaticFC } from
|
|
23
|
-
import { serialize } from
|
|
24
|
-
import { AppContext } from
|
|
25
|
-
import { Routes, ssrCreateContext, createStore } from
|
|
8
|
+
findRoute,
|
|
9
|
+
getManifest,
|
|
10
|
+
logGreen,
|
|
11
|
+
normalizePath,
|
|
12
|
+
getAsyncCssChunk,
|
|
13
|
+
getAsyncJsChunk,
|
|
14
|
+
splitPageInfo,
|
|
15
|
+
reactRefreshFragment,
|
|
16
|
+
localStorageWrapper,
|
|
17
|
+
checkRoute,
|
|
18
|
+
useStore,
|
|
19
|
+
isReact18,
|
|
20
|
+
getClientEntry,
|
|
21
|
+
} from "ssr-common-utils";
|
|
22
|
+
import { ISSRContext, IConfig, ReactESMPreloadFeRouteItem, DynamicFC, StaticFC } from "ssr-types";
|
|
23
|
+
import { serialize } from "ssr-serialize-javascript";
|
|
24
|
+
import { AppContext } from "./context";
|
|
25
|
+
import { Routes, ssrCreateContext, createStore } from "./create";
|
|
26
26
|
|
|
27
|
-
const { FeRoutes, layoutFetch, state, Layout } = Routes
|
|
27
|
+
const { FeRoutes, layoutFetch, state, Layout } = Routes;
|
|
28
28
|
|
|
29
29
|
type ReactDOMServerType = typeof ReactDOMServer & {
|
|
30
|
-
|
|
31
|
-
}
|
|
30
|
+
renderToPipeableStream: typeof renderToPipeableStream;
|
|
31
|
+
};
|
|
32
32
|
const serverRender = async (ctx: ISSRContext, config: IConfig) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
33
|
+
const context = ssrCreateContext();
|
|
34
|
+
const {
|
|
35
|
+
mode,
|
|
36
|
+
parallelFetch,
|
|
37
|
+
prefix,
|
|
38
|
+
isVite,
|
|
39
|
+
isDev,
|
|
40
|
+
clientPrefix,
|
|
41
|
+
onReady,
|
|
42
|
+
onError,
|
|
43
|
+
stream,
|
|
44
|
+
rootId,
|
|
45
|
+
hashRouter,
|
|
46
|
+
streamHighWaterMark,
|
|
47
|
+
} = config;
|
|
48
|
+
const rawPath = ctx.request.path ?? ctx.request.url;
|
|
49
|
+
const path = normalizePath(rawPath, prefix);
|
|
50
|
+
const routeItem = findRoute<ReactESMPreloadFeRouteItem>(FeRoutes, path);
|
|
51
|
+
checkRoute({ routeItem, path });
|
|
52
|
+
const { fetch, webpackChunkName, component } = routeItem;
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
54
|
+
const fn = async () => {
|
|
55
|
+
const dynamicCssOrder = await getAsyncCssChunk(ctx, webpackChunkName, config);
|
|
56
|
+
const dynamicJsOrder = await getAsyncJsChunk(ctx, webpackChunkName, config);
|
|
57
|
+
const manifest = await getManifest(config);
|
|
58
|
+
const injectCss = (
|
|
59
|
+
isVite && isDev
|
|
60
|
+
? [
|
|
61
|
+
<script src="/@vite/client" type="module" key="vite-client" />,
|
|
62
|
+
<script
|
|
63
|
+
key="vite-react-refresh"
|
|
64
|
+
type="module"
|
|
65
|
+
dangerouslySetInnerHTML={{
|
|
66
|
+
__html: reactRefreshFragment,
|
|
67
|
+
}}
|
|
68
|
+
/>,
|
|
69
|
+
]
|
|
70
|
+
: dynamicCssOrder
|
|
71
|
+
.map((css) => manifest[css])
|
|
72
|
+
.filter(Boolean)
|
|
73
|
+
.map((css) => <link rel="stylesheet" key={css} href={css} />)
|
|
74
|
+
).concat(
|
|
75
|
+
isVite && isDev
|
|
76
|
+
? []
|
|
77
|
+
: dynamicJsOrder
|
|
78
|
+
.map((js) => manifest[js])
|
|
79
|
+
.filter(Boolean)
|
|
80
|
+
.map((js) => (
|
|
81
|
+
<link href={js} as="script" rel={isVite ? "modulepreload" : "preload"} key={js} />
|
|
82
|
+
)),
|
|
83
|
+
);
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
85
|
+
const injectScript = [
|
|
86
|
+
...(isVite
|
|
87
|
+
? [
|
|
88
|
+
<script
|
|
89
|
+
key="viteWindowInit"
|
|
90
|
+
dangerouslySetInnerHTML={{
|
|
91
|
+
__html: "window.__USE_VITE__=true",
|
|
92
|
+
}}
|
|
93
|
+
/>,
|
|
94
|
+
]
|
|
95
|
+
: []),
|
|
96
|
+
...(isVite && isDev
|
|
97
|
+
? [
|
|
98
|
+
<script
|
|
99
|
+
type="module"
|
|
100
|
+
src={`/node_modules/ssr-plugin-react/esm/entry/${getClientEntry()}.js`}
|
|
101
|
+
key="vite-react-entry"
|
|
102
|
+
/>,
|
|
103
|
+
]
|
|
104
|
+
: []),
|
|
105
|
+
...dynamicJsOrder
|
|
106
|
+
.map((js) => manifest[js])
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.map((item) => (
|
|
109
|
+
<script key={item} src={item} type={isVite ? "module" : "text/javascript"} />
|
|
110
|
+
)),
|
|
111
|
+
];
|
|
112
|
+
const staticList = {
|
|
113
|
+
injectCss,
|
|
114
|
+
injectScript,
|
|
115
|
+
};
|
|
112
116
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
const isCsr = !!(mode === "csr" || ctx.request.query?.csr);
|
|
118
|
+
const Component = isCsr
|
|
119
|
+
? React.Fragment
|
|
120
|
+
: component.name === "dynamicComponent"
|
|
121
|
+
? (await (component as DynamicFC)()).default
|
|
122
|
+
: (component as StaticFC);
|
|
119
123
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
124
|
+
if (isCsr) {
|
|
125
|
+
logGreen(`Current path ${path} use csr render mode`);
|
|
126
|
+
}
|
|
123
127
|
|
|
124
|
-
|
|
128
|
+
let [layoutFetchData, fetchData] = [{}, {}];
|
|
125
129
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
if (!isCsr) {
|
|
131
|
+
const currentFetch = fetch ? (await fetch()).default : null;
|
|
132
|
+
const lF = layoutFetch ? layoutFetch({ ctx }) : Promise.resolve({});
|
|
133
|
+
const CF = currentFetch ? currentFetch({ ctx }) : Promise.resolve({});
|
|
134
|
+
[layoutFetchData, fetchData] = parallelFetch
|
|
135
|
+
? await Promise.all([lF, CF])
|
|
136
|
+
: [await lF, await CF];
|
|
137
|
+
}
|
|
132
138
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
139
|
+
const combineData = isCsr
|
|
140
|
+
? null
|
|
141
|
+
: Object.assign({}, state ?? {}, layoutFetchData ?? {}, fetchData ?? {});
|
|
142
|
+
const ssrDevInfo = { manifest: isDev ? manifest : "", rootId };
|
|
143
|
+
const innerHTML = splitPageInfo({
|
|
144
|
+
"window.__USE_SSR__": !isCsr,
|
|
145
|
+
"window.__INITIAL_DATA__": isCsr ? {} : serialize(combineData),
|
|
146
|
+
"window.__USE_VITE__": isVite,
|
|
147
|
+
"window.prefix": `"${prefix}"`,
|
|
148
|
+
"window.clientPrefix": `"${clientPrefix ?? ""}"`,
|
|
149
|
+
"window.ssrDevInfo": JSON.stringify(ssrDevInfo),
|
|
150
|
+
"window.hashRouter": Boolean(hashRouter),
|
|
151
|
+
"window.__VALTIO_DATA__": isCsr ? {} : serialize(useStore()),
|
|
152
|
+
});
|
|
153
|
+
const injectState = <script dangerouslySetInnerHTML={{ __html: innerHTML }} />;
|
|
154
|
+
// with jsx type error, use createElement here
|
|
155
|
+
const ele = createElement(
|
|
156
|
+
StaticRouter,
|
|
157
|
+
{
|
|
158
|
+
location: ctx.request.url,
|
|
159
|
+
basename: prefix === "/" ? undefined : prefix,
|
|
160
|
+
},
|
|
161
|
+
createElement(AppContext, {
|
|
162
|
+
context: context as any,
|
|
163
|
+
initialState: combineData,
|
|
164
|
+
children: createElement(
|
|
165
|
+
Layout,
|
|
166
|
+
{
|
|
167
|
+
ctx: ctx,
|
|
168
|
+
config: config,
|
|
169
|
+
staticList: staticList,
|
|
170
|
+
injectState: injectState,
|
|
171
|
+
},
|
|
172
|
+
createElement(Component, null),
|
|
173
|
+
),
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
// for ctx.body will loose asynclocalstorage context, consume stream in advance like vue2/3
|
|
177
|
+
if (isReact18()) {
|
|
178
|
+
return stream
|
|
179
|
+
? (ReactDOMServer as ReactDOMServerType)
|
|
180
|
+
.renderToPipeableStream(ele, {
|
|
181
|
+
onAllReady: onReady,
|
|
182
|
+
onError: onError as any,
|
|
183
|
+
})
|
|
184
|
+
.pipe(new PassThrough({ highWaterMark: streamHighWaterMark }))
|
|
185
|
+
: ReactDOMServer.renderToString(ele);
|
|
186
|
+
} else {
|
|
187
|
+
return stream
|
|
188
|
+
? ReactDOMServer.renderToNodeStream(ele).pipe(
|
|
189
|
+
new PassThrough({ highWaterMark: streamHighWaterMark }),
|
|
190
|
+
)
|
|
191
|
+
: ReactDOMServer.renderToString(ele);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
184
194
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
195
|
+
return await localStorageWrapper.run(
|
|
196
|
+
{
|
|
197
|
+
context: context as any,
|
|
198
|
+
ctx,
|
|
199
|
+
store: createStore(),
|
|
200
|
+
},
|
|
201
|
+
fn,
|
|
202
|
+
);
|
|
203
|
+
};
|
|
194
204
|
|
|
195
|
-
export { serverRender, Routes }
|
|
205
|
+
export { serverRender, Routes };
|
package/src/index.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { resolve } from
|
|
2
|
-
import { loadConfig, getCwd } from
|
|
1
|
+
import { resolve } from "path";
|
|
2
|
+
import { loadConfig, getCwd } from "ssr-common-utils";
|
|
3
3
|
|
|
4
|
-
const { tool } = loadConfig()
|
|
4
|
+
const { tool } = loadConfig();
|
|
5
5
|
|
|
6
6
|
export function clientPlugin() {
|
|
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
|
-
|
|
41
|
-
|
|
7
|
+
const cwd = getCwd();
|
|
8
|
+
const webpackPath = resolve(cwd, "./node_modules/ssr-webpack");
|
|
9
|
+
const vitePath = resolve(cwd, "./node_modules/ssr-vite");
|
|
10
|
+
const rspackPath = resolve(cwd, "./node_modules/ssr-rspack");
|
|
11
|
+
return {
|
|
12
|
+
name: "plugin-react",
|
|
13
|
+
start: async () => {
|
|
14
|
+
if (tool === "vite") {
|
|
15
|
+
const { start } = await import(vitePath);
|
|
16
|
+
await start();
|
|
17
|
+
} else {
|
|
18
|
+
if (tool === "rspack") {
|
|
19
|
+
const { start } = await import(rspackPath);
|
|
20
|
+
await start();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const { start } = await import(webpackPath);
|
|
24
|
+
await start();
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
build: async () => {
|
|
28
|
+
if (tool === "vite") {
|
|
29
|
+
const { build } = await import(vitePath);
|
|
30
|
+
await build();
|
|
31
|
+
} else {
|
|
32
|
+
if (tool === "rspack") {
|
|
33
|
+
const { build } = await import(rspackPath);
|
|
34
|
+
await build();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const { build } = await import(webpackPath);
|
|
38
|
+
await build();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
42
|
}
|
package/tsconfig.cjs.json
CHANGED