@modern-js/runtime 1.4.4 → 1.5.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 +25 -0
- package/dist/js/modern/cli/index.js +1 -3
- package/dist/js/modern/core/app-config.js +10 -0
- package/dist/js/modern/core/compatible.js +218 -0
- package/dist/js/modern/core/index.js +7 -0
- package/dist/js/modern/core/loader/index.js +1 -0
- package/dist/js/modern/core/loader/loaderManager.js +189 -0
- package/dist/js/modern/core/loader/useLoader.js +105 -0
- package/dist/js/modern/core/plugin.js +63 -0
- package/dist/js/modern/core/runtime-context.js +2 -0
- package/dist/js/modern/exports/styled.js +2 -2
- package/dist/js/modern/index.js +1 -1
- package/dist/js/modern/router/runtime/plugin.js +1 -1
- package/dist/js/modern/router/runtime/utils.js +3 -1
- package/dist/js/modern/ssr/index.node.js +1 -1
- package/dist/js/modern/ssr/prefetch.js +2 -2
- package/dist/js/modern/ssr/serverRender/index.js +1 -1
- package/dist/js/modern/ssr/serverRender/styledComponent.js +1 -1
- package/dist/js/modern/state/runtime/plugin.js +1 -1
- package/dist/js/node/cli/index.js +1 -3
- package/dist/js/node/core/app-config.js +22 -0
- package/dist/js/node/core/compatible.js +247 -0
- package/dist/js/node/core/index.js +111 -0
- package/dist/js/node/core/loader/index.js +15 -0
- package/dist/js/node/core/loader/loaderManager.js +201 -0
- package/dist/js/node/core/loader/useLoader.js +118 -0
- package/dist/js/node/core/plugin.js +79 -0
- package/dist/js/node/core/runtime-context.js +11 -0
- package/dist/js/node/exports/styled.js +5 -5
- package/dist/js/node/index.js +10 -10
- package/dist/js/node/router/runtime/plugin.js +3 -3
- package/dist/js/node/router/runtime/utils.js +3 -1
- package/dist/js/node/ssr/index.node.js +2 -2
- package/dist/js/node/ssr/serverRender/index.js +2 -2
- package/dist/js/node/ssr/serverRender/styledComponent.js +2 -2
- package/dist/js/node/state/runtime/plugin.js +3 -3
- package/dist/js/treeshaking/cli/index.js +1 -3
- package/dist/js/treeshaking/core/app-config.js +13 -0
- package/dist/js/treeshaking/core/compatible.js +255 -0
- package/dist/js/treeshaking/core/index.js +7 -0
- package/dist/js/treeshaking/core/loader/index.js +1 -0
- package/dist/js/treeshaking/core/loader/loaderManager.js +267 -0
- package/dist/js/treeshaking/core/loader/useLoader.js +107 -0
- package/dist/js/treeshaking/core/plugin.js +105 -0
- package/dist/js/treeshaking/core/runtime-context.js +2 -0
- package/dist/js/treeshaking/exports/styled.js +2 -2
- package/dist/js/treeshaking/index.js +1 -1
- package/dist/js/treeshaking/router/runtime/plugin.js +1 -1
- package/dist/js/treeshaking/router/runtime/utils.js +3 -1
- package/dist/js/treeshaking/ssr/index.node.js +1 -1
- package/dist/js/treeshaking/ssr/prefetch.js +2 -2
- package/dist/js/treeshaking/ssr/serverRender/index.js +1 -1
- package/dist/js/treeshaking/ssr/serverRender/styledComponent.js +1 -1
- package/dist/js/treeshaking/state/runtime/plugin.js +1 -1
- package/dist/types/core/app-config.d.ts +6 -0
- package/dist/types/core/compatible.d.ts +13 -0
- package/dist/types/core/index.d.ts +9 -0
- package/dist/types/core/loader/index.d.ts +2 -0
- package/dist/types/core/loader/loaderManager.d.ts +57 -0
- package/dist/types/core/loader/useLoader.d.ts +54 -0
- package/dist/types/core/plugin.d.ts +208 -0
- package/dist/types/core/runtime-context.d.ts +13 -0
- package/dist/types/exports/styled.d.ts +2 -2
- package/dist/types/index.d.ts +4 -9
- package/dist/types/router/runtime/plugin.d.ts +1 -1
- package/dist/types/ssr/index.d.ts +2 -2
- package/dist/types/ssr/index.node.d.ts +1 -1
- package/dist/types/ssr/prefetch.d.ts +2 -2
- package/dist/types/ssr/serverRender/entry.d.ts +1 -1
- package/dist/types/ssr/serverRender/index.d.ts +1 -1
- package/dist/types/ssr/serverRender/type.d.ts +1 -1
- package/dist/types/state/runtime/plugin.d.ts +2 -2
- package/lib/types.d.ts +10 -0
- package/package.json +35 -25
|
@@ -8,7 +8,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
8
8
|
|
|
9
9
|
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
10
10
|
import path from 'path';
|
|
11
|
-
import { registerPrefetch } from
|
|
11
|
+
import { registerPrefetch } from "../core";
|
|
12
12
|
import { isBrowser } from "../common";
|
|
13
13
|
import prefetch from "./prefetch";
|
|
14
14
|
import { formatServer } from "./utils";
|
|
@@ -3,10 +3,10 @@ import fs from 'fs';
|
|
|
3
3
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
4
|
import { run } from '@modern-js/utils/ssr';
|
|
5
5
|
import { LOADABLE_STATS_FILE } from '@modern-js/utils/constants';
|
|
6
|
-
import { ChunkExtractor } from '@loadable/server';
|
|
7
|
-
|
|
6
|
+
import { ChunkExtractor } from '@loadable/server';
|
|
8
7
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
9
8
|
|
|
9
|
+
// todo: SSRContext
|
|
10
10
|
const prefetch = async (App, context) => run(context.ssrContext.request.headers, async () => {
|
|
11
11
|
var _context$store;
|
|
12
12
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RuntimeContext } from '@modern-js/runtime-core';
|
|
2
1
|
import { run } from '@modern-js/utils/ssr';
|
|
2
|
+
import { RuntimeContext } from "../../core";
|
|
3
3
|
import { PreRender } from "../react/prerender";
|
|
4
4
|
import SSREntry from "./entry";
|
|
5
5
|
import { time } from "./measure";
|
|
@@ -5,10 +5,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
7
7
|
import { useContext } from 'react';
|
|
8
|
-
import { RuntimeReactContext } from '@modern-js/runtime-core';
|
|
9
8
|
import { createStore } from '@modern-js-reduck/store';
|
|
10
9
|
import { Provider } from '@modern-js-reduck/react';
|
|
11
10
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
11
|
+
import { RuntimeReactContext } from "../../core";
|
|
12
12
|
import { isBrowser } from "../../common";
|
|
13
13
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
14
|
|
|
@@ -39,9 +39,7 @@ var _default = () => ({
|
|
|
39
39
|
* But it will not be installed under the user project.
|
|
40
40
|
* So need to add alias
|
|
41
41
|
*/
|
|
42
|
-
'styled-components': require.resolve('styled-components'
|
|
43
|
-
paths: [require.resolve('@modern-js/runtime-core')]
|
|
44
|
-
})
|
|
42
|
+
'styled-components': require.resolve('styled-components')
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getConfig = exports.defineConfig = void 0;
|
|
7
|
+
const APP_CONFIG_SYMBOL = 'config';
|
|
8
|
+
|
|
9
|
+
const getConfig = Component => // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
Component[APP_CONFIG_SYMBOL];
|
|
12
|
+
|
|
13
|
+
exports.getConfig = getConfig;
|
|
14
|
+
|
|
15
|
+
const defineConfig = (Component, config) => {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
Component[APP_CONFIG_SYMBOL] = config;
|
|
19
|
+
return Component;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.defineConfig = defineConfig;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useRuntimeContext = exports.createApp = exports.bootstrap = void 0;
|
|
7
|
+
|
|
8
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
9
|
+
|
|
10
|
+
var _reactDom = _interopRequireDefault(require("react-dom"));
|
|
11
|
+
|
|
12
|
+
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
|
13
|
+
|
|
14
|
+
var _plugin = require("./plugin");
|
|
15
|
+
|
|
16
|
+
var _runtimeContext = require("./runtime-context");
|
|
17
|
+
|
|
18
|
+
var _loaderManager = require("./loader/loaderManager");
|
|
19
|
+
|
|
20
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
21
|
+
|
|
22
|
+
const _excluded = ["context"];
|
|
23
|
+
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
|
|
26
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
+
|
|
28
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
|
+
|
|
30
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
31
|
+
|
|
32
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
33
|
+
|
|
34
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
35
|
+
|
|
36
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
37
|
+
|
|
38
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
39
|
+
|
|
40
|
+
function isClientArgs(id) {
|
|
41
|
+
return typeof id === 'string' || typeof HTMLElement !== 'undefined' && id instanceof HTMLElement;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const runnerMap = new WeakMap();
|
|
45
|
+
|
|
46
|
+
const getInitialContext = runner => ({
|
|
47
|
+
loaderManager: (0, _loaderManager.createLoaderManager)({}),
|
|
48
|
+
runner,
|
|
49
|
+
isBrowser: true
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const createApp = ({
|
|
53
|
+
plugins
|
|
54
|
+
}) => {
|
|
55
|
+
const appRuntime = _plugin.runtime.clone();
|
|
56
|
+
|
|
57
|
+
appRuntime.usePlugin(...plugins);
|
|
58
|
+
return App => {
|
|
59
|
+
const runner = appRuntime.init();
|
|
60
|
+
|
|
61
|
+
const WrapperComponent = props => {
|
|
62
|
+
const element = /*#__PURE__*/_react.default.createElement(App || _react.default.Fragment, _objectSpread({}, props), props.children);
|
|
63
|
+
|
|
64
|
+
const context = (0, _react.useContext)(_runtimeContext.RuntimeReactContext);
|
|
65
|
+
return runner.provide({
|
|
66
|
+
element,
|
|
67
|
+
props: _objectSpread({}, props),
|
|
68
|
+
context
|
|
69
|
+
}, {
|
|
70
|
+
onLast: ({
|
|
71
|
+
element
|
|
72
|
+
}) => element
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (App) {
|
|
77
|
+
(0, _hoistNonReactStatics.default)(WrapperComponent, App);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const HOCApp = runner.hoc({
|
|
81
|
+
App: WrapperComponent
|
|
82
|
+
}, {
|
|
83
|
+
onLast: ({
|
|
84
|
+
App
|
|
85
|
+
}) => {
|
|
86
|
+
const WrapComponent = _ref => {
|
|
87
|
+
var _contextValue;
|
|
88
|
+
|
|
89
|
+
let {
|
|
90
|
+
context
|
|
91
|
+
} = _ref,
|
|
92
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
93
|
+
|
|
94
|
+
let contextValue = context; // We should construct the context, when root component is not passed into `bootstrap`.
|
|
95
|
+
|
|
96
|
+
if (!((_contextValue = contextValue) !== null && _contextValue !== void 0 && _contextValue.runner)) {
|
|
97
|
+
contextValue = getInitialContext(runner);
|
|
98
|
+
runner.init({
|
|
99
|
+
context: contextValue
|
|
100
|
+
}, {
|
|
101
|
+
onLast: ({
|
|
102
|
+
context: context1
|
|
103
|
+
}) => {
|
|
104
|
+
var _App$init;
|
|
105
|
+
|
|
106
|
+
return App === null || App === void 0 ? void 0 : (_App$init = App.init) === null || _App$init === void 0 ? void 0 : _App$init.call(App, context1);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_runtimeContext.RuntimeReactContext.Provider, {
|
|
112
|
+
value: contextValue,
|
|
113
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(App, _objectSpread({}, props))
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
return (0, _hoistNonReactStatics.default)(WrapComponent, App);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
runnerMap.set(HOCApp, runner);
|
|
121
|
+
return HOCApp;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
exports.createApp = createApp;
|
|
126
|
+
|
|
127
|
+
const bootstrap = async (BootApp,
|
|
128
|
+
/**
|
|
129
|
+
* When csr, id is root id.
|
|
130
|
+
* When ssr, id is serverContext
|
|
131
|
+
*/
|
|
132
|
+
id) => {
|
|
133
|
+
let App = BootApp;
|
|
134
|
+
let runner = runnerMap.get(App); // ensure Component used is created by `createApp`
|
|
135
|
+
|
|
136
|
+
if (!runner) {
|
|
137
|
+
App = createApp({
|
|
138
|
+
plugins: []
|
|
139
|
+
})(App);
|
|
140
|
+
runner = runnerMap.get(App);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const context = getInitialContext(runner);
|
|
144
|
+
|
|
145
|
+
const runInit = _context => runner.init({
|
|
146
|
+
context: _context
|
|
147
|
+
}, {
|
|
148
|
+
onLast: ({
|
|
149
|
+
context: context1
|
|
150
|
+
}) => {
|
|
151
|
+
var _App, _App$init2;
|
|
152
|
+
|
|
153
|
+
return (_App = App) === null || _App === void 0 ? void 0 : (_App$init2 = _App.init) === null || _App$init2 === void 0 ? void 0 : _App$init2.call(_App, context1);
|
|
154
|
+
}
|
|
155
|
+
}); // don't mount the App, let user in charge of it.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
if (!id) {
|
|
159
|
+
return /*#__PURE__*/_react.default.createElement(App, {
|
|
160
|
+
context
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const isBrowser = typeof window !== 'undefined' && window.name !== 'nodejs';
|
|
165
|
+
|
|
166
|
+
if (isBrowser) {
|
|
167
|
+
if (isClientArgs(id)) {
|
|
168
|
+
var _ssrData$data, _ssrData$data2;
|
|
169
|
+
|
|
170
|
+
const ssrData = window._SSR_DATA;
|
|
171
|
+
const loadersData = (ssrData === null || ssrData === void 0 ? void 0 : (_ssrData$data = ssrData.data) === null || _ssrData$data === void 0 ? void 0 : _ssrData$data.loadersData) || {};
|
|
172
|
+
const initialLoadersState = Object.keys(loadersData).reduce((res, key) => {
|
|
173
|
+
const loaderData = loadersData[key];
|
|
174
|
+
|
|
175
|
+
if (loaderData.loading !== false) {
|
|
176
|
+
return res;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
res[key] = loaderData;
|
|
180
|
+
return res;
|
|
181
|
+
}, {});
|
|
182
|
+
Object.assign(context, _objectSpread({
|
|
183
|
+
loaderManager: (0, _loaderManager.createLoaderManager)(initialLoadersState, {
|
|
184
|
+
skipStatic: true
|
|
185
|
+
})
|
|
186
|
+
}, ssrData ? {
|
|
187
|
+
ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
|
|
188
|
+
} : {}));
|
|
189
|
+
context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData$data2 = ssrData.data) === null || _ssrData$data2 === void 0 ? void 0 : _ssrData$data2.initialData;
|
|
190
|
+
const initialData = await runInit(context);
|
|
191
|
+
|
|
192
|
+
if (initialData) {
|
|
193
|
+
context.initialData = initialData;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return runner.client({
|
|
197
|
+
App,
|
|
198
|
+
context,
|
|
199
|
+
rootElement: typeof id !== 'string' ? id : document.getElementById(id || 'root')
|
|
200
|
+
}, {
|
|
201
|
+
onLast: ({
|
|
202
|
+
App,
|
|
203
|
+
rootElement
|
|
204
|
+
}) => {
|
|
205
|
+
_reactDom.default.render( /*#__PURE__*/_react.default.createElement(App, {
|
|
206
|
+
context
|
|
207
|
+
}), rootElement);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
throw Error('`bootstrap` needs id in browser environment, it needs to be string or element');
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
Object.assign(context, {
|
|
215
|
+
ssrContext: id,
|
|
216
|
+
isBrowser: false,
|
|
217
|
+
loaderManager: (0, _loaderManager.createLoaderManager)({}, {
|
|
218
|
+
skipNonStatic: id.staticGenerate,
|
|
219
|
+
// if not static generate, only non-static loader can exec on prod env
|
|
220
|
+
skipStatic: process.env.NODE_ENV === 'production' && !id.staticGenerate
|
|
221
|
+
})
|
|
222
|
+
});
|
|
223
|
+
const initialData = await runInit(context);
|
|
224
|
+
context.initialData = initialData;
|
|
225
|
+
return runner.server({
|
|
226
|
+
App,
|
|
227
|
+
context
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
exports.bootstrap = bootstrap;
|
|
233
|
+
|
|
234
|
+
const useRuntimeContext = () => {
|
|
235
|
+
const context = (0, _react.useContext)(_runtimeContext.RuntimeReactContext);
|
|
236
|
+
const memoizedContext = (0, _react.useMemo)(() => context.runner.pickContext({
|
|
237
|
+
context,
|
|
238
|
+
pickedContext: {}
|
|
239
|
+
}, {
|
|
240
|
+
onLast: ({
|
|
241
|
+
pickedContext
|
|
242
|
+
}) => pickedContext
|
|
243
|
+
}), [context]);
|
|
244
|
+
return memoizedContext;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
exports.useRuntimeContext = useRuntimeContext;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
createPlugin: true,
|
|
8
|
+
createRuntime: true,
|
|
9
|
+
runtime: true,
|
|
10
|
+
registerInit: true,
|
|
11
|
+
registerPrefetch: true,
|
|
12
|
+
defineConfig: true,
|
|
13
|
+
getConfig: true,
|
|
14
|
+
RuntimeReactContext: true
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "RuntimeReactContext", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return _runtimeContext.RuntimeReactContext;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(exports, "createPlugin", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () {
|
|
25
|
+
return _plugin.createPlugin;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports, "createRuntime", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () {
|
|
31
|
+
return _plugin.createRuntime;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports, "defineConfig", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () {
|
|
37
|
+
return _appConfig.defineConfig;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(exports, "getConfig", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () {
|
|
43
|
+
return _appConfig.getConfig;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(exports, "registerInit", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function () {
|
|
49
|
+
return _plugin.registerInit;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "registerPrefetch", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _plugin.registerPrefetch;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(exports, "runtime", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: function () {
|
|
61
|
+
return _plugin.runtime;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
var _plugin = require("./plugin");
|
|
66
|
+
|
|
67
|
+
var _appConfig = require("./app-config");
|
|
68
|
+
|
|
69
|
+
var _compatible = require("./compatible");
|
|
70
|
+
|
|
71
|
+
Object.keys(_compatible).forEach(function (key) {
|
|
72
|
+
if (key === "default" || key === "__esModule") return;
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
74
|
+
if (key in exports && exports[key] === _compatible[key]) return;
|
|
75
|
+
Object.defineProperty(exports, key, {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function () {
|
|
78
|
+
return _compatible[key];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
var _runtimeContext = require("./runtime-context");
|
|
84
|
+
|
|
85
|
+
var _loader = require("./loader");
|
|
86
|
+
|
|
87
|
+
Object.keys(_loader).forEach(function (key) {
|
|
88
|
+
if (key === "default" || key === "__esModule") return;
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
90
|
+
if (key in exports && exports[key] === _loader[key]) return;
|
|
91
|
+
Object.defineProperty(exports, key, {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function () {
|
|
94
|
+
return _loader[key];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
var _plugin2 = require("@modern-js/plugin");
|
|
100
|
+
|
|
101
|
+
Object.keys(_plugin2).forEach(function (key) {
|
|
102
|
+
if (key === "default" || key === "__esModule") return;
|
|
103
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
104
|
+
if (key in exports && exports[key] === _plugin2[key]) return;
|
|
105
|
+
Object.defineProperty(exports, key, {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function () {
|
|
108
|
+
return _plugin2[key];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "useLoader", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _useLoader.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
var _useLoader = _interopRequireDefault(require("./useLoader"));
|
|
14
|
+
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createLoaderManager = exports.LoaderStatus = void 0;
|
|
7
|
+
|
|
8
|
+
var _invariant = _interopRequireDefault(require("invariant"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Calc id from string or object
|
|
14
|
+
*/
|
|
15
|
+
const createGetId = () => {
|
|
16
|
+
const idCache = new Map();
|
|
17
|
+
return objectId => {
|
|
18
|
+
const cachedId = idCache.get(objectId);
|
|
19
|
+
|
|
20
|
+
if (cachedId) {
|
|
21
|
+
return cachedId;
|
|
22
|
+
} // WARNING: id should be unique after serialize.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const id = JSON.stringify(objectId);
|
|
26
|
+
(0, _invariant.default)(id, 'params should be not null value');
|
|
27
|
+
idCache.set(objectId, id);
|
|
28
|
+
return id;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
let LoaderStatus;
|
|
33
|
+
exports.LoaderStatus = LoaderStatus;
|
|
34
|
+
|
|
35
|
+
(function (LoaderStatus) {
|
|
36
|
+
LoaderStatus[LoaderStatus["idle"] = 0] = "idle";
|
|
37
|
+
LoaderStatus[LoaderStatus["loading"] = 1] = "loading";
|
|
38
|
+
LoaderStatus[LoaderStatus["fulfilled"] = 2] = "fulfilled";
|
|
39
|
+
LoaderStatus[LoaderStatus["rejected"] = 3] = "rejected";
|
|
40
|
+
})(LoaderStatus || (exports.LoaderStatus = LoaderStatus = {}));
|
|
41
|
+
|
|
42
|
+
const createLoader = (id, initialData = {
|
|
43
|
+
loading: false,
|
|
44
|
+
reloading: false,
|
|
45
|
+
data: undefined,
|
|
46
|
+
error: undefined
|
|
47
|
+
}, loaderFn, skip = false) => {
|
|
48
|
+
let promise;
|
|
49
|
+
let status = LoaderStatus.idle;
|
|
50
|
+
let {
|
|
51
|
+
data,
|
|
52
|
+
error
|
|
53
|
+
} = initialData;
|
|
54
|
+
let hasLoaded = false;
|
|
55
|
+
const handlers = new Set();
|
|
56
|
+
|
|
57
|
+
const load = async () => {
|
|
58
|
+
if (skip) {
|
|
59
|
+
return promise;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (status === LoaderStatus.loading) {
|
|
63
|
+
return promise;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
status = LoaderStatus.loading;
|
|
67
|
+
notify();
|
|
68
|
+
promise = loaderFn().then(value => {
|
|
69
|
+
data = value;
|
|
70
|
+
error = null;
|
|
71
|
+
status = LoaderStatus.fulfilled;
|
|
72
|
+
}).catch(e => {
|
|
73
|
+
error = e;
|
|
74
|
+
data = null;
|
|
75
|
+
status = LoaderStatus.rejected;
|
|
76
|
+
}).finally(() => {
|
|
77
|
+
promise = null;
|
|
78
|
+
hasLoaded = true;
|
|
79
|
+
notify();
|
|
80
|
+
});
|
|
81
|
+
return promise;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const getResult = () => ({
|
|
85
|
+
loading: !hasLoaded && status === LoaderStatus.loading,
|
|
86
|
+
reloading: hasLoaded && status === LoaderStatus.loading,
|
|
87
|
+
data,
|
|
88
|
+
error: error instanceof Error ? `${error.message}` : error,
|
|
89
|
+
// redundant fields for ssr log
|
|
90
|
+
_error: error
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const notify = () => {
|
|
94
|
+
// don't iterate handlers directly, since it could be modified during iteration
|
|
95
|
+
[...handlers].forEach(handler => {
|
|
96
|
+
handler(status, getResult());
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const onChange = handler => {
|
|
101
|
+
handlers.add(handler);
|
|
102
|
+
return () => {
|
|
103
|
+
handlers.delete(handler);
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
get result() {
|
|
109
|
+
return getResult();
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
get promise() {
|
|
113
|
+
return promise;
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
onChange,
|
|
117
|
+
load
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Create loaders manager. It's returned instance will add to context
|
|
123
|
+
* @param initialDataMap used to initialing loader data
|
|
124
|
+
*/
|
|
125
|
+
const createLoaderManager = (initialDataMap, managerOptions = {}) => {
|
|
126
|
+
const {
|
|
127
|
+
skipStatic = false,
|
|
128
|
+
skipNonStatic = false
|
|
129
|
+
} = managerOptions;
|
|
130
|
+
const loadersMap = new Map();
|
|
131
|
+
const getId = createGetId();
|
|
132
|
+
|
|
133
|
+
const add = (loaderFn, loaderOptions) => {
|
|
134
|
+
const id = getId(loaderOptions.params);
|
|
135
|
+
let loader = loadersMap.get(id); // private property for opting out loader cache, maybe change in future
|
|
136
|
+
|
|
137
|
+
const cache = loaderOptions._cache;
|
|
138
|
+
|
|
139
|
+
if (!loader || cache === false) {
|
|
140
|
+
// ignore non-static loader on static phase
|
|
141
|
+
const ignoreNonStatic = skipNonStatic && !loaderOptions.static; // ignore static loader on non-static phase
|
|
142
|
+
|
|
143
|
+
const ignoreStatic = skipStatic && loaderOptions.static;
|
|
144
|
+
const skipExec = ignoreNonStatic || ignoreStatic;
|
|
145
|
+
loader = createLoader(id, typeof initialDataMap[id] !== 'undefined' ? initialDataMap[id] : {
|
|
146
|
+
data: loaderOptions.initialData
|
|
147
|
+
}, loaderFn, // Todo whether static loader is exec when CSR
|
|
148
|
+
skipExec);
|
|
149
|
+
loadersMap.set(id, loader);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return id;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const get = id => loadersMap.get(id); // check if there has pending loaders
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
const hasPendingLoaders = () => {
|
|
159
|
+
for (const loader of loadersMap.values()) {
|
|
160
|
+
const {
|
|
161
|
+
promise
|
|
162
|
+
} = loader;
|
|
163
|
+
|
|
164
|
+
if (promise instanceof Promise) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return false;
|
|
170
|
+
}; // waiting for all pending loaders to be settled
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
const awaitPendingLoaders = async () => {
|
|
174
|
+
const pendingLoaders = [];
|
|
175
|
+
|
|
176
|
+
for (const [id, loader] of loadersMap) {
|
|
177
|
+
const {
|
|
178
|
+
promise
|
|
179
|
+
} = loader;
|
|
180
|
+
|
|
181
|
+
if (promise instanceof Promise) {
|
|
182
|
+
pendingLoaders.push([id, loader]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
await Promise.all(pendingLoaders.map(item => item[1].promise));
|
|
187
|
+
return pendingLoaders.reduce((res, [id, loader]) => {
|
|
188
|
+
res[id] = loader.result;
|
|
189
|
+
return res;
|
|
190
|
+
}, {});
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
hasPendingLoaders,
|
|
195
|
+
awaitPendingLoaders,
|
|
196
|
+
add,
|
|
197
|
+
get
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
exports.createLoaderManager = createLoaderManager;
|