@modern-js/runtime 1.15.0 → 1.17.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 +56 -0
- package/dist/js/modern/cli/index.js +5 -12
- package/dist/js/modern/core/compatible.js +31 -8
- package/dist/js/modern/core/index.js +3 -3
- 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 +32 -15
- 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/node/cli/index.js +4 -12
- 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 +35 -17
- 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/treeshaking/cli/index.js +5 -11
- package/dist/js/treeshaking/core/compatible.js +63 -23
- package/dist/js/treeshaking/core/index.js +3 -3
- 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 +42 -34
- 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/types/common.d.ts +8 -1
- package/dist/types/core/app-config.d.ts +1 -3
- package/dist/types/core/compatible.d.ts +11 -2
- package/dist/types/core/index.d.ts +2 -3
- package/dist/types/core/loader/useLoader.d.ts +1 -0
- package/dist/types/core/plugin.d.ts +15 -8
- package/dist/types/index.d.ts +4 -8
- 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 -12
- package/package.json +14 -11
- package/dist/types/core/runtime-context.d.ts +0 -13
|
@@ -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,
|
|
4
|
+
import { PLUGIN_SCHEMAS, 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";
|
|
@@ -11,35 +11,29 @@ export default (function () {
|
|
|
11
11
|
post: ['@modern-js/plugin-router', '@modern-js/plugin-ssr', '@modern-js/plugin-state', '@modern-js/plugin-design-token'],
|
|
12
12
|
usePlugins: [PluginState(), PluginRouter(), PluginSSR()],
|
|
13
13
|
setup: function setup(api) {
|
|
14
|
-
var runtimeExportsUtils = {};
|
|
15
14
|
return {
|
|
16
15
|
config: function config() {
|
|
17
|
-
var dir = api.useAppContext().internalDirectory;
|
|
18
|
-
|
|
16
|
+
var dir = api.useAppContext().internalDirectory || '';
|
|
17
|
+
process.env.IS_REACT18 = isReact18(path.join(dir, '../../')).toString();
|
|
19
18
|
return {
|
|
20
19
|
runtime: {},
|
|
21
20
|
runtimeByEntries: {},
|
|
22
21
|
source: {
|
|
23
22
|
alias: {
|
|
24
|
-
'@modern-js/runtime$': runtimeExportsUtils.getPath(),
|
|
25
|
-
|
|
26
23
|
/**
|
|
27
24
|
* twin.macro inserts styled-components into the code during the compilation process
|
|
28
25
|
* But it will not be installed under the user project.
|
|
29
26
|
* So need to add alias
|
|
30
27
|
*/
|
|
31
28
|
'styled-components': require.resolve('styled-components')
|
|
32
|
-
}
|
|
29
|
+
},
|
|
30
|
+
envVars: ['IS_REACT18']
|
|
33
31
|
}
|
|
34
32
|
};
|
|
35
33
|
},
|
|
36
34
|
validateSchema: function validateSchema() {
|
|
37
35
|
return PLUGIN_SCHEMAS['@modern-js/runtime'];
|
|
38
36
|
},
|
|
39
|
-
addRuntimeExports: function addRuntimeExports() {
|
|
40
|
-
var runtimePackage = path.resolve(__dirname, '../../../../');
|
|
41
|
-
runtimeExportsUtils.addExport("export * from '".concat(runtimePackage, "'"));
|
|
42
|
-
},
|
|
43
37
|
beforeRestart: function beforeRestart() {
|
|
44
38
|
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
45
39
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
@@ -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,34 @@ 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 ReactDOM,
|
|
109
|
+
App,
|
|
110
|
+
runner,
|
|
111
|
+
context,
|
|
112
|
+
runInit,
|
|
113
|
+
isBrowser,
|
|
114
|
+
_ssrData$data,
|
|
115
|
+
_ssrData$data2,
|
|
116
|
+
ssrData,
|
|
117
|
+
loadersData,
|
|
118
|
+
initialLoadersState,
|
|
119
|
+
initialData,
|
|
120
|
+
_rootElement,
|
|
121
|
+
ModernRender,
|
|
122
|
+
ModernHydrate,
|
|
123
|
+
_initialData,
|
|
124
|
+
_args = arguments;
|
|
104
125
|
|
|
105
126
|
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
106
127
|
while (1) {
|
|
107
128
|
switch (_context2.prev = _context2.next) {
|
|
108
129
|
case 0:
|
|
130
|
+
ReactDOM = _args.length > 3 && _args[3] !== undefined ? _args[3] : defaultReactDOM;
|
|
109
131
|
App = BootApp;
|
|
110
132
|
runner = runnerMap.get(App); // ensure Component used is created by `createApp`
|
|
111
133
|
|
|
@@ -133,7 +155,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
133
155
|
|
|
134
156
|
|
|
135
157
|
if (id) {
|
|
136
|
-
_context2.next =
|
|
158
|
+
_context2.next = 8;
|
|
137
159
|
break;
|
|
138
160
|
}
|
|
139
161
|
|
|
@@ -141,16 +163,16 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
141
163
|
context: context
|
|
142
164
|
}));
|
|
143
165
|
|
|
144
|
-
case
|
|
166
|
+
case 8:
|
|
145
167
|
isBrowser = typeof window !== 'undefined' && window.name !== 'nodejs';
|
|
146
168
|
|
|
147
169
|
if (!isBrowser) {
|
|
148
|
-
_context2.next =
|
|
170
|
+
_context2.next = 29;
|
|
149
171
|
break;
|
|
150
172
|
}
|
|
151
173
|
|
|
152
174
|
if (!isClientArgs(id)) {
|
|
153
|
-
_context2.next =
|
|
175
|
+
_context2.next = 26;
|
|
154
176
|
break;
|
|
155
177
|
}
|
|
156
178
|
|
|
@@ -174,38 +196,56 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
174
196
|
ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
|
|
175
197
|
} : {}));
|
|
176
198
|
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 =
|
|
199
|
+
_context2.next = 18;
|
|
178
200
|
return runInit(context);
|
|
179
201
|
|
|
180
|
-
case
|
|
202
|
+
case 18:
|
|
181
203
|
initialData = _context2.sent;
|
|
182
204
|
|
|
183
205
|
if (initialData) {
|
|
184
206
|
context.initialData = initialData;
|
|
185
207
|
}
|
|
186
208
|
|
|
209
|
+
_rootElement = typeof id !== 'string' ? id : document.getElementById(id || 'root'); // https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html
|
|
210
|
+
|
|
211
|
+
ModernRender = function ModernRender(App) {
|
|
212
|
+
if (IS_REACT18) {
|
|
213
|
+
(root || ReactDOM.createRoot(_rootElement)).render(App);
|
|
214
|
+
} else {
|
|
215
|
+
ReactDOM.render(App, _rootElement);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
ModernHydrate = function ModernHydrate(App, callback) {
|
|
220
|
+
if (IS_REACT18) {
|
|
221
|
+
ReactDOM.hydrateRoot(_rootElement, App);
|
|
222
|
+
} else {
|
|
223
|
+
ReactDOM.hydrate(App, _rootElement, callback);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
187
227
|
return _context2.abrupt("return", runner.client({
|
|
188
228
|
App: App,
|
|
189
229
|
context: context,
|
|
190
|
-
|
|
230
|
+
ModernRender: ModernRender,
|
|
231
|
+
ModernHydrate: ModernHydrate
|
|
191
232
|
}, {
|
|
192
233
|
onLast: function onLast(_ref8) {
|
|
193
|
-
var App = _ref8.App
|
|
194
|
-
|
|
195
|
-
ReactDOM.render( /*#__PURE__*/React.createElement(App, {
|
|
234
|
+
var App = _ref8.App;
|
|
235
|
+
ModernRender( /*#__PURE__*/React.createElement(App, {
|
|
196
236
|
context: context
|
|
197
|
-
})
|
|
237
|
+
}));
|
|
198
238
|
}
|
|
199
239
|
}));
|
|
200
240
|
|
|
201
|
-
case
|
|
241
|
+
case 26:
|
|
202
242
|
throw Error('`bootstrap` needs id in browser environment, it needs to be string or element');
|
|
203
243
|
|
|
204
|
-
case
|
|
205
|
-
_context2.next =
|
|
244
|
+
case 27:
|
|
245
|
+
_context2.next = 35;
|
|
206
246
|
break;
|
|
207
247
|
|
|
208
|
-
case
|
|
248
|
+
case 29:
|
|
209
249
|
Object.assign(context, {
|
|
210
250
|
ssrContext: id,
|
|
211
251
|
isBrowser: false,
|
|
@@ -215,10 +255,10 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
215
255
|
skipStatic: process.env.NODE_ENV === 'production' && !id.staticGenerate
|
|
216
256
|
})
|
|
217
257
|
});
|
|
218
|
-
_context2.next =
|
|
258
|
+
_context2.next = 32;
|
|
219
259
|
return runInit(context);
|
|
220
260
|
|
|
221
|
-
case
|
|
261
|
+
case 32:
|
|
222
262
|
_initialData = _context2.sent;
|
|
223
263
|
context.initialData = _initialData;
|
|
224
264
|
return _context2.abrupt("return", runner.server({
|
|
@@ -226,7 +266,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
226
266
|
context: context
|
|
227
267
|
}));
|
|
228
268
|
|
|
229
|
-
case
|
|
269
|
+
case 35:
|
|
230
270
|
case "end":
|
|
231
271
|
return _context2.stop();
|
|
232
272
|
}
|
|
@@ -234,7 +274,7 @@ export var bootstrap = /*#__PURE__*/function () {
|
|
|
234
274
|
}, _callee);
|
|
235
275
|
}));
|
|
236
276
|
|
|
237
|
-
return function bootstrap(_x, _x2) {
|
|
277
|
+
return function bootstrap(_x, _x2, _x3) {
|
|
238
278
|
return _ref6.apply(this, arguments);
|
|
239
279
|
};
|
|
240
280
|
}();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createPlugin, createRuntime, runtime, registerInit, registerPrefetch } from "./plugin";
|
|
2
|
-
export { defineConfig, getConfig } from "./app-config";
|
|
3
|
-
|
|
2
|
+
export { defineConfig, getConfig } from "./app-config"; // compatible
|
|
3
|
+
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
if (!(renderLevel === RenderLevel.CLIENT_RENDER)) {
|
|
30
|
-
_context.next = 8;
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
_context.next = 5;
|
|
35
|
-
return App === null || App === void 0 ? void 0 : (_prefetch = (_ref2 = App).prefetch) === null || _prefetch === void 0 ? void 0 : _prefetch.call(_ref2, context);
|
|
27
|
+
App = _ref.App, context = _ref.context, ModernRender = _ref.ModernRender, ModernHydrate = _ref.ModernHydrate;
|
|
28
|
+
// if render level not exist, use client render
|
|
29
|
+
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; // client render and server prefetch use same logic
|
|
36
30
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
break;
|
|
43
|
-
|
|
44
|
-
case 8:
|
|
45
|
-
if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
31
|
+
if (renderLevel === RenderLevel.CLIENT_RENDER || renderLevel === RenderLevel.SERVER_PREFETCH) {
|
|
32
|
+
ModernRender( /*#__PURE__*/_jsx(App, {
|
|
33
|
+
context: context
|
|
34
|
+
}));
|
|
35
|
+
} else if (renderLevel === RenderLevel.SERVER_RENDER) {
|
|
46
36
|
loadableReady(function () {
|
|
47
37
|
var hydrateContext = _objectSpread(_objectSpread({}, context), {}, {
|
|
48
38
|
_hydration: true
|
|
49
39
|
});
|
|
50
40
|
|
|
51
|
-
|
|
52
|
-
context: hydrateContext
|
|
53
|
-
}), rootElement, function () {
|
|
41
|
+
var callback = function callback() {
|
|
54
42
|
// won't cause component re-render because context's reference identity doesn't change
|
|
55
43
|
delete hydrateContext._hydration;
|
|
56
|
-
}
|
|
44
|
+
}; // callback: https://github.com/reactwg/react-18/discussions/5
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if (IS_REACT18) {
|
|
48
|
+
var SSRApp = function SSRApp() {
|
|
49
|
+
return /*#__PURE__*/_jsx(WithCallback, {
|
|
50
|
+
callback: callback,
|
|
51
|
+
children: /*#__PURE__*/_jsx(App, {
|
|
52
|
+
context: hydrateContext
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
SSRApp = hoistNonReactStatics(SSRApp, App);
|
|
58
|
+
ModernHydrate( /*#__PURE__*/_jsx(SSRApp, {}));
|
|
59
|
+
} else {
|
|
60
|
+
ModernHydrate( /*#__PURE__*/_jsx(App, {
|
|
61
|
+
context: hydrateContext
|
|
62
|
+
}), callback);
|
|
63
|
+
}
|
|
57
64
|
});
|
|
58
65
|
} else {
|
|
59
66
|
// unknown renderlevel or renderlevel is server prefetch.
|
|
60
|
-
|
|
67
|
+
console.warn("unknow render level: ".concat(renderLevel, ", execute render()"));
|
|
68
|
+
ModernRender( /*#__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,10 +84,10 @@ 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) {
|
|
@@ -98,11 +106,11 @@ var ssr = function ssr() {
|
|
|
98
106
|
context: context
|
|
99
107
|
});
|
|
100
108
|
},
|
|
101
|
-
pickContext: function pickContext(
|
|
109
|
+
pickContext: function pickContext(_ref3, next) {
|
|
102
110
|
var _window3, _window3$_SSR_DATA, _window3$_SSR_DATA$co;
|
|
103
111
|
|
|
104
|
-
var context =
|
|
105
|
-
pickedContext =
|
|
112
|
+
var context = _ref3.context,
|
|
113
|
+
pickedContext = _ref3.pickedContext;
|
|
106
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;
|
|
107
115
|
var initialData = context.initialData;
|
|
108
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";
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import type { StateConfig } from './state';
|
|
2
|
+
import type { RouterConfig } from './router';
|
|
3
|
+
export declare const isBrowser: () => boolean;
|
|
4
|
+
export interface AppConfig {
|
|
5
|
+
router?: RouterConfig | boolean;
|
|
6
|
+
state?: StateConfig | boolean;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
|
-
|
|
3
|
-
[key: string]: any;
|
|
4
|
-
}
|
|
2
|
+
import { AppConfig } from '../common';
|
|
5
3
|
export declare const getConfig: (Component: React.ComponentType<any>) => AppConfig | undefined;
|
|
6
4
|
export declare const defineConfig: (Component: React.ComponentType<any>, config: AppConfig) => React.ComponentType<any>;
|
|
@@ -1,13 +1,22 @@
|
|
|
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, ReactDOM: {
|
|
15
|
+
render: (children: React.ReactNode, rootElement?: HTMLElement) => void;
|
|
16
|
+
hydrate: HydrateFunc;
|
|
17
|
+
createRoot?: (rootElement: HTMLElement) => any;
|
|
18
|
+
hydrateRoot: HydrateFunc;
|
|
19
|
+
}) => Promise<T>;
|
|
11
20
|
export declare const bootstrap: BootStrap;
|
|
12
21
|
export declare const useRuntimeContext: () => TRuntimeContext;
|
|
13
22
|
export {};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export { createPlugin, createRuntime, runtime, registerInit, registerPrefetch } from './plugin';
|
|
2
2
|
export type { Plugin } from './plugin';
|
|
3
3
|
export { defineConfig, getConfig } from './app-config';
|
|
4
|
-
export type { AppConfig } from './app-config';
|
|
5
4
|
export * from './compatible';
|
|
6
|
-
export type { TRuntimeContext, RuntimeContext } from '
|
|
7
|
-
export { RuntimeReactContext } from '
|
|
5
|
+
export type { TRuntimeContext, RuntimeContext } from '../runtime-context';
|
|
6
|
+
export { RuntimeReactContext } from '../runtime-context';
|
|
8
7
|
export * from './loader';
|
|
9
8
|
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,11 +1,7 @@
|
|
|
1
1
|
import type { StateConfig } from './state';
|
|
2
2
|
import type { RouterConfig } from './router';
|
|
3
|
-
export type { Plugin
|
|
3
|
+
export type { Plugin } from './core';
|
|
4
|
+
export type { AppConfig } from './common';
|
|
5
|
+
export type { BaseRuntimeContext, RuntimeContext, BaseTRuntimeContext, TRuntimeContext } from './runtime-context';
|
|
4
6
|
export { createApp, createPlugin, useLoader, bootstrap, RuntimeReactContext, registerPrefetch, defineConfig, registerInit, useRuntimeContext } from './core';
|
|
5
|
-
export { StateConfig, RouterConfig };
|
|
6
|
-
declare module './core' {
|
|
7
|
-
interface AppConfig {
|
|
8
|
-
router?: RouterConfig | boolean;
|
|
9
|
-
state?: StateConfig | boolean;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
7
|
+
export { StateConfig, RouterConfig };
|