@modern-js/runtime 2.26.0 → 2.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/cjs/core/compatible.js +16 -17
- package/dist/cjs/core/loader/useLoader.js +10 -12
- package/dist/cjs/core/plugin.js +3 -2
- package/dist/cjs/document/Html.js +4 -3
- package/dist/cjs/document/cli/index.js +138 -138
- package/dist/cjs/router/cli/index.js +91 -92
- package/dist/cjs/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/cjs/router/runtime/PrefetchLink.js +8 -4
- package/dist/cjs/router/runtime/plugin.js +4 -3
- package/dist/cjs/router/runtime/plugin.node.js +11 -4
- package/dist/cjs/router/runtime/utils.js +3 -3
- package/dist/cjs/ssr/cli/index.js +122 -123
- package/dist/cjs/ssr/index.js +105 -107
- package/dist/cjs/ssr/index.node.js +37 -38
- package/dist/cjs/ssr/prefetch.js +37 -39
- package/dist/cjs/ssr/react/prerender/util.js +4 -2
- package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
- package/dist/cjs/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/cjs/state/cli/index.js +70 -72
- package/dist/cjs/state/runtime/plugin.js +40 -42
- package/dist/esm/core/compatible.js +10 -9
- package/dist/esm/core/loader/useLoader.js +9 -9
- package/dist/esm/core/plugin.js +3 -2
- package/dist/esm/document/Html.js +4 -3
- package/dist/esm/document/cli/index.js +4 -3
- package/dist/esm/router/cli/index.js +2 -1
- package/dist/esm/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/esm/router/runtime/PrefetchLink.js +8 -4
- package/dist/esm/router/runtime/plugin.js +4 -3
- package/dist/esm/router/runtime/plugin.node.js +11 -4
- package/dist/esm/router/runtime/utils.js +3 -3
- package/dist/esm/ssr/cli/index.js +7 -6
- package/dist/esm/ssr/index.js +6 -6
- package/dist/esm/ssr/index.node.js +2 -1
- package/dist/esm/ssr/prefetch.js +2 -2
- package/dist/esm/ssr/react/prerender/util.js +4 -2
- package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +7 -7
- package/dist/esm/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/esm/state/runtime/plugin.js +2 -2
- package/dist/esm-node/core/compatible.js +16 -17
- package/dist/esm-node/core/loader/useLoader.js +10 -12
- package/dist/esm-node/core/plugin.js +3 -2
- package/dist/esm-node/document/Html.js +4 -3
- package/dist/esm-node/document/cli/index.js +138 -138
- package/dist/esm-node/router/cli/index.js +91 -92
- package/dist/esm-node/router/runtime/DeferredDataScripts.node.js +4 -2
- package/dist/esm-node/router/runtime/PrefetchLink.js +8 -4
- package/dist/esm-node/router/runtime/plugin.js +4 -3
- package/dist/esm-node/router/runtime/plugin.node.js +11 -4
- package/dist/esm-node/router/runtime/utils.js +3 -3
- package/dist/esm-node/ssr/cli/index.js +122 -123
- package/dist/esm-node/ssr/cli/loadable-bundler-plugin.js +2 -2
- package/dist/esm-node/ssr/index.js +105 -107
- package/dist/esm-node/ssr/index.node.js +37 -38
- package/dist/esm-node/ssr/prefetch.js +37 -39
- package/dist/esm-node/ssr/react/prerender/util.js +4 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
- package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
- package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
- package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +2 -1
- package/dist/esm-node/state/cli/index.js +70 -72
- package/dist/esm-node/state/runtime/plugin.js +40 -42
- package/package.json +13 -10
|
@@ -20,102 +20,101 @@ _export(exports, {
|
|
|
20
20
|
const _utils = require("@modern-js/utils");
|
|
21
21
|
const PLUGIN_IDENTIFIER = "router";
|
|
22
22
|
const ROUTES_IDENTIFIER = "routes";
|
|
23
|
-
const routerPlugin = () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
|
|
48
|
-
}
|
|
23
|
+
const routerPlugin = () => ({
|
|
24
|
+
name: "@modern-js/plugin-router",
|
|
25
|
+
required: [
|
|
26
|
+
"@modern-js/runtime"
|
|
27
|
+
],
|
|
28
|
+
setup: (api) => {
|
|
29
|
+
const runtimeConfigMap = /* @__PURE__ */ new Map();
|
|
30
|
+
let pluginsExportsUtils;
|
|
31
|
+
return {
|
|
32
|
+
config() {
|
|
33
|
+
const appContext = api.useAppContext();
|
|
34
|
+
pluginsExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
|
|
35
|
+
return {
|
|
36
|
+
source: {
|
|
37
|
+
include: [
|
|
38
|
+
// react-router v6 is no longer support ie 11
|
|
39
|
+
// so we need to compile these packages to ensure the compatibility
|
|
40
|
+
// https://github.com/remix-run/react-router/commit/f6df0697e1b2064a2b3a12e8b39577326fdd945b
|
|
41
|
+
/node_modules\/react-router/,
|
|
42
|
+
/node_modules\/react-router-dom/,
|
|
43
|
+
/node_modules\/@remix-run\/router/
|
|
44
|
+
],
|
|
45
|
+
alias: {
|
|
46
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
|
|
49
47
|
}
|
|
50
|
-
};
|
|
51
|
-
},
|
|
52
|
-
validateSchema() {
|
|
53
|
-
return [
|
|
54
|
-
{
|
|
55
|
-
target: "runtime.router",
|
|
56
|
-
schema: {
|
|
57
|
-
type: [
|
|
58
|
-
"boolean",
|
|
59
|
-
"object"
|
|
60
|
-
]
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
];
|
|
64
|
-
},
|
|
65
|
-
modifyEntryImports({ entrypoint, imports }) {
|
|
66
|
-
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
67
|
-
const userConfig = api.useResolvedConfigContext();
|
|
68
|
-
const { packageName } = api.useAppContext();
|
|
69
|
-
const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
70
|
-
runtimeConfigMap.set(entryName, runtimeConfig);
|
|
71
|
-
if (runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.router) {
|
|
72
|
-
if (!(0, _utils.isRouterV5)(userConfig)) {
|
|
73
|
-
imports.push({
|
|
74
|
-
value: "@modern-js/runtime/plugins",
|
|
75
|
-
specifiers: [
|
|
76
|
-
{
|
|
77
|
-
imported: PLUGIN_IDENTIFIER
|
|
78
|
-
}
|
|
79
|
-
]
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
} else if (fileSystemRoutes) {
|
|
83
|
-
throw new Error(`should enable runtime.router for entry ${entryName}`);
|
|
84
48
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
plugins.push({
|
|
98
|
-
name: PLUGIN_IDENTIFIER,
|
|
99
|
-
options: JSON.stringify({
|
|
100
|
-
serverBase,
|
|
101
|
-
...runtimeConfig.router,
|
|
102
|
-
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
|
103
|
-
}).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
|
|
104
|
-
});
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
validateSchema() {
|
|
52
|
+
return [
|
|
53
|
+
{
|
|
54
|
+
target: "runtime.router",
|
|
55
|
+
schema: {
|
|
56
|
+
type: [
|
|
57
|
+
"boolean",
|
|
58
|
+
"object"
|
|
59
|
+
]
|
|
60
|
+
}
|
|
105
61
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
62
|
+
];
|
|
63
|
+
},
|
|
64
|
+
modifyEntryImports({ entrypoint, imports }) {
|
|
65
|
+
var _runtimeConfig;
|
|
66
|
+
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
67
|
+
const userConfig = api.useResolvedConfigContext();
|
|
68
|
+
const { packageName } = api.useAppContext();
|
|
69
|
+
const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
70
|
+
runtimeConfigMap.set(entryName, runtimeConfig);
|
|
71
|
+
if ((_runtimeConfig = runtimeConfig) === null || _runtimeConfig === void 0 ? void 0 : _runtimeConfig.router) {
|
|
113
72
|
if (!(0, _utils.isRouterV5)(userConfig)) {
|
|
114
|
-
|
|
73
|
+
imports.push({
|
|
74
|
+
value: "@modern-js/runtime/plugins",
|
|
75
|
+
specifiers: [
|
|
76
|
+
{
|
|
77
|
+
imported: PLUGIN_IDENTIFIER
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
});
|
|
115
81
|
}
|
|
82
|
+
} else if (fileSystemRoutes) {
|
|
83
|
+
throw new Error(`should enable runtime.router for entry ${entryName}`);
|
|
116
84
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
85
|
+
return {
|
|
86
|
+
entrypoint,
|
|
87
|
+
imports
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
|
91
|
+
const { entryName, fileSystemRoutes } = entrypoint;
|
|
92
|
+
const { serverRoutes } = api.useAppContext();
|
|
93
|
+
const userConfig = api.useResolvedConfigContext();
|
|
94
|
+
const runtimeConfig = runtimeConfigMap.get(entryName);
|
|
95
|
+
if (runtimeConfig.router && !(0, _utils.isRouterV5)(userConfig)) {
|
|
96
|
+
const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
|
|
97
|
+
plugins.push({
|
|
98
|
+
name: PLUGIN_IDENTIFIER,
|
|
99
|
+
options: JSON.stringify({
|
|
100
|
+
serverBase,
|
|
101
|
+
...runtimeConfig.router,
|
|
102
|
+
routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
|
|
103
|
+
}).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
entrypoint,
|
|
108
|
+
plugins
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
addRuntimeExports() {
|
|
112
|
+
const userConfig = api.useResolvedConfigContext();
|
|
113
|
+
if (!(0, _utils.isRouterV5)(userConfig)) {
|
|
114
|
+
pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/runtime/router'`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
});
|
|
121
120
|
const _default = routerPlugin;
|
|
@@ -18,6 +18,7 @@ const setupFnStr = `function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DA
|
|
|
18
18
|
const resolveFnStr = `function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};`;
|
|
19
19
|
const preResolvedFnStr = `function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};`;
|
|
20
20
|
const DeferredDataScripts = (props) => {
|
|
21
|
+
var _props;
|
|
21
22
|
const context = (0, _react.useContext)(_router.UNSAFE_DataRouterContext);
|
|
22
23
|
const { staticContext } = context || {};
|
|
23
24
|
const hydratedRef = (0, _react.useRef)(false);
|
|
@@ -45,8 +46,9 @@ const DeferredDataScripts = (props) => {
|
|
|
45
46
|
const { deferredKeys } = deferredData;
|
|
46
47
|
const deferredKeyPromiseStr = deferredKeys.map((key) => {
|
|
47
48
|
if (pendingKeys.has(key)) {
|
|
49
|
+
var _props2;
|
|
48
50
|
deferredDataScripts.push(/* @__PURE__ */ (0, _jsxruntime.jsx)(DeferredDataScript, {
|
|
49
|
-
nonce: props === null ||
|
|
51
|
+
nonce: (_props2 = props) === null || _props2 === void 0 ? void 0 : _props2.nonce,
|
|
50
52
|
data: deferredData.data[key],
|
|
51
53
|
dataKey: key,
|
|
52
54
|
routeId
|
|
@@ -82,7 +84,7 @@ const DeferredDataScripts = (props) => {
|
|
|
82
84
|
children: [
|
|
83
85
|
!hydratedRef.current && /* @__PURE__ */ (0, _jsxruntime.jsx)("script", {
|
|
84
86
|
async: true,
|
|
85
|
-
nonce: props === null ||
|
|
87
|
+
nonce: (_props = props) === null || _props === void 0 ? void 0 : _props.nonce,
|
|
86
88
|
suppressHydrationWarning: true,
|
|
87
89
|
dangerouslySetInnerHTML: {
|
|
88
90
|
__html: deferredScripts[0]
|
|
@@ -27,7 +27,8 @@ const _router = require("@modern-js/utils/runtime/router");
|
|
|
27
27
|
const _core = require("../../core");
|
|
28
28
|
function composeEventHandlers(theirHandler, ourHandler) {
|
|
29
29
|
return (event) => {
|
|
30
|
-
|
|
30
|
+
var _theirHandler;
|
|
31
|
+
(_theirHandler = theirHandler) === null || _theirHandler === void 0 ? void 0 : _theirHandler(event);
|
|
31
32
|
if (!event.defaultPrevented) {
|
|
32
33
|
ourHandler(event);
|
|
33
34
|
}
|
|
@@ -93,7 +94,8 @@ async function loadRouteModule(route, routeAssets) {
|
|
|
93
94
|
}
|
|
94
95
|
try {
|
|
95
96
|
await Promise.all(chunkIds.map((chunkId) => {
|
|
96
|
-
|
|
97
|
+
var __webpack_chunk_load__1;
|
|
98
|
+
return (__webpack_chunk_load__1 = __webpack_chunk_load__) === null || __webpack_chunk_load__1 === void 0 ? void 0 : __webpack_chunk_load__1(String(chunkId));
|
|
97
99
|
}));
|
|
98
100
|
} catch (error) {
|
|
99
101
|
console.error(error);
|
|
@@ -127,7 +129,8 @@ const PrefetchPageLinks = ({ pathname }) => {
|
|
|
127
129
|
const { routeAssets } = routeManifest || {};
|
|
128
130
|
const matches = Array.isArray(routes) ? (0, _router.matchRoutes)(routes, pathname) : [];
|
|
129
131
|
if (Array.isArray(matches) && routeAssets) {
|
|
130
|
-
|
|
132
|
+
var _matches;
|
|
133
|
+
(_matches = matches) === null || _matches === void 0 ? void 0 : _matches.forEach((match) => loadRouteModule(match.route, routeAssets));
|
|
131
134
|
}
|
|
132
135
|
if (!window._SSR_DATA) {
|
|
133
136
|
return null;
|
|
@@ -142,7 +145,8 @@ const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
|
|
|
142
145
|
const currentMatches = (0, _router.useMatches)();
|
|
143
146
|
const basename = (0, _router.useHref)("/");
|
|
144
147
|
const dataHrefs = (0, _react.useMemo)(() => {
|
|
145
|
-
|
|
148
|
+
var _matches;
|
|
149
|
+
return (_matches = matches) === null || _matches === void 0 ? void 0 : _matches.filter((match, index) => {
|
|
146
150
|
if (!match.route.loader || typeof match.route.loader !== "function" || match.route.loader.length === 0) {
|
|
147
151
|
return false;
|
|
148
152
|
}
|
|
@@ -73,6 +73,7 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
|
|
|
73
73
|
});
|
|
74
74
|
},
|
|
75
75
|
hoc: ({ App }, next) => {
|
|
76
|
+
var _routesConfig;
|
|
76
77
|
if (!finalRouteConfig && !createRoutes) {
|
|
77
78
|
return next({
|
|
78
79
|
App
|
|
@@ -80,7 +81,7 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
|
|
|
80
81
|
}
|
|
81
82
|
const getRouteApp = () => {
|
|
82
83
|
return (props) => {
|
|
83
|
-
var _window__SERVER_DATA;
|
|
84
|
+
var _window__SERVER_DATA, _hydrationData;
|
|
84
85
|
beforeCreateRouter = false;
|
|
85
86
|
routes = createRoutes ? createRoutes() : (0, _router.createRoutesFromElements)((0, _utils.renderRoutes)({
|
|
86
87
|
routesConfig: finalRouteConfig,
|
|
@@ -91,7 +92,7 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
|
|
|
91
92
|
const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
|
|
92
93
|
const _basename = baseUrl === "/" ? (0, _utils.urlJoin)(baseUrl, basename) : baseUrl;
|
|
93
94
|
let hydrationData = window._ROUTER_DATA;
|
|
94
|
-
if (hydrationData === null ||
|
|
95
|
+
if ((_hydrationData = hydrationData) === null || _hydrationData === void 0 ? void 0 : _hydrationData.errors) {
|
|
95
96
|
hydrationData = {
|
|
96
97
|
...hydrationData,
|
|
97
98
|
errors: (0, _utils.deserializeErrors)(hydrationData.errors)
|
|
@@ -136,7 +137,7 @@ const routerPlugin = ({ serverBase = [], supportHtml5History = true, basename =
|
|
|
136
137
|
if (App) {
|
|
137
138
|
RouteApp = (0, _hoistnonreactstatics.default)(RouteApp, App);
|
|
138
139
|
}
|
|
139
|
-
if (routesConfig === null ||
|
|
140
|
+
if ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp) {
|
|
140
141
|
return next({
|
|
141
142
|
App: (0, _hoistnonreactstatics.default)(RouteApp, routesConfig.globalApp)
|
|
142
143
|
});
|
|
@@ -76,13 +76,15 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
|
|
|
76
76
|
const { request, mode: ssrMode, nonce } = context.ssrContext;
|
|
77
77
|
const baseUrl = request.baseUrl;
|
|
78
78
|
const _basename = baseUrl === "/" ? (0, _utils.urlJoin)(baseUrl, basename) : baseUrl;
|
|
79
|
-
|
|
79
|
+
let routes = createRoutes ? createRoutes() : (0, _router1.createRoutesFromElements)((0, _utils.renderRoutes)({
|
|
80
80
|
routesConfig,
|
|
81
81
|
ssrMode,
|
|
82
82
|
props: {
|
|
83
83
|
nonce
|
|
84
84
|
}
|
|
85
85
|
}));
|
|
86
|
+
const runner = api.useHookRunners();
|
|
87
|
+
routes = runner.modifyRoutes(routes);
|
|
86
88
|
const { query } = (0, _remixrouter.createStaticHandler)(routes, {
|
|
87
89
|
basename: _basename
|
|
88
90
|
});
|
|
@@ -96,13 +98,12 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
|
|
|
96
98
|
context.routerContext = routerContext;
|
|
97
99
|
context.routes = routes;
|
|
98
100
|
context.routeManifest = context.ssrContext.routeManifest;
|
|
99
|
-
const runner = api.useHookRunners();
|
|
100
|
-
runner.modifyRoutes(routes);
|
|
101
101
|
return next({
|
|
102
102
|
context
|
|
103
103
|
});
|
|
104
104
|
},
|
|
105
105
|
hoc: ({ App }, next) => {
|
|
106
|
+
var _routesConfig;
|
|
106
107
|
if (!routesConfig) {
|
|
107
108
|
return next({
|
|
108
109
|
App
|
|
@@ -122,7 +123,7 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
|
|
|
122
123
|
};
|
|
123
124
|
};
|
|
124
125
|
const RouteApp = getRouteApp();
|
|
125
|
-
if (routesConfig === null ||
|
|
126
|
+
if ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp) {
|
|
126
127
|
return next({
|
|
127
128
|
App: (0, _hoistnonreactstatics.default)(RouteApp, routesConfig.globalApp)
|
|
128
129
|
});
|
|
@@ -133,6 +134,12 @@ const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
|
|
|
133
134
|
},
|
|
134
135
|
pickContext: ({ context, pickedContext }, next) => {
|
|
135
136
|
const { remixRouter } = context;
|
|
137
|
+
if (!remixRouter) {
|
|
138
|
+
return next({
|
|
139
|
+
context,
|
|
140
|
+
pickedContext
|
|
141
|
+
});
|
|
142
|
+
}
|
|
136
143
|
const router = {
|
|
137
144
|
navigate: remixRouter.navigate,
|
|
138
145
|
get location() {
|
|
@@ -93,9 +93,9 @@ function renderRoutes({ routesConfig, props, ssrMode }) {
|
|
|
93
93
|
return routeElements;
|
|
94
94
|
}
|
|
95
95
|
function getLocation(serverContext) {
|
|
96
|
-
var _url_replace;
|
|
97
|
-
const { pathname, url } = (serverContext === null ||
|
|
98
|
-
const cleanUrl = (
|
|
96
|
+
var _serverContext, _url_replace, _url;
|
|
97
|
+
const { pathname, url } = ((_serverContext = serverContext) === null || _serverContext === void 0 ? void 0 : _serverContext.request) || {};
|
|
98
|
+
const cleanUrl = (_url = url) === null || _url === void 0 ? void 0 : (_url_replace = _url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
|
|
99
99
|
const index = (cleanUrl || "").indexOf(pathname);
|
|
100
100
|
if (index === -1) {
|
|
101
101
|
return pathname;
|
|
@@ -22,12 +22,13 @@ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
|
22
22
|
const _utils = require("@modern-js/utils");
|
|
23
23
|
const PLUGIN_IDENTIFIER = "ssr";
|
|
24
24
|
const hasStringSSREntry = (userConfig) => {
|
|
25
|
+
var _server, _server1;
|
|
25
26
|
const isStreaming = (ssr) => ssr && typeof ssr === "object" && ssr.mode === "stream";
|
|
26
27
|
const { server } = userConfig;
|
|
27
|
-
if ((server === null ||
|
|
28
|
+
if (((_server = server) === null || _server === void 0 ? void 0 : _server.ssr) && !isStreaming(server.ssr)) {
|
|
28
29
|
return true;
|
|
29
30
|
}
|
|
30
|
-
if ((server === null ||
|
|
31
|
+
if (((_server1 = server) === null || _server1 === void 0 ? void 0 : _server1.ssrByEntries) && typeof server.ssrByEntries === "object") {
|
|
31
32
|
for (const name of Object.keys(server.ssrByEntries)) {
|
|
32
33
|
if (!isStreaming(server.ssrByEntries[name])) {
|
|
33
34
|
return true;
|
|
@@ -36,144 +37,142 @@ const hasStringSSREntry = (userConfig) => {
|
|
|
36
37
|
}
|
|
37
38
|
return false;
|
|
38
39
|
};
|
|
39
|
-
const ssrPlugin = () => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
const ssrPlugin = () => ({
|
|
41
|
+
name: "@modern-js/plugin-ssr",
|
|
42
|
+
required: [
|
|
43
|
+
"@modern-js/runtime"
|
|
44
|
+
],
|
|
45
|
+
setup: (api) => {
|
|
46
|
+
const ssrConfigMap = /* @__PURE__ */ new Map();
|
|
47
|
+
let pluginsExportsUtils;
|
|
48
|
+
return {
|
|
49
|
+
config() {
|
|
50
|
+
const appContext = api.useAppContext();
|
|
51
|
+
pluginsExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "plugins");
|
|
52
|
+
const userConfig = api.useConfigContext();
|
|
53
|
+
const { bundlerType = "webpack" } = api.useAppContext();
|
|
54
|
+
const babelConfig = (() => {
|
|
55
|
+
if (bundlerType === "webpack") {
|
|
56
|
+
return (config) => {
|
|
57
|
+
var _config_plugins;
|
|
58
|
+
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(_path.default.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
59
|
+
if ((0, _utils.isUseSSRBundle)(userConfig) && hasStringSSREntry(userConfig)) {
|
|
60
|
+
var _config_plugins1;
|
|
61
|
+
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
} else if (bundlerType === "rspack") {
|
|
65
|
+
if ((0, _utils.isUseSSRBundle)(userConfig)) {
|
|
56
66
|
return (config) => {
|
|
57
67
|
var _config_plugins;
|
|
58
68
|
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(_path.default.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
59
|
-
if (
|
|
69
|
+
if (hasStringSSREntry(userConfig)) {
|
|
60
70
|
var _config_plugins1;
|
|
61
71
|
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
62
72
|
}
|
|
63
73
|
};
|
|
64
|
-
} else if (bundlerType === "rspack") {
|
|
65
|
-
if ((0, _utils.isUseSSRBundle)(userConfig)) {
|
|
66
|
-
return (config) => {
|
|
67
|
-
var _config_plugins;
|
|
68
|
-
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(_path.default.join(__dirname, "./babel-plugin-ssr-loader-id"));
|
|
69
|
-
if (hasStringSSREntry(userConfig)) {
|
|
70
|
-
var _config_plugins1;
|
|
71
|
-
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(require.resolve("@loadable/babel-plugin"));
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
74
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
globalVars: (values, { target }) => {
|
|
85
|
-
values["process.env.MODERN_TARGET"] = target === "node" || target === "service-worker" ? "node" : "browser";
|
|
86
|
-
}
|
|
75
|
+
}
|
|
76
|
+
})();
|
|
77
|
+
return {
|
|
78
|
+
source: {
|
|
79
|
+
alias: {
|
|
80
|
+
// ensure that all packages use the same storage in @modern-js/utils/runtime-node
|
|
81
|
+
"@modern-js/utils/runtime-node$": require.resolve("@modern-js/utils/runtime-node"),
|
|
82
|
+
"@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
|
|
87
83
|
},
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const userConfig2 = api.useResolvedConfigContext();
|
|
91
|
-
if ((0, _utils.isUseSSRBundle)(userConfig2) && !isServer && !isServiceWorker && hasStringSSREntry(userConfig2)) {
|
|
92
|
-
const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
|
|
93
|
-
chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableBundlerPlugin, [
|
|
94
|
-
{
|
|
95
|
-
filename: _utils.LOADABLE_STATS_FILE
|
|
96
|
-
}
|
|
97
|
-
]);
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
babel: babelConfig
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
},
|
|
104
|
-
modifyEntryImports({ entrypoint, imports }) {
|
|
105
|
-
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
106
|
-
const userConfig = api.useResolvedConfigContext();
|
|
107
|
-
const { packageName, entrypoints } = api.useAppContext();
|
|
108
|
-
pluginsExportsUtils.addExport(`export { default as ssr } from '@modern-js/runtime/ssr'`);
|
|
109
|
-
const ssrConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
|
|
110
|
-
if (typeof ssrConfig === "object" && ssrConfig.mode === "stream") {
|
|
111
|
-
var _ref;
|
|
112
|
-
const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
113
|
-
if (((_ref = runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.router) === null || _ref === void 0 ? void 0 : _ref.mode) === "react-router-5") {
|
|
114
|
-
throw new Error(`router v5 plugin doesn't support streaming SSR, check your config 'runtime.router'`);
|
|
115
|
-
}
|
|
116
|
-
if (fileSystemRoutes && !entrypoint.nestedRoutesEntry) {
|
|
117
|
-
throw new Error("You should switch to file-system based router to support streaming SSR.");
|
|
84
|
+
globalVars: (values, { target }) => {
|
|
85
|
+
values["process.env.MODERN_TARGET"] = target === "node" || target === "service-worker" ? "node" : "browser";
|
|
118
86
|
}
|
|
87
|
+
},
|
|
88
|
+
tools: {
|
|
89
|
+
bundlerChain(chain, { isServer, isServiceWorker, CHAIN_ID }) {
|
|
90
|
+
const userConfig2 = api.useResolvedConfigContext();
|
|
91
|
+
if ((0, _utils.isUseSSRBundle)(userConfig2) && !isServer && !isServiceWorker && hasStringSSREntry(userConfig2)) {
|
|
92
|
+
const LoadableBundlerPlugin = require("./loadable-bundler-plugin.js");
|
|
93
|
+
chain.plugin(CHAIN_ID.PLUGIN.LOADABLE).use(LoadableBundlerPlugin, [
|
|
94
|
+
{
|
|
95
|
+
filename: _utils.LOADABLE_STATS_FILE
|
|
96
|
+
}
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
babel: babelConfig
|
|
119
101
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
modifyEntryImports({ entrypoint, imports }) {
|
|
105
|
+
const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
|
|
106
|
+
const userConfig = api.useResolvedConfigContext();
|
|
107
|
+
const { packageName, entrypoints } = api.useAppContext();
|
|
108
|
+
pluginsExportsUtils.addExport(`export { default as ssr } from '@modern-js/runtime/ssr'`);
|
|
109
|
+
const ssrConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.server.ssr, userConfig.server.ssrByEntries, packageName);
|
|
110
|
+
if (typeof ssrConfig === "object" && ssrConfig.mode === "stream") {
|
|
111
|
+
var _this, _runtimeConfig;
|
|
112
|
+
const runtimeConfig = (0, _utils.getEntryOptions)(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
|
|
113
|
+
if (((_this = (_runtimeConfig = runtimeConfig) === null || _runtimeConfig === void 0 ? void 0 : _runtimeConfig.router) === null || _this === void 0 ? void 0 : _this.mode) === "react-router-5") {
|
|
114
|
+
throw new Error(`router v5 plugin doesn't support streaming SSR, check your config 'runtime.router'`);
|
|
131
115
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
imports
|
|
135
|
-
};
|
|
136
|
-
},
|
|
137
|
-
modifyEntryRuntimePlugins({ entrypoint, plugins, bundlerConfigs }) {
|
|
138
|
-
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
139
|
-
var _bundlerConfigs_find, _bundlerConfigs_find_output;
|
|
140
|
-
const chunkLoadingGlobal = (_bundlerConfigs_find = bundlerConfigs === null || bundlerConfigs === void 0 ? void 0 : bundlerConfigs.find((config2) => config2.name === "client")) === null || _bundlerConfigs_find === void 0 ? void 0 : (_bundlerConfigs_find_output = _bundlerConfigs_find.output) === null || _bundlerConfigs_find_output === void 0 ? void 0 : _bundlerConfigs_find_output.chunkLoadingGlobal;
|
|
141
|
-
const config = api.useResolvedConfigContext();
|
|
142
|
-
const { crossorigin, scriptLoading } = config.html;
|
|
143
|
-
plugins.push({
|
|
144
|
-
name: PLUGIN_IDENTIFIER,
|
|
145
|
-
options: JSON.stringify({
|
|
146
|
-
...ssrConfigMap.get(entrypoint.entryName) || {},
|
|
147
|
-
crossorigin,
|
|
148
|
-
scriptLoading,
|
|
149
|
-
chunkLoadingGlobal
|
|
150
|
-
})
|
|
151
|
-
});
|
|
116
|
+
if (fileSystemRoutes && !entrypoint.nestedRoutesEntry) {
|
|
117
|
+
throw new Error("You should switch to file-system based router to support streaming SSR.");
|
|
152
118
|
}
|
|
119
|
+
}
|
|
120
|
+
const useSSG = (0, _utils.isSSGEntry)(userConfig, entryName, entrypoints);
|
|
121
|
+
ssrConfigMap.set(entryName, ssrConfig || useSSG);
|
|
122
|
+
if (ssrConfig || useSSG) {
|
|
123
|
+
imports.push({
|
|
124
|
+
value: "@modern-js/runtime/plugins",
|
|
125
|
+
specifiers: [
|
|
126
|
+
{
|
|
127
|
+
imported: PLUGIN_IDENTIFIER
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
entrypoint,
|
|
134
|
+
imports
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins, bundlerConfigs }) {
|
|
138
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
139
|
+
var _bundlerConfigs_find_output, _bundlerConfigs_find, _bundlerConfigs;
|
|
140
|
+
const chunkLoadingGlobal = (_bundlerConfigs = bundlerConfigs) === null || _bundlerConfigs === void 0 ? void 0 : (_bundlerConfigs_find = _bundlerConfigs.find((config2) => config2.name === "client")) === null || _bundlerConfigs_find === void 0 ? void 0 : (_bundlerConfigs_find_output = _bundlerConfigs_find.output) === null || _bundlerConfigs_find_output === void 0 ? void 0 : _bundlerConfigs_find_output.chunkLoadingGlobal;
|
|
141
|
+
const config = api.useResolvedConfigContext();
|
|
142
|
+
const { crossorigin, scriptLoading } = config.html;
|
|
143
|
+
plugins.push({
|
|
144
|
+
name: PLUGIN_IDENTIFIER,
|
|
145
|
+
options: JSON.stringify({
|
|
146
|
+
...ssrConfigMap.get(entrypoint.entryName) || {},
|
|
147
|
+
crossorigin,
|
|
148
|
+
scriptLoading,
|
|
149
|
+
chunkLoadingGlobal
|
|
150
|
+
})
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
entrypoint,
|
|
155
|
+
plugins
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
modifyEntryExport({ entrypoint, exportStatement }) {
|
|
159
|
+
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
153
160
|
return {
|
|
154
161
|
entrypoint,
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
},
|
|
158
|
-
modifyEntryExport({ entrypoint, exportStatement }) {
|
|
159
|
-
if (ssrConfigMap.get(entrypoint.entryName)) {
|
|
160
|
-
return {
|
|
161
|
-
entrypoint,
|
|
162
|
-
exportStatement: [
|
|
163
|
-
`export function ${_utils.SERVER_RENDER_FUNCTION_NAME}(context) {
|
|
162
|
+
exportStatement: [
|
|
163
|
+
`export function ${_utils.SERVER_RENDER_FUNCTION_NAME}(context) {
|
|
164
164
|
return bootstrap(AppWrapper, context)
|
|
165
165
|
}`,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
return {
|
|
171
|
-
entrypoint,
|
|
172
|
-
exportStatement
|
|
166
|
+
exportStatement
|
|
167
|
+
].join("\n")
|
|
173
168
|
};
|
|
174
169
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
};
|
|
170
|
+
return {
|
|
171
|
+
entrypoint,
|
|
172
|
+
exportStatement
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
});
|
|
179
178
|
const _default = ssrPlugin;
|