@bleedingdev/modern-js-runtime 3.2.0-ultramodern.10 → 3.2.0-ultramodern.101

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 (97) hide show
  1. package/dist/cjs/boundary-debugger/index.js +299 -0
  2. package/dist/cjs/cli/ssr/index.js +5 -15
  3. package/dist/cjs/cli/template.server.js +1 -0
  4. package/dist/cjs/core/react/wrapper.js +9 -3
  5. package/dist/cjs/core/server/federatedCss.js +47 -0
  6. package/dist/cjs/core/server/helmet.js +8 -2
  7. package/dist/cjs/core/server/scriptOrder.js +59 -0
  8. package/dist/cjs/core/server/stream/afterTemplate.js +13 -5
  9. package/dist/cjs/core/server/stream/beforeTemplate.js +13 -20
  10. package/dist/cjs/core/server/stream/beforeTemplate.worker.js +98 -0
  11. package/dist/cjs/core/server/stream/createReadableStream.js +7 -2
  12. package/dist/cjs/core/server/stream/createReadableStream.worker.js +4 -2
  13. package/dist/cjs/core/server/stream/shared.js +3 -1
  14. package/dist/cjs/core/server/string/index.js +16 -9
  15. package/dist/cjs/core/server/string/loadable.js +74 -10
  16. package/dist/cjs/exports/head.js +196 -5
  17. package/dist/cjs/exports/loadable.js +34 -4
  18. package/dist/cjs/exports/tanstack-router.js +311 -54
  19. package/dist/cjs/router/cli/code/tanstackTypes.js +116 -51
  20. package/dist/cjs/router/cli/code/templates.js +15 -9
  21. package/dist/cjs/router/runtime/tanstack/hydrationBoundary.js +44 -0
  22. package/dist/cjs/router/runtime/tanstack/outlet.js +54 -0
  23. package/dist/cjs/router/runtime/tanstack/plugin.js +190 -88
  24. package/dist/cjs/router/runtime/tanstack/plugin.node.js +4 -14
  25. package/dist/cjs/router/runtime/tanstack/routeTree.js +64 -12
  26. package/dist/cjs/rsc/server.worker.js +58 -0
  27. package/dist/cjs/ssr/serverRender/renderToString/entry.js +9 -8
  28. package/dist/esm/boundary-debugger/index.mjs +263 -0
  29. package/dist/esm/cli/ssr/index.mjs +5 -15
  30. package/dist/esm/cli/template.server.mjs +1 -0
  31. package/dist/esm/core/react/wrapper.mjs +9 -3
  32. package/dist/esm/core/server/federatedCss.mjs +13 -0
  33. package/dist/esm/core/server/helmet.mjs +5 -2
  34. package/dist/esm/core/server/scriptOrder.mjs +25 -0
  35. package/dist/esm/core/server/stream/afterTemplate.mjs +14 -6
  36. package/dist/esm/core/server/stream/beforeTemplate.mjs +14 -11
  37. package/dist/esm/core/server/stream/beforeTemplate.worker.mjs +64 -0
  38. package/dist/esm/core/server/stream/createReadableStream.mjs +7 -2
  39. package/dist/esm/core/server/stream/createReadableStream.worker.mjs +4 -2
  40. package/dist/esm/core/server/stream/shared.mjs +3 -1
  41. package/dist/esm/core/server/string/index.mjs +17 -9
  42. package/dist/esm/core/server/string/loadable.mjs +70 -9
  43. package/dist/esm/exports/head.mjs +189 -4
  44. package/dist/esm/exports/loadable.mjs +20 -3
  45. package/dist/esm/exports/tanstack-router.mjs +2 -1
  46. package/dist/esm/router/cli/code/tanstackTypes.mjs +116 -51
  47. package/dist/esm/router/cli/code/templates.mjs +15 -9
  48. package/dist/esm/router/runtime/tanstack/hydrationBoundary.mjs +10 -0
  49. package/dist/esm/router/runtime/tanstack/outlet.mjs +17 -0
  50. package/dist/esm/router/runtime/tanstack/plugin.mjs +193 -91
  51. package/dist/esm/router/runtime/tanstack/plugin.node.mjs +5 -15
  52. package/dist/esm/router/runtime/tanstack/routeTree.mjs +65 -13
  53. package/dist/esm/rsc/server.worker.mjs +1 -0
  54. package/dist/esm/ssr/serverRender/renderToString/entry.mjs +9 -6
  55. package/dist/esm-node/boundary-debugger/index.mjs +264 -0
  56. package/dist/esm-node/cli/ssr/index.mjs +5 -15
  57. package/dist/esm-node/cli/template.server.mjs +1 -0
  58. package/dist/esm-node/core/react/wrapper.mjs +9 -3
  59. package/dist/esm-node/core/server/federatedCss.mjs +14 -0
  60. package/dist/esm-node/core/server/helmet.mjs +5 -2
  61. package/dist/esm-node/core/server/scriptOrder.mjs +26 -0
  62. package/dist/esm-node/core/server/stream/afterTemplate.mjs +14 -6
  63. package/dist/esm-node/core/server/stream/beforeTemplate.mjs +14 -11
  64. package/dist/esm-node/core/server/stream/beforeTemplate.worker.mjs +65 -0
  65. package/dist/esm-node/core/server/stream/createReadableStream.mjs +7 -2
  66. package/dist/esm-node/core/server/stream/createReadableStream.worker.mjs +4 -2
  67. package/dist/esm-node/core/server/stream/shared.mjs +3 -1
  68. package/dist/esm-node/core/server/string/index.mjs +17 -9
  69. package/dist/esm-node/core/server/string/loadable.mjs +70 -9
  70. package/dist/esm-node/exports/head.mjs +189 -4
  71. package/dist/esm-node/exports/loadable.mjs +20 -3
  72. package/dist/esm-node/exports/tanstack-router.mjs +2 -1
  73. package/dist/esm-node/router/cli/code/tanstackTypes.mjs +116 -51
  74. package/dist/esm-node/router/cli/code/templates.mjs +15 -9
  75. package/dist/esm-node/router/runtime/tanstack/hydrationBoundary.mjs +11 -0
  76. package/dist/esm-node/router/runtime/tanstack/outlet.mjs +18 -0
  77. package/dist/esm-node/router/runtime/tanstack/plugin.mjs +193 -91
  78. package/dist/esm-node/router/runtime/tanstack/plugin.node.mjs +5 -15
  79. package/dist/esm-node/router/runtime/tanstack/routeTree.mjs +65 -13
  80. package/dist/esm-node/rsc/server.worker.mjs +2 -0
  81. package/dist/esm-node/ssr/serverRender/renderToString/entry.mjs +9 -6
  82. package/dist/types/boundary-debugger/index.d.ts +28 -0
  83. package/dist/types/core/context/runtime.d.ts +4 -0
  84. package/dist/types/core/server/federatedCss.d.ts +5 -0
  85. package/dist/types/core/server/helmet.d.ts +5 -3
  86. package/dist/types/core/server/scriptOrder.d.ts +1 -0
  87. package/dist/types/core/server/stream/beforeTemplate.d.ts +1 -0
  88. package/dist/types/core/server/stream/beforeTemplate.worker.d.ts +10 -0
  89. package/dist/types/core/server/stream/shared.d.ts +8 -0
  90. package/dist/types/core/server/string/loadable.d.ts +11 -0
  91. package/dist/types/exports/head.d.ts +10 -3
  92. package/dist/types/exports/loadable.d.ts +8 -1
  93. package/dist/types/exports/tanstack-router.d.ts +3 -1
  94. package/dist/types/router/runtime/tanstack/hydrationBoundary.d.ts +2 -0
  95. package/dist/types/router/runtime/tanstack/outlet.d.ts +2 -0
  96. package/dist/types/rsc/server.worker.d.ts +1 -0
  97. package/package.json +24 -18
@@ -39,13 +39,11 @@ const node_namespaceObject = require("@modern-js/runtime-utils/node");
39
39
  const time_namespaceObject = require("@modern-js/runtime-utils/time");
40
40
  const external_react_namespaceObject = require("react");
41
41
  var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
42
- const external_react_helmet_namespaceObject = require("react-helmet");
43
- var external_react_helmet_default = /*#__PURE__*/ __webpack_require__.n(external_react_helmet_namespaceObject);
42
+ const external_react_helmet_async_namespaceObject = require("react-helmet-async");
43
+ const helmet_js_namespaceObject = require("../../../core/server/helmet.js");
44
44
  const utils_js_namespaceObject = require("../../../router/runtime/utils.js");
45
45
  const external_prefetch_namespaceObject = require("../../prefetch");
46
46
  var external_prefetch_default = /*#__PURE__*/ __webpack_require__.n(external_prefetch_namespaceObject);
47
- const external_helmet_namespaceObject = require("../helmet");
48
- var external_helmet_default = /*#__PURE__*/ __webpack_require__.n(external_helmet_namespaceObject);
49
47
  const external_tracker_namespaceObject = require("../tracker");
50
48
  const external_types_js_namespaceObject = require("../types.js");
51
49
  const external_utils_namespaceObject = require("../utils");
@@ -101,8 +99,8 @@ class Entry {
101
99
  (0, external_buildHtml_namespaceObject.createReplaceSSRDataScript)(ssrDataScripts),
102
100
  ...this.htmlModifiers
103
101
  ]);
104
- const helmetData = external_react_helmet_default().renderStatic();
105
- return helmetData ? external_helmet_default()(html, helmetData) : html;
102
+ const helmetData = (0, helmet_js_namespaceObject.getHelmetData)(context);
103
+ return helmetData ? (0, helmet_js_namespaceObject.helmetReplace)(html, helmetData) : html;
106
104
  }
107
105
  async prefetch(context) {
108
106
  let prefetchData;
@@ -123,11 +121,14 @@ class Entry {
123
121
  const end = (0, time_namespaceObject.time)();
124
122
  const { ssrContext } = context;
125
123
  try {
126
- const App = external_react_default().createElement(this.App, {
124
+ const helmetContext = context._helmetContext ??= {};
125
+ const App = external_react_default().createElement(external_react_helmet_async_namespaceObject.HelmetProvider, {
126
+ context: helmetContext
127
+ }, external_react_default().createElement(this.App, {
127
128
  context: Object.assign(context, {
128
129
  ssr: true
129
130
  })
130
- });
131
+ }));
131
132
  html = await (0, external_render_namespaceObject.createRender)(App).addCollector((0, external_styledComponent_namespaceObject.createStyledCollector)(this.result)).addCollector((0, external_loadable_namespaceObject.createLoadableCollector)({
132
133
  stats: ssrContext.loadableStats,
133
134
  result: this.result,
@@ -0,0 +1,263 @@
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: 10,
173
+ boxShadow: '0 10px 28px rgba(0, 0, 0, 0.14)',
174
+ color: '#111827',
175
+ display: 'flex',
176
+ font: '600 13px/1.2 system-ui, sans-serif',
177
+ gap: 8,
178
+ left: 'max(12px, env(safe-area-inset-left))',
179
+ maxWidth: 'calc(100vw - 24px)',
180
+ padding: '9px 11px',
181
+ position: 'fixed',
182
+ top: 'max(12px, env(safe-area-inset-top))',
183
+ zIndex: 2147483000
184
+ },
185
+ children: [
186
+ /*#__PURE__*/ jsx("input", {
187
+ checked: enabled,
188
+ onChange: (event)=>setEnabled(event.currentTarget.checked),
189
+ type: "checkbox"
190
+ }),
191
+ /*#__PURE__*/ jsx("span", {
192
+ children: toggleLabel
193
+ })
194
+ ]
195
+ }) : null,
196
+ enabled ? /*#__PURE__*/ jsx("div", {
197
+ "aria-hidden": "true",
198
+ children: boxes.map((box)=>/*#__PURE__*/ jsx("div", {
199
+ style: {
200
+ border: `2px solid ${box.color}`,
201
+ borderRadius: 8,
202
+ boxShadow: `0 0 0 1px rgba(255,255,255,.72), 0 6px 20px color-mix(in srgb, ${box.color} 20%, transparent)`,
203
+ height: box.height,
204
+ left: box.left,
205
+ pointerEvents: 'none',
206
+ position: 'fixed',
207
+ top: box.top,
208
+ width: box.width,
209
+ zIndex: 2147482999
210
+ },
211
+ children: /*#__PURE__*/ jsxs("span", {
212
+ style: {
213
+ background: box.color,
214
+ borderRadius: 999,
215
+ color: '#111827',
216
+ display: 'grid',
217
+ font: '800 11px/1.1 system-ui, sans-serif',
218
+ gap: 3,
219
+ maxWidth: 'min(280px, calc(100vw - 24px))',
220
+ padding: '5px 8px',
221
+ position: 'absolute',
222
+ right: 4,
223
+ top: 4,
224
+ whiteSpace: 'nowrap'
225
+ },
226
+ children: [
227
+ /*#__PURE__*/ jsx("span", {
228
+ children: box.label
229
+ }),
230
+ box.detail ? /*#__PURE__*/ jsx("span", {
231
+ style: {
232
+ font: '700 10px/1.1 system-ui, sans-serif',
233
+ opacity: 0.82,
234
+ overflow: 'hidden',
235
+ textOverflow: 'ellipsis'
236
+ },
237
+ children: box.detail
238
+ }) : null
239
+ ]
240
+ })
241
+ }, box.id))
242
+ }) : null
243
+ ]
244
+ });
245
+ }
246
+ const ultramodernBoundaryDebuggerPlugin = (options)=>({
247
+ name: '@modern-js/runtime/boundary-debugger',
248
+ setup: (api)=>{
249
+ api.wrapRoot((App)=>(props)=>/*#__PURE__*/ jsxs(Fragment, {
250
+ children: [
251
+ /*#__PURE__*/ jsx(App, {
252
+ ...props
253
+ }),
254
+ /*#__PURE__*/ jsx(BoundaryDebugger, {
255
+ ...options
256
+ })
257
+ ]
258
+ }));
259
+ }
260
+ });
261
+ const boundary_debugger = ultramodernBoundaryDebuggerPlugin;
262
+ export default boundary_debugger;
263
+ export { ultramodernBoundaryDebuggerPlugin };
@@ -84,30 +84,20 @@ 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 useModuleFederationNodeOutput = hasServerRendering && isModuleFederationAppSSR && isNodeEnvironmentTarget(config.output.target);
94
+ const isModuleFederationAppSSR = hasServerRendering && hasExplicitMfSsrFlag && !isCloudflareWorkerSSR;
95
95
  const ssrEnv = userConfig.deploy?.worker?.ssr || userConfig.server?.rsc ? 'edge' : 'node';
96
96
  const appContext = modernAPI.getAppContext();
97
97
  const { appDirectory, entrypoints } = appContext;
98
- const serverBundlerChain = useModuleFederationNodeOutput ? (chain)=>{
99
- chain.target('async-node');
100
- chain.output.module(false);
101
- chain.output.chunkFormat('commonjs');
102
- chain.output.chunkLoading('async-node');
103
- chain.output.library({
104
- ...chain.output.get('library') || {},
105
- type: 'commonjs-module'
106
- });
107
- } : void 0;
108
98
  const useLoadablePlugin = isUseSSRBundle(userConfig) && !isServerEnvironment && checkUseStringSSR(userConfig, appDirectory, entrypoints);
109
99
  const outputConfig = {
110
- module: isServerEnvironment && !useModuleFederationNodeOutput && outputModule
100
+ module: isServerEnvironment && (outputModule || 'workerSSR' === name && userConfig.deploy?.target === 'cloudflare')
111
101
  };
112
102
  const useLoadableComponents = isUseSSRBundle(userConfig) && checkUseStringSSR(userConfig, appDirectory, entrypoints);
113
103
  return mergeEnvironmentConfig(config, {
@@ -120,13 +110,13 @@ const ssrBuilderPlugin = (modernAPI, outputModule, exportLoadablePath)=>({
120
110
  },
121
111
  output: outputConfig,
122
112
  tools: {
123
- bundlerChain: serverBundlerChain || (useLoadablePlugin ? (chain)=>{
113
+ bundlerChain: useLoadablePlugin ? (chain)=>{
124
114
  chain.plugin('loadable').use(loadable_bundler_plugin, [
125
115
  {
126
116
  filename: LOADABLE_STATS_FILE
127
117
  }
128
118
  ]);
129
- } : void 0),
119
+ } : void 0,
130
120
  swc: useLoadableComponents ? {
131
121
  jsc: {
132
122
  experimental: {
@@ -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
  );
@@ -1,7 +1,10 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
+ import { HelmetProvider } from "react-helmet-async";
2
3
  import { InternalRuntimeContext, RuntimeContext } from "../context/index.mjs";
3
4
  function wrapRuntimeContextProvider(App, contextValue) {
4
- const { isBrowser, initialData, routes, routerFramework, context, routeManifest, routerRuntime, routerInstance, routerHydrationScript, routerMatchedRouteIds, routerServerSnapshot, routerContext, unstable_getBlockNavState, ssrContext, _internalContext, _internalRouterBaseName, ...rest } = contextValue;
5
+ const { isBrowser, initialData, routes, routerFramework, context, routeManifest, routerRuntime, routerInstance, routerHydrationScript, routerMatchedRouteIds, routerServerSnapshot, routerContext, unstable_getBlockNavState, ssrContext, _internalContext, _internalRouterBaseName, _helmetContext, ...rest } = contextValue;
6
+ const internalContextValue = contextValue;
7
+ internalContextValue._helmetContext ??= {};
5
8
  const runtimeContextValue = {
6
9
  isBrowser,
7
10
  initialData,
@@ -11,10 +14,13 @@ function wrapRuntimeContextProvider(App, contextValue) {
11
14
  ...rest
12
15
  };
13
16
  return /*#__PURE__*/ jsx(InternalRuntimeContext.Provider, {
14
- value: contextValue,
17
+ value: internalContextValue,
15
18
  children: /*#__PURE__*/ jsx(RuntimeContext.Provider, {
16
19
  value: runtimeContextValue,
17
- children: App
20
+ children: /*#__PURE__*/ jsx(HelmetProvider, {
21
+ context: internalContextValue._helmetContext,
22
+ children: App
23
+ })
18
24
  })
19
25
  });
20
26
  }
@@ -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,10 +1,13 @@
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>/;
6
6
  const RE_TITLE = /<title[^>]*>([\s\S\n\r]*?)<\/title>/;
7
7
  const TEST_TITLE_CONTENT = /(?<=<title[^>]*>)([\s\S\n\r]*?)([.|\S])([\s\S\n\r]*?)(?=<\/title>)/;
8
+ function getHelmetData(runtimeContext) {
9
+ return runtimeContext._helmetContext?.helmet ?? void 0;
10
+ }
8
11
  function createReplaceHelemt(helmetData) {
9
12
  return helmetData ? (template)=>helmetReplace(template, helmetData) : (tempalte)=>tempalte;
10
13
  }
@@ -35,4 +38,4 @@ function helmetReplace(content, helmetData) {
35
38
  ].reduce((pre, cur)=>pre + (cur.length > 0 ? ` ${cur}${EOL}` : ''), '');
36
39
  return safeReplace(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
37
40
  }
38
- export { createReplaceHelemt, helmetReplace };
41
+ export { createReplaceHelemt, getHelmetData, helmetReplace };
@@ -0,0 +1,25 @@
1
+ function getScriptTags(template) {
2
+ const scriptRegExp = /<script\b[^>]*\bsrc=(["'])(.*?)\1[^>]*><\/script>/g;
3
+ return Array.from(template.matchAll(scriptRegExp)).map((match)=>({
4
+ index: match.index ?? 0,
5
+ tag: match[0],
6
+ src: match[2]
7
+ }));
8
+ }
9
+ function getAssetBasename(src) {
10
+ const withoutQuery = src.split(/[?#]/)[0];
11
+ return withoutQuery.split('/').pop() || withoutQuery;
12
+ }
13
+ function isEntryScript(src, entryName, asyncEntry) {
14
+ const basename = getAssetBasename(src);
15
+ const prefix = asyncEntry ? `async-${entryName}` : entryName;
16
+ return basename === `${prefix}.js` || basename.startsWith(`${prefix}.`) || basename.startsWith(`${prefix}-`);
17
+ }
18
+ function injectBeforeHydrationEntryScript(template, scripts, entryName = 'index') {
19
+ if (!scripts) return template;
20
+ const scriptTags = getScriptTags(template);
21
+ const target = scriptTags.find((match)=>isEntryScript(match.src, entryName, false)) ?? scriptTags.find((match)=>isEntryScript(match.src, entryName, true));
22
+ if (!target) return template;
23
+ return `${template.slice(0, target.index)}${scripts}${template.slice(target.index)}`;
24
+ }
25
+ export { injectBeforeHydrationEntryScript };
@@ -1,7 +1,8 @@
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
+ import { injectBeforeHydrationEntryScript } from "../scriptOrder.mjs";
5
6
  import { buildHtml } from "../shared.mjs";
6
7
  import { attributesToString, safeReplace } from "../utils.mjs";
7
8
  function buildShellAfterTemplate(afterAppTemplate, options) {
@@ -22,11 +23,18 @@ function buildShellAfterTemplate(afterAppTemplate, options) {
22
23
  if (!routeManifest) return template;
23
24
  const { routeAssets } = routeManifest;
24
25
  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);
26
+ const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext) ?? [];
27
+ const assetEntries = [
28
+ ...matchedRouteIds.map((routeId)=>routeAssets[routeId]),
29
+ routeAssets[`async-${entryName}`]
30
+ ].filter(Boolean);
31
+ const jsAssets = Array.from(new Set(assetEntries.flatMap((entry)=>(entry.assets ?? []).filter((asset)=>asset.endsWith('.js')))));
32
+ const nonceAttr = nonce ? ` nonce="${nonce}"` : '';
33
+ const jsChunkStr = jsAssets.filter((asset)=>!template.includes(asset)).map((asset)=>`<script src=${asset}${nonceAttr}></script>`).join(' ');
34
+ if (jsChunkStr) {
35
+ const withoutPlaceholder = safeReplace(template, '<!--<?- chunksMap.js ?>-->', '');
36
+ const withEarlyScripts = injectBeforeHydrationEntryScript(withoutPlaceholder, jsChunkStr, entryName);
37
+ return withEarlyScripts !== withoutPlaceholder ? withEarlyScripts : safeReplace(template, '<!--<?- chunksMap.js ?>-->', jsChunkStr);
30
38
  }
31
39
  return template;
32
40
  }
@@ -1,8 +1,8 @@
1
1
  import { matchRoutes } from "@modern-js/runtime-utils/router";
2
- import react_helmet from "react-helmet";
3
2
  import { getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
4
3
  import { CHUNK_CSS_PLACEHOLDER } from "../constants.mjs";
5
- import { createReplaceHelemt } from "../helmet.mjs";
4
+ import { createFederatedCssLinks } from "../federatedCss.mjs";
5
+ import { createReplaceHelemt, getHelmetData } from "../helmet.mjs";
6
6
  import { buildHtml } from "../shared.mjs";
7
7
  import { checkIsNode, safeReplace } from "../utils.mjs";
8
8
  const readAsset = async (chunk)=>{
@@ -17,8 +17,8 @@ const checkIsInline = (chunk, enableInline)=>{
17
17
  return Boolean(enableInline);
18
18
  };
19
19
  async function buildShellBeforeTemplate(beforeAppTemplate, options) {
20
- const { config, runtimeContext, styledComponentsStyleTags, entryName } = options;
21
- const helmetData = react_helmet.renderStatic();
20
+ const { config, runtimeContext, styledComponentsStyleTags, entryName, moduleFederationCssAssets } = options;
21
+ const helmetData = getHelmetData(runtimeContext);
22
22
  const callbacks = [
23
23
  createReplaceHelemt(helmetData),
24
24
  (template)=>injectCss(template, entryName, styledComponentsStyleTags)
@@ -27,33 +27,36 @@ async function buildShellBeforeTemplate(beforeAppTemplate, options) {
27
27
  async function injectCss(template, entryName, styledComponentsStyleTags) {
28
28
  let css = await getCssChunks();
29
29
  if (styledComponentsStyleTags) css += styledComponentsStyleTags;
30
+ css += createFederatedCssLinks(moduleFederationCssAssets, {
31
+ template,
32
+ existingAssets: css.match(/href="([^"]+)"/g)?.map((item)=>item.replace(/^href="/, '').replace(/"$/, ''))
33
+ });
30
34
  return safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
31
35
  async function getCssChunks() {
32
36
  const { routeManifest, routerContext, routes } = runtimeContext;
33
37
  if (!routeManifest) return '';
34
38
  const { routeAssets } = routeManifest;
35
- let matchedRouteManifests;
39
+ let matchedRouteManifests = [];
36
40
  const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext);
37
41
  if (matchedRouteIds?.length) matchedRouteManifests = matchedRouteIds.map((routeId)=>routeAssets[routeId]).filter(Boolean);
38
- else {
39
- if (!routerContext || !routes) return '';
42
+ else if (routerContext && routes) {
40
43
  const matches = matchRoutes(routes, routerContext.location, routerContext.basename);
41
44
  matchedRouteManifests = matches?.map((match, index)=>{
42
45
  if (!index) return;
43
46
  const routeId = match.route.id;
44
47
  if (routeId) return routeAssets[routeId];
45
- }).filter(Boolean);
48
+ }).filter(Boolean) ?? [];
46
49
  }
47
50
  const asyncEntry = routeAssets[`async-${entryName}`];
48
- if (asyncEntry) matchedRouteManifests?.push(asyncEntry);
49
- const cssChunks = matchedRouteManifests ? matchedRouteManifests.reduce((chunks, routeManifest)=>{
51
+ if (asyncEntry) matchedRouteManifests.push(asyncEntry);
52
+ const cssChunks = matchedRouteManifests.reduce((chunks, routeManifest)=>{
50
53
  const { referenceCssAssets = [] } = routeManifest;
51
54
  const _cssChunks = referenceCssAssets.filter((asset)=>asset?.endsWith('.css') && !template.includes(asset));
52
55
  return [
53
56
  ...chunks,
54
57
  ..._cssChunks
55
58
  ];
56
- }, []) : [];
59
+ }, []);
57
60
  const { inlineStyles } = config;
58
61
  const styles = await Promise.all(cssChunks.map(async (chunk)=>{
59
62
  const link = `<link href="${chunk}" rel="stylesheet" />`;
@@ -0,0 +1,64 @@
1
+ import { matchRoutes } from "@modern-js/runtime-utils/router";
2
+ import { getRouterMatchedRouteIds } from "../../../router/runtime/lifecycle.mjs";
3
+ import { CHUNK_CSS_PLACEHOLDER } from "../constants.mjs";
4
+ import { createFederatedCssLinks } from "../federatedCss.mjs";
5
+ import { createReplaceHelemt, getHelmetData } from "../helmet.mjs";
6
+ import { buildHtml } from "../shared.mjs";
7
+ import { safeReplace } from "../utils.mjs";
8
+ const checkIsInline = (chunk, enableInline)=>{
9
+ if ('production' !== process.env.NODE_ENV) return false;
10
+ if (enableInline instanceof RegExp) return enableInline.test(chunk);
11
+ return Boolean(enableInline);
12
+ };
13
+ async function buildShellBeforeTemplate(beforeAppTemplate, options) {
14
+ const { config, runtimeContext, styledComponentsStyleTags, entryName, moduleFederationCssAssets } = options;
15
+ const helmetData = getHelmetData(runtimeContext);
16
+ const callbacks = [
17
+ createReplaceHelemt(helmetData),
18
+ (template)=>injectCss(template, entryName, styledComponentsStyleTags)
19
+ ];
20
+ return buildHtml(beforeAppTemplate, callbacks);
21
+ async function injectCss(template, entryName, styledComponentsStyleTags) {
22
+ let css = await getCssChunks();
23
+ if (styledComponentsStyleTags) css += styledComponentsStyleTags;
24
+ css += createFederatedCssLinks(moduleFederationCssAssets, {
25
+ template,
26
+ existingAssets: css.match(/href="([^"]+)"/g)?.map((item)=>item.replace(/^href="/, '').replace(/"$/, ''))
27
+ });
28
+ return safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
29
+ async function getCssChunks() {
30
+ const { routeManifest, routerContext, routes } = runtimeContext;
31
+ if (!routeManifest) return '';
32
+ const { routeAssets } = routeManifest;
33
+ let matchedRouteManifests = [];
34
+ const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext);
35
+ if (matchedRouteIds?.length) matchedRouteManifests = matchedRouteIds.map((routeId)=>routeAssets[routeId]).filter(Boolean);
36
+ else if (routerContext && routes) {
37
+ const matches = matchRoutes(routes, routerContext.location, routerContext.basename);
38
+ matchedRouteManifests = matches?.map((match, index)=>{
39
+ if (!index) return;
40
+ const routeId = match.route.id;
41
+ if (routeId) return routeAssets[routeId];
42
+ }).filter(Boolean) ?? [];
43
+ }
44
+ const asyncEntry = routeAssets[`async-${entryName}`];
45
+ if (asyncEntry) matchedRouteManifests.push(asyncEntry);
46
+ const cssChunks = matchedRouteManifests.reduce((chunks, routeManifest)=>{
47
+ const { referenceCssAssets = [] } = routeManifest;
48
+ const _cssChunks = referenceCssAssets.filter((asset)=>asset?.endsWith('.css') && !template.includes(asset));
49
+ return [
50
+ ...chunks,
51
+ ..._cssChunks
52
+ ];
53
+ }, []);
54
+ const { inlineStyles } = config;
55
+ const styles = cssChunks.map((chunk)=>{
56
+ const link = `<link href="${chunk}" rel="stylesheet" />`;
57
+ checkIsInline(chunk, inlineStyles);
58
+ return link;
59
+ });
60
+ return `${styles.join('')}`;
61
+ }
62
+ }
63
+ }
64
+ export { buildShellBeforeTemplate };