@modern-js/runtime 1.5.0 → 1.16.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.
- package/CHANGELOG.md +48 -0
- package/dist/js/modern/cli/index.js +5 -3
- package/dist/js/modern/core/compatible.js +31 -8
- package/dist/js/modern/core/index.js +1 -1
- package/dist/js/modern/core/loader/useLoader.js +1 -1
- package/dist/js/modern/{core/runtime-context.js → runtime-context.js} +0 -0
- package/dist/js/modern/ssr/index.js +34 -13
- package/dist/js/modern/ssr/index.node.js +4 -1
- package/dist/js/modern/ssr/react/withCallback/index.js +17 -0
- package/dist/js/modern/ssr/serverRender/helmet.js +2 -2
- package/dist/js/modern/ssr/serverRender/index.js +0 -1
- package/dist/js/modern/ssr/utils.js +9 -3
- package/dist/js/node/cli/index.js +4 -2
- package/dist/js/node/core/compatible.js +32 -8
- package/dist/js/node/core/index.js +1 -1
- package/dist/js/node/core/loader/useLoader.js +1 -1
- package/dist/js/node/{core/runtime-context.js → runtime-context.js} +0 -0
- package/dist/js/node/ssr/index.js +37 -15
- package/dist/js/node/ssr/index.node.js +4 -1
- package/dist/js/node/ssr/react/withCallback/index.js +27 -0
- package/dist/js/node/ssr/serverRender/helmet.js +2 -2
- package/dist/js/node/ssr/serverRender/index.js +0 -2
- package/dist/js/node/ssr/utils.js +9 -3
- package/dist/js/treeshaking/cli/index.js +5 -3
- package/dist/js/treeshaking/core/compatible.js +65 -23
- package/dist/js/treeshaking/core/index.js +1 -1
- package/dist/js/treeshaking/core/loader/useLoader.js +1 -1
- package/dist/js/treeshaking/{core/runtime-context.js → runtime-context.js} +0 -0
- package/dist/js/treeshaking/ssr/index.js +45 -33
- package/dist/js/treeshaking/ssr/index.node.js +12 -9
- package/dist/js/treeshaking/ssr/react/withCallback/index.js +16 -0
- package/dist/js/treeshaking/ssr/serverRender/helmet.js +2 -2
- package/dist/js/treeshaking/ssr/serverRender/index.js +0 -1
- package/dist/js/treeshaking/ssr/utils.js +8 -3
- package/dist/types/core/compatible.d.ts +6 -2
- package/dist/types/core/index.d.ts +2 -2
- package/dist/types/core/plugin.d.ts +15 -8
- package/dist/types/index.d.ts +2 -1
- package/dist/types/runtime-context.d.ts +24 -0
- package/dist/types/ssr/index.d.ts +0 -7
- package/dist/types/ssr/react/nossr/index.d.ts +1 -1
- package/dist/types/ssr/react/withCallback/index.d.ts +5 -0
- package/dist/types/ssr/serverRender/index.d.ts +0 -5
- package/dist/types/state/runtime/plugin.d.ts +1 -7
- package/package.json +13 -11
- package/dist/types/core/runtime-context.d.ts +0 -13
|
@@ -28,19 +28,25 @@ exports.formatServer = formatServer;
|
|
|
28
28
|
|
|
29
29
|
const getQuery = () => window.location.search.substring(1).split('&').reduce((res, item) => {
|
|
30
30
|
const [key, value] = item.split('=');
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
if (key) {
|
|
33
|
+
res[key] = value;
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
return res;
|
|
33
37
|
}, {});
|
|
34
38
|
|
|
35
39
|
const formatClient = request => {
|
|
40
|
+
var _request$headers, _request$headers2;
|
|
41
|
+
|
|
36
42
|
return {
|
|
37
43
|
params: request.params || {},
|
|
38
44
|
host: request.host || location.host,
|
|
39
45
|
pathname: request.pathname || location.pathname,
|
|
40
46
|
headers: request.headers || {},
|
|
41
47
|
cookieMap: request.cookieMap || {},
|
|
42
|
-
cookie: request.headers.cookie || document.cookie,
|
|
43
|
-
userAgent: request.headers['user-agent'] || navigator.userAgent,
|
|
48
|
+
cookie: ((_request$headers = request.headers) === null || _request$headers === void 0 ? void 0 : _request$headers.cookie) || document.cookie,
|
|
49
|
+
userAgent: ((_request$headers2 = request.headers) === null || _request$headers2 === void 0 ? void 0 : _request$headers2['user-agent']) || navigator.userAgent,
|
|
44
50
|
referer: request.referer || document.referrer,
|
|
45
51
|
query: request.query || getQuery(),
|
|
46
52
|
url: location.href
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
2
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
3
|
import path from 'path';
|
|
4
|
-
import { PLUGIN_SCHEMAS, createRuntimeExportsUtils, cleanRequireCache } from '@modern-js/utils';
|
|
4
|
+
import { PLUGIN_SCHEMAS, createRuntimeExportsUtils, cleanRequireCache, isReact18 } from '@modern-js/utils';
|
|
5
5
|
import PluginState from "../state/cli";
|
|
6
6
|
import PluginSSR from "../ssr/cli";
|
|
7
7
|
import PluginRouter from "../router/cli";
|
|
@@ -14,8 +14,9 @@ export default (function () {
|
|
|
14
14
|
var runtimeExportsUtils = {};
|
|
15
15
|
return {
|
|
16
16
|
config: function config() {
|
|
17
|
-
var dir = api.useAppContext().internalDirectory;
|
|
17
|
+
var dir = api.useAppContext().internalDirectory || '';
|
|
18
18
|
runtimeExportsUtils = createRuntimeExportsUtils(dir, 'index');
|
|
19
|
+
process.env.IS_REACT18 = isReact18(path.join(dir, '../../')).toString();
|
|
19
20
|
return {
|
|
20
21
|
runtime: {},
|
|
21
22
|
runtimeByEntries: {},
|
|
@@ -29,7 +30,8 @@ export default (function () {
|
|
|
29
30
|
* So need to add alias
|
|
30
31
|
*/
|
|
31
32
|
'styled-components': require.resolve('styled-components')
|
|
32
|
-
}
|
|
33
|
+
},
|
|
34
|
+
envVars: ['IS_REACT18']
|
|
33
35
|
}
|
|
34
36
|
};
|
|
35
37
|
},
|
|
@@ -5,12 +5,13 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
|
5
5
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
6
6
|
var _excluded = ["context"];
|
|
7
7
|
import React, { useContext, useMemo } from 'react';
|
|
8
|
-
import
|
|
8
|
+
import defaultReactDOM from 'react-dom';
|
|
9
9
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
10
|
+
import { RuntimeReactContext } from "../runtime-context";
|
|
10
11
|
import { runtime } from "./plugin";
|
|
11
|
-
import { RuntimeReactContext } from "./runtime-context";
|
|
12
12
|
import { createLoaderManager } from "./loader/loaderManager";
|
|
13
13
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
var IS_REACT18 = process.env.IS_REACT18 === 'true';
|
|
14
15
|
|
|
15
16
|
function isClientArgs(id) {
|
|
16
17
|
return typeof id === 'string' || typeof HTMLElement !== 'undefined' && id instanceof HTMLElement;
|
|
@@ -99,13 +100,36 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
99
100
|
* When csr, id is root id.
|
|
100
101
|
* When ssr, id is serverContext
|
|
101
102
|
*/
|
|
102
|
-
id
|
|
103
|
-
|
|
103
|
+
id,
|
|
104
|
+
/**
|
|
105
|
+
* root.render need use root to run function
|
|
106
|
+
*/
|
|
107
|
+
root) {
|
|
108
|
+
var render,
|
|
109
|
+
hydrate,
|
|
110
|
+
App,
|
|
111
|
+
runner,
|
|
112
|
+
context,
|
|
113
|
+
runInit,
|
|
114
|
+
isBrowser,
|
|
115
|
+
_ssrData$data,
|
|
116
|
+
_ssrData$data2,
|
|
117
|
+
ssrData,
|
|
118
|
+
loadersData,
|
|
119
|
+
initialLoadersState,
|
|
120
|
+
initialData,
|
|
121
|
+
_rootElement,
|
|
122
|
+
ModernRender,
|
|
123
|
+
ModernHydrate,
|
|
124
|
+
_initialData,
|
|
125
|
+
_args = arguments;
|
|
104
126
|
|
|
105
127
|
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
106
128
|
while (1) {
|
|
107
129
|
switch (_context2.prev = _context2.next) {
|
|
108
130
|
case 0:
|
|
131
|
+
render = _args.length > 3 && _args[3] !== undefined ? _args[3] : defaultReactDOM.render;
|
|
132
|
+
hydrate = _args.length > 4 && _args[4] !== undefined ? _args[4] : defaultReactDOM.hydrate;
|
|
109
133
|
App = BootApp;
|
|
110
134
|
runner = runnerMap.get(App); // ensure Component used is created by `createApp`
|
|
111
135
|
|
|
@@ -133,7 +157,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
133
157
|
|
|
134
158
|
|
|
135
159
|
if (id) {
|
|
136
|
-
_context2.next =
|
|
160
|
+
_context2.next = 9;
|
|
137
161
|
break;
|
|
138
162
|
}
|
|
139
163
|
|
|
@@ -141,16 +165,16 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
141
165
|
context: context
|
|
142
166
|
}));
|
|
143
167
|
|
|
144
|
-
case
|
|
168
|
+
case 9:
|
|
145
169
|
isBrowser = typeof window !== 'undefined' && window.name !== 'nodejs';
|
|
146
170
|
|
|
147
171
|
if (!isBrowser) {
|
|
148
|
-
_context2.next =
|
|
172
|
+
_context2.next = 30;
|
|
149
173
|
break;
|
|
150
174
|
}
|
|
151
175
|
|
|
152
176
|
if (!isClientArgs(id)) {
|
|
153
|
-
_context2.next =
|
|
177
|
+
_context2.next = 27;
|
|
154
178
|
break;
|
|
155
179
|
}
|
|
156
180
|
|
|
@@ -174,38 +198,56 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
174
198
|
ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
|
|
175
199
|
} : {}));
|
|
176
200
|
context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData$data2 = ssrData.data) === null || _ssrData$data2 === void 0 ? void 0 : _ssrData$data2.initialData;
|
|
177
|
-
_context2.next =
|
|
201
|
+
_context2.next = 19;
|
|
178
202
|
return runInit(context);
|
|
179
203
|
|
|
180
|
-
case
|
|
204
|
+
case 19:
|
|
181
205
|
initialData = _context2.sent;
|
|
182
206
|
|
|
183
207
|
if (initialData) {
|
|
184
208
|
context.initialData = initialData;
|
|
185
209
|
}
|
|
186
210
|
|
|
211
|
+
_rootElement = typeof id !== 'string' ? id : document.getElementById(id || 'root'); // https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html
|
|
212
|
+
|
|
213
|
+
ModernRender = function ModernRender(App) {
|
|
214
|
+
if (IS_REACT18) {
|
|
215
|
+
root.render(App);
|
|
216
|
+
} else {
|
|
217
|
+
render(App, _rootElement);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
ModernHydrate = function ModernHydrate(App, callback) {
|
|
222
|
+
if (IS_REACT18) {
|
|
223
|
+
hydrate(_rootElement, App);
|
|
224
|
+
} else {
|
|
225
|
+
hydrate(App, _rootElement, callback);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
187
229
|
return _context2.abrupt("return", runner.client({
|
|
188
230
|
App: App,
|
|
189
231
|
context: context,
|
|
190
|
-
|
|
232
|
+
ModernRender: ModernRender,
|
|
233
|
+
ModernHydrate: ModernHydrate
|
|
191
234
|
}, {
|
|
192
235
|
onLast: function onLast(_ref8) {
|
|
193
|
-
var App = _ref8.App
|
|
194
|
-
|
|
195
|
-
ReactDOM.render( /*#__PURE__*/React.createElement(App, {
|
|
236
|
+
var App = _ref8.App;
|
|
237
|
+
ModernRender( /*#__PURE__*/React.createElement(App, {
|
|
196
238
|
context: context
|
|
197
|
-
})
|
|
239
|
+
}));
|
|
198
240
|
}
|
|
199
241
|
}));
|
|
200
242
|
|
|
201
|
-
case
|
|
243
|
+
case 27:
|
|
202
244
|
throw Error('`bootstrap` needs id in browser environment, it needs to be string or element');
|
|
203
245
|
|
|
204
|
-
case
|
|
205
|
-
_context2.next =
|
|
246
|
+
case 28:
|
|
247
|
+
_context2.next = 36;
|
|
206
248
|
break;
|
|
207
249
|
|
|
208
|
-
case
|
|
250
|
+
case 30:
|
|
209
251
|
Object.assign(context, {
|
|
210
252
|
ssrContext: id,
|
|
211
253
|
isBrowser: false,
|
|
@@ -215,10 +257,10 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
215
257
|
skipStatic: process.env.NODE_ENV === 'production' && !id.staticGenerate
|
|
216
258
|
})
|
|
217
259
|
});
|
|
218
|
-
_context2.next =
|
|
260
|
+
_context2.next = 33;
|
|
219
261
|
return runInit(context);
|
|
220
262
|
|
|
221
|
-
case
|
|
263
|
+
case 33:
|
|
222
264
|
_initialData = _context2.sent;
|
|
223
265
|
context.initialData = _initialData;
|
|
224
266
|
return _context2.abrupt("return", runner.server({
|
|
@@ -226,7 +268,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
226
268
|
context: context
|
|
227
269
|
}));
|
|
228
270
|
|
|
229
|
-
case
|
|
271
|
+
case 36:
|
|
230
272
|
case "end":
|
|
231
273
|
return _context2.stop();
|
|
232
274
|
}
|
|
@@ -234,7 +276,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
234
276
|
}, _callee);
|
|
235
277
|
}));
|
|
236
278
|
|
|
237
|
-
return function bootstrap(_x, _x2) {
|
|
279
|
+
return function bootstrap(_x, _x2, _x3) {
|
|
238
280
|
return _ref6.apply(this, arguments);
|
|
239
281
|
};
|
|
240
282
|
}();
|
|
@@ -2,6 +2,6 @@ export { createPlugin, createRuntime, runtime, registerInit, registerPrefetch }
|
|
|
2
2
|
export { defineConfig, getConfig } from "./app-config";
|
|
3
3
|
// compatible
|
|
4
4
|
export * from "./compatible";
|
|
5
|
-
export { RuntimeReactContext } from "
|
|
5
|
+
export { RuntimeReactContext } from "../runtime-context";
|
|
6
6
|
export * from "./loader";
|
|
7
7
|
export * from '@modern-js/plugin';
|
|
@@ -2,7 +2,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
|
2
2
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
3
|
import { useContext, useRef, useMemo, useState, useCallback, useEffect } from 'react';
|
|
4
4
|
import invariant from 'invariant';
|
|
5
|
-
import { RuntimeReactContext } from "
|
|
5
|
+
import { RuntimeReactContext } from "../../runtime-context";
|
|
6
6
|
import { LoaderStatus } from "./loaderManager";
|
|
7
7
|
|
|
8
8
|
var useLoader = function useLoader(loaderFn) {
|
|
File without changes
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
2
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
3
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
4
|
-
import ReactDOM from 'react-dom';
|
|
5
4
|
import { loadableReady } from '@loadable/component';
|
|
5
|
+
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
6
6
|
import { RenderLevel } from "./serverRender/type";
|
|
7
|
+
import { WithCallback } from "./react/withCallback";
|
|
7
8
|
import { formatClient, mockResponse } from "./utils";
|
|
8
9
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
+
var IS_REACT18 = process.env.IS_REACT18 === 'true';
|
|
9
11
|
|
|
10
12
|
var ssr = function ssr() {
|
|
11
13
|
return {
|
|
@@ -17,52 +19,58 @@ var ssr = function ssr() {
|
|
|
17
19
|
var _client = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
18
20
|
var _window, _window$_SSR_DATA;
|
|
19
21
|
|
|
20
|
-
var App, context,
|
|
21
|
-
|
|
22
|
+
var App, context, ModernRender, ModernHydrate, renderLevel;
|
|
22
23
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
23
24
|
while (1) {
|
|
24
25
|
switch (_context.prev = _context.next) {
|
|
25
26
|
case 0:
|
|
26
|
-
App = _ref.App, context = _ref.context,
|
|
27
|
+
App = _ref.App, context = _ref.context, ModernRender = _ref.ModernRender, ModernHydrate = _ref.ModernHydrate;
|
|
27
28
|
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;
|
|
28
29
|
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
case 5:
|
|
38
|
-
ReactDOM.render( /*#__PURE__*/_jsx(App, {
|
|
39
|
-
context: context
|
|
40
|
-
}), rootElement);
|
|
41
|
-
_context.next = 9;
|
|
42
|
-
break;
|
|
43
|
-
|
|
44
|
-
case 8:
|
|
45
|
-
if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
30
|
+
if (renderLevel === RenderLevel.CLIENT_RENDER) {
|
|
31
|
+
// prefetch block render while csr
|
|
32
|
+
// await (App as any)?.prefetch?.(context);
|
|
33
|
+
ModernRender( /*#__PURE__*/_jsx(App, {
|
|
34
|
+
context: context
|
|
35
|
+
}));
|
|
36
|
+
} else if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
46
37
|
loadableReady(function () {
|
|
47
38
|
var hydrateContext = _objectSpread(_objectSpread({}, context), {}, {
|
|
48
39
|
_hydration: true
|
|
49
40
|
});
|
|
50
41
|
|
|
51
|
-
|
|
52
|
-
context: hydrateContext
|
|
53
|
-
}), rootElement, function () {
|
|
42
|
+
var callback = function callback() {
|
|
54
43
|
// won't cause component re-render because context's reference identity doesn't change
|
|
55
44
|
delete hydrateContext._hydration;
|
|
56
|
-
}
|
|
45
|
+
}; // callback: https://github.com/reactwg/react-18/discussions/5
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if (IS_REACT18) {
|
|
49
|
+
var SSRApp = function SSRApp() {
|
|
50
|
+
return /*#__PURE__*/_jsx(WithCallback, {
|
|
51
|
+
callback: callback,
|
|
52
|
+
children: /*#__PURE__*/_jsx(App, {
|
|
53
|
+
context: hydrateContext
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
59
|
+
ModernHydrate( /*#__PURE__*/_jsx(SSRApp, {}));
|
|
60
|
+
} else {
|
|
61
|
+
ModernHydrate( /*#__PURE__*/_jsx(App, {
|
|
62
|
+
context: hydrateContext
|
|
63
|
+
}), callback);
|
|
64
|
+
}
|
|
57
65
|
});
|
|
58
66
|
} else {
|
|
59
67
|
// unknown renderlevel or renderlevel is server prefetch.
|
|
60
|
-
|
|
68
|
+
ModernHydrate( /*#__PURE__*/_jsx(App, {
|
|
61
69
|
context: context
|
|
62
|
-
})
|
|
70
|
+
}));
|
|
63
71
|
}
|
|
64
72
|
|
|
65
|
-
case
|
|
73
|
+
case 3:
|
|
66
74
|
case "end":
|
|
67
75
|
return _context.stop();
|
|
68
76
|
}
|
|
@@ -76,13 +84,17 @@ var ssr = function ssr() {
|
|
|
76
84
|
|
|
77
85
|
return client;
|
|
78
86
|
}(),
|
|
79
|
-
init: function init(
|
|
87
|
+
init: function init(_ref2, next) {
|
|
80
88
|
var _window2, _window2$_SSR_DATA, _window2$_SSR_DATA$co;
|
|
81
89
|
|
|
82
|
-
var context =
|
|
90
|
+
var context = _ref2.context;
|
|
83
91
|
var 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;
|
|
84
92
|
|
|
85
93
|
if (!request) {
|
|
94
|
+
context.ssrContext = _objectSpread(_objectSpread({}, context.ssrContext), {}, {
|
|
95
|
+
response: mockResp,
|
|
96
|
+
request: formatClient({})
|
|
97
|
+
});
|
|
86
98
|
return next({
|
|
87
99
|
context: context
|
|
88
100
|
});
|
|
@@ -94,11 +106,11 @@ var ssr = function ssr() {
|
|
|
94
106
|
context: context
|
|
95
107
|
});
|
|
96
108
|
},
|
|
97
|
-
pickContext: function pickContext(
|
|
109
|
+
pickContext: function pickContext(_ref3, next) {
|
|
98
110
|
var _window3, _window3$_SSR_DATA, _window3$_SSR_DATA$co;
|
|
99
111
|
|
|
100
|
-
var context =
|
|
101
|
-
pickedContext =
|
|
112
|
+
var context = _ref3.context,
|
|
113
|
+
pickedContext = _ref3.pickedContext;
|
|
102
114
|
var 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;
|
|
103
115
|
var initialData = context.initialData;
|
|
104
116
|
|
|
@@ -19,7 +19,8 @@ var plugin = function plugin() {
|
|
|
19
19
|
return {
|
|
20
20
|
server: function () {
|
|
21
21
|
var _server = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
22
|
-
var App, context, html;
|
|
22
|
+
var App, context, _context$ssrContext, html;
|
|
23
|
+
|
|
23
24
|
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
24
25
|
while (1) {
|
|
25
26
|
switch (_context2.prev = _context2.next) {
|
|
@@ -39,7 +40,7 @@ var plugin = function plugin() {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
_context2.next = 5;
|
|
42
|
-
return require("./serverRender").render(context, (context === null || context === void 0 ? void 0 : context.ssrContext.distDir) || path.join(process.cwd(), 'dist'), App);
|
|
43
|
+
return require("./serverRender").render(context, (context === null || context === void 0 ? void 0 : (_context$ssrContext = context.ssrContext) === null || _context$ssrContext === void 0 ? void 0 : _context$ssrContext.distDir) || path.join(process.cwd(), 'dist'), App);
|
|
43
44
|
|
|
44
45
|
case 5:
|
|
45
46
|
html = _context2.sent;
|
|
@@ -64,19 +65,21 @@ var plugin = function plugin() {
|
|
|
64
65
|
}(),
|
|
65
66
|
init: function init(_ref2, next) {
|
|
66
67
|
var context = _ref2.context;
|
|
67
|
-
var
|
|
68
|
+
var _ref3 = context.ssrContext,
|
|
69
|
+
request = _ref3.request;
|
|
68
70
|
context.ssrContext.request = formatServer(request);
|
|
69
71
|
return next({
|
|
70
72
|
context: context
|
|
71
73
|
});
|
|
72
74
|
},
|
|
73
|
-
pickContext: function pickContext(
|
|
74
|
-
var context =
|
|
75
|
-
pickedContext =
|
|
75
|
+
pickContext: function pickContext(_ref4, next) {
|
|
76
|
+
var context = _ref4.context,
|
|
77
|
+
pickedContext = _ref4.pickedContext;
|
|
76
78
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
80
|
+
var _ref5 = context === null || context === void 0 ? void 0 : context.ssrContext,
|
|
81
|
+
request = _ref5.request,
|
|
82
|
+
response = _ref5.response;
|
|
80
83
|
|
|
81
84
|
var initialData = context.initialData;
|
|
82
85
|
return next({
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// See https://github.com/reactwg/react-18/discussions/5#discussioncomment-2276079
|
|
2
|
+
import { useRef, useLayoutEffect } from 'react';
|
|
3
|
+
export var WithCallback = function WithCallback(_ref) {
|
|
4
|
+
var callback = _ref.callback,
|
|
5
|
+
children = _ref.children;
|
|
6
|
+
var once = useRef(false);
|
|
7
|
+
useLayoutEffect(function () {
|
|
8
|
+
if (once.current) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
once.current = true;
|
|
13
|
+
callback();
|
|
14
|
+
}, [callback]);
|
|
15
|
+
return children;
|
|
16
|
+
};
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
var RE_HTML_ATTR = /<html[^>]*>/;
|
|
3
3
|
var RE_BODY_ATTR = /<body[^>]*>/;
|
|
4
4
|
var RE_LAST_IN_HEAD = /<\/head>/;
|
|
5
|
-
var RE_TITLE = /<title[^>]*>([\s\S\n\r]*?)<\/title
|
|
6
|
-
var TEST_TITLE_CONTENT = /(?<=<title[^>]*>)([\s\S\n\r]*?)([.|\S])([\s\S\n\r]*?)(?=<\/title>)
|
|
5
|
+
var RE_TITLE = /<title[^>]*>([\s\S\n\r]*?)<\/title>/;
|
|
6
|
+
var TEST_TITLE_CONTENT = /(?<=<title[^>]*>)([\s\S\n\r]*?)([.|\S])([\s\S\n\r]*?)(?=<\/title>)/; // 通过 react-helmet 修改模板
|
|
7
7
|
|
|
8
8
|
export default function helmet(content, helmetData) {
|
|
9
9
|
var result = content;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
2
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
3
|
import { run } from '@modern-js/utils/ssr';
|
|
4
|
-
import { RuntimeContext } from "../../core";
|
|
5
4
|
import { PreRender } from "../react/prerender";
|
|
6
5
|
import SSREntry from "./entry";
|
|
7
6
|
import { time } from "./measure";
|
|
@@ -22,20 +22,25 @@ var getQuery = function getQuery() {
|
|
|
22
22
|
key = _item$split2[0],
|
|
23
23
|
value = _item$split2[1];
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
if (key) {
|
|
26
|
+
res[key] = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
26
29
|
return res;
|
|
27
30
|
}, {});
|
|
28
31
|
};
|
|
29
32
|
|
|
30
33
|
export var formatClient = function formatClient(request) {
|
|
34
|
+
var _request$headers, _request$headers2;
|
|
35
|
+
|
|
31
36
|
return {
|
|
32
37
|
params: request.params || {},
|
|
33
38
|
host: request.host || location.host,
|
|
34
39
|
pathname: request.pathname || location.pathname,
|
|
35
40
|
headers: request.headers || {},
|
|
36
41
|
cookieMap: request.cookieMap || {},
|
|
37
|
-
cookie: request.headers.cookie || document.cookie,
|
|
38
|
-
userAgent: request.headers['user-agent'] || navigator.userAgent,
|
|
42
|
+
cookie: ((_request$headers = request.headers) === null || _request$headers === void 0 ? void 0 : _request$headers.cookie) || document.cookie,
|
|
43
|
+
userAgent: ((_request$headers2 = request.headers) === null || _request$headers2 === void 0 ? void 0 : _request$headers2['user-agent']) || navigator.userAgent,
|
|
39
44
|
referer: request.referer || document.referrer,
|
|
40
45
|
query: request.query || getQuery(),
|
|
41
46
|
url: location.href
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { TRuntimeContext } from '../runtime-context';
|
|
2
3
|
import { Plugin } from './plugin';
|
|
3
|
-
import { TRuntimeContext } from './runtime-context';
|
|
4
4
|
export declare type CreateAppOptions = {
|
|
5
5
|
plugins: Plugin[];
|
|
6
6
|
};
|
|
7
7
|
export declare const createApp: ({
|
|
8
8
|
plugins
|
|
9
9
|
}: CreateAppOptions) => (App?: React.ComponentType<any>) => React.ComponentType<any>;
|
|
10
|
-
|
|
10
|
+
interface HydrateFunc {
|
|
11
|
+
(container: Element | Document, initialChildren: React.ReactNode): void;
|
|
12
|
+
(initialChildren: React.ReactNode, container: Element | Document, callback?: () => void): void;
|
|
13
|
+
}
|
|
14
|
+
declare type BootStrap<T = unknown> = (App: React.ComponentType, id?: string | Record<string, any> | HTMLElement, root?: any, render?: (children: React.ReactNode, rootElement?: HTMLElement) => void, hydrate?: HydrateFunc) => Promise<T>;
|
|
11
15
|
export declare const bootstrap: BootStrap;
|
|
12
16
|
export declare const useRuntimeContext: () => TRuntimeContext;
|
|
13
17
|
export {};
|
|
@@ -3,7 +3,7 @@ export type { Plugin } from './plugin';
|
|
|
3
3
|
export { defineConfig, getConfig } from './app-config';
|
|
4
4
|
export type { AppConfig } from './app-config';
|
|
5
5
|
export * from './compatible';
|
|
6
|
-
export type { TRuntimeContext, RuntimeContext } from '
|
|
7
|
-
export { RuntimeReactContext } from '
|
|
6
|
+
export type { TRuntimeContext, RuntimeContext } from '../runtime-context';
|
|
7
|
+
export { RuntimeReactContext } from '../runtime-context';
|
|
8
8
|
export * from './loader';
|
|
9
9
|
export * from '@modern-js/plugin';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
2
|
import { Setup, ToThreads, CommonAPI, PluginOptions } from '@modern-js/plugin';
|
|
3
|
-
import type { RuntimeContext, TRuntimeContext } from '
|
|
3
|
+
import type { RuntimeContext, TRuntimeContext } from '../runtime-context';
|
|
4
4
|
export interface AppProps {}
|
|
5
5
|
declare const runtimeHooks: {
|
|
6
6
|
hoc: import("@modern-js/plugin").Pipeline<{
|
|
@@ -14,7 +14,8 @@ declare const runtimeHooks: {
|
|
|
14
14
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
15
15
|
App: React.ComponentType<any>;
|
|
16
16
|
readonly context?: RuntimeContext | undefined;
|
|
17
|
-
|
|
17
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
18
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
18
19
|
}, void>;
|
|
19
20
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
20
21
|
App: React.ComponentType<any>;
|
|
@@ -52,7 +53,8 @@ export declare const createRuntime: () => import("@modern-js/plugin").Manager<{
|
|
|
52
53
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
53
54
|
App: React.ComponentType<any>;
|
|
54
55
|
readonly context?: RuntimeContext | undefined;
|
|
55
|
-
|
|
56
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
57
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
56
58
|
}, void>;
|
|
57
59
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
58
60
|
App: React.ComponentType<any>;
|
|
@@ -90,7 +92,8 @@ export declare const runtime: import("@modern-js/plugin").Manager<{
|
|
|
90
92
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
91
93
|
App: React.ComponentType<any>;
|
|
92
94
|
readonly context?: RuntimeContext | undefined;
|
|
93
|
-
|
|
95
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
96
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
94
97
|
}, void>;
|
|
95
98
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
96
99
|
App: React.ComponentType<any>;
|
|
@@ -116,7 +119,8 @@ export declare const createPlugin: (setup?: Setup<{
|
|
|
116
119
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
117
120
|
App: React.ComponentType<any>;
|
|
118
121
|
readonly context?: RuntimeContext | undefined;
|
|
119
|
-
|
|
122
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
123
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
120
124
|
}, void>;
|
|
121
125
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
122
126
|
App: React.ComponentType<any>;
|
|
@@ -141,7 +145,8 @@ export declare const createPlugin: (setup?: Setup<{
|
|
|
141
145
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
142
146
|
App: React.ComponentType<any>;
|
|
143
147
|
readonly context?: RuntimeContext | undefined;
|
|
144
|
-
|
|
148
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
149
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
145
150
|
}, void>;
|
|
146
151
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
147
152
|
App: React.ComponentType<any>;
|
|
@@ -166,7 +171,8 @@ export declare const createPlugin: (setup?: Setup<{
|
|
|
166
171
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
167
172
|
App: React.ComponentType<any>;
|
|
168
173
|
readonly context?: RuntimeContext | undefined;
|
|
169
|
-
|
|
174
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
175
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
170
176
|
}, void>;
|
|
171
177
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
172
178
|
App: React.ComponentType<any>;
|
|
@@ -191,7 +197,8 @@ export declare const createPlugin: (setup?: Setup<{
|
|
|
191
197
|
client: import("@modern-js/plugin").AsyncPipeline<{
|
|
192
198
|
App: React.ComponentType<any>;
|
|
193
199
|
readonly context?: RuntimeContext | undefined;
|
|
194
|
-
|
|
200
|
+
ModernRender: (App: React.ReactNode) => void;
|
|
201
|
+
ModernHydrate: (App: React.ReactNode, callback?: () => void) => void;
|
|
195
202
|
}, void>;
|
|
196
203
|
server: import("@modern-js/plugin").AsyncPipeline<{
|
|
197
204
|
App: React.ComponentType<any>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { StateConfig } from './state';
|
|
2
2
|
import type { RouterConfig } from './router';
|
|
3
|
-
export type { Plugin,
|
|
3
|
+
export type { Plugin, AppConfig } from './core';
|
|
4
|
+
export type { BaseRuntimeContext, RuntimeContext, BaseTRuntimeContext, TRuntimeContext } from './runtime-context';
|
|
4
5
|
export { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, registerPrefetch, defineConfig, registerInit, useRuntimeContext } from './core';
|
|
5
6
|
export { StateConfig, RouterConfig };
|
|
6
7
|
declare module './core' {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Store } from '@modern-js-reduck/store';
|
|
3
|
+
import { createLoaderManager } from './core/loader/loaderManager';
|
|
4
|
+
import { runtime } from './core/plugin';
|
|
5
|
+
import { SSRServerContext } from './ssr/serverRender/type';
|
|
6
|
+
export interface BaseRuntimeContext {
|
|
7
|
+
loaderManager: ReturnType<typeof createLoaderManager>;
|
|
8
|
+
runner: ReturnType<typeof runtime.init>;
|
|
9
|
+
ssrContext?: SSRServerContext;
|
|
10
|
+
store?: Store;
|
|
11
|
+
}
|
|
12
|
+
export interface RuntimeContext extends BaseRuntimeContext {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
export declare const RuntimeReactContext: import("react").Context<RuntimeContext>;
|
|
16
|
+
export interface BaseTRuntimeContext {
|
|
17
|
+
initialData?: Record<string, unknown>;
|
|
18
|
+
request?: SSRServerContext['request'];
|
|
19
|
+
response?: SSRServerContext['response'];
|
|
20
|
+
store?: Store;
|
|
21
|
+
}
|
|
22
|
+
export interface TRuntimeContext extends BaseTRuntimeContext {
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}
|