@bleedingdev/modern-js-runtime 3.2.0-ultramodern.8 → 3.2.0-ultramodern.80

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.
Files changed (65) hide show
  1. package/dist/cjs/boundary-debugger/index.js +298 -0
  2. package/dist/cjs/cli/ssr/index.js +3 -2
  3. package/dist/cjs/cli/template.server.js +1 -0
  4. package/dist/cjs/core/server/federatedCss.js +47 -0
  5. package/dist/cjs/core/server/helmet.js +2 -2
  6. package/dist/cjs/core/server/stream/afterTemplate.js +9 -6
  7. package/dist/cjs/core/server/stream/beforeTemplate.js +12 -8
  8. package/dist/cjs/core/server/stream/beforeTemplate.worker.js +109 -0
  9. package/dist/cjs/core/server/stream/createReadableStream.js +4 -2
  10. package/dist/cjs/core/server/stream/createReadableStream.worker.js +4 -2
  11. package/dist/cjs/core/server/stream/shared.js +3 -1
  12. package/dist/cjs/core/server/string/index.js +3 -1
  13. package/dist/cjs/core/server/string/loadable.js +33 -7
  14. package/dist/cjs/router/cli/code/tanstackTypes.js +116 -51
  15. package/dist/cjs/router/cli/code/templates.js +1 -8
  16. package/dist/cjs/router/runtime/tanstack/plugin.js +4 -5
  17. package/dist/cjs/router/runtime/tanstack/plugin.node.js +2 -13
  18. package/dist/cjs/router/runtime/tanstack/routeTree.js +40 -4
  19. package/dist/cjs/rsc/server.worker.js +58 -0
  20. package/dist/esm/boundary-debugger/index.mjs +262 -0
  21. package/dist/esm/cli/ssr/index.mjs +3 -2
  22. package/dist/esm/cli/template.server.mjs +1 -0
  23. package/dist/esm/core/server/federatedCss.mjs +13 -0
  24. package/dist/esm/core/server/helmet.mjs +1 -1
  25. package/dist/esm/core/server/stream/afterTemplate.mjs +10 -7
  26. package/dist/esm/core/server/stream/beforeTemplate.mjs +12 -8
  27. package/dist/esm/core/server/stream/beforeTemplate.worker.mjs +65 -0
  28. package/dist/esm/core/server/stream/createReadableStream.mjs +4 -2
  29. package/dist/esm/core/server/stream/createReadableStream.worker.mjs +4 -2
  30. package/dist/esm/core/server/stream/shared.mjs +3 -1
  31. package/dist/esm/core/server/string/index.mjs +3 -1
  32. package/dist/esm/core/server/string/loadable.mjs +33 -7
  33. package/dist/esm/router/cli/code/tanstackTypes.mjs +116 -51
  34. package/dist/esm/router/cli/code/templates.mjs +1 -8
  35. package/dist/esm/router/runtime/tanstack/plugin.mjs +8 -9
  36. package/dist/esm/router/runtime/tanstack/plugin.node.mjs +3 -14
  37. package/dist/esm/router/runtime/tanstack/routeTree.mjs +40 -4
  38. package/dist/esm/rsc/server.worker.mjs +1 -0
  39. package/dist/esm-node/boundary-debugger/index.mjs +263 -0
  40. package/dist/esm-node/cli/ssr/index.mjs +3 -2
  41. package/dist/esm-node/cli/template.server.mjs +1 -0
  42. package/dist/esm-node/core/server/federatedCss.mjs +14 -0
  43. package/dist/esm-node/core/server/helmet.mjs +1 -1
  44. package/dist/esm-node/core/server/stream/afterTemplate.mjs +10 -7
  45. package/dist/esm-node/core/server/stream/beforeTemplate.mjs +12 -8
  46. package/dist/esm-node/core/server/stream/beforeTemplate.worker.mjs +66 -0
  47. package/dist/esm-node/core/server/stream/createReadableStream.mjs +4 -2
  48. package/dist/esm-node/core/server/stream/createReadableStream.worker.mjs +4 -2
  49. package/dist/esm-node/core/server/stream/shared.mjs +3 -1
  50. package/dist/esm-node/core/server/string/index.mjs +3 -1
  51. package/dist/esm-node/core/server/string/loadable.mjs +33 -7
  52. package/dist/esm-node/router/cli/code/tanstackTypes.mjs +116 -51
  53. package/dist/esm-node/router/cli/code/templates.mjs +1 -8
  54. package/dist/esm-node/router/runtime/tanstack/plugin.mjs +8 -9
  55. package/dist/esm-node/router/runtime/tanstack/plugin.node.mjs +3 -14
  56. package/dist/esm-node/router/runtime/tanstack/routeTree.mjs +40 -4
  57. package/dist/esm-node/rsc/server.worker.mjs +2 -0
  58. package/dist/types/boundary-debugger/index.d.ts +28 -0
  59. package/dist/types/core/server/federatedCss.d.ts +5 -0
  60. package/dist/types/core/server/stream/beforeTemplate.d.ts +1 -0
  61. package/dist/types/core/server/stream/beforeTemplate.worker.d.ts +10 -0
  62. package/dist/types/core/server/stream/shared.d.ts +8 -0
  63. package/dist/types/core/server/string/loadable.d.ts +4 -0
  64. package/dist/types/rsc/server.worker.d.ts +1 -0
  65. package/package.json +22 -15
@@ -0,0 +1,262 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useMemo, useState } from "react";
3
+ const defaultStorageKey = 'modernjs:boundary-debugger:enabled';
4
+ const queryParamName = 'modern-boundaries';
5
+ const boundarySelector = '[data-modern-boundary-id]';
6
+ const defaultLabels = {
7
+ cs: {
8
+ toggle: 'zobrazit hranice týmů'
9
+ },
10
+ en: {
11
+ toggle: 'show team boundaries'
12
+ }
13
+ };
14
+ const palette = [
15
+ '#ff5a5f',
16
+ '#30e27a',
17
+ '#f6cf45',
18
+ '#7c8cff',
19
+ '#29b6f6'
20
+ ];
21
+ const readStoredEnabled = (storageKey, fallback)=>{
22
+ if ("u" < typeof window) return fallback;
23
+ try {
24
+ const stored = window.localStorage.getItem(storageKey);
25
+ return null === stored ? fallback : 'true' === stored;
26
+ } catch {
27
+ return fallback;
28
+ }
29
+ };
30
+ const writeStoredEnabled = (storageKey, enabled)=>{
31
+ if ("u" < typeof window) return;
32
+ try {
33
+ window.localStorage.setItem(storageKey, String(enabled));
34
+ } catch {}
35
+ };
36
+ const parseEnabledOverride = (value)=>{
37
+ if (null === value) return;
38
+ const normalized = value.toLowerCase();
39
+ if ('1' === normalized || 'true' === normalized) return true;
40
+ if ('0' === normalized || 'false' === normalized) return false;
41
+ };
42
+ const readQueryEnabledOverride = ()=>{
43
+ if ("u" < typeof window) return;
44
+ try {
45
+ return parseEnabledOverride(new URLSearchParams(window.location.search).get(queryParamName));
46
+ } catch {
47
+ return;
48
+ }
49
+ };
50
+ const detectLanguage = ()=>{
51
+ if ("u" < typeof document) return 'en';
52
+ const htmlLanguage = document.documentElement.lang;
53
+ if (htmlLanguage) return htmlLanguage.split('-')[0] || 'en';
54
+ if ("u" < typeof window) return 'en';
55
+ return window.location.pathname.split('/').filter(Boolean)[0] || 'en';
56
+ };
57
+ const hashBoundaryId = (id)=>{
58
+ let hash = 0;
59
+ for(let index = 0; index < id.length; index++)hash = 31 * hash + id.charCodeAt(index) >>> 0;
60
+ return hash;
61
+ };
62
+ const formatRectKey = (rect)=>[
63
+ Math.round(100 * rect.left) / 100,
64
+ Math.round(100 * rect.top) / 100,
65
+ Math.round(100 * rect.width) / 100,
66
+ Math.round(100 * rect.height) / 100
67
+ ].join(':');
68
+ const getBoundaryId = (element)=>element.dataset.modernBoundaryId ?? element.dataset.mfRemote ?? element.getAttribute('data-mf-remote') ?? void 0;
69
+ const collectBoundaryElements = (legacySelector)=>{
70
+ const elements = new Set();
71
+ for (const element of document.querySelectorAll(boundarySelector))elements.add(element);
72
+ if (!legacySelector) return Array.from(elements);
73
+ try {
74
+ for (const element of document.querySelectorAll(legacySelector))elements.add(element);
75
+ } catch {}
76
+ return Array.from(elements);
77
+ };
78
+ function BoundaryDebugger({ controlMode = 'visible', enabledByDefault = false, labels = defaultLabels, legacySelector, metadata, storageKey = defaultStorageKey }) {
79
+ const [mounted, setMounted] = useState(false);
80
+ const [enabled, setEnabled] = useState(false);
81
+ const [boxes, setBoxes] = useState([]);
82
+ const boundaries = useMemo(()=>new Map(metadata.boundaries.map((entry, index)=>[
83
+ entry.mfName,
84
+ {
85
+ ...entry,
86
+ color: entry.color ?? palette[index % palette.length],
87
+ label: entry.label ?? entry.appId
88
+ }
89
+ ])), [
90
+ metadata
91
+ ]);
92
+ const language = mounted ? detectLanguage() : 'en';
93
+ const toggleLabel = labels[language]?.toggle ?? labels.en?.toggle ?? defaultLabels.en?.toggle ?? 'show team boundaries';
94
+ useEffect(()=>{
95
+ setMounted(true);
96
+ const queryOverride = readQueryEnabledOverride();
97
+ setEnabled(queryOverride ?? readStoredEnabled(storageKey, enabledByDefault));
98
+ }, [
99
+ enabledByDefault,
100
+ storageKey
101
+ ]);
102
+ useEffect(()=>{
103
+ if (!mounted) return;
104
+ writeStoredEnabled(storageKey, enabled);
105
+ }, [
106
+ enabled,
107
+ mounted,
108
+ storageKey
109
+ ]);
110
+ useEffect(()=>{
111
+ if (!enabled) return void setBoxes([]);
112
+ const readBoxes = ()=>{
113
+ const seenBoxes = new Set();
114
+ const nextBoxes = collectBoundaryElements(legacySelector).map((element)=>{
115
+ const boundaryId = getBoundaryId(element);
116
+ if (!boundaryId) return;
117
+ const rect = element.getBoundingClientRect();
118
+ if (rect.width <= 0 || rect.height <= 0) return;
119
+ const rectKey = formatRectKey(rect);
120
+ const boxKey = `${boundaryId}:${rectKey}`;
121
+ if (seenBoxes.has(boxKey)) return;
122
+ seenBoxes.add(boxKey);
123
+ const boundary = boundaries.get(boundaryId);
124
+ const color = boundary?.color ?? palette[hashBoundaryId(boundaryId) % palette.length];
125
+ const label = boundary?.label ?? boundary?.appId ?? boundaryId;
126
+ const expose = element.dataset.modernMfExpose;
127
+ const detail = expose && expose !== label && expose !== boundaryId ? expose : void 0;
128
+ const box = {
129
+ color,
130
+ height: rect.height,
131
+ id: boxKey,
132
+ label,
133
+ left: rect.left,
134
+ top: rect.top,
135
+ width: rect.width
136
+ };
137
+ if (detail) box.detail = detail;
138
+ return box;
139
+ }).filter((box)=>void 0 !== box);
140
+ setBoxes(nextBoxes);
141
+ };
142
+ readBoxes();
143
+ const resizeObserver = "u" < typeof ResizeObserver ? void 0 : new ResizeObserver(readBoxes);
144
+ for (const element of collectBoundaryElements(legacySelector))resizeObserver?.observe(element);
145
+ const mutationObserver = new MutationObserver(readBoxes);
146
+ mutationObserver.observe(document.body, {
147
+ childList: true,
148
+ subtree: true
149
+ });
150
+ window.addEventListener('resize', readBoxes);
151
+ window.addEventListener('scroll', readBoxes, true);
152
+ return ()=>{
153
+ mutationObserver.disconnect();
154
+ resizeObserver?.disconnect();
155
+ window.removeEventListener('resize', readBoxes);
156
+ window.removeEventListener('scroll', readBoxes, true);
157
+ };
158
+ }, [
159
+ boundaries,
160
+ enabled,
161
+ legacySelector
162
+ ]);
163
+ if (!mounted) return null;
164
+ const shouldRenderToggle = 'visible' === controlMode || 'hidden-when-off' === controlMode && enabled;
165
+ return /*#__PURE__*/ jsxs(Fragment, {
166
+ children: [
167
+ shouldRenderToggle ? /*#__PURE__*/ jsxs("label", {
168
+ style: {
169
+ alignItems: 'center',
170
+ background: 'rgba(255, 255, 255, 0.96)',
171
+ border: '1px solid rgba(0, 0, 0, 0.1)',
172
+ borderRadius: 12,
173
+ bottom: 20,
174
+ boxShadow: '0 16px 40px rgba(0, 0, 0, 0.16)',
175
+ color: '#111827',
176
+ display: 'flex',
177
+ font: '600 14px/1.2 system-ui, sans-serif',
178
+ gap: 8,
179
+ left: 20,
180
+ padding: '12px 14px',
181
+ position: 'fixed',
182
+ zIndex: 2147483000
183
+ },
184
+ children: [
185
+ /*#__PURE__*/ jsx("input", {
186
+ checked: enabled,
187
+ onChange: (event)=>setEnabled(event.currentTarget.checked),
188
+ type: "checkbox"
189
+ }),
190
+ /*#__PURE__*/ jsx("span", {
191
+ children: toggleLabel
192
+ })
193
+ ]
194
+ }) : null,
195
+ enabled ? /*#__PURE__*/ jsx("div", {
196
+ "aria-hidden": "true",
197
+ children: boxes.map((box)=>/*#__PURE__*/ jsx("div", {
198
+ style: {
199
+ border: `2px solid ${box.color}`,
200
+ borderRadius: 8,
201
+ boxShadow: `0 0 0 1px rgba(255,255,255,.72), 0 6px 20px color-mix(in srgb, ${box.color} 20%, transparent)`,
202
+ height: box.height,
203
+ left: box.left,
204
+ pointerEvents: 'none',
205
+ position: 'fixed',
206
+ top: box.top,
207
+ width: box.width,
208
+ zIndex: 2147482999
209
+ },
210
+ children: /*#__PURE__*/ jsxs("span", {
211
+ style: {
212
+ background: box.color,
213
+ borderRadius: 999,
214
+ color: '#111827',
215
+ display: 'grid',
216
+ font: '800 11px/1.1 system-ui, sans-serif',
217
+ gap: 3,
218
+ maxWidth: 'min(280px, calc(100vw - 24px))',
219
+ padding: '5px 8px',
220
+ position: 'absolute',
221
+ right: 4,
222
+ top: 4,
223
+ whiteSpace: 'nowrap'
224
+ },
225
+ children: [
226
+ /*#__PURE__*/ jsx("span", {
227
+ children: box.label
228
+ }),
229
+ box.detail ? /*#__PURE__*/ jsx("span", {
230
+ style: {
231
+ font: '700 10px/1.1 system-ui, sans-serif',
232
+ opacity: 0.82,
233
+ overflow: 'hidden',
234
+ textOverflow: 'ellipsis'
235
+ },
236
+ children: box.detail
237
+ }) : null
238
+ ]
239
+ })
240
+ }, box.id))
241
+ }) : null
242
+ ]
243
+ });
244
+ }
245
+ const ultramodernBoundaryDebuggerPlugin = (options)=>({
246
+ name: '@modern-js/runtime/boundary-debugger',
247
+ setup: (api)=>{
248
+ api.wrapRoot((App)=>(props)=>/*#__PURE__*/ jsxs(Fragment, {
249
+ children: [
250
+ /*#__PURE__*/ jsx(App, {
251
+ ...props
252
+ }),
253
+ /*#__PURE__*/ jsx(BoundaryDebugger, {
254
+ ...options
255
+ })
256
+ ]
257
+ }));
258
+ }
259
+ });
260
+ const boundary_debugger = ultramodernBoundaryDebuggerPlugin;
261
+ export default boundary_debugger;
262
+ export { ultramodernBoundaryDebuggerPlugin };
@@ -84,13 +84,14 @@ const ssrBuilderPlugin = (modernAPI, outputModule, exportLoadablePath)=>({
84
84
  const hasServerRendering = hasServerRenderingConfig(userConfig);
85
85
  const hasModuleFederationRuntimeMarker = hasServerRendering && shouldUseModuleFederationNodeOutput(config);
86
86
  const hasExplicitMfSsrFlag = isModuleFederationAppSSREnabled(userConfig);
87
+ const isCloudflareWorkerSSR = 'workerSSR' === name && userConfig.deploy?.target === 'cloudflare';
87
88
  const requireExplicitMfSsrFlag = 'true' === process.env.MODERN_MF_APP_SSR_REQUIRE_EXPLICIT;
88
89
  if (hasServerRendering && hasModuleFederationRuntimeMarker && !hasExplicitMfSsrFlag) {
89
90
  const warningMessage = '[modernjs][mf-ssr] Module Federation SSR was auto-detected from runtime markers. Set server.ssr.moduleFederationAppSSR=true explicitly in host and remotes to avoid heuristic drift.';
90
91
  if (requireExplicitMfSsrFlag) throw new Error(`${warningMessage} (enforced by MODERN_MF_APP_SSR_REQUIRE_EXPLICIT=true)`);
91
92
  console.warn(warningMessage);
92
93
  }
93
- const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag;
94
+ const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag && !isCloudflareWorkerSSR;
94
95
  const useModuleFederationNodeOutput = hasServerRendering && isModuleFederationAppSSR && isNodeEnvironmentTarget(config.output.target);
95
96
  const ssrEnv = userConfig.deploy?.worker?.ssr || userConfig.server?.rsc ? 'edge' : 'node';
96
97
  const appContext = modernAPI.getAppContext();
@@ -107,7 +108,7 @@ const ssrBuilderPlugin = (modernAPI, outputModule, exportLoadablePath)=>({
107
108
  } : void 0;
108
109
  const useLoadablePlugin = isUseSSRBundle(userConfig) && !isServerEnvironment && checkUseStringSSR(userConfig, appDirectory, entrypoints);
109
110
  const outputConfig = {
110
- module: isServerEnvironment && !useModuleFederationNodeOutput && outputModule
111
+ module: isServerEnvironment && !useModuleFederationNodeOutput && (outputModule || 'workerSSR' === name && userConfig.deploy?.target === 'cloudflare')
111
112
  };
112
113
  const useLoadableComponents = isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig, appDirectory, entrypoints);
113
114
  return mergeEnvironmentConfig(config, {
@@ -35,6 +35,7 @@ const handleRequest = async (request, ServerRoot, options) => {
35
35
  </ServerRoot>,
36
36
  {
37
37
  ...options,
38
+ rscManifest: __rspack_rsc_manifest__,
38
39
  rscRoot: options.rscRoot,
39
40
  },
40
41
  );
@@ -0,0 +1,13 @@
1
+ import { attributesToString } from "./utils.mjs";
2
+ const createFederatedCssLinks = (assets, options)=>{
3
+ if (!assets?.length) return '';
4
+ const seen = new Set(options.existingAssets || []);
5
+ const attributes = attributesToString(options.attributes || {});
6
+ const links = [];
7
+ for (const asset of assets)if (!(!asset || seen.has(asset) || options.template.includes(asset))) {
8
+ seen.add(asset);
9
+ links.push(`<link${attributes} href="${asset}" rel="stylesheet" />`);
10
+ }
11
+ return links.join('');
12
+ };
13
+ export { createFederatedCssLinks };
@@ -1,5 +1,5 @@
1
- import { EOL } from "os";
2
1
  import { safeReplace } from "./utils.mjs";
2
+ const EOL = '\n';
3
3
  const RE_HTML_ATTR = /<html[^>]*>/;
4
4
  const RE_BODY_ATTR = /<body[^>]*>/;
5
5
  const RE_LAST_IN_HEAD = /<\/head>/;
@@ -1,5 +1,5 @@
1
1
  import { serializeJson } from "@modern-js/runtime-utils/node";
2
- import { getRouterHydrationScripts } from "../../../router/runtime/lifecycle.mjs";
2
+ import { getRouterHydrationScripts, getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
3
3
  import { SSR_DATA_JSON_ID } from "../../constants.mjs";
4
4
  import { SSR_DATA_PLACEHOLDER } from "../constants.mjs";
5
5
  import { buildHtml } from "../shared.mjs";
@@ -22,12 +22,15 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
22
22
  if (!routeManifest) return template;
23
23
  const { routeAssets } = routeManifest;
24
24
  if (!routeAssets) return template;
25
- const asyncEntry = routeAssets[`async-${entryName}`];
26
- if (asyncEntry) {
27
- const { assets } = asyncEntry;
28
- const jsChunkStr = assets?.filter((asset)=>asset.endsWith('.js'))?.map((asset)=>`<script src=${asset} nonce="${nonce}"></script>`).join(' ');
29
- if (jsChunkStr) return safeReplace(template, '<!--<?- chunksMap.js ?>-->', jsChunkStr);
30
- }
25
+ const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext) ?? [];
26
+ const assetEntries = [
27
+ ...matchedRouteIds.map((routeId)=>routeAssets[routeId]),
28
+ routeAssets[`async-${entryName}`]
29
+ ].filter(Boolean);
30
+ const jsAssets = Array.from(new Set(assetEntries.flatMap((entry)=>(entry.assets ?? []).filter((asset)=>asset.endsWith('.js')))));
31
+ const nonceAttr = nonce ? ` nonce="${nonce}"` : '';
32
+ const jsChunkStr = jsAssets.filter((asset)=>!template.includes(asset)).map((asset)=>`<script src=${asset}${nonceAttr}></script>`).join(' ');
33
+ if (jsChunkStr) return safeReplace(template, '<!--<?- chunksMap.js ?>-->', jsChunkStr);
31
34
  return template;
32
35
  }
33
36
  return buildHtml(afterAppTemplate, callbacks);
@@ -2,6 +2,7 @@ import { matchRoutes } from "@modern-js/runtime-utils/router";
2
2
  import react_helmet from "react-helmet";
3
3
  import { getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
4
4
  import { CHUNK_CSS_PLACEHOLDER } from "../constants.mjs";
5
+ import { createFederatedCssLinks } from "../federatedCss.mjs";
5
6
  import { createReplaceHelemt } from "../helmet.mjs";
6
7
  import { buildHtml } from "../shared.mjs";
7
8
  import { checkIsNode, safeReplace } from "../utils.mjs";
@@ -17,7 +18,7 @@ const checkIsInline = (chunk, enableInline)=>{
17
18
  return Boolean(enableInline);
18
19
  };
19
20
  async function buildShellBeforeTemplate(beforeAppTemplate, options) {
20
- const { config, runtimeContext, styledComponentsStyleTags, entryName } = options;
21
+ const { config, runtimeContext, styledComponentsStyleTags, entryName, moduleFederationCssAssets } = options;
21
22
  const helmetData = react_helmet.renderStatic();
22
23
  const callbacks = [
23
24
  createReplaceHelemt(helmetData),
@@ -27,33 +28,36 @@ async function buildShellBeforeTemplate(beforeAppTemplate, options) {
27
28
  async function injectCss(template, entryName, styledComponentsStyleTags) {
28
29
  let css = await getCssChunks();
29
30
  if (styledComponentsStyleTags) css += styledComponentsStyleTags;
31
+ css += createFederatedCssLinks(moduleFederationCssAssets, {
32
+ template,
33
+ existingAssets: css.match(/href="([^"]+)"/g)?.map((item)=>item.replace(/^href="/, '').replace(/"$/, ''))
34
+ });
30
35
  return safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
31
36
  async function getCssChunks() {
32
37
  const { routeManifest, routerContext, routes } = runtimeContext;
33
38
  if (!routeManifest) return '';
34
39
  const { routeAssets } = routeManifest;
35
- let matchedRouteManifests;
40
+ let matchedRouteManifests = [];
36
41
  const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext);
37
42
  if (matchedRouteIds?.length) matchedRouteManifests = matchedRouteIds.map((routeId)=>routeAssets[routeId]).filter(Boolean);
38
- else {
39
- if (!routerContext || !routes) return '';
43
+ else if (routerContext && routes) {
40
44
  const matches = matchRoutes(routes, routerContext.location, routerContext.basename);
41
45
  matchedRouteManifests = matches?.map((match, index)=>{
42
46
  if (!index) return;
43
47
  const routeId = match.route.id;
44
48
  if (routeId) return routeAssets[routeId];
45
- }).filter(Boolean);
49
+ }).filter(Boolean) ?? [];
46
50
  }
47
51
  const asyncEntry = routeAssets[`async-${entryName}`];
48
- if (asyncEntry) matchedRouteManifests?.push(asyncEntry);
49
- const cssChunks = matchedRouteManifests ? matchedRouteManifests.reduce((chunks, routeManifest)=>{
52
+ if (asyncEntry) matchedRouteManifests.push(asyncEntry);
53
+ const cssChunks = matchedRouteManifests.reduce((chunks, routeManifest)=>{
50
54
  const { referenceCssAssets = [] } = routeManifest;
51
55
  const _cssChunks = referenceCssAssets.filter((asset)=>asset?.endsWith('.css') && !template.includes(asset));
52
56
  return [
53
57
  ...chunks,
54
58
  ..._cssChunks
55
59
  ];
56
- }, []) : [];
60
+ }, []);
57
61
  const { inlineStyles } = config;
58
62
  const styles = await Promise.all(cssChunks.map(async (chunk)=>{
59
63
  const link = `<link href="${chunk}" rel="stylesheet" />`;
@@ -0,0 +1,65 @@
1
+ import { matchRoutes } from "@modern-js/runtime-utils/router";
2
+ import react_helmet from "react-helmet";
3
+ import { getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
4
+ import { CHUNK_CSS_PLACEHOLDER } from "../constants.mjs";
5
+ import { createFederatedCssLinks } from "../federatedCss.mjs";
6
+ import { createReplaceHelemt } from "../helmet.mjs";
7
+ import { buildHtml } from "../shared.mjs";
8
+ import { safeReplace } from "../utils.mjs";
9
+ const checkIsInline = (chunk, enableInline)=>{
10
+ if ('production' !== process.env.NODE_ENV) return false;
11
+ if (enableInline instanceof RegExp) return enableInline.test(chunk);
12
+ return Boolean(enableInline);
13
+ };
14
+ async function buildShellBeforeTemplate(beforeAppTemplate, options) {
15
+ const { config, runtimeContext, styledComponentsStyleTags, entryName, moduleFederationCssAssets } = options;
16
+ const helmetData = react_helmet.renderStatic();
17
+ const callbacks = [
18
+ createReplaceHelemt(helmetData),
19
+ (template)=>injectCss(template, entryName, styledComponentsStyleTags)
20
+ ];
21
+ return buildHtml(beforeAppTemplate, callbacks);
22
+ async function injectCss(template, entryName, styledComponentsStyleTags) {
23
+ let css = await getCssChunks();
24
+ if (styledComponentsStyleTags) css += styledComponentsStyleTags;
25
+ css += createFederatedCssLinks(moduleFederationCssAssets, {
26
+ template,
27
+ existingAssets: css.match(/href="([^"]+)"/g)?.map((item)=>item.replace(/^href="/, '').replace(/"$/, ''))
28
+ });
29
+ return safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
30
+ async function getCssChunks() {
31
+ const { routeManifest, routerContext, routes } = runtimeContext;
32
+ if (!routeManifest) return '';
33
+ const { routeAssets } = routeManifest;
34
+ let matchedRouteManifests = [];
35
+ const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext);
36
+ if (matchedRouteIds?.length) matchedRouteManifests = matchedRouteIds.map((routeId)=>routeAssets[routeId]).filter(Boolean);
37
+ else if (routerContext && routes) {
38
+ const matches = matchRoutes(routes, routerContext.location, routerContext.basename);
39
+ matchedRouteManifests = matches?.map((match, index)=>{
40
+ if (!index) return;
41
+ const routeId = match.route.id;
42
+ if (routeId) return routeAssets[routeId];
43
+ }).filter(Boolean) ?? [];
44
+ }
45
+ const asyncEntry = routeAssets[`async-${entryName}`];
46
+ if (asyncEntry) matchedRouteManifests.push(asyncEntry);
47
+ const cssChunks = matchedRouteManifests.reduce((chunks, routeManifest)=>{
48
+ const { referenceCssAssets = [] } = routeManifest;
49
+ const _cssChunks = referenceCssAssets.filter((asset)=>asset?.endsWith('.css') && !template.includes(asset));
50
+ return [
51
+ ...chunks,
52
+ ..._cssChunks
53
+ ];
54
+ }, []);
55
+ const { inlineStyles } = config;
56
+ const styles = cssChunks.map((chunk)=>{
57
+ const link = `<link href="${chunk}" rel="stylesheet" />`;
58
+ checkIsInline(chunk, inlineStyles);
59
+ return link;
60
+ });
61
+ return `${styles.join('')}`;
62
+ }
63
+ }
64
+ }
65
+ export { buildShellBeforeTemplate };
@@ -15,7 +15,7 @@ const defaultExtender = {
15
15
  };
16
16
  const createReadableStreamFromElement = async (request, rootElement, options)=>{
17
17
  const { renderToPipeableStream } = await import("react-dom/server");
18
- const { runtimeContext, htmlTemplate, config, ssrConfig, entryName } = options;
18
+ const { runtimeContext, htmlTemplate, config, ssrConfig, entryName, moduleFederationCssAssets } = options;
19
19
  let shellChunkStatus = ShellChunkStatus.START;
20
20
  let renderLevel = RenderLevel.SERVER_RENDER;
21
21
  const forceStream2String = Boolean(process.env.MODERN_JS_STREAM_TO_STRING);
@@ -52,6 +52,7 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
52
52
  runtimeContext,
53
53
  config,
54
54
  entryName,
55
+ moduleFederationCssAssets,
55
56
  styledComponentsStyleTags
56
57
  }).then(({ shellAfter, shellBefore })=>{
57
58
  const pendingScripts = [];
@@ -109,7 +110,8 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
109
110
  renderLevel,
110
111
  runtimeContext,
111
112
  entryName,
112
- config
113
+ config,
114
+ moduleFederationCssAssets
113
115
  }).then(({ shellAfter, shellBefore })=>{
114
116
  const fallbackHtml = `${shellBefore}${shellAfter}`;
115
117
  const readableStream = getReadableStreamFromString(fallbackHtml);
@@ -8,19 +8,21 @@ import { getTemplates } from "./template.mjs";
8
8
  const createReadableStreamFromElement = async (request, rootElement, options)=>{
9
9
  let shellChunkStatus = ShellChunkStatus.START;
10
10
  const chunkVec = [];
11
- const { htmlTemplate, runtimeContext, config, ssrConfig, entryName, rscRoot } = options;
11
+ const { htmlTemplate, runtimeContext, config, ssrConfig, entryName, moduleFederationCssAssets, rscManifest, rscRoot } = options;
12
12
  const { shellBefore, shellAfter } = await getTemplates(htmlTemplate, {
13
13
  renderLevel: RenderLevel.SERVER_RENDER,
14
14
  runtimeContext,
15
15
  ssrConfig,
16
16
  request,
17
17
  config,
18
- entryName
18
+ entryName,
19
+ moduleFederationCssAssets
19
20
  });
20
21
  try {
21
22
  const readableOriginal = await renderSSRStream(rootElement, {
22
23
  request,
23
24
  nonce: config.nonce,
25
+ rscManifest,
24
26
  rscRoot: rscRoot,
25
27
  routes: runtimeContext.routes,
26
28
  onError (error) {
@@ -63,7 +63,7 @@ function createRenderStreaming(createReadableStreamPromise) {
63
63
  const end = time();
64
64
  const { runtimeContext, config, resource } = options;
65
65
  const { onError, onTiming } = options;
66
- const { htmlTemplate, entryName } = resource;
66
+ const { htmlTemplate, entryName, moduleFederationCssAssets } = resource;
67
67
  const ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
68
68
  const StreamServerRootWrapper = ({ children })=>/*#__PURE__*/ jsxs(Fragment, {
69
69
  children: [
@@ -83,9 +83,11 @@ function createRenderStreaming(createReadableStreamPromise) {
83
83
  runtimeContext,
84
84
  ssrConfig,
85
85
  entryName,
86
+ moduleFederationCssAssets,
86
87
  rscClientManifest: options.rscClientManifest,
87
88
  rscSSRManifest: options.rscSSRManifest,
88
89
  rscServerManifest: options.rscServerManifest,
90
+ rscManifest: options.rscManifest,
89
91
  rscRoot: options.rscRoot,
90
92
  onShellReady () {
91
93
  const cost = end();
@@ -19,7 +19,7 @@ const renderString = async (request, serverRoot, options)=>{
19
19
  onTiming
20
20
  };
21
21
  const routerContext = runtimeContext.routerContext;
22
- const { htmlTemplate, entryName, loadableStats, routeManifest } = resource;
22
+ const { htmlTemplate, entryName, loadableStats, routeManifest, moduleFederationCssAssets } = resource;
23
23
  const ssrConfig = getSSRConfigByEntry(entryName, config.ssr, config.ssrByEntries);
24
24
  const chunkSet = {
25
25
  renderLevel: RenderLevel.CLIENT_RENDER,
@@ -32,8 +32,10 @@ const renderString = async (request, serverRoot, options)=>{
32
32
  stats: loadableStats,
33
33
  nonce: config.nonce,
34
34
  routeManifest,
35
+ runtimeContext,
35
36
  template: htmlTemplate,
36
37
  entryName,
38
+ moduleFederationCssAssets,
37
39
  chunkSet,
38
40
  config
39
41
  }),
@@ -1,4 +1,6 @@
1
1
  import { ChunkExtractor } from "@loadable/server";
2
+ import { getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
3
+ import { createFederatedCssLinks } from "../federatedCss.mjs";
2
4
  import { attributesToString, checkIsNode } from "../utils.mjs";
3
5
  const extname = (uri)=>{
4
6
  if ('string' != typeof uri || !uri.includes('.')) return '';
@@ -21,6 +23,20 @@ class LoadableCollector {
21
23
  const { routeManifest, entryName } = this.options;
22
24
  return routeManifest?.routeAssets?.[entryName]?.assets;
23
25
  }
26
+ getMatchedRouteChunks() {
27
+ const { routeManifest, runtimeContext } = this.options;
28
+ const routeAssets = routeManifest?.routeAssets;
29
+ if (!routeAssets) return [];
30
+ const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext) ?? [];
31
+ return matchedRouteIds.flatMap((routeId)=>{
32
+ const routeAsset = routeAssets[routeId];
33
+ return (routeAsset?.assets ?? []).map((asset)=>({
34
+ filename: asset.replace(/^\//, ''),
35
+ path: asset,
36
+ url: asset
37
+ }));
38
+ });
39
+ }
24
40
  collect(comopnent) {
25
41
  const { stats, entryName } = this.options;
26
42
  if (!stats) return comopnent;
@@ -33,20 +49,21 @@ class LoadableCollector {
33
49
  return this.extractor.collectChunks(comopnent);
34
50
  }
35
51
  async effect() {
36
- if (!this.extractor) return;
37
52
  const { extractor, options } = this;
38
53
  const { entryName, config } = options;
39
54
  const asyncChunks = [];
40
- if (config.enableAsyncEntry) try {
55
+ if (extractor && config.enableAsyncEntry) try {
41
56
  asyncChunks.push(...extractor.getChunkAssets([
42
57
  `async-${entryName}`
43
58
  ]));
44
59
  } catch (e) {}
45
- const chunks = [].concat(asyncChunks).concat(extractor.getChunkAssets(extractor.chunks));
60
+ const chunks = [].concat(asyncChunks).concat(extractor ? extractor.getChunkAssets(extractor.chunks) : []).concat(this.getMatchedRouteChunks());
46
61
  const scriptChunks = generateChunks(chunks, 'js');
47
62
  const styleChunks = generateChunks(chunks, 'css');
48
- this.emitLoadableScripts(extractor);
49
- await this.emitScriptAssets(scriptChunks);
63
+ if (extractor) {
64
+ this.emitLoadableScripts(extractor);
65
+ await this.emitScriptAssets(scriptChunks);
66
+ }
50
67
  await this.emitStyleAssets(styleChunks);
51
68
  }
52
69
  emitLoadableScripts(extractor) {
@@ -81,19 +98,28 @@ class LoadableCollector {
81
98
  chunkSet.jsChunk += scripts.filter((script)=>Boolean(script)).join('');
82
99
  }
83
100
  async emitStyleAssets(chunks) {
84
- const { template, chunkSet, config, entryName } = this.options;
101
+ const { template, chunkSet, config, moduleFederationCssAssets } = this.options;
85
102
  const { inlineStyles } = config;
86
103
  const atrributes = attributesToString(this.generateAttributes());
87
104
  const linkRegExp = /<link .*?href="([^"]+)".*?>/g;
88
105
  const matchs = template.matchAll(linkRegExp);
89
106
  const existedLinks = [];
90
107
  for (const match of matchs)existedLinks.push(match[1]);
91
- const css = await Promise.all(chunks.filter((chunk)=>!existedLinks.includes(chunk.url) && !this.existsAssets?.includes(chunk.path)).map(async (chunk)=>{
108
+ const emittedChunks = chunks.filter((chunk)=>!existedLinks.includes(chunk.url) && !this.existsAssets?.includes(chunk.path));
109
+ const css = await Promise.all(emittedChunks.map(async (chunk)=>{
92
110
  const link = `<link${atrributes} href="${chunk.url}" rel="stylesheet" />`;
93
111
  if (checkIsNode() && checkIsInline(chunk, inlineStyles)) return readAsset(chunk).then((content)=>`<style>${content}</style>`).catch((_)=>link);
94
112
  return link;
95
113
  }));
96
114
  chunkSet.cssChunk += css.filter((css)=>Boolean(css)).join('');
115
+ chunkSet.cssChunk += createFederatedCssLinks(moduleFederationCssAssets, {
116
+ template,
117
+ attributes: this.generateAttributes(),
118
+ existingAssets: [
119
+ ...existedLinks,
120
+ ...emittedChunks.map((chunk)=>chunk.url)
121
+ ]
122
+ });
97
123
  }
98
124
  generateAttributes(extraAtr = {}) {
99
125
  const { config } = this.options;