remote-components 0.3.2 → 0.3.4

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 (213) hide show
  1. package/dist/config/nextjs.cjs +122 -62
  2. package/dist/config/nextjs.cjs.map +1 -1
  3. package/dist/config/nextjs.js +116 -56
  4. package/dist/config/nextjs.js.map +1 -1
  5. package/dist/config/webpack.cjs +5 -1
  6. package/dist/config/webpack.cjs.map +1 -1
  7. package/dist/config/webpack.js +5 -1
  8. package/dist/config/webpack.js.map +1 -1
  9. package/dist/host/defaults/app.cjs +2 -5
  10. package/dist/host/defaults/app.cjs.map +1 -1
  11. package/dist/host/defaults/app.d.ts +1 -3
  12. package/dist/host/defaults/app.js +1 -4
  13. package/dist/host/defaults/app.js.map +1 -1
  14. package/dist/host/defaults/pages.cjs +2 -5
  15. package/dist/host/defaults/pages.cjs.map +1 -1
  16. package/dist/host/defaults/pages.d.ts +1 -3
  17. package/dist/host/defaults/pages.js +1 -4
  18. package/dist/host/defaults/pages.js.map +1 -1
  19. package/dist/host/defaults/shared.cjs +32 -0
  20. package/dist/host/defaults/shared.cjs.map +1 -0
  21. package/dist/host/defaults/shared.d.ts +3 -0
  22. package/dist/host/defaults/shared.js +8 -0
  23. package/dist/host/defaults/shared.js.map +1 -0
  24. package/dist/host/html.cjs +533 -351
  25. package/dist/host/html.cjs.map +1 -1
  26. package/dist/host/html.js +533 -351
  27. package/dist/host/html.js.map +1 -1
  28. package/dist/host/nextjs/app/client-only.cjs +494 -285
  29. package/dist/host/nextjs/app/client-only.cjs.map +1 -1
  30. package/dist/host/nextjs/app/client-only.js +468 -259
  31. package/dist/host/nextjs/app/client-only.js.map +1 -1
  32. package/dist/host/react.cjs +412 -241
  33. package/dist/host/react.cjs.map +1 -1
  34. package/dist/host/react.js +412 -241
  35. package/dist/host/react.js.map +1 -1
  36. package/dist/internal/config/webpack/apply-shared-modules.cjs +6 -4
  37. package/dist/internal/config/webpack/apply-shared-modules.cjs.map +1 -1
  38. package/dist/internal/config/webpack/apply-shared-modules.js +6 -4
  39. package/dist/internal/config/webpack/apply-shared-modules.js.map +1 -1
  40. package/dist/internal/config/webpack/next-client-pages-loader.cjs +6 -7
  41. package/dist/internal/config/webpack/next-client-pages-loader.cjs.map +1 -1
  42. package/dist/internal/config/webpack/next-client-pages-loader.js +6 -7
  43. package/dist/internal/config/webpack/next-client-pages-loader.js.map +1 -1
  44. package/dist/internal/host/nextjs/app-client.cjs +6 -22
  45. package/dist/internal/host/nextjs/app-client.cjs.map +1 -1
  46. package/dist/internal/host/nextjs/app-client.js +5 -11
  47. package/dist/internal/host/nextjs/app-client.js.map +1 -1
  48. package/dist/internal/host/nextjs/dom-flight.cjs +19 -39
  49. package/dist/internal/host/nextjs/dom-flight.cjs.map +1 -1
  50. package/dist/internal/host/nextjs/dom-flight.js +31 -39
  51. package/dist/internal/host/nextjs/dom-flight.js.map +1 -1
  52. package/dist/internal/host/nextjs/image-shared.cjs +39 -3
  53. package/dist/internal/host/nextjs/image-shared.cjs.map +1 -1
  54. package/dist/internal/host/nextjs/image-shared.d.ts +5 -10
  55. package/dist/internal/host/nextjs/image-shared.js +29 -3
  56. package/dist/internal/host/nextjs/image-shared.js.map +1 -1
  57. package/dist/internal/host/nextjs/shared-import.cjs +46 -0
  58. package/dist/internal/host/nextjs/shared-import.cjs.map +1 -0
  59. package/dist/internal/host/nextjs/shared-import.d.ts +3 -0
  60. package/dist/internal/host/nextjs/shared-import.js +12 -0
  61. package/dist/internal/host/nextjs/shared-import.js.map +1 -0
  62. package/dist/internal/host/server/fetch-remote-component.cjs +2 -1
  63. package/dist/internal/host/server/fetch-remote-component.cjs.map +1 -1
  64. package/dist/internal/host/server/fetch-remote-component.js +2 -1
  65. package/dist/internal/host/server/fetch-remote-component.js.map +1 -1
  66. package/dist/internal/host/shared/polyfill.cjs +8 -7
  67. package/dist/internal/host/shared/polyfill.cjs.map +1 -1
  68. package/dist/internal/host/shared/polyfill.js +8 -7
  69. package/dist/internal/host/shared/polyfill.js.map +1 -1
  70. package/dist/internal/host/shared/shared-module-resolver.cjs +117 -0
  71. package/dist/internal/host/shared/shared-module-resolver.cjs.map +1 -0
  72. package/dist/internal/host/shared/shared-module-resolver.d.ts +64 -0
  73. package/dist/internal/host/shared/shared-module-resolver.js +78 -0
  74. package/dist/internal/host/shared/shared-module-resolver.js.map +1 -0
  75. package/dist/internal/remote/nextjs/next-internals.cjs +53 -0
  76. package/dist/internal/remote/nextjs/next-internals.cjs.map +1 -0
  77. package/dist/internal/remote/nextjs/next-internals.d.ts +42 -0
  78. package/dist/internal/remote/nextjs/next-internals.js +26 -0
  79. package/dist/internal/remote/nextjs/next-internals.js.map +1 -0
  80. package/dist/internal/runtime/html/apply-origin.cjs +11 -32
  81. package/dist/internal/runtime/html/apply-origin.cjs.map +1 -1
  82. package/dist/internal/runtime/html/apply-origin.js +11 -32
  83. package/dist/internal/runtime/html/apply-origin.js.map +1 -1
  84. package/dist/internal/runtime/html/html-spec.cjs +78 -0
  85. package/dist/internal/runtime/html/html-spec.cjs.map +1 -0
  86. package/dist/internal/runtime/html/html-spec.d.ts +23 -0
  87. package/dist/internal/runtime/html/html-spec.js +41 -0
  88. package/dist/internal/runtime/html/html-spec.js.map +1 -0
  89. package/dist/internal/runtime/html/parse-remote-html.cjs +15 -12
  90. package/dist/internal/runtime/html/parse-remote-html.cjs.map +1 -1
  91. package/dist/internal/runtime/html/parse-remote-html.js +29 -12
  92. package/dist/internal/runtime/html/parse-remote-html.js.map +1 -1
  93. package/dist/internal/runtime/html/rewrite-srcset.cjs +38 -0
  94. package/dist/internal/runtime/html/rewrite-srcset.cjs.map +1 -0
  95. package/dist/internal/runtime/html/rewrite-srcset.d.ts +12 -0
  96. package/dist/internal/runtime/html/rewrite-srcset.js +14 -0
  97. package/dist/internal/runtime/html/rewrite-srcset.js.map +1 -0
  98. package/dist/internal/runtime/loaders/component-loader.cjs +25 -44
  99. package/dist/internal/runtime/loaders/component-loader.cjs.map +1 -1
  100. package/dist/internal/runtime/loaders/component-loader.d.ts +3 -1
  101. package/dist/internal/runtime/loaders/component-loader.js +28 -44
  102. package/dist/internal/runtime/loaders/component-loader.js.map +1 -1
  103. package/dist/internal/runtime/loaders/script-loader.cjs +7 -0
  104. package/dist/internal/runtime/loaders/script-loader.cjs.map +1 -1
  105. package/dist/internal/runtime/loaders/script-loader.js +7 -0
  106. package/dist/internal/runtime/loaders/script-loader.js.map +1 -1
  107. package/dist/internal/runtime/loaders/static-loader.cjs +15 -21
  108. package/dist/internal/runtime/loaders/static-loader.cjs.map +1 -1
  109. package/dist/internal/runtime/loaders/static-loader.d.ts +3 -1
  110. package/dist/internal/runtime/loaders/static-loader.js +15 -21
  111. package/dist/internal/runtime/loaders/static-loader.js.map +1 -1
  112. package/dist/internal/runtime/namespace.cjs +82 -0
  113. package/dist/internal/runtime/namespace.cjs.map +1 -0
  114. package/dist/internal/runtime/namespace.d.ts +5 -0
  115. package/dist/internal/runtime/namespace.js +58 -0
  116. package/dist/internal/runtime/namespace.js.map +1 -0
  117. package/dist/internal/runtime/rsc-imports.cjs +86 -0
  118. package/dist/internal/runtime/rsc-imports.cjs.map +1 -0
  119. package/dist/internal/runtime/rsc-imports.d.ts +39 -0
  120. package/dist/internal/runtime/rsc-imports.js +50 -0
  121. package/dist/internal/runtime/rsc-imports.js.map +1 -0
  122. package/dist/internal/{host/shared/resolved-data.cjs → runtime/rsc-runtime.cjs} +3 -3
  123. package/dist/internal/runtime/rsc-runtime.cjs.map +1 -0
  124. package/dist/internal/runtime/rsc-runtime.d.ts +37 -0
  125. package/dist/internal/runtime/rsc-runtime.js +1 -0
  126. package/dist/internal/runtime/rsc.cjs +23 -12
  127. package/dist/internal/runtime/rsc.cjs.map +1 -1
  128. package/dist/internal/runtime/rsc.d.ts +19 -2
  129. package/dist/internal/runtime/rsc.js +20 -11
  130. package/dist/internal/runtime/rsc.js.map +1 -1
  131. package/dist/internal/runtime/turbopack/chunk-loader.cjs +53 -35
  132. package/dist/internal/runtime/turbopack/chunk-loader.cjs.map +1 -1
  133. package/dist/internal/runtime/turbopack/chunk-loader.d.ts +1 -1
  134. package/dist/internal/runtime/turbopack/chunk-loader.js +53 -35
  135. package/dist/internal/runtime/turbopack/chunk-loader.js.map +1 -1
  136. package/dist/internal/runtime/turbopack/module.cjs +19 -17
  137. package/dist/internal/runtime/turbopack/module.cjs.map +1 -1
  138. package/dist/internal/runtime/turbopack/module.d.ts +23 -1
  139. package/dist/internal/runtime/turbopack/module.js +19 -18
  140. package/dist/internal/runtime/turbopack/module.js.map +1 -1
  141. package/dist/internal/runtime/turbopack/patterns.cjs +26 -10
  142. package/dist/internal/runtime/turbopack/patterns.cjs.map +1 -1
  143. package/dist/internal/runtime/turbopack/patterns.d.ts +44 -49
  144. package/dist/internal/runtime/turbopack/patterns.js +23 -7
  145. package/dist/internal/runtime/turbopack/patterns.js.map +1 -1
  146. package/dist/internal/runtime/turbopack/{webpack-runtime.cjs → remote-scope-setup.cjs} +35 -32
  147. package/dist/internal/runtime/turbopack/remote-scope-setup.cjs.map +1 -0
  148. package/dist/internal/runtime/turbopack/remote-scope-setup.d.ts +25 -0
  149. package/dist/internal/runtime/turbopack/{webpack-runtime.js → remote-scope-setup.js} +36 -19
  150. package/dist/internal/runtime/turbopack/remote-scope-setup.js.map +1 -0
  151. package/dist/internal/runtime/turbopack/remote-scope.cjs +4 -6
  152. package/dist/internal/runtime/turbopack/remote-scope.cjs.map +1 -1
  153. package/dist/internal/runtime/turbopack/remote-scope.d.ts +20 -1
  154. package/dist/internal/runtime/turbopack/remote-scope.js +4 -6
  155. package/dist/internal/runtime/turbopack/remote-scope.js.map +1 -1
  156. package/dist/internal/runtime/turbopack/shared-modules.cjs +23 -15
  157. package/dist/internal/runtime/turbopack/shared-modules.cjs.map +1 -1
  158. package/dist/internal/runtime/turbopack/shared-modules.d.ts +13 -1
  159. package/dist/internal/runtime/turbopack/shared-modules.js +25 -18
  160. package/dist/internal/runtime/turbopack/shared-modules.js.map +1 -1
  161. package/dist/internal/runtime/types.cjs.map +1 -1
  162. package/dist/internal/runtime/types.d.ts +5 -41
  163. package/dist/internal/utils/logger.cjs +1 -1
  164. package/dist/internal/utils/logger.cjs.map +1 -1
  165. package/dist/internal/utils/logger.d.ts +2 -2
  166. package/dist/internal/utils/logger.js +1 -1
  167. package/dist/internal/utils/logger.js.map +1 -1
  168. package/dist/internal/utils/project-id-env.cjs +31 -0
  169. package/dist/internal/utils/project-id-env.cjs.map +1 -0
  170. package/dist/internal/utils/project-id-env.d.ts +7 -0
  171. package/dist/internal/utils/project-id-env.js +7 -0
  172. package/dist/internal/utils/project-id-env.js.map +1 -0
  173. package/dist/internal/utils/project-id.cjs +46 -0
  174. package/dist/internal/utils/project-id.cjs.map +1 -0
  175. package/dist/internal/utils/project-id.d.ts +8 -0
  176. package/dist/internal/utils/project-id.js +22 -0
  177. package/dist/internal/utils/project-id.js.map +1 -0
  178. package/dist/remote/defaults/app.cjs +2 -5
  179. package/dist/remote/defaults/app.cjs.map +1 -1
  180. package/dist/remote/defaults/app.d.ts +1 -3
  181. package/dist/remote/defaults/app.js +1 -4
  182. package/dist/remote/defaults/app.js.map +1 -1
  183. package/dist/remote/defaults/pages.cjs +2 -5
  184. package/dist/remote/defaults/pages.cjs.map +1 -1
  185. package/dist/remote/defaults/pages.d.ts +1 -3
  186. package/dist/remote/defaults/pages.js +1 -4
  187. package/dist/remote/defaults/pages.js.map +1 -1
  188. package/dist/remote/defaults/shared.cjs +32 -0
  189. package/dist/remote/defaults/shared.cjs.map +1 -0
  190. package/dist/remote/defaults/shared.d.ts +3 -0
  191. package/dist/remote/defaults/shared.js +8 -0
  192. package/dist/remote/defaults/shared.js.map +1 -0
  193. package/dist/remote/html.cjs +78 -22
  194. package/dist/remote/html.cjs.map +1 -1
  195. package/dist/remote/html.js +78 -22
  196. package/dist/remote/html.js.map +1 -1
  197. package/dist/remote/nextjs/app.cjs +16 -56
  198. package/dist/remote/nextjs/app.cjs.map +1 -1
  199. package/dist/remote/nextjs/app.js +26 -46
  200. package/dist/remote/nextjs/app.js.map +1 -1
  201. package/dist/remote/nextjs/pages.cjs +2 -1
  202. package/dist/remote/nextjs/pages.cjs.map +1 -1
  203. package/dist/remote/nextjs/pages.js +2 -1
  204. package/dist/remote/nextjs/pages.js.map +1 -1
  205. package/dist/types-59251814.d.ts +94 -0
  206. package/package.json +1 -1
  207. package/dist/internal/host/shared/resolved-data.cjs.map +0 -1
  208. package/dist/internal/host/shared/resolved-data.d.ts +0 -48
  209. package/dist/internal/host/shared/resolved-data.js +0 -1
  210. package/dist/internal/runtime/turbopack/webpack-runtime.cjs.map +0 -1
  211. package/dist/internal/runtime/turbopack/webpack-runtime.d.ts +0 -23
  212. package/dist/internal/runtime/turbopack/webpack-runtime.js.map +0 -1
  213. /package/dist/internal/{host/shared/resolved-data.js.map → runtime/rsc-runtime.js.map} +0 -0
package/dist/host/html.js CHANGED
@@ -183,7 +183,7 @@ var init_logger = __esm({
183
183
  init_constants();
184
184
  init_error();
185
185
  PREFIX = "remote-components";
186
- DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true";
186
+ DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true" || typeof process !== "undefined" && process.env.RC_DEBUG === "true";
187
187
  }
188
188
  });
189
189
 
@@ -218,6 +218,152 @@ var init_constants2 = __esm({
218
218
  }
219
219
  });
220
220
 
221
+ // src/runtime/namespace.ts
222
+ function getNamespace() {
223
+ const g = globalThis;
224
+ const existing = g.__remote_components__;
225
+ if (existing) {
226
+ return existing;
227
+ }
228
+ const ns = {
229
+ scopes: /* @__PURE__ */ new Map(),
230
+ chunkCache: {},
231
+ mountFns: {},
232
+ unmountFns: {},
233
+ bundleUrls: {},
234
+ moduleRegistry: {},
235
+ dispatcherRuntime: void 0,
236
+ hostSharedModules: {},
237
+ cssCache: {},
238
+ shadowRoots: {}
239
+ };
240
+ const nsRecord = ns;
241
+ for (const { global, prop } of LEGACY_ALIASES) {
242
+ const legacyValue = g[global];
243
+ if (legacyValue != null) {
244
+ nsRecord[prop] = legacyValue;
245
+ }
246
+ g[global] = ns[prop];
247
+ }
248
+ const gRecord = g;
249
+ for (const key of Object.keys(gRecord)) {
250
+ if (key.startsWith(SHADOW_ROOT_PREFIX)) {
251
+ const suffix = key.slice(SHADOW_ROOT_PREFIX.length);
252
+ ns.shadowRoots[suffix] = gRecord[key];
253
+ delete gRecord[key];
254
+ }
255
+ }
256
+ g.__remote_components__ = ns;
257
+ return ns;
258
+ }
259
+ var SHADOW_ROOT_PREFIX, LEGACY_ALIASES;
260
+ var init_namespace = __esm({
261
+ "src/runtime/namespace.ts"() {
262
+ "use strict";
263
+ SHADOW_ROOT_PREFIX = "__remote_components_shadowroot_";
264
+ LEGACY_ALIASES = [
265
+ { global: "__remote_component_scopes__", prop: "scopes" },
266
+ {
267
+ global: "__remote_components_turbopack_chunk_loader_promise__",
268
+ prop: "chunkCache"
269
+ },
270
+ { global: "__remote_script_entrypoint_mount__", prop: "mountFns" },
271
+ { global: "__remote_script_entrypoint_unmount__", prop: "unmountFns" },
272
+ { global: "__remote_bundle_url__", prop: "bundleUrls" },
273
+ { global: "__rc_module_registry__", prop: "moduleRegistry" },
274
+ {
275
+ global: "__remote_component_host_shared_modules__",
276
+ prop: "hostSharedModules"
277
+ },
278
+ { global: "__remote_next_css__", prop: "cssCache" }
279
+ ];
280
+ }
281
+ });
282
+
283
+ // src/runtime/patterns.ts
284
+ function collapseDoubleSlashes(path) {
285
+ return path.replace(DOUBLE_SLASH_RE, "/");
286
+ }
287
+ var REMOTE_COMPONENT_REGEX, NEXT_BUNDLE_PATH_RE, DOUBLE_SLASH_RE;
288
+ var init_patterns = __esm({
289
+ "src/runtime/patterns.ts"() {
290
+ "use strict";
291
+ REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
292
+ NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
293
+ DOUBLE_SLASH_RE = /(?<!:)\/\//g;
294
+ }
295
+ });
296
+
297
+ // src/runtime/turbopack/remote-scope.ts
298
+ function getRegistry() {
299
+ return getNamespace().scopes;
300
+ }
301
+ function createScope(name, url, runtime, resolveClientUrl) {
302
+ const isCrossOrigin = url.origin !== location.origin;
303
+ const scopedName = computeScopedName(name, {
304
+ remoteHost: url.host,
305
+ isCrossOrigin
306
+ });
307
+ const globalKey = getBundleKey(scopedName);
308
+ return {
309
+ name,
310
+ scopedName,
311
+ globalKey,
312
+ url,
313
+ runtime,
314
+ resolveClientUrl,
315
+ moduleCache: {},
316
+ sharedModules: {},
317
+ moduleGlobal: {},
318
+ turbopackModules: []
319
+ };
320
+ }
321
+ function registerScope(scope) {
322
+ const registry = getRegistry();
323
+ registry.set(scope.scopedName, scope);
324
+ if (scope.scopedName !== scope.name) {
325
+ const existing = registry.get(scope.name);
326
+ if (existing && existing.scopedName !== scope.scopedName) {
327
+ logWarn(
328
+ "RemoteScope",
329
+ `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
330
+ );
331
+ }
332
+ registry.set(scope.name, scope);
333
+ }
334
+ logDebug(
335
+ "RemoteScope",
336
+ `Registered scope "${scope.scopedName}" (${registry.size} total)`
337
+ );
338
+ }
339
+ function getScope(name) {
340
+ return getRegistry().get(name);
341
+ }
342
+ function formatRemoteId(scope, path) {
343
+ return `[${scope.scopedName}] ${path}`;
344
+ }
345
+ function parseRemoteId(id) {
346
+ const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
347
+ if (groups?.bundle && groups.id) {
348
+ return {
349
+ bundle: groups.bundle,
350
+ path: groups.id,
351
+ prefix: groups.prefix ?? ""
352
+ };
353
+ }
354
+ return { bundle: "default", path: id, prefix: "" };
355
+ }
356
+ var init_remote_scope = __esm({
357
+ "src/runtime/turbopack/remote-scope.ts"() {
358
+ "use strict";
359
+ init_constants2();
360
+ init_namespace();
361
+ init_patterns();
362
+ init_utils();
363
+ init_logger();
364
+ }
365
+ });
366
+
221
367
  // src/config/webpack/apply-shared-modules.ts
222
368
  function applySharedModules(bundle, resolve) {
223
369
  logDebug(
@@ -229,13 +375,15 @@ function applySharedModules(bundle, resolve) {
229
375
  `Shared modules to resolve: ${Object.keys(resolve)}`
230
376
  );
231
377
  const self = globalThis;
232
- if (self.__remote_webpack_require__?.[bundle]) {
378
+ const scope = getScope(bundle);
379
+ const webpackBundle = scope?.webpackRequire ?? self.__remote_webpack_require__?.[bundle];
380
+ if (webpackBundle) {
233
381
  const modulePaths = Object.keys(
234
- self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
382
+ self.__remote_webpack_module_map__?.[bundle] ?? webpackBundle.m ?? {}
235
383
  );
236
384
  logDebug(
237
385
  "SharedModules",
238
- `Available module paths in __remote_webpack_require__[${bundle}]: ${modulePaths}`
386
+ `Available module paths for bundle "${bundle}": ${modulePaths}`
239
387
  );
240
388
  for (const [key, value] of Object.entries(resolve)) {
241
389
  const exactIds = modulePaths.filter((p) => p === key);
@@ -247,7 +395,6 @@ function applySharedModules(bundle, resolve) {
247
395
  );
248
396
  }
249
397
  for (const id of ids) {
250
- const webpackBundle = self.__remote_webpack_require__[bundle];
251
398
  if (webpackBundle.m) {
252
399
  const resolvedId = self.__remote_webpack_module_map__?.[bundle]?.[id] ? `${self.__remote_webpack_module_map__[bundle][id]}` : id;
253
400
  if (resolvedId !== id) {
@@ -282,6 +429,7 @@ var DEDUPLICATION_SKIPPED;
282
429
  var init_apply_shared_modules = __esm({
283
430
  "src/config/webpack/apply-shared-modules.ts"() {
284
431
  "use strict";
432
+ init_remote_scope();
285
433
  init_logger();
286
434
  DEDUPLICATION_SKIPPED = "shared module deduplication skipped. The remote may load its own copy of shared dependencies.";
287
435
  }
@@ -353,10 +501,8 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
353
501
  ];
354
502
  const { default: Component } = componentLoader();
355
503
  const { default: App } = appLoader();
356
- if (!self.__remote_next_css__) {
357
- self.__remote_next_css__ = {};
358
- }
359
- if (!self.__remote_next_css__[bundle]) {
504
+ const cssCache = getNamespace().cssCache;
505
+ if (!cssCache[bundle]) {
360
506
  const cssRE = /\.s?css$/;
361
507
  Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
362
508
  self.__remote_webpack_require__?.[bundle]?.(id);
@@ -374,15 +520,15 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
374
520
  node.remove();
375
521
  node = nextCss.previousSibling;
376
522
  }
377
- self.__remote_next_css__[bundle] = elements;
523
+ cssCache[bundle] = elements;
378
524
  }
379
525
  if (styleContainer) {
380
- const elements = self.__remote_next_css__[bundle];
526
+ const elements = cssCache[bundle];
381
527
  elements.forEach((el) => {
382
528
  styleContainer.appendChild(el.cloneNode(true));
383
529
  });
384
530
  } else {
385
- const elements = self.__remote_next_css__[bundle];
531
+ const elements = cssCache[bundle];
386
532
  elements.forEach((el) => {
387
533
  document.head.appendChild(el);
388
534
  });
@@ -401,96 +547,11 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
401
547
  var init_next_client_pages_loader = __esm({
402
548
  "src/config/webpack/next-client-pages-loader.ts"() {
403
549
  "use strict";
550
+ init_namespace();
404
551
  init_error();
405
552
  }
406
553
  });
407
554
 
408
- // src/runtime/patterns.ts
409
- function collapseDoubleSlashes(path) {
410
- return path.replace(DOUBLE_SLASH_RE, "/");
411
- }
412
- var REMOTE_COMPONENT_REGEX, NEXT_BUNDLE_PATH_RE, DOUBLE_SLASH_RE;
413
- var init_patterns = __esm({
414
- "src/runtime/patterns.ts"() {
415
- "use strict";
416
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
417
- NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
418
- DOUBLE_SLASH_RE = /(?<!:)\/\//g;
419
- }
420
- });
421
-
422
- // src/runtime/turbopack/remote-scope.ts
423
- function getRegistry() {
424
- const self = globalThis;
425
- if (!self.__remote_component_scopes__) {
426
- self.__remote_component_scopes__ = /* @__PURE__ */ new Map();
427
- }
428
- return self.__remote_component_scopes__;
429
- }
430
- function createScope(name, url, runtime, resolveClientUrl) {
431
- const isCrossOrigin = url.origin !== location.origin;
432
- const scopedName = computeScopedName(name, {
433
- remoteHost: url.host,
434
- isCrossOrigin
435
- });
436
- const globalKey = getBundleKey(scopedName);
437
- return {
438
- name,
439
- scopedName,
440
- globalKey,
441
- url,
442
- runtime,
443
- resolveClientUrl,
444
- moduleCache: {},
445
- sharedModules: {},
446
- moduleGlobal: {}
447
- };
448
- }
449
- function registerScope(scope) {
450
- const registry = getRegistry();
451
- registry.set(scope.scopedName, scope);
452
- if (scope.scopedName !== scope.name) {
453
- const existing = registry.get(scope.name);
454
- if (existing && existing.scopedName !== scope.scopedName) {
455
- logWarn(
456
- "RemoteScope",
457
- `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
458
- );
459
- }
460
- registry.set(scope.name, scope);
461
- }
462
- logDebug(
463
- "RemoteScope",
464
- `Registered scope "${scope.scopedName}" (${registry.size} total)`
465
- );
466
- }
467
- function getScope(name) {
468
- return getRegistry().get(name);
469
- }
470
- function formatRemoteId(scope, path) {
471
- return `[${scope.scopedName}] ${path}`;
472
- }
473
- function parseRemoteId(id) {
474
- const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
475
- if (groups?.bundle && groups.id) {
476
- return {
477
- bundle: groups.bundle,
478
- path: groups.id,
479
- prefix: groups.prefix ?? ""
480
- };
481
- }
482
- return { bundle: "default", path: id, prefix: "" };
483
- }
484
- var init_remote_scope = __esm({
485
- "src/runtime/turbopack/remote-scope.ts"() {
486
- "use strict";
487
- init_constants2();
488
- init_patterns();
489
- init_utils();
490
- init_logger();
491
- }
492
- });
493
-
494
555
  // src/host/shared/remote-image-loader.ts
495
556
  function createRemoteImageLoader(bundle, resolveClientUrl) {
496
557
  const loader = Object.assign(
@@ -523,9 +584,9 @@ var init_remote_image_loader = __esm({
523
584
  // src/host/shared/polyfill.tsx
524
585
  import { jsx } from "react/jsx-runtime";
525
586
  function sharedPolyfills(shared, resolveClientUrl) {
526
- const self = globalThis;
587
+ const hostShared = getNamespace().hostSharedModules;
527
588
  const polyfill = {
528
- "next/dist/client/components/navigation": self.__remote_component_host_shared_modules__?.["next/navigation"] ?? shared?.["next/navigation"] ?? (() => Promise.resolve({
589
+ "next/dist/client/components/navigation": hostShared["next/navigation"] ?? shared?.["next/navigation"] ?? (() => Promise.resolve({
529
590
  useRouter() {
530
591
  return {
531
592
  push: (routerUrl) => {
@@ -556,7 +617,7 @@ function sharedPolyfills(shared, resolveClientUrl) {
556
617
  },
557
618
  __esModule: true
558
619
  })),
559
- "next/dist/client/app-dir/link": self.__remote_component_host_shared_modules__?.["next/link"] ?? shared?.["next/link"] ?? (() => Promise.resolve({
620
+ "next/dist/client/app-dir/link": hostShared["next/link"] ?? shared?.["next/link"] ?? (() => Promise.resolve({
560
621
  default: ({
561
622
  scroll: _,
562
623
  replace,
@@ -606,7 +667,7 @@ function sharedPolyfills(shared, resolveClientUrl) {
606
667
  },
607
668
  __esModule: true
608
669
  })),
609
- "next/dist/client/app-dir/form": self.__remote_component_host_shared_modules__?.["next/form"] ?? shared?.["next/form"] ?? (() => Promise.resolve({
670
+ "next/dist/client/app-dir/form": hostShared["next/form"] ?? shared?.["next/form"] ?? (() => Promise.resolve({
610
671
  default: () => {
611
672
  throw new Error("Next.js <Form> component not implemented");
612
673
  },
@@ -617,17 +678,17 @@ function sharedPolyfills(shared, resolveClientUrl) {
617
678
  // This gives us full next/image fidelity (fill, priority, srcSet, blur
618
679
  // placeholders, error handling) while routing image optimization through the
619
680
  // remote app's /_next/image endpoint.
620
- "next/dist/shared/lib/image-loader": self.__remote_component_host_shared_modules__?.["next/dist/shared/lib/image-loader"] ?? shared?.["next/dist/shared/lib/image-loader"] ?? ((bundle) => Promise.resolve({
681
+ "next/dist/shared/lib/image-loader": hostShared["next/dist/shared/lib/image-loader"] ?? shared?.["next/dist/shared/lib/image-loader"] ?? ((bundle) => Promise.resolve({
621
682
  default: createRemoteImageLoader(bundle, resolveClientUrl),
622
683
  __esModule: true
623
684
  })),
624
- "next/dist/client/script": self.__remote_component_host_shared_modules__?.["next/script"] ?? shared?.["next/script"] ?? (() => Promise.resolve({
685
+ "next/dist/client/script": hostShared["next/script"] ?? shared?.["next/script"] ?? (() => Promise.resolve({
625
686
  // TODO: implement <Script> component for non-Next.js host applications
626
687
  // do not throw an error for now
627
688
  default: () => null,
628
689
  __esModule: true
629
690
  })),
630
- "next/router": self.__remote_component_host_shared_modules__?.["next/router"] ?? shared?.["next/router"] ?? (() => (
691
+ "next/router": hostShared["next/router"] ?? shared?.["next/router"] ?? (() => (
631
692
  // TODO: incomplete implementation
632
693
  Promise.resolve({
633
694
  useRouter() {
@@ -666,7 +727,78 @@ var init_polyfill = __esm({
666
727
  "src/host/shared/polyfill.tsx"() {
667
728
  "use strict";
668
729
  init_remote_image_loader();
730
+ init_namespace();
731
+ init_logger();
732
+ }
733
+ });
734
+
735
+ // src/host/shared/shared-module-resolver.ts
736
+ function buildCoreShared(userShared) {
737
+ return {
738
+ react: async () => (await import("react")).default,
739
+ "react-dom": async () => (await import("react-dom")).default,
740
+ "react/jsx-dev-runtime": async () => (await import("react/jsx-dev-runtime")).default,
741
+ "react/jsx-runtime": async () => (await import("react/jsx-runtime")).default,
742
+ "react-dom/client": async () => (await import("react-dom/client")).default,
743
+ ...userShared
744
+ };
745
+ }
746
+ function buildHostShared(userShared, resolveClientUrl, options) {
747
+ const self = globalThis;
748
+ const result = {
749
+ ...sharedPolyfills(userShared, resolveClientUrl),
750
+ ...self.__remote_component_host_shared_modules__,
751
+ ...userShared
752
+ };
753
+ if (options?.includeRemoteComponentShared) {
754
+ Object.assign(result, self.__remote_component_shared__);
755
+ }
756
+ return result;
757
+ }
758
+ async function buildWebpackResolve(hostShared, remoteShared, bundle, reactModules, callerTag = "SharedModuleResolver") {
759
+ const resolve = {
760
+ ...reactModules,
761
+ ...Object.entries(remoteShared).reduce(
762
+ (acc, [key, value]) => {
763
+ if (typeof hostShared[value] !== "undefined") {
764
+ acc[key.replace(/^\(ssr\)\/(?<relative>\.\/)?/, "")] = hostShared[value];
765
+ } else {
766
+ logDebug(
767
+ callerTag,
768
+ `Remote requests "${value}" but host doesn't provide it`
769
+ );
770
+ }
771
+ return acc;
772
+ },
773
+ {}
774
+ )
775
+ };
776
+ await Promise.all(
777
+ Object.entries(resolve).map(async ([key, value]) => {
778
+ if (typeof value === "function") {
779
+ resolve[key] = await value(bundle);
780
+ }
781
+ return Promise.resolve(value);
782
+ })
783
+ );
784
+ return resolve;
785
+ }
786
+ var CORE_REACT_PATH_MAP, VENDOR_SHARED;
787
+ var init_shared_module_resolver = __esm({
788
+ "src/host/shared/shared-module-resolver.ts"() {
789
+ "use strict";
790
+ init_polyfill();
669
791
  init_logger();
792
+ CORE_REACT_PATH_MAP = {
793
+ react: "/react/index.js",
794
+ "react/jsx-dev-runtime": "/react/jsx-dev-runtime.js",
795
+ "react/jsx-runtime": "/react/jsx-runtime.js",
796
+ "react-dom": "/react-dom/index.js",
797
+ "react-dom/client": "/react-dom/client.js"
798
+ };
799
+ VENDOR_SHARED = Object.fromEntries(
800
+ Object.entries(CORE_REACT_PATH_MAP).filter(([key]) => key !== "react-dom/client").map(([key, path]) => [key, `'${path}'`])
801
+ );
670
802
  }
671
803
  });
672
804
 
@@ -681,6 +813,13 @@ async function loadScripts(scripts, resolveClientUrl) {
681
813
  location.origin
682
814
  ).href;
683
815
  const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
816
+ const alreadyLoaded = Array.from(
817
+ document.querySelectorAll("script[src]")
818
+ ).some((s) => s.src === resolvedSrc);
819
+ if (alreadyLoaded) {
820
+ resolve();
821
+ return;
822
+ }
684
823
  const newScript = document.createElement("script");
685
824
  newScript.onload = () => resolve();
686
825
  newScript.onerror = () => {
@@ -762,34 +901,19 @@ async function webpackRuntime(bundle, shared, remoteShared, resolveClientUrl) {
762
901
  ];
763
902
  });
764
903
  await loadScripts(scriptSrcs, resolveClientUrl);
765
- const hostShared = {
766
- ...sharedPolyfills(shared, resolveClientUrl),
767
- ...self.__remote_component_host_shared_modules__,
768
- ...shared
769
- };
770
- const resolve = {
771
- "/react/index.js": (await import("react")).default,
772
- "/react/jsx-dev-runtime.js": (await import("react/jsx-dev-runtime")).default,
773
- "/react/jsx-runtime.js": (await import("react/jsx-runtime")).default,
774
- "/react-dom/index.js": (await import("react-dom")).default,
775
- "/react-dom/client.js": (await import("react-dom/client")).default,
776
- ...Object.entries(remoteShared ?? {}).reduce(
777
- (acc, [key, value]) => {
778
- if (typeof hostShared[value] !== "undefined") {
779
- acc[key.replace(/^\(ssr\)\/(?<relative>\.\/)?/, "")] = hostShared[value];
780
- }
781
- return acc;
782
- },
783
- {}
784
- )
785
- };
786
- await Promise.all(
787
- Object.entries(resolve).map(async ([key, value]) => {
788
- if (typeof value === "function") {
789
- resolve[key] = await value(remoteBundle);
790
- }
791
- return Promise.resolve(value);
792
- })
904
+ const hostShared = buildHostShared(shared, resolveClientUrl);
905
+ const resolve = await buildWebpackResolve(
906
+ hostShared,
907
+ remoteShared ?? {},
908
+ remoteBundle,
909
+ {
910
+ "/react/index.js": (await import("react")).default,
911
+ "/react/jsx-dev-runtime.js": (await import("react/jsx-dev-runtime")).default,
912
+ "/react/jsx-runtime.js": (await import("react/jsx-runtime")).default,
913
+ "/react-dom/index.js": (await import("react-dom")).default,
914
+ "/react-dom/client.js": (await import("react-dom/client")).default
915
+ },
916
+ "WebpackRuntime"
793
917
  );
794
918
  applySharedModules(remoteBundle, resolve);
795
919
  }
@@ -806,7 +930,7 @@ var init_webpack = __esm({
806
930
  "use strict";
807
931
  init_apply_shared_modules();
808
932
  init_next_client_pages_loader();
809
- init_polyfill();
933
+ init_shared_module_resolver();
810
934
  init_script_loader();
811
935
  init_patterns();
812
936
  init_error();
@@ -814,15 +938,31 @@ var init_webpack = __esm({
814
938
  });
815
939
 
816
940
  // src/runtime/turbopack/patterns.ts
817
- var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
941
+ function stripQuotes(value) {
942
+ if (value.startsWith('"') && value.endsWith('"')) {
943
+ return value.slice(1, -1);
944
+ }
945
+ return value;
946
+ }
947
+ function extractGroup(re, input, group) {
948
+ const raw = re.exec(input)?.groups?.[group];
949
+ return raw ? stripQuotes(raw) : void 0;
950
+ }
951
+ var MODULE_ID_PATTERN, REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_CALLBACK_RE, TURBOPACK_GLOBAL_RE;
818
952
  var init_patterns2 = __esm({
819
953
  "src/runtime/turbopack/patterns.ts"() {
820
954
  "use strict";
955
+ MODULE_ID_PATTERN = '"[^"]+"|[0-9]+';
821
956
  REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
822
- REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|[a-z])\.A\((?<sharedModuleId>[0-9]+)\)/;
823
- ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
824
- ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
825
- ASYNC_MODULE_ALL_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<vCb>parentImport|t)=>Promise\.all\(\["[^"]+"\]\.map\((?<mapCb>chunk|t)=>\k<ctx>\.l\(\k<mapCb>\)\)\)\.then\(\(\)=>\k<vCb>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
957
+ REMOTE_SHARED_ASSIGNMENT_RE = new RegExp(
958
+ `\\.TURBOPACK_REMOTE_SHARED\\s*=\\s*await (?:__turbopack_context__|[a-z])\\.A\\((?<sharedModuleId>${MODULE_ID_PATTERN})\\)`
959
+ );
960
+ ASYNC_MODULE_LOADER_RE = new RegExp(
961
+ `(?:__turbopack_context__|e)\\.A\\((?<asyncSharedModuleId>${MODULE_ID_PATTERN})\\)`
962
+ );
963
+ ASYNC_MODULE_CALLBACK_RE = new RegExp(
964
+ `(?:parentImport|[a-z])\\((?<sharedModuleId>${MODULE_ID_PATTERN})\\)`
965
+ );
826
966
  TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
827
967
  }
828
968
  });
@@ -834,6 +974,7 @@ function loadChunkWithScope(scope, chunkId) {
834
974
  `loadChunkWithScope: "${chunkId}" (scope: "${scope.scopedName}")`
835
975
  );
836
976
  const self = globalThis;
977
+ const ns = getNamespace();
837
978
  const { bundle, path, prefix } = parseRemoteId(chunkId);
838
979
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : scope.runtime;
839
980
  if (remoteRuntime === RUNTIME_WEBPACK) {
@@ -844,35 +985,31 @@ function loadChunkWithScope(scope, chunkId) {
844
985
  if (url.endsWith(".css")) {
845
986
  return;
846
987
  }
847
- if (!self.__remote_components_turbopack_chunk_loader_promise__) {
848
- self.__remote_components_turbopack_chunk_loader_promise__ = {};
849
- }
850
- if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
851
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
988
+ if (ns.chunkCache[url]) {
989
+ logDebug("ChunkLoader", `Cache hit for "${chunkId}" (url="${url}")`);
990
+ return ns.chunkCache[url];
852
991
  }
853
992
  const resolvedUrl = scope.resolveClientUrl?.(url) ?? url;
854
993
  if (resolvedUrl !== url) {
855
994
  logDebug("ChunkLoader", `Proxied chunk URL: "${url}" \u2192 "${resolvedUrl}"`);
856
995
  }
857
- self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise(
858
- (resolve, reject) => {
859
- fetch(resolvedUrl).then((res) => res.text()).then((code) => {
860
- const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
861
- if (hasTurbopack) {
862
- return handleTurbopackChunk(code, scope, url);
863
- }
864
- }).then(resolve).catch((error) => {
865
- const isProxied = isProxiedUrl(resolvedUrl);
866
- if (isProxied) {
867
- reject(failedProxiedAssetError("chunk", url, resolvedUrl));
868
- } else {
869
- warnCrossOriginFetchError("ChunkLoader", url);
870
- reject(error);
871
- }
872
- });
873
- }
874
- );
875
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
996
+ ns.chunkCache[url] = new Promise((resolve, reject) => {
997
+ fetch(resolvedUrl).then((res) => res.text()).then((code) => {
998
+ const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
999
+ if (hasTurbopack) {
1000
+ return handleTurbopackChunk(code, scope, url);
1001
+ }
1002
+ }).then(resolve).catch((error) => {
1003
+ const isProxied = isProxiedUrl(resolvedUrl);
1004
+ if (isProxied) {
1005
+ reject(failedProxiedAssetError("chunk", url, resolvedUrl));
1006
+ } else {
1007
+ warnCrossOriginFetchError("ChunkLoader", url);
1008
+ reject(error);
1009
+ }
1010
+ });
1011
+ });
1012
+ return ns.chunkCache[url];
876
1013
  }
877
1014
  function createChunkDispatcher() {
878
1015
  return function __chunk_dispatcher__(chunkId, scriptBundle) {
@@ -921,19 +1058,39 @@ async function handleTurbopackChunk(code, scope, url) {
921
1058
  `//# sourceMappingURL=${new URL(".", new URL(url, scope.url)).href}$1$2.js.map`
922
1059
  );
923
1060
  if (!self[`TURBOPACK_${globalKey}`]) {
924
- const chunkData = [];
925
- const turbopackObject = {
926
- push: (item) => {
927
- if (Array.isArray(item)) {
928
- chunkData.push(item);
929
- } else {
930
- chunkData.push([item]);
1061
+ const wrapPush = (target) => {
1062
+ const originalPush = target.push;
1063
+ if (typeof originalPush !== "function")
1064
+ return target;
1065
+ target.push = (...items) => {
1066
+ for (const item of items) {
1067
+ if (Array.isArray(item)) {
1068
+ for (const entry of item) {
1069
+ scope.turbopackModules.push(entry);
1070
+ }
1071
+ } else {
1072
+ scope.turbopackModules.push(item);
1073
+ }
931
1074
  }
932
- return chunkData.length;
933
- },
934
- __chunks__: chunkData
1075
+ return originalPush.apply(target, items);
1076
+ };
1077
+ return target;
935
1078
  };
936
- self[`TURBOPACK_${globalKey}`] = turbopackObject;
1079
+ const globalProp = `TURBOPACK_${globalKey}`;
1080
+ let currentValue = wrapPush([]);
1081
+ Object.defineProperty(self, globalProp, {
1082
+ get() {
1083
+ return currentValue;
1084
+ },
1085
+ set(newValue) {
1086
+ if (newValue && typeof newValue === "object") {
1087
+ wrapPush(newValue);
1088
+ }
1089
+ currentValue = newValue;
1090
+ },
1091
+ configurable: true,
1092
+ enumerable: true
1093
+ });
937
1094
  }
938
1095
  await new Promise((scriptResolve, scriptReject) => {
939
1096
  const blob = new Blob([transformedCode], {
@@ -985,6 +1142,7 @@ var init_chunk_loader = __esm({
985
1142
  "src/runtime/turbopack/chunk-loader.ts"() {
986
1143
  "use strict";
987
1144
  init_constants2();
1145
+ init_namespace();
988
1146
  init_patterns();
989
1147
  init_protected_rc_fallback();
990
1148
  init_error();
@@ -996,13 +1154,13 @@ var init_chunk_loader = __esm({
996
1154
 
997
1155
  // src/runtime/turbopack/shared-modules.ts
998
1156
  function getTurbopackModules(scope) {
1157
+ if (scope.turbopackModules.length > 0) {
1158
+ return scope.turbopackModules;
1159
+ }
999
1160
  const self = globalThis;
1000
1161
  const raw = self[`TURBOPACK_${scope.globalKey}`];
1001
1162
  if (!raw)
1002
1163
  return void 0;
1003
- if (typeof raw === "object" && "__chunks__" in raw) {
1004
- return raw.__chunks__.flat();
1005
- }
1006
1164
  if (Array.isArray(raw)) {
1007
1165
  return raw.flat();
1008
1166
  }
@@ -1026,7 +1184,11 @@ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}
1026
1184
  if (sharedModuleInitializerIndex > 0) {
1027
1185
  const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1028
1186
  const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1029
- const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1187
+ const sharedModuleId = extractGroup(
1188
+ REMOTE_SHARED_ASSIGNMENT_RE,
1189
+ sharedModuleInitializerCode,
1190
+ "sharedModuleId"
1191
+ );
1030
1192
  if (sharedModuleId) {
1031
1193
  const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1032
1194
  scope,
@@ -1084,20 +1246,22 @@ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}
1084
1246
  }
1085
1247
  function extractSharedModuleIds(shared, scope) {
1086
1248
  return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1087
- const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1249
+ const asyncSharedModuleId = extractGroup(
1250
+ ASYNC_MODULE_LOADER_RE,
1251
+ value.toString(),
1252
+ "asyncSharedModuleId"
1253
+ );
1088
1254
  if (asyncSharedModuleId) {
1089
- const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1090
- let asyncSharedModule;
1091
- const turbopackModules = getTurbopackModules(scope) ?? [];
1092
- const asyncSharedModuleIdIndex = turbopackModules.indexOf(
1093
- asyncSharedModuleIdNumber
1255
+ const asyncSharedModule = findModuleInit(
1256
+ getTurbopackModules(scope),
1257
+ asyncSharedModuleId
1094
1258
  );
1095
- if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
1096
- asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
1097
- }
1098
1259
  if (asyncSharedModule) {
1099
- const asyncSharedModuleCode = asyncSharedModule.toString();
1100
- const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1260
+ const sharedModuleId = extractGroup(
1261
+ ASYNC_MODULE_CALLBACK_RE,
1262
+ asyncSharedModule.toString(),
1263
+ "sharedModuleId"
1264
+ );
1101
1265
  acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1102
1266
  "__remote_shared_module_",
1103
1267
  ""
@@ -1137,10 +1301,8 @@ function requireModule(scope, moduleId, fullId) {
1137
1301
  if (scope.moduleCache[idStr])
1138
1302
  return scope.moduleCache[idStr];
1139
1303
  const sharedModule = getSharedModule(scope, moduleId);
1140
- if (sharedModule) {
1141
- scope.moduleCache[idStr] = sharedModule;
1304
+ if (sharedModule)
1142
1305
  return sharedModule;
1143
- }
1144
1306
  return handleTurbopackModule(
1145
1307
  scope,
1146
1308
  idStr,
@@ -1148,19 +1310,10 @@ function requireModule(scope, moduleId, fullId) {
1148
1310
  );
1149
1311
  }
1150
1312
  function handleTurbopackModule(scope, moduleId, id) {
1151
- const self = globalThis;
1152
1313
  if (scope.moduleCache[moduleId]) {
1153
1314
  return scope.moduleCache[moduleId];
1154
1315
  }
1155
- const raw = self[`TURBOPACK_${scope.globalKey}`];
1156
- let modules;
1157
- if (raw && typeof raw === "object" && "__chunks__" in raw) {
1158
- modules = raw.__chunks__.flat();
1159
- } else if (Array.isArray(raw)) {
1160
- modules = raw.flat();
1161
- } else {
1162
- modules = raw;
1163
- }
1316
+ const modules = getTurbopackModules(scope);
1164
1317
  if (!modules) {
1165
1318
  logError(
1166
1319
  "TurbopackModule",
@@ -1210,9 +1363,17 @@ function findModuleInit(modules, moduleId) {
1210
1363
  if (idx >= 0) {
1211
1364
  return flat.slice(idx + 1).find((e) => typeof e === "function");
1212
1365
  }
1213
- return flat.find(
1214
- (e) => Boolean(e && typeof e === "object" && moduleId in e)
1215
- )?.[moduleId];
1366
+ for (const entry of flat) {
1367
+ if (!entry || typeof entry !== "object")
1368
+ continue;
1369
+ const obj = entry;
1370
+ if (moduleId in obj)
1371
+ return obj[moduleId];
1372
+ const prefixKey = Object.keys(obj).find((k) => k.startsWith(moduleId));
1373
+ if (prefixKey)
1374
+ return obj[prefixKey];
1375
+ }
1376
+ return void 0;
1216
1377
  }
1217
1378
  function createTurbopackContext(scope, exports, moduleExports, modules, moduleInit, id) {
1218
1379
  const scopedRequire = (moduleId) => requireModule(scope, moduleId, formatRemoteId(scope, String(moduleId)));
@@ -1291,7 +1452,10 @@ function createTurbopackContext(scope, exports, moduleExports, modules, moduleIn
1291
1452
  }
1292
1453
  if (typeof mod !== "object" || mod === null) {
1293
1454
  mod = { default: mod };
1294
- } else if (!("default" in mod) && mod.toString() !== "[object Module]") {
1455
+ } else if (!("default" in mod) && // ES module namespace objects have a null prototype, so calling
1456
+ // mod.toString() directly throws. Use Object.prototype.toString
1457
+ // to safely detect them.
1458
+ Object.prototype.toString.call(mod) !== "[object Module]") {
1295
1459
  try {
1296
1460
  mod.default = mod;
1297
1461
  } catch {
@@ -1364,21 +1528,38 @@ var init_module = __esm({
1364
1528
  }
1365
1529
  });
1366
1530
 
1367
- // src/runtime/turbopack/webpack-runtime.ts
1368
- async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
1531
+ // src/runtime/turbopack/remote-scope-setup.ts
1532
+ async function setupRemoteScope(runtime, scripts = [], url = new URL(location.href), bundle, resolveClientUrl) {
1369
1533
  const self = globalThis;
1534
+ const ns = getNamespace();
1370
1535
  const bundleName = bundle ?? "default";
1536
+ const existingScope = getScope(bundleName);
1537
+ if (existingScope && existingScope.url.origin === url.origin) {
1538
+ logDebug(
1539
+ "WebpackRuntime",
1540
+ `Reusing scope "${existingScope.scopedName}" (turbopackModules=${existingScope.turbopackModules.length})`
1541
+ );
1542
+ existingScope.resolveClientUrl = resolveClientUrl;
1543
+ if (runtime === RUNTIME_TURBOPACK) {
1544
+ await Promise.allSettled(
1545
+ scripts.map(
1546
+ (script) => script.src ? loadChunkWithScope(existingScope, script.src) : Promise.resolve(void 0)
1547
+ )
1548
+ );
1549
+ }
1550
+ return existingScope;
1551
+ }
1371
1552
  const scope = createScope(bundleName, url, runtime, resolveClientUrl);
1372
1553
  registerScope(scope);
1373
- if (!self.__remote_bundle_url__) {
1374
- self.__remote_bundle_url__ = {};
1554
+ if (runtime === RUNTIME_WEBPACK && self.__remote_webpack_require__?.[bundleName]) {
1555
+ scope.webpackRequire = self.__remote_webpack_require__[bundleName];
1375
1556
  }
1376
- self.__remote_bundle_url__[bundleName] = url;
1557
+ ns.bundleUrls[bundleName] = url;
1377
1558
  if (scope.scopedName !== bundleName) {
1378
- self.__remote_bundle_url__[scope.scopedName] = url;
1559
+ ns.bundleUrls[scope.scopedName] = url;
1379
1560
  }
1380
1561
  self.__webpack_get_script_filename__ = () => null;
1381
- const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack";
1562
+ const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || ns.dispatcherRuntime !== "turbopack";
1382
1563
  if (willCreateDispatchers) {
1383
1564
  if (!self.__original_webpack_require__ && !self.__original_webpack_chunk_load__) {
1384
1565
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
@@ -1386,6 +1567,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1386
1567
  }
1387
1568
  self.__webpack_chunk_load__ = createChunkDispatcher();
1388
1569
  self.__webpack_require__ = createModuleDispatcher(runtime);
1570
+ ns.dispatcherRuntime = runtime;
1389
1571
  self.__webpack_require_type__ = runtime;
1390
1572
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
1391
1573
  self.__remote_webpack_require__[bundleName] = self.__webpack_require__;
@@ -1413,15 +1595,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1413
1595
  }
1414
1596
  }
1415
1597
  }
1416
- const coreShared = {
1417
- react: async () => (await import("react")).default,
1418
- "react-dom": async () => (await import("react-dom")).default,
1419
- "react/jsx-dev-runtime": async () => (await import("react/jsx-dev-runtime")).default,
1420
- "react/jsx-runtime": async () => (await import("react/jsx-runtime")).default,
1421
- "react-dom/client": async () => (await import("react-dom/client")).default,
1422
- ...shared
1423
- };
1424
- await initializeSharedModules(scope, coreShared, remoteShared);
1598
+ return scope;
1425
1599
  }
1426
1600
  function createModuleDispatcher(runtime) {
1427
1601
  return (id) => {
@@ -1438,6 +1612,9 @@ function createModuleDispatcher(runtime) {
1438
1612
  );
1439
1613
  try {
1440
1614
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
1615
+ const scope2 = getScope(bundle);
1616
+ if (scope2?.webpackRequire)
1617
+ return scope2.webpackRequire(moduleId);
1441
1618
  return self.__remote_webpack_require__?.[bundle]?.(moduleId);
1442
1619
  }
1443
1620
  const scope = getScope(bundleName);
@@ -1475,17 +1652,17 @@ function createModuleDispatcher(runtime) {
1475
1652
  }
1476
1653
  };
1477
1654
  }
1478
- var init_webpack_runtime = __esm({
1479
- "src/runtime/turbopack/webpack-runtime.ts"() {
1655
+ var init_remote_scope_setup = __esm({
1656
+ "src/runtime/turbopack/remote-scope-setup.ts"() {
1480
1657
  "use strict";
1481
1658
  init_constants2();
1659
+ init_namespace();
1482
1660
  init_patterns();
1483
1661
  init_error();
1484
1662
  init_logger();
1485
1663
  init_chunk_loader();
1486
1664
  init_module();
1487
1665
  init_remote_scope();
1488
- init_shared_modules();
1489
1666
  }
1490
1667
  });
1491
1668
 
@@ -1496,36 +1673,28 @@ __export(turbopack_exports, {
1496
1673
  });
1497
1674
  async function turbopackRuntime(url, bundle, shared, remoteShared, resolveClientUrl) {
1498
1675
  const self = globalThis;
1499
- const hostShared = {
1500
- ...sharedPolyfills(shared, resolveClientUrl),
1501
- ...self.__remote_component_host_shared_modules__,
1502
- ...shared,
1503
- ...self.__remote_component_shared__
1504
- };
1505
- await setupWebpackRuntime(
1506
- "turbopack",
1507
- [],
1508
- url,
1509
- bundle,
1510
- hostShared,
1511
- remoteShared,
1512
- resolveClientUrl
1513
- );
1676
+ const hostShared = buildHostShared(shared, resolveClientUrl, {
1677
+ includeRemoteComponentShared: true
1678
+ });
1679
+ await setupRemoteScope("turbopack", [], url, bundle, resolveClientUrl);
1514
1680
  const {
1515
1681
  default: { createFromReadableStream }
1516
1682
  } = await import("react-server-dom-webpack/client.browser");
1517
- function preloadScripts(scripts, __) {
1518
- return setupWebpackRuntime(
1683
+ async function preloadScripts(scripts, __) {
1684
+ const scope = await setupRemoteScope(
1519
1685
  "turbopack",
1520
1686
  scripts.map((script) => ({
1521
1687
  src: script.getAttribute("src") || script.getAttribute("data-src") || script.src
1522
1688
  })),
1523
1689
  url,
1524
1690
  bundle,
1525
- hostShared,
1526
- remoteShared,
1527
1691
  resolveClientUrl
1528
1692
  );
1693
+ await initializeSharedModules(
1694
+ scope,
1695
+ buildCoreShared(hostShared),
1696
+ remoteShared ?? {}
1697
+ );
1529
1698
  }
1530
1699
  return {
1531
1700
  self,
@@ -1540,8 +1709,9 @@ var init_turbopack = __esm({
1540
1709
  "use strict";
1541
1710
  init_apply_shared_modules();
1542
1711
  init_next_client_pages_loader();
1543
- init_polyfill();
1544
- init_webpack_runtime();
1712
+ init_shared_module_resolver();
1713
+ init_remote_scope_setup();
1714
+ init_shared_modules();
1545
1715
  }
1546
1716
  });
1547
1717
 
@@ -1588,10 +1758,9 @@ async function importViaCallback(absoluteSrc, resolveClientUrl) {
1588
1758
  URL.revokeObjectURL(moduleBlobUrl);
1589
1759
  URL.revokeObjectURL(wrapperBlobUrl);
1590
1760
  }
1591
- const registry = globalThis.__rc_module_registry__;
1592
- const mod = registry?.[absoluteSrc] ?? {};
1593
- if (registry)
1594
- delete registry[absoluteSrc];
1761
+ const registry = getNamespace().moduleRegistry;
1762
+ const mod = registry[absoluteSrc] ?? {};
1763
+ delete registry[absoluteSrc];
1595
1764
  return mod;
1596
1765
  }
1597
1766
  async function importDirectly(absoluteSrc) {
@@ -1621,12 +1790,12 @@ function resolveScriptSrc(script, url) {
1621
1790
  return rawSrc;
1622
1791
  }
1623
1792
  async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1624
- const self = globalThis;
1625
- if (self.__remote_script_entrypoint_mount__?.[url.href]) {
1626
- self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
1793
+ const ns = getNamespace();
1794
+ if (ns.mountFns[url.href]) {
1795
+ ns.mountFns[url.href] = /* @__PURE__ */ new Set();
1627
1796
  }
1628
- if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
1629
- self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
1797
+ if (ns.unmountFns[url.href]) {
1798
+ ns.unmountFns[url.href] = /* @__PURE__ */ new Set();
1630
1799
  }
1631
1800
  const mountUnmountSets = await Promise.all(
1632
1801
  scripts.map(async (script) => {
@@ -1638,25 +1807,19 @@ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1638
1807
  URL.revokeObjectURL(src);
1639
1808
  }
1640
1809
  if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
1641
- if (!self.__remote_script_entrypoint_mount__) {
1642
- self.__remote_script_entrypoint_mount__ = {};
1643
- }
1644
- if (!self.__remote_script_entrypoint_mount__[url.href]) {
1645
- self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
1810
+ if (!ns.mountFns[url.href]) {
1811
+ ns.mountFns[url.href] = /* @__PURE__ */ new Set();
1646
1812
  }
1647
- self.__remote_script_entrypoint_mount__[url.href]?.add(
1813
+ ns.mountFns[url.href]?.add(
1648
1814
  mod.mount || mod.default?.mount || (() => {
1649
1815
  })
1650
1816
  );
1651
1817
  }
1652
1818
  if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
1653
- if (!self.__remote_script_entrypoint_unmount__) {
1654
- self.__remote_script_entrypoint_unmount__ = {};
1819
+ if (!ns.unmountFns[url.href]) {
1820
+ ns.unmountFns[url.href] = /* @__PURE__ */ new Set();
1655
1821
  }
1656
- if (!self.__remote_script_entrypoint_unmount__[url.href]) {
1657
- self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
1658
- }
1659
- self.__remote_script_entrypoint_unmount__[url.href]?.add(
1822
+ ns.unmountFns[url.href]?.add(
1660
1823
  mod.unmount || mod.default?.unmount || (() => {
1661
1824
  })
1662
1825
  );
@@ -1697,6 +1860,7 @@ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1697
1860
  var init_static_loader = __esm({
1698
1861
  "src/runtime/loaders/static-loader.ts"() {
1699
1862
  "use strict";
1863
+ init_namespace();
1700
1864
  init_logger();
1701
1865
  }
1702
1866
  });
@@ -1817,8 +1981,8 @@ function resolveNameFromSrc(src, defaultName) {
1817
1981
  // src/host/html/index.tsx
1818
1982
  init_constants2();
1819
1983
 
1820
- // src/runtime/html/apply-origin.ts
1821
- var tagNames = [
1984
+ // src/runtime/html/html-spec.ts
1985
+ var ORIGIN_REWRITE_TAGS = [
1822
1986
  "img",
1823
1987
  "source",
1824
1988
  "video",
@@ -1829,10 +1993,37 @@ var tagNames = [
1829
1993
  "script",
1830
1994
  "link"
1831
1995
  ];
1996
+ var ID_SUFFIX_RSC = "_rsc";
1997
+ var ID_SUFFIX_SSR = "_ssr";
1998
+ var ID_SUFFIX_SHARED = "_shared";
1999
+ var DATA_BUNDLE = "data-bundle";
2000
+ var DATA_ROUTE = "data-route";
2001
+ var DATA_RUNTIME = "data-runtime";
2002
+ var DATA_TYPE = "data-type";
2003
+ var DATA_SRC = "data-src";
2004
+ var DATA_REMOTE_COMPONENTS_SHARED = "data-remote-components-shared";
2005
+ var TAG_REMOTE_COMPONENT = "remote-component";
2006
+ var NEXT_DATA_ID = "__NEXT_DATA__";
2007
+ var REMOTE_NEXT_DATA_ID = "__REMOTE_NEXT_DATA__";
2008
+ var NEXT_CONTAINER_ID = "__next";
2009
+
2010
+ // src/runtime/html/rewrite-srcset.ts
2011
+ function rewriteSrcset(srcset, base, resolve) {
2012
+ return srcset.split(",").map((entry) => {
2013
+ const [url, descriptor] = entry.trim().split(/\s+/);
2014
+ if (!url)
2015
+ return entry;
2016
+ const absoluteUrl = new URL(url, base).href;
2017
+ const resolvedUrl = resolve ? resolve(absoluteUrl) : absoluteUrl;
2018
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
2019
+ }).join(", ");
2020
+ }
2021
+
2022
+ // src/runtime/html/apply-origin.ts
1832
2023
  function applyOriginToNodes(doc, url, resolveClientUrl) {
1833
2024
  if (url.origin !== location.origin) {
1834
2025
  const nodes = doc.querySelectorAll(
1835
- tagNames.map(
2026
+ ORIGIN_REWRITE_TAGS.map(
1836
2027
  (type) => `${type}[src],${type}[srcset],${type}[href],${type}[imagesrcset]`
1837
2028
  ).join(",")
1838
2029
  );
@@ -1850,29 +2041,17 @@ function applyOriginToNodes(doc, url, resolveClientUrl) {
1850
2041
  );
1851
2042
  }
1852
2043
  if (node.hasAttribute("srcset")) {
1853
- const srcSet = node.getAttribute("srcset")?.split(",").map((entry) => {
1854
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1855
- if (!urlPart)
1856
- return entry;
1857
- const absoluteUrl = new URL(urlPart, url).href;
1858
- const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1859
- return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1860
- }).join(", ");
1861
- if (srcSet) {
1862
- node.setAttribute("srcset", srcSet);
2044
+ const raw = node.getAttribute("srcset");
2045
+ if (raw) {
2046
+ const resolve = resolveClientUrl ? (abs) => resolveClientUrl(abs) ?? abs : void 0;
2047
+ node.setAttribute("srcset", rewriteSrcset(raw, url, resolve));
1863
2048
  }
1864
2049
  }
1865
2050
  if (node.hasAttribute("imagesrcset")) {
1866
- const srcSet = node.getAttribute("imagesrcset")?.split(",").map((entry) => {
1867
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1868
- if (!urlPart)
1869
- return entry;
1870
- const absoluteUrl = new URL(urlPart, url).href;
1871
- const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1872
- return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1873
- }).join(", ");
1874
- if (srcSet) {
1875
- node.setAttribute("imagesrcset", srcSet);
2051
+ const raw = node.getAttribute("imagesrcset");
2052
+ if (raw) {
2053
+ const resolve = resolveClientUrl ? (abs) => resolveClientUrl(abs) ?? abs : void 0;
2054
+ node.setAttribute("imagesrcset", rewriteSrcset(raw, url, resolve));
1876
2055
  }
1877
2056
  }
1878
2057
  });
@@ -1918,26 +2097,28 @@ function buildMetadata(attrs, url) {
1918
2097
  // src/runtime/html/parse-remote-html.ts
1919
2098
  init_error();
1920
2099
  function validateSingleComponent(doc, name, url) {
1921
- if (doc.querySelectorAll("div[data-bundle][data-route]").length > 1 && !doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) || doc.querySelectorAll("remote-component:not([src])").length > 1 && !doc.querySelector(`remote-component[name="${name}"]`)) {
2100
+ if (doc.querySelectorAll(`div[${DATA_BUNDLE}][${DATA_ROUTE}]`).length > 1 && !doc.querySelector(
2101
+ `div[${DATA_BUNDLE}][${DATA_ROUTE}][id^="${name}"]`
2102
+ ) || doc.querySelectorAll(`${TAG_REMOTE_COMPONENT}:not([src])`).length > 1 && !doc.querySelector(`${TAG_REMOTE_COMPONENT}[name="${name}"]`)) {
1922
2103
  throw multipleRemoteComponentsError(url);
1923
2104
  }
1924
2105
  }
1925
2106
  function findComponentElement(doc, name) {
1926
- return doc.querySelector(`div[data-bundle][data-route][id^="${name}"]`) ?? doc.querySelector("div[data-bundle][data-route]") ?? doc.querySelector("div#__next") ?? doc.querySelector(`remote-component[name="${name}"]:not([src])`) ?? doc.querySelector("remote-component:not([src])");
2107
+ return doc.querySelector(`div[${DATA_BUNDLE}][${DATA_ROUTE}][id^="${name}"]`) ?? doc.querySelector(`div[${DATA_BUNDLE}][${DATA_ROUTE}]`) ?? doc.querySelector(`div#${NEXT_CONTAINER_ID}`) ?? doc.querySelector(`${TAG_REMOTE_COMPONENT}[name="${name}"]:not([src])`) ?? doc.querySelector(`${TAG_REMOTE_COMPONENT}:not([src])`);
1927
2108
  }
1928
2109
  function parseNextData(doc) {
1929
2110
  return JSON.parse(
1930
- (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
2111
+ (doc.querySelector(`#${NEXT_DATA_ID}`) ?? doc.querySelector(`#${REMOTE_NEXT_DATA_ID}`))?.textContent ?? "null"
1931
2112
  );
1932
2113
  }
1933
2114
  function resolveComponentName(component, nextData, fallbackName) {
1934
- const isRemoteComponent = component?.tagName.toLowerCase() === "remote-component";
1935
- const name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
2115
+ const isRemoteComponent = component?.tagName.toLowerCase() === TAG_REMOTE_COMPONENT;
2116
+ const name = component?.getAttribute("id")?.replace(new RegExp(`${ID_SUFFIX_SSR}$`), "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
1936
2117
  return { name, isRemoteComponent };
1937
2118
  }
1938
2119
  function extractRemoteShared(doc, name, nextData) {
1939
2120
  const remoteSharedEl = doc.querySelector(
1940
- `#${name}_shared[data-remote-components-shared]`
2121
+ `#${name}${ID_SUFFIX_SHARED}[${DATA_REMOTE_COMPONENTS_SHARED}]`
1941
2122
  );
1942
2123
  const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
1943
2124
  remoteSharedEl?.remove();
@@ -1958,7 +2139,7 @@ function extractLinks(doc, component) {
1958
2139
  function extractScripts(doc, component, isRemoteComponent) {
1959
2140
  return Array.from(
1960
2141
  (isRemoteComponent ? component : doc).querySelectorAll(
1961
- "script[src],script[data-src]"
2142
+ `script[src],script[${DATA_SRC}]`
1962
2143
  )
1963
2144
  );
1964
2145
  }
@@ -1971,15 +2152,15 @@ function parseRemoteComponentDocument(doc, name, url) {
1971
2152
  nextData,
1972
2153
  name
1973
2154
  );
1974
- const rsc = doc.querySelector(`#${resolvedName}_rsc`);
2155
+ const rsc = doc.querySelector(`#${resolvedName}${ID_SUFFIX_RSC}`);
1975
2156
  const metadata = buildMetadata(
1976
2157
  {
1977
2158
  name: resolvedName,
1978
- bundle: component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle,
1979
- route: component?.getAttribute("data-route") ?? nextData?.page,
1980
- runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? RUNTIME_SCRIPT,
2159
+ bundle: component?.getAttribute(DATA_BUNDLE) || nextData?.props.__REMOTE_COMPONENT__?.bundle,
2160
+ route: component?.getAttribute(DATA_ROUTE) ?? nextData?.page,
2161
+ runtime: component?.getAttribute(DATA_RUNTIME) ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? RUNTIME_SCRIPT,
1981
2162
  id: component?.getAttribute("id"),
1982
- type: component?.getAttribute("data-type")
2163
+ type: component?.getAttribute(DATA_TYPE)
1983
2164
  },
1984
2165
  url
1985
2166
  );
@@ -2007,6 +2188,9 @@ function parseRemoteComponentDocument(doc, name, url) {
2007
2188
  };
2008
2189
  }
2009
2190
 
2191
+ // src/host/html/index.tsx
2192
+ init_namespace();
2193
+
2010
2194
  // src/runtime/rsc.ts
2011
2195
  import { ReadableStream } from "web-streams-polyfill";
2012
2196
  function fixPayload(payload) {
@@ -2027,6 +2211,18 @@ function fixPayload(payload) {
2027
2211
  }
2028
2212
  }
2029
2213
  }
2214
+ function buildRSCChunks(rscName, data) {
2215
+ const chunks = [];
2216
+ for (const chunk of data) {
2217
+ for (const line of chunk.split("\n")) {
2218
+ const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
2219
+ if (match?.groups?.rsc) {
2220
+ chunks.push(JSON.parse(`"${match.groups.rsc}"`));
2221
+ }
2222
+ }
2223
+ }
2224
+ return chunks;
2225
+ }
2030
2226
  function createRSCStream(rscName, data) {
2031
2227
  return new ReadableStream({
2032
2228
  type: "bytes",
@@ -2034,16 +2230,11 @@ function createRSCStream(rscName, data) {
2034
2230
  const encoder = new TextEncoder();
2035
2231
  const self = globalThis;
2036
2232
  if (data.length > 0) {
2037
- data.forEach((chunk) => {
2038
- const lines = chunk.split("\n");
2039
- for (const line of lines) {
2040
- const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
2041
- if (match?.groups?.rsc) {
2042
- self[rscName] = self[rscName] ?? [];
2043
- self[rscName].push(JSON.parse(`"${match.groups.rsc}"`));
2044
- }
2045
- }
2046
- });
2233
+ const parsed = buildRSCChunks(rscName, data);
2234
+ if (parsed.length > 0) {
2235
+ self[rscName] = self[rscName] ?? [];
2236
+ self[rscName].push(...parsed);
2237
+ }
2047
2238
  }
2048
2239
  const allChunks = (self[rscName] ?? [`0:[null]
2049
2240
  `]).join("");
@@ -2419,11 +2610,7 @@ if (typeof HTMLElement !== "undefined") {
2419
2610
  this.name = resolvedName;
2420
2611
  this.bundle = parsedMetadata.bundle;
2421
2612
  if (url) {
2422
- const self2 = globalThis;
2423
- if (!self2.__remote_bundle_url__) {
2424
- self2.__remote_bundle_url__ = {};
2425
- }
2426
- self2.__remote_bundle_url__[this.bundle] = url;
2613
+ getNamespace().bundleUrls[this.bundle] = url;
2427
2614
  }
2428
2615
  const metadataEl = document.createElement("script");
2429
2616
  metadataEl.type = "application/json";
@@ -2447,22 +2634,22 @@ if (typeof HTMLElement !== "undefined") {
2447
2634
  if (this.hostState.prevIsRemoteComponent) {
2448
2635
  if (this.hostState.prevUrl) {
2449
2636
  const prevUrl = this.hostState.prevUrl;
2450
- const self2 = globalThis;
2451
- if (self2.__remote_script_entrypoint_unmount__?.[prevUrl.href]) {
2637
+ const nsUnmount = getNamespace();
2638
+ if (nsUnmount.unmountFns[prevUrl.href]) {
2452
2639
  await Promise.all(
2453
- Array.from(
2454
- self2.__remote_script_entrypoint_unmount__[prevUrl.href] ?? []
2455
- ).map(async (unmount) => {
2456
- try {
2457
- await unmount(this.root);
2458
- } catch (e) {
2459
- logError(
2460
- "HtmlHost",
2461
- `Error while calling unmount() for Remote Component from ${prevUrl.href}.`,
2462
- e
2463
- );
2640
+ Array.from(nsUnmount.unmountFns[prevUrl.href] ?? []).map(
2641
+ async (unmount) => {
2642
+ try {
2643
+ await unmount(this.root);
2644
+ } catch (e) {
2645
+ logError(
2646
+ "HtmlHost",
2647
+ `Error while calling unmount() for Remote Component from ${prevUrl.href}.`,
2648
+ e
2649
+ );
2650
+ }
2464
2651
  }
2465
- })
2652
+ )
2466
2653
  );
2467
2654
  if (!isCurrentLoad()) {
2468
2655
  return abandonLoad();
@@ -2753,21 +2940,21 @@ if (typeof HTMLElement !== "undefined") {
2753
2940
  if (this.fouc) {
2754
2941
  this.root.removeChild(this.fouc);
2755
2942
  }
2756
- } else if (self.__remote_script_entrypoint_mount__?.[url.href]) {
2943
+ } else if (getNamespace().mountFns[url.href]) {
2757
2944
  await Promise.all(
2758
- Array.from(
2759
- self.__remote_script_entrypoint_mount__[url.href] ?? []
2760
- ).map(async (mount) => {
2761
- try {
2762
- await mount(this.root);
2763
- } catch (e) {
2764
- logError(
2765
- "HtmlHost",
2766
- `Error while calling mount() for Remote Component from ${url.href}.`,
2767
- e
2768
- );
2945
+ Array.from(getNamespace().mountFns[url.href] ?? []).map(
2946
+ async (mount) => {
2947
+ try {
2948
+ await mount(this.root);
2949
+ } catch (e) {
2950
+ logError(
2951
+ "HtmlHost",
2952
+ `Error while calling mount() for Remote Component from ${url.href}.`,
2953
+ e
2954
+ );
2955
+ }
2769
2956
  }
2770
- })
2957
+ )
2771
2958
  );
2772
2959
  this.dispatchLifecycleEvent("load", { src: this.src });
2773
2960
  } else {
@@ -2781,14 +2968,9 @@ if (typeof HTMLElement !== "undefined") {
2781
2968
  customElements.define("remote-component", RemoteComponent);
2782
2969
  }
2783
2970
  function registerSharedModules(modules = {}) {
2784
- const self = globalThis;
2785
- if (!self.__remote_component_host_shared_modules__) {
2786
- self.__remote_component_host_shared_modules__ = {};
2787
- }
2971
+ const ns = getNamespace();
2788
2972
  Object.entries(modules).forEach(([key, value]) => {
2789
- if (self.__remote_component_host_shared_modules__) {
2790
- self.__remote_component_host_shared_modules__[key] = value;
2791
- }
2973
+ ns.hostSharedModules[key] = value;
2792
2974
  });
2793
2975
  }
2794
2976
  export {