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
@@ -205,7 +205,7 @@ var init_logger = __esm({
205
205
  init_constants();
206
206
  init_error();
207
207
  PREFIX = "remote-components";
208
- DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true";
208
+ DEBUG = typeof window !== "undefined" && localStorage.getItem("RC_DEBUG") === "true" || typeof process !== "undefined" && process.env.RC_DEBUG === "true";
209
209
  }
210
210
  });
211
211
 
@@ -240,6 +240,152 @@ var init_constants2 = __esm({
240
240
  }
241
241
  });
242
242
 
243
+ // src/runtime/namespace.ts
244
+ function getNamespace() {
245
+ const g = globalThis;
246
+ const existing = g.__remote_components__;
247
+ if (existing) {
248
+ return existing;
249
+ }
250
+ const ns = {
251
+ scopes: /* @__PURE__ */ new Map(),
252
+ chunkCache: {},
253
+ mountFns: {},
254
+ unmountFns: {},
255
+ bundleUrls: {},
256
+ moduleRegistry: {},
257
+ dispatcherRuntime: void 0,
258
+ hostSharedModules: {},
259
+ cssCache: {},
260
+ shadowRoots: {}
261
+ };
262
+ const nsRecord = ns;
263
+ for (const { global, prop } of LEGACY_ALIASES) {
264
+ const legacyValue = g[global];
265
+ if (legacyValue != null) {
266
+ nsRecord[prop] = legacyValue;
267
+ }
268
+ g[global] = ns[prop];
269
+ }
270
+ const gRecord = g;
271
+ for (const key of Object.keys(gRecord)) {
272
+ if (key.startsWith(SHADOW_ROOT_PREFIX)) {
273
+ const suffix = key.slice(SHADOW_ROOT_PREFIX.length);
274
+ ns.shadowRoots[suffix] = gRecord[key];
275
+ delete gRecord[key];
276
+ }
277
+ }
278
+ g.__remote_components__ = ns;
279
+ return ns;
280
+ }
281
+ var SHADOW_ROOT_PREFIX, LEGACY_ALIASES;
282
+ var init_namespace = __esm({
283
+ "src/runtime/namespace.ts"() {
284
+ "use strict";
285
+ SHADOW_ROOT_PREFIX = "__remote_components_shadowroot_";
286
+ LEGACY_ALIASES = [
287
+ { global: "__remote_component_scopes__", prop: "scopes" },
288
+ {
289
+ global: "__remote_components_turbopack_chunk_loader_promise__",
290
+ prop: "chunkCache"
291
+ },
292
+ { global: "__remote_script_entrypoint_mount__", prop: "mountFns" },
293
+ { global: "__remote_script_entrypoint_unmount__", prop: "unmountFns" },
294
+ { global: "__remote_bundle_url__", prop: "bundleUrls" },
295
+ { global: "__rc_module_registry__", prop: "moduleRegistry" },
296
+ {
297
+ global: "__remote_component_host_shared_modules__",
298
+ prop: "hostSharedModules"
299
+ },
300
+ { global: "__remote_next_css__", prop: "cssCache" }
301
+ ];
302
+ }
303
+ });
304
+
305
+ // src/runtime/patterns.ts
306
+ function collapseDoubleSlashes(path) {
307
+ return path.replace(DOUBLE_SLASH_RE, "/");
308
+ }
309
+ var REMOTE_COMPONENT_REGEX, NEXT_BUNDLE_PATH_RE, DOUBLE_SLASH_RE;
310
+ var init_patterns = __esm({
311
+ "src/runtime/patterns.ts"() {
312
+ "use strict";
313
+ REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
314
+ NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
315
+ DOUBLE_SLASH_RE = /(?<!:)\/\//g;
316
+ }
317
+ });
318
+
319
+ // src/runtime/turbopack/remote-scope.ts
320
+ function getRegistry() {
321
+ return getNamespace().scopes;
322
+ }
323
+ function createScope(name, url, runtime, resolveClientUrl) {
324
+ const isCrossOrigin = url.origin !== location.origin;
325
+ const scopedName = computeScopedName(name, {
326
+ remoteHost: url.host,
327
+ isCrossOrigin
328
+ });
329
+ const globalKey = getBundleKey(scopedName);
330
+ return {
331
+ name,
332
+ scopedName,
333
+ globalKey,
334
+ url,
335
+ runtime,
336
+ resolveClientUrl,
337
+ moduleCache: {},
338
+ sharedModules: {},
339
+ moduleGlobal: {},
340
+ turbopackModules: []
341
+ };
342
+ }
343
+ function registerScope(scope) {
344
+ const registry = getRegistry();
345
+ registry.set(scope.scopedName, scope);
346
+ if (scope.scopedName !== scope.name) {
347
+ const existing = registry.get(scope.name);
348
+ if (existing && existing.scopedName !== scope.scopedName) {
349
+ logWarn(
350
+ "RemoteScope",
351
+ `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
352
+ );
353
+ }
354
+ registry.set(scope.name, scope);
355
+ }
356
+ logDebug(
357
+ "RemoteScope",
358
+ `Registered scope "${scope.scopedName}" (${registry.size} total)`
359
+ );
360
+ }
361
+ function getScope(name) {
362
+ return getRegistry().get(name);
363
+ }
364
+ function formatRemoteId(scope, path) {
365
+ return `[${scope.scopedName}] ${path}`;
366
+ }
367
+ function parseRemoteId(id) {
368
+ const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
369
+ if (groups?.bundle && groups.id) {
370
+ return {
371
+ bundle: groups.bundle,
372
+ path: groups.id,
373
+ prefix: groups.prefix ?? ""
374
+ };
375
+ }
376
+ return { bundle: "default", path: id, prefix: "" };
377
+ }
378
+ var init_remote_scope = __esm({
379
+ "src/runtime/turbopack/remote-scope.ts"() {
380
+ "use strict";
381
+ init_constants2();
382
+ init_namespace();
383
+ init_patterns();
384
+ init_utils();
385
+ init_logger();
386
+ }
387
+ });
388
+
243
389
  // src/config/webpack/apply-shared-modules.ts
244
390
  function applySharedModules(bundle, resolve) {
245
391
  logDebug(
@@ -251,13 +397,15 @@ function applySharedModules(bundle, resolve) {
251
397
  `Shared modules to resolve: ${Object.keys(resolve)}`
252
398
  );
253
399
  const self = globalThis;
254
- if (self.__remote_webpack_require__?.[bundle]) {
400
+ const scope = getScope(bundle);
401
+ const webpackBundle = scope?.webpackRequire ?? self.__remote_webpack_require__?.[bundle];
402
+ if (webpackBundle) {
255
403
  const modulePaths = Object.keys(
256
- self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
404
+ self.__remote_webpack_module_map__?.[bundle] ?? webpackBundle.m ?? {}
257
405
  );
258
406
  logDebug(
259
407
  "SharedModules",
260
- `Available module paths in __remote_webpack_require__[${bundle}]: ${modulePaths}`
408
+ `Available module paths for bundle "${bundle}": ${modulePaths}`
261
409
  );
262
410
  for (const [key, value] of Object.entries(resolve)) {
263
411
  const exactIds = modulePaths.filter((p) => p === key);
@@ -269,7 +417,6 @@ function applySharedModules(bundle, resolve) {
269
417
  );
270
418
  }
271
419
  for (const id of ids) {
272
- const webpackBundle = self.__remote_webpack_require__[bundle];
273
420
  if (webpackBundle.m) {
274
421
  const resolvedId = self.__remote_webpack_module_map__?.[bundle]?.[id] ? `${self.__remote_webpack_module_map__[bundle][id]}` : id;
275
422
  if (resolvedId !== id) {
@@ -304,6 +451,7 @@ var DEDUPLICATION_SKIPPED;
304
451
  var init_apply_shared_modules = __esm({
305
452
  "src/config/webpack/apply-shared-modules.ts"() {
306
453
  "use strict";
454
+ init_remote_scope();
307
455
  init_logger();
308
456
  DEDUPLICATION_SKIPPED = "shared module deduplication skipped. The remote may load its own copy of shared dependencies.";
309
457
  }
@@ -375,10 +523,8 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
375
523
  ];
376
524
  const { default: Component } = componentLoader();
377
525
  const { default: App } = appLoader();
378
- if (!self.__remote_next_css__) {
379
- self.__remote_next_css__ = {};
380
- }
381
- if (!self.__remote_next_css__[bundle]) {
526
+ const cssCache = getNamespace().cssCache;
527
+ if (!cssCache[bundle]) {
382
528
  const cssRE = /\.s?css$/;
383
529
  Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
384
530
  self.__remote_webpack_require__?.[bundle]?.(id);
@@ -396,15 +542,15 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
396
542
  node.remove();
397
543
  node = nextCss.previousSibling;
398
544
  }
399
- self.__remote_next_css__[bundle] = elements;
545
+ cssCache[bundle] = elements;
400
546
  }
401
547
  if (styleContainer) {
402
- const elements = self.__remote_next_css__[bundle];
548
+ const elements = cssCache[bundle];
403
549
  elements.forEach((el) => {
404
550
  styleContainer.appendChild(el.cloneNode(true));
405
551
  });
406
552
  } else {
407
- const elements = self.__remote_next_css__[bundle];
553
+ const elements = cssCache[bundle];
408
554
  elements.forEach((el) => {
409
555
  document.head.appendChild(el);
410
556
  });
@@ -423,96 +569,11 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
423
569
  var init_next_client_pages_loader = __esm({
424
570
  "src/config/webpack/next-client-pages-loader.ts"() {
425
571
  "use strict";
572
+ init_namespace();
426
573
  init_error();
427
574
  }
428
575
  });
429
576
 
430
- // src/runtime/patterns.ts
431
- function collapseDoubleSlashes(path) {
432
- return path.replace(DOUBLE_SLASH_RE, "/");
433
- }
434
- var REMOTE_COMPONENT_REGEX, NEXT_BUNDLE_PATH_RE, DOUBLE_SLASH_RE;
435
- var init_patterns = __esm({
436
- "src/runtime/patterns.ts"() {
437
- "use strict";
438
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
439
- NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
440
- DOUBLE_SLASH_RE = /(?<!:)\/\//g;
441
- }
442
- });
443
-
444
- // src/runtime/turbopack/remote-scope.ts
445
- function getRegistry() {
446
- const self = globalThis;
447
- if (!self.__remote_component_scopes__) {
448
- self.__remote_component_scopes__ = /* @__PURE__ */ new Map();
449
- }
450
- return self.__remote_component_scopes__;
451
- }
452
- function createScope(name, url, runtime, resolveClientUrl) {
453
- const isCrossOrigin = url.origin !== location.origin;
454
- const scopedName = computeScopedName(name, {
455
- remoteHost: url.host,
456
- isCrossOrigin
457
- });
458
- const globalKey = getBundleKey(scopedName);
459
- return {
460
- name,
461
- scopedName,
462
- globalKey,
463
- url,
464
- runtime,
465
- resolveClientUrl,
466
- moduleCache: {},
467
- sharedModules: {},
468
- moduleGlobal: {}
469
- };
470
- }
471
- function registerScope(scope) {
472
- const registry = getRegistry();
473
- registry.set(scope.scopedName, scope);
474
- if (scope.scopedName !== scope.name) {
475
- const existing = registry.get(scope.name);
476
- if (existing && existing.scopedName !== scope.scopedName) {
477
- logWarn(
478
- "RemoteScope",
479
- `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
480
- );
481
- }
482
- registry.set(scope.name, scope);
483
- }
484
- logDebug(
485
- "RemoteScope",
486
- `Registered scope "${scope.scopedName}" (${registry.size} total)`
487
- );
488
- }
489
- function getScope(name) {
490
- return getRegistry().get(name);
491
- }
492
- function formatRemoteId(scope, path) {
493
- return `[${scope.scopedName}] ${path}`;
494
- }
495
- function parseRemoteId(id) {
496
- const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
497
- if (groups?.bundle && groups.id) {
498
- return {
499
- bundle: groups.bundle,
500
- path: groups.id,
501
- prefix: groups.prefix ?? ""
502
- };
503
- }
504
- return { bundle: "default", path: id, prefix: "" };
505
- }
506
- var init_remote_scope = __esm({
507
- "src/runtime/turbopack/remote-scope.ts"() {
508
- "use strict";
509
- init_constants2();
510
- init_patterns();
511
- init_utils();
512
- init_logger();
513
- }
514
- });
515
-
516
577
  // src/host/shared/remote-image-loader.ts
517
578
  function createRemoteImageLoader(bundle, resolveClientUrl) {
518
579
  const loader = Object.assign(
@@ -544,9 +605,9 @@ var init_remote_image_loader = __esm({
544
605
 
545
606
  // src/host/shared/polyfill.tsx
546
607
  function sharedPolyfills(shared, resolveClientUrl) {
547
- const self = globalThis;
608
+ const hostShared = getNamespace().hostSharedModules;
548
609
  const polyfill = {
549
- "next/dist/client/components/navigation": self.__remote_component_host_shared_modules__?.["next/navigation"] ?? shared?.["next/navigation"] ?? (() => Promise.resolve({
610
+ "next/dist/client/components/navigation": hostShared["next/navigation"] ?? shared?.["next/navigation"] ?? (() => Promise.resolve({
550
611
  useRouter() {
551
612
  return {
552
613
  push: (routerUrl) => {
@@ -577,7 +638,7 @@ function sharedPolyfills(shared, resolveClientUrl) {
577
638
  },
578
639
  __esModule: true
579
640
  })),
580
- "next/dist/client/app-dir/link": self.__remote_component_host_shared_modules__?.["next/link"] ?? shared?.["next/link"] ?? (() => Promise.resolve({
641
+ "next/dist/client/app-dir/link": hostShared["next/link"] ?? shared?.["next/link"] ?? (() => Promise.resolve({
581
642
  default: ({
582
643
  scroll: _,
583
644
  replace,
@@ -627,7 +688,7 @@ function sharedPolyfills(shared, resolveClientUrl) {
627
688
  },
628
689
  __esModule: true
629
690
  })),
630
- "next/dist/client/app-dir/form": self.__remote_component_host_shared_modules__?.["next/form"] ?? shared?.["next/form"] ?? (() => Promise.resolve({
691
+ "next/dist/client/app-dir/form": hostShared["next/form"] ?? shared?.["next/form"] ?? (() => Promise.resolve({
631
692
  default: () => {
632
693
  throw new Error("Next.js <Form> component not implemented");
633
694
  },
@@ -638,17 +699,17 @@ function sharedPolyfills(shared, resolveClientUrl) {
638
699
  // This gives us full next/image fidelity (fill, priority, srcSet, blur
639
700
  // placeholders, error handling) while routing image optimization through the
640
701
  // remote app's /_next/image endpoint.
641
- "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({
702
+ "next/dist/shared/lib/image-loader": hostShared["next/dist/shared/lib/image-loader"] ?? shared?.["next/dist/shared/lib/image-loader"] ?? ((bundle) => Promise.resolve({
642
703
  default: createRemoteImageLoader(bundle, resolveClientUrl),
643
704
  __esModule: true
644
705
  })),
645
- "next/dist/client/script": self.__remote_component_host_shared_modules__?.["next/script"] ?? shared?.["next/script"] ?? (() => Promise.resolve({
706
+ "next/dist/client/script": hostShared["next/script"] ?? shared?.["next/script"] ?? (() => Promise.resolve({
646
707
  // TODO: implement <Script> component for non-Next.js host applications
647
708
  // do not throw an error for now
648
709
  default: () => null,
649
710
  __esModule: true
650
711
  })),
651
- "next/router": self.__remote_component_host_shared_modules__?.["next/router"] ?? shared?.["next/router"] ?? (() => (
712
+ "next/router": hostShared["next/router"] ?? shared?.["next/router"] ?? (() => (
652
713
  // TODO: incomplete implementation
653
714
  Promise.resolve({
654
715
  useRouter() {
@@ -688,11 +749,82 @@ var init_polyfill = __esm({
688
749
  "src/host/shared/polyfill.tsx"() {
689
750
  "use strict";
690
751
  init_remote_image_loader();
752
+ init_namespace();
691
753
  init_logger();
692
754
  import_jsx_runtime = require("react/jsx-runtime");
693
755
  }
694
756
  });
695
757
 
758
+ // src/host/shared/shared-module-resolver.ts
759
+ function buildCoreShared(userShared) {
760
+ return {
761
+ react: async () => (await import("react")).default,
762
+ "react-dom": async () => (await import("react-dom")).default,
763
+ "react/jsx-dev-runtime": async () => (await import("react/jsx-dev-runtime")).default,
764
+ "react/jsx-runtime": async () => (await import("react/jsx-runtime")).default,
765
+ "react-dom/client": async () => (await import("react-dom/client")).default,
766
+ ...userShared
767
+ };
768
+ }
769
+ function buildHostShared(userShared, resolveClientUrl, options) {
770
+ const self = globalThis;
771
+ const result = {
772
+ ...sharedPolyfills(userShared, resolveClientUrl),
773
+ ...self.__remote_component_host_shared_modules__,
774
+ ...userShared
775
+ };
776
+ if (options?.includeRemoteComponentShared) {
777
+ Object.assign(result, self.__remote_component_shared__);
778
+ }
779
+ return result;
780
+ }
781
+ async function buildWebpackResolve(hostShared, remoteShared, bundle, reactModules, callerTag = "SharedModuleResolver") {
782
+ const resolve = {
783
+ ...reactModules,
784
+ ...Object.entries(remoteShared).reduce(
785
+ (acc, [key, value]) => {
786
+ if (typeof hostShared[value] !== "undefined") {
787
+ acc[key.replace(/^\(ssr\)\/(?<relative>\.\/)?/, "")] = hostShared[value];
788
+ } else {
789
+ logDebug(
790
+ callerTag,
791
+ `Remote requests "${value}" but host doesn't provide it`
792
+ );
793
+ }
794
+ return acc;
795
+ },
796
+ {}
797
+ )
798
+ };
799
+ await Promise.all(
800
+ Object.entries(resolve).map(async ([key, value]) => {
801
+ if (typeof value === "function") {
802
+ resolve[key] = await value(bundle);
803
+ }
804
+ return Promise.resolve(value);
805
+ })
806
+ );
807
+ return resolve;
808
+ }
809
+ var CORE_REACT_PATH_MAP, VENDOR_SHARED;
810
+ var init_shared_module_resolver = __esm({
811
+ "src/host/shared/shared-module-resolver.ts"() {
812
+ "use strict";
813
+ init_polyfill();
814
+ init_logger();
815
+ CORE_REACT_PATH_MAP = {
816
+ react: "/react/index.js",
817
+ "react/jsx-dev-runtime": "/react/jsx-dev-runtime.js",
818
+ "react/jsx-runtime": "/react/jsx-runtime.js",
819
+ "react-dom": "/react-dom/index.js",
820
+ "react-dom/client": "/react-dom/client.js"
821
+ };
822
+ VENDOR_SHARED = Object.fromEntries(
823
+ Object.entries(CORE_REACT_PATH_MAP).filter(([key]) => key !== "react-dom/client").map(([key, path]) => [key, `'${path}'`])
824
+ );
825
+ }
826
+ });
827
+
696
828
  // src/runtime/loaders/script-loader.ts
697
829
  async function loadScripts(scripts, resolveClientUrl) {
698
830
  await Promise.all(
@@ -704,6 +836,13 @@ async function loadScripts(scripts, resolveClientUrl) {
704
836
  location.origin
705
837
  ).href;
706
838
  const resolvedSrc = resolveClientUrl?.(newSrc) ?? newSrc;
839
+ const alreadyLoaded = Array.from(
840
+ document.querySelectorAll("script[src]")
841
+ ).some((s) => s.src === resolvedSrc);
842
+ if (alreadyLoaded) {
843
+ resolve();
844
+ return;
845
+ }
707
846
  const newScript = document.createElement("script");
708
847
  newScript.onload = () => resolve();
709
848
  newScript.onerror = () => {
@@ -785,34 +924,19 @@ async function webpackRuntime(bundle, shared, remoteShared, resolveClientUrl) {
785
924
  ];
786
925
  });
787
926
  await loadScripts(scriptSrcs, resolveClientUrl);
788
- const hostShared = {
789
- ...sharedPolyfills(shared, resolveClientUrl),
790
- ...self.__remote_component_host_shared_modules__,
791
- ...shared
792
- };
793
- const resolve = {
794
- "/react/index.js": (await import("react")).default,
795
- "/react/jsx-dev-runtime.js": (await import("react/jsx-dev-runtime")).default,
796
- "/react/jsx-runtime.js": (await import("react/jsx-runtime")).default,
797
- "/react-dom/index.js": (await import("react-dom")).default,
798
- "/react-dom/client.js": (await import("react-dom/client")).default,
799
- ...Object.entries(remoteShared ?? {}).reduce(
800
- (acc, [key, value]) => {
801
- if (typeof hostShared[value] !== "undefined") {
802
- acc[key.replace(/^\(ssr\)\/(?<relative>\.\/)?/, "")] = hostShared[value];
803
- }
804
- return acc;
805
- },
806
- {}
807
- )
808
- };
809
- await Promise.all(
810
- Object.entries(resolve).map(async ([key, value]) => {
811
- if (typeof value === "function") {
812
- resolve[key] = await value(remoteBundle);
813
- }
814
- return Promise.resolve(value);
815
- })
927
+ const hostShared = buildHostShared(shared, resolveClientUrl);
928
+ const resolve = await buildWebpackResolve(
929
+ hostShared,
930
+ remoteShared ?? {},
931
+ remoteBundle,
932
+ {
933
+ "/react/index.js": (await import("react")).default,
934
+ "/react/jsx-dev-runtime.js": (await import("react/jsx-dev-runtime")).default,
935
+ "/react/jsx-runtime.js": (await import("react/jsx-runtime")).default,
936
+ "/react-dom/index.js": (await import("react-dom")).default,
937
+ "/react-dom/client.js": (await import("react-dom/client")).default
938
+ },
939
+ "WebpackRuntime"
816
940
  );
817
941
  applySharedModules(remoteBundle, resolve);
818
942
  }
@@ -829,7 +953,7 @@ var init_webpack = __esm({
829
953
  "use strict";
830
954
  init_apply_shared_modules();
831
955
  init_next_client_pages_loader();
832
- init_polyfill();
956
+ init_shared_module_resolver();
833
957
  init_script_loader();
834
958
  init_patterns();
835
959
  init_error();
@@ -837,15 +961,31 @@ var init_webpack = __esm({
837
961
  });
838
962
 
839
963
  // src/runtime/turbopack/patterns.ts
840
- var REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_RESOLVE_RE, ASYNC_MODULE_ALL_RE, TURBOPACK_GLOBAL_RE;
964
+ function stripQuotes(value) {
965
+ if (value.startsWith('"') && value.endsWith('"')) {
966
+ return value.slice(1, -1);
967
+ }
968
+ return value;
969
+ }
970
+ function extractGroup(re, input, group) {
971
+ const raw = re.exec(input)?.groups?.[group];
972
+ return raw ? stripQuotes(raw) : void 0;
973
+ }
974
+ var MODULE_ID_PATTERN, REMOTE_SHARED_MARKER_RE, REMOTE_SHARED_ASSIGNMENT_RE, ASYNC_MODULE_LOADER_RE, ASYNC_MODULE_CALLBACK_RE, TURBOPACK_GLOBAL_RE;
841
975
  var init_patterns2 = __esm({
842
976
  "src/runtime/turbopack/patterns.ts"() {
843
977
  "use strict";
978
+ MODULE_ID_PATTERN = '"[^"]+"|[0-9]+';
844
979
  REMOTE_SHARED_MARKER_RE = /(?:self|[a-z])\.TURBOPACK_REMOTE_SHARED/;
845
- REMOTE_SHARED_ASSIGNMENT_RE = /\.TURBOPACK_REMOTE_SHARED=await (?:__turbopack_context__|[a-z])\.A\((?<sharedModuleId>[0-9]+)\)/;
846
- ASYNC_MODULE_LOADER_RE = /(?:__turbopack_context__|e)\.A\((?<asyncSharedModuleId>[0-9]+)\)/;
847
- ASYNC_MODULE_RESOLVE_RE = /(?<ctx>__turbopack_context__|e)=>\{\k<ctx>\.v\((?<inner>parentImport|e)=>Promise\.resolve\(\)\.then\(\(\)=>\k<inner>\((?<sharedModuleId>[0-9]+)\)\)\)\}/;
848
- 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]+)\)\)\)\}/;
980
+ REMOTE_SHARED_ASSIGNMENT_RE = new RegExp(
981
+ `\\.TURBOPACK_REMOTE_SHARED\\s*=\\s*await (?:__turbopack_context__|[a-z])\\.A\\((?<sharedModuleId>${MODULE_ID_PATTERN})\\)`
982
+ );
983
+ ASYNC_MODULE_LOADER_RE = new RegExp(
984
+ `(?:__turbopack_context__|e)\\.A\\((?<asyncSharedModuleId>${MODULE_ID_PATTERN})\\)`
985
+ );
986
+ ASYNC_MODULE_CALLBACK_RE = new RegExp(
987
+ `(?:parentImport|[a-z])\\((?<sharedModuleId>${MODULE_ID_PATTERN})\\)`
988
+ );
849
989
  TURBOPACK_GLOBAL_RE = /(?:globalThis|self)\s*(?:\.TURBOPACK|\[\s*["']TURBOPACK["']\s*\])/;
850
990
  }
851
991
  });
@@ -857,6 +997,7 @@ function loadChunkWithScope(scope, chunkId) {
857
997
  `loadChunkWithScope: "${chunkId}" (scope: "${scope.scopedName}")`
858
998
  );
859
999
  const self = globalThis;
1000
+ const ns = getNamespace();
860
1001
  const { bundle, path, prefix } = parseRemoteId(chunkId);
861
1002
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : scope.runtime;
862
1003
  if (remoteRuntime === RUNTIME_WEBPACK) {
@@ -867,35 +1008,31 @@ function loadChunkWithScope(scope, chunkId) {
867
1008
  if (url.endsWith(".css")) {
868
1009
  return;
869
1010
  }
870
- if (!self.__remote_components_turbopack_chunk_loader_promise__) {
871
- self.__remote_components_turbopack_chunk_loader_promise__ = {};
872
- }
873
- if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
874
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1011
+ if (ns.chunkCache[url]) {
1012
+ logDebug("ChunkLoader", `Cache hit for "${chunkId}" (url="${url}")`);
1013
+ return ns.chunkCache[url];
875
1014
  }
876
1015
  const resolvedUrl = scope.resolveClientUrl?.(url) ?? url;
877
1016
  if (resolvedUrl !== url) {
878
1017
  logDebug("ChunkLoader", `Proxied chunk URL: "${url}" \u2192 "${resolvedUrl}"`);
879
1018
  }
880
- self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise(
881
- (resolve, reject) => {
882
- fetch(resolvedUrl).then((res) => res.text()).then((code) => {
883
- const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
884
- if (hasTurbopack) {
885
- return handleTurbopackChunk(code, scope, url);
886
- }
887
- }).then(resolve).catch((error) => {
888
- const isProxied = isProxiedUrl(resolvedUrl);
889
- if (isProxied) {
890
- reject(failedProxiedAssetError("chunk", url, resolvedUrl));
891
- } else {
892
- warnCrossOriginFetchError("ChunkLoader", url);
893
- reject(error);
894
- }
895
- });
896
- }
897
- );
898
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
1019
+ ns.chunkCache[url] = new Promise((resolve, reject) => {
1020
+ fetch(resolvedUrl).then((res) => res.text()).then((code) => {
1021
+ const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
1022
+ if (hasTurbopack) {
1023
+ return handleTurbopackChunk(code, scope, url);
1024
+ }
1025
+ }).then(resolve).catch((error) => {
1026
+ const isProxied = isProxiedUrl(resolvedUrl);
1027
+ if (isProxied) {
1028
+ reject(failedProxiedAssetError("chunk", url, resolvedUrl));
1029
+ } else {
1030
+ warnCrossOriginFetchError("ChunkLoader", url);
1031
+ reject(error);
1032
+ }
1033
+ });
1034
+ });
1035
+ return ns.chunkCache[url];
899
1036
  }
900
1037
  function createChunkDispatcher() {
901
1038
  return function __chunk_dispatcher__(chunkId, scriptBundle) {
@@ -944,19 +1081,39 @@ async function handleTurbopackChunk(code, scope, url) {
944
1081
  `//# sourceMappingURL=${new URL(".", new URL(url, scope.url)).href}$1$2.js.map`
945
1082
  );
946
1083
  if (!self[`TURBOPACK_${globalKey}`]) {
947
- const chunkData = [];
948
- const turbopackObject = {
949
- push: (item) => {
950
- if (Array.isArray(item)) {
951
- chunkData.push(item);
952
- } else {
953
- chunkData.push([item]);
1084
+ const wrapPush = (target) => {
1085
+ const originalPush = target.push;
1086
+ if (typeof originalPush !== "function")
1087
+ return target;
1088
+ target.push = (...items) => {
1089
+ for (const item of items) {
1090
+ if (Array.isArray(item)) {
1091
+ for (const entry of item) {
1092
+ scope.turbopackModules.push(entry);
1093
+ }
1094
+ } else {
1095
+ scope.turbopackModules.push(item);
1096
+ }
954
1097
  }
955
- return chunkData.length;
956
- },
957
- __chunks__: chunkData
1098
+ return originalPush.apply(target, items);
1099
+ };
1100
+ return target;
958
1101
  };
959
- self[`TURBOPACK_${globalKey}`] = turbopackObject;
1102
+ const globalProp = `TURBOPACK_${globalKey}`;
1103
+ let currentValue = wrapPush([]);
1104
+ Object.defineProperty(self, globalProp, {
1105
+ get() {
1106
+ return currentValue;
1107
+ },
1108
+ set(newValue) {
1109
+ if (newValue && typeof newValue === "object") {
1110
+ wrapPush(newValue);
1111
+ }
1112
+ currentValue = newValue;
1113
+ },
1114
+ configurable: true,
1115
+ enumerable: true
1116
+ });
960
1117
  }
961
1118
  await new Promise((scriptResolve, scriptReject) => {
962
1119
  const blob = new Blob([transformedCode], {
@@ -1008,6 +1165,7 @@ var init_chunk_loader = __esm({
1008
1165
  "src/runtime/turbopack/chunk-loader.ts"() {
1009
1166
  "use strict";
1010
1167
  init_constants2();
1168
+ init_namespace();
1011
1169
  init_patterns();
1012
1170
  init_protected_rc_fallback();
1013
1171
  init_error();
@@ -1019,13 +1177,13 @@ var init_chunk_loader = __esm({
1019
1177
 
1020
1178
  // src/runtime/turbopack/shared-modules.ts
1021
1179
  function getTurbopackModules(scope) {
1180
+ if (scope.turbopackModules.length > 0) {
1181
+ return scope.turbopackModules;
1182
+ }
1022
1183
  const self = globalThis;
1023
1184
  const raw = self[`TURBOPACK_${scope.globalKey}`];
1024
1185
  if (!raw)
1025
1186
  return void 0;
1026
- if (typeof raw === "object" && "__chunks__" in raw) {
1027
- return raw.__chunks__.flat();
1028
- }
1029
1187
  if (Array.isArray(raw)) {
1030
1188
  return raw.flat();
1031
1189
  }
@@ -1049,7 +1207,11 @@ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}
1049
1207
  if (sharedModuleInitializerIndex > 0) {
1050
1208
  const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1051
1209
  const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1052
- const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1210
+ const sharedModuleId = extractGroup(
1211
+ REMOTE_SHARED_ASSIGNMENT_RE,
1212
+ sharedModuleInitializerCode,
1213
+ "sharedModuleId"
1214
+ );
1053
1215
  if (sharedModuleId) {
1054
1216
  const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1055
1217
  scope,
@@ -1107,20 +1269,22 @@ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}
1107
1269
  }
1108
1270
  function extractSharedModuleIds(shared, scope) {
1109
1271
  return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1110
- const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1272
+ const asyncSharedModuleId = extractGroup(
1273
+ ASYNC_MODULE_LOADER_RE,
1274
+ value.toString(),
1275
+ "asyncSharedModuleId"
1276
+ );
1111
1277
  if (asyncSharedModuleId) {
1112
- const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1113
- let asyncSharedModule;
1114
- const turbopackModules = getTurbopackModules(scope) ?? [];
1115
- const asyncSharedModuleIdIndex = turbopackModules.indexOf(
1116
- asyncSharedModuleIdNumber
1278
+ const asyncSharedModule = findModuleInit(
1279
+ getTurbopackModules(scope),
1280
+ asyncSharedModuleId
1117
1281
  );
1118
- if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
1119
- asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
1120
- }
1121
1282
  if (asyncSharedModule) {
1122
- const asyncSharedModuleCode = asyncSharedModule.toString();
1123
- const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1283
+ const sharedModuleId = extractGroup(
1284
+ ASYNC_MODULE_CALLBACK_RE,
1285
+ asyncSharedModule.toString(),
1286
+ "sharedModuleId"
1287
+ );
1124
1288
  acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1125
1289
  "__remote_shared_module_",
1126
1290
  ""
@@ -1160,10 +1324,8 @@ function requireModule(scope, moduleId, fullId) {
1160
1324
  if (scope.moduleCache[idStr])
1161
1325
  return scope.moduleCache[idStr];
1162
1326
  const sharedModule = getSharedModule(scope, moduleId);
1163
- if (sharedModule) {
1164
- scope.moduleCache[idStr] = sharedModule;
1327
+ if (sharedModule)
1165
1328
  return sharedModule;
1166
- }
1167
1329
  return handleTurbopackModule(
1168
1330
  scope,
1169
1331
  idStr,
@@ -1171,19 +1333,10 @@ function requireModule(scope, moduleId, fullId) {
1171
1333
  );
1172
1334
  }
1173
1335
  function handleTurbopackModule(scope, moduleId, id) {
1174
- const self = globalThis;
1175
1336
  if (scope.moduleCache[moduleId]) {
1176
1337
  return scope.moduleCache[moduleId];
1177
1338
  }
1178
- const raw = self[`TURBOPACK_${scope.globalKey}`];
1179
- let modules;
1180
- if (raw && typeof raw === "object" && "__chunks__" in raw) {
1181
- modules = raw.__chunks__.flat();
1182
- } else if (Array.isArray(raw)) {
1183
- modules = raw.flat();
1184
- } else {
1185
- modules = raw;
1186
- }
1339
+ const modules = getTurbopackModules(scope);
1187
1340
  if (!modules) {
1188
1341
  logError(
1189
1342
  "TurbopackModule",
@@ -1233,9 +1386,17 @@ function findModuleInit(modules, moduleId) {
1233
1386
  if (idx >= 0) {
1234
1387
  return flat.slice(idx + 1).find((e) => typeof e === "function");
1235
1388
  }
1236
- return flat.find(
1237
- (e) => Boolean(e && typeof e === "object" && moduleId in e)
1238
- )?.[moduleId];
1389
+ for (const entry of flat) {
1390
+ if (!entry || typeof entry !== "object")
1391
+ continue;
1392
+ const obj = entry;
1393
+ if (moduleId in obj)
1394
+ return obj[moduleId];
1395
+ const prefixKey = Object.keys(obj).find((k) => k.startsWith(moduleId));
1396
+ if (prefixKey)
1397
+ return obj[prefixKey];
1398
+ }
1399
+ return void 0;
1239
1400
  }
1240
1401
  function createTurbopackContext(scope, exports, moduleExports, modules, moduleInit, id) {
1241
1402
  const scopedRequire = (moduleId) => requireModule(scope, moduleId, formatRemoteId(scope, String(moduleId)));
@@ -1314,7 +1475,10 @@ function createTurbopackContext(scope, exports, moduleExports, modules, moduleIn
1314
1475
  }
1315
1476
  if (typeof mod !== "object" || mod === null) {
1316
1477
  mod = { default: mod };
1317
- } else if (!("default" in mod) && mod.toString() !== "[object Module]") {
1478
+ } else if (!("default" in mod) && // ES module namespace objects have a null prototype, so calling
1479
+ // mod.toString() directly throws. Use Object.prototype.toString
1480
+ // to safely detect them.
1481
+ Object.prototype.toString.call(mod) !== "[object Module]") {
1318
1482
  try {
1319
1483
  mod.default = mod;
1320
1484
  } catch {
@@ -1387,21 +1551,38 @@ var init_module = __esm({
1387
1551
  }
1388
1552
  });
1389
1553
 
1390
- // src/runtime/turbopack/webpack-runtime.ts
1391
- async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
1554
+ // src/runtime/turbopack/remote-scope-setup.ts
1555
+ async function setupRemoteScope(runtime, scripts = [], url = new URL(location.href), bundle, resolveClientUrl) {
1392
1556
  const self = globalThis;
1557
+ const ns = getNamespace();
1393
1558
  const bundleName = bundle ?? "default";
1559
+ const existingScope = getScope(bundleName);
1560
+ if (existingScope && existingScope.url.origin === url.origin) {
1561
+ logDebug(
1562
+ "WebpackRuntime",
1563
+ `Reusing scope "${existingScope.scopedName}" (turbopackModules=${existingScope.turbopackModules.length})`
1564
+ );
1565
+ existingScope.resolveClientUrl = resolveClientUrl;
1566
+ if (runtime === RUNTIME_TURBOPACK) {
1567
+ await Promise.allSettled(
1568
+ scripts.map(
1569
+ (script) => script.src ? loadChunkWithScope(existingScope, script.src) : Promise.resolve(void 0)
1570
+ )
1571
+ );
1572
+ }
1573
+ return existingScope;
1574
+ }
1394
1575
  const scope = createScope(bundleName, url, runtime, resolveClientUrl);
1395
1576
  registerScope(scope);
1396
- if (!self.__remote_bundle_url__) {
1397
- self.__remote_bundle_url__ = {};
1577
+ if (runtime === RUNTIME_WEBPACK && self.__remote_webpack_require__?.[bundleName]) {
1578
+ scope.webpackRequire = self.__remote_webpack_require__[bundleName];
1398
1579
  }
1399
- self.__remote_bundle_url__[bundleName] = url;
1580
+ ns.bundleUrls[bundleName] = url;
1400
1581
  if (scope.scopedName !== bundleName) {
1401
- self.__remote_bundle_url__[scope.scopedName] = url;
1582
+ ns.bundleUrls[scope.scopedName] = url;
1402
1583
  }
1403
1584
  self.__webpack_get_script_filename__ = () => null;
1404
- const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack";
1585
+ const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || ns.dispatcherRuntime !== "turbopack";
1405
1586
  if (willCreateDispatchers) {
1406
1587
  if (!self.__original_webpack_require__ && !self.__original_webpack_chunk_load__) {
1407
1588
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
@@ -1409,6 +1590,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1409
1590
  }
1410
1591
  self.__webpack_chunk_load__ = createChunkDispatcher();
1411
1592
  self.__webpack_require__ = createModuleDispatcher(runtime);
1593
+ ns.dispatcherRuntime = runtime;
1412
1594
  self.__webpack_require_type__ = runtime;
1413
1595
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
1414
1596
  self.__remote_webpack_require__[bundleName] = self.__webpack_require__;
@@ -1436,15 +1618,7 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1436
1618
  }
1437
1619
  }
1438
1620
  }
1439
- const coreShared = {
1440
- react: async () => (await import("react")).default,
1441
- "react-dom": async () => (await import("react-dom")).default,
1442
- "react/jsx-dev-runtime": async () => (await import("react/jsx-dev-runtime")).default,
1443
- "react/jsx-runtime": async () => (await import("react/jsx-runtime")).default,
1444
- "react-dom/client": async () => (await import("react-dom/client")).default,
1445
- ...shared
1446
- };
1447
- await initializeSharedModules(scope, coreShared, remoteShared);
1621
+ return scope;
1448
1622
  }
1449
1623
  function createModuleDispatcher(runtime) {
1450
1624
  return (id) => {
@@ -1461,6 +1635,9 @@ function createModuleDispatcher(runtime) {
1461
1635
  );
1462
1636
  try {
1463
1637
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
1638
+ const scope2 = getScope(bundle);
1639
+ if (scope2?.webpackRequire)
1640
+ return scope2.webpackRequire(moduleId);
1464
1641
  return self.__remote_webpack_require__?.[bundle]?.(moduleId);
1465
1642
  }
1466
1643
  const scope = getScope(bundleName);
@@ -1498,17 +1675,17 @@ function createModuleDispatcher(runtime) {
1498
1675
  }
1499
1676
  };
1500
1677
  }
1501
- var init_webpack_runtime = __esm({
1502
- "src/runtime/turbopack/webpack-runtime.ts"() {
1678
+ var init_remote_scope_setup = __esm({
1679
+ "src/runtime/turbopack/remote-scope-setup.ts"() {
1503
1680
  "use strict";
1504
1681
  init_constants2();
1682
+ init_namespace();
1505
1683
  init_patterns();
1506
1684
  init_error();
1507
1685
  init_logger();
1508
1686
  init_chunk_loader();
1509
1687
  init_module();
1510
1688
  init_remote_scope();
1511
- init_shared_modules();
1512
1689
  }
1513
1690
  });
1514
1691
 
@@ -1519,36 +1696,28 @@ __export(turbopack_exports, {
1519
1696
  });
1520
1697
  async function turbopackRuntime(url, bundle, shared, remoteShared, resolveClientUrl) {
1521
1698
  const self = globalThis;
1522
- const hostShared = {
1523
- ...sharedPolyfills(shared, resolveClientUrl),
1524
- ...self.__remote_component_host_shared_modules__,
1525
- ...shared,
1526
- ...self.__remote_component_shared__
1527
- };
1528
- await setupWebpackRuntime(
1529
- "turbopack",
1530
- [],
1531
- url,
1532
- bundle,
1533
- hostShared,
1534
- remoteShared,
1535
- resolveClientUrl
1536
- );
1699
+ const hostShared = buildHostShared(shared, resolveClientUrl, {
1700
+ includeRemoteComponentShared: true
1701
+ });
1702
+ await setupRemoteScope("turbopack", [], url, bundle, resolveClientUrl);
1537
1703
  const {
1538
1704
  default: { createFromReadableStream }
1539
1705
  } = await import("react-server-dom-webpack/client.browser");
1540
- function preloadScripts(scripts, __) {
1541
- return setupWebpackRuntime(
1706
+ async function preloadScripts(scripts, __) {
1707
+ const scope = await setupRemoteScope(
1542
1708
  "turbopack",
1543
1709
  scripts.map((script) => ({
1544
1710
  src: script.getAttribute("src") || script.getAttribute("data-src") || script.src
1545
1711
  })),
1546
1712
  url,
1547
1713
  bundle,
1548
- hostShared,
1549
- remoteShared,
1550
1714
  resolveClientUrl
1551
1715
  );
1716
+ await initializeSharedModules(
1717
+ scope,
1718
+ buildCoreShared(hostShared),
1719
+ remoteShared ?? {}
1720
+ );
1552
1721
  }
1553
1722
  return {
1554
1723
  self,
@@ -1563,8 +1732,9 @@ var init_turbopack = __esm({
1563
1732
  "use strict";
1564
1733
  init_apply_shared_modules();
1565
1734
  init_next_client_pages_loader();
1566
- init_polyfill();
1567
- init_webpack_runtime();
1735
+ init_shared_module_resolver();
1736
+ init_remote_scope_setup();
1737
+ init_shared_modules();
1568
1738
  }
1569
1739
  });
1570
1740
 
@@ -1611,10 +1781,9 @@ async function importViaCallback(absoluteSrc, resolveClientUrl) {
1611
1781
  URL.revokeObjectURL(moduleBlobUrl);
1612
1782
  URL.revokeObjectURL(wrapperBlobUrl);
1613
1783
  }
1614
- const registry = globalThis.__rc_module_registry__;
1615
- const mod = registry?.[absoluteSrc] ?? {};
1616
- if (registry)
1617
- delete registry[absoluteSrc];
1784
+ const registry = getNamespace().moduleRegistry;
1785
+ const mod = registry[absoluteSrc] ?? {};
1786
+ delete registry[absoluteSrc];
1618
1787
  return mod;
1619
1788
  }
1620
1789
  async function importDirectly(absoluteSrc) {
@@ -1644,12 +1813,12 @@ function resolveScriptSrc(script, url) {
1644
1813
  return rawSrc;
1645
1814
  }
1646
1815
  async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1647
- const self = globalThis;
1648
- if (self.__remote_script_entrypoint_mount__?.[url.href]) {
1649
- self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
1816
+ const ns = getNamespace();
1817
+ if (ns.mountFns[url.href]) {
1818
+ ns.mountFns[url.href] = /* @__PURE__ */ new Set();
1650
1819
  }
1651
- if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
1652
- self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
1820
+ if (ns.unmountFns[url.href]) {
1821
+ ns.unmountFns[url.href] = /* @__PURE__ */ new Set();
1653
1822
  }
1654
1823
  const mountUnmountSets = await Promise.all(
1655
1824
  scripts.map(async (script) => {
@@ -1661,25 +1830,19 @@ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1661
1830
  URL.revokeObjectURL(src);
1662
1831
  }
1663
1832
  if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
1664
- if (!self.__remote_script_entrypoint_mount__) {
1665
- self.__remote_script_entrypoint_mount__ = {};
1666
- }
1667
- if (!self.__remote_script_entrypoint_mount__[url.href]) {
1668
- self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
1833
+ if (!ns.mountFns[url.href]) {
1834
+ ns.mountFns[url.href] = /* @__PURE__ */ new Set();
1669
1835
  }
1670
- self.__remote_script_entrypoint_mount__[url.href]?.add(
1836
+ ns.mountFns[url.href]?.add(
1671
1837
  mod.mount || mod.default?.mount || (() => {
1672
1838
  })
1673
1839
  );
1674
1840
  }
1675
1841
  if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
1676
- if (!self.__remote_script_entrypoint_unmount__) {
1677
- self.__remote_script_entrypoint_unmount__ = {};
1842
+ if (!ns.unmountFns[url.href]) {
1843
+ ns.unmountFns[url.href] = /* @__PURE__ */ new Set();
1678
1844
  }
1679
- if (!self.__remote_script_entrypoint_unmount__[url.href]) {
1680
- self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
1681
- }
1682
- self.__remote_script_entrypoint_unmount__[url.href]?.add(
1845
+ ns.unmountFns[url.href]?.add(
1683
1846
  mod.unmount || mod.default?.unmount || (() => {
1684
1847
  })
1685
1848
  );
@@ -1720,6 +1883,7 @@ async function loadStaticRemoteComponent(scripts, url, resolveClientUrl) {
1720
1883
  var init_static_loader = __esm({
1721
1884
  "src/runtime/loaders/static-loader.ts"() {
1722
1885
  "use strict";
1886
+ init_namespace();
1723
1887
  init_logger();
1724
1888
  }
1725
1889
  });
@@ -1845,8 +2009,8 @@ function resolveNameFromSrc(src, defaultName) {
1845
2009
  // src/host/html/index.tsx
1846
2010
  init_constants2();
1847
2011
 
1848
- // src/runtime/html/apply-origin.ts
1849
- var tagNames = [
2012
+ // src/runtime/html/html-spec.ts
2013
+ var ORIGIN_REWRITE_TAGS = [
1850
2014
  "img",
1851
2015
  "source",
1852
2016
  "video",
@@ -1857,10 +2021,37 @@ var tagNames = [
1857
2021
  "script",
1858
2022
  "link"
1859
2023
  ];
2024
+ var ID_SUFFIX_RSC = "_rsc";
2025
+ var ID_SUFFIX_SSR = "_ssr";
2026
+ var ID_SUFFIX_SHARED = "_shared";
2027
+ var DATA_BUNDLE = "data-bundle";
2028
+ var DATA_ROUTE = "data-route";
2029
+ var DATA_RUNTIME = "data-runtime";
2030
+ var DATA_TYPE = "data-type";
2031
+ var DATA_SRC = "data-src";
2032
+ var DATA_REMOTE_COMPONENTS_SHARED = "data-remote-components-shared";
2033
+ var TAG_REMOTE_COMPONENT = "remote-component";
2034
+ var NEXT_DATA_ID = "__NEXT_DATA__";
2035
+ var REMOTE_NEXT_DATA_ID = "__REMOTE_NEXT_DATA__";
2036
+ var NEXT_CONTAINER_ID = "__next";
2037
+
2038
+ // src/runtime/html/rewrite-srcset.ts
2039
+ function rewriteSrcset(srcset, base, resolve) {
2040
+ return srcset.split(",").map((entry) => {
2041
+ const [url, descriptor] = entry.trim().split(/\s+/);
2042
+ if (!url)
2043
+ return entry;
2044
+ const absoluteUrl = new URL(url, base).href;
2045
+ const resolvedUrl = resolve ? resolve(absoluteUrl) : absoluteUrl;
2046
+ return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
2047
+ }).join(", ");
2048
+ }
2049
+
2050
+ // src/runtime/html/apply-origin.ts
1860
2051
  function applyOriginToNodes(doc, url, resolveClientUrl) {
1861
2052
  if (url.origin !== location.origin) {
1862
2053
  const nodes = doc.querySelectorAll(
1863
- tagNames.map(
2054
+ ORIGIN_REWRITE_TAGS.map(
1864
2055
  (type) => `${type}[src],${type}[srcset],${type}[href],${type}[imagesrcset]`
1865
2056
  ).join(",")
1866
2057
  );
@@ -1878,29 +2069,17 @@ function applyOriginToNodes(doc, url, resolveClientUrl) {
1878
2069
  );
1879
2070
  }
1880
2071
  if (node.hasAttribute("srcset")) {
1881
- const srcSet = node.getAttribute("srcset")?.split(",").map((entry) => {
1882
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1883
- if (!urlPart)
1884
- return entry;
1885
- const absoluteUrl = new URL(urlPart, url).href;
1886
- const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1887
- return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1888
- }).join(", ");
1889
- if (srcSet) {
1890
- node.setAttribute("srcset", srcSet);
2072
+ const raw = node.getAttribute("srcset");
2073
+ if (raw) {
2074
+ const resolve = resolveClientUrl ? (abs) => resolveClientUrl(abs) ?? abs : void 0;
2075
+ node.setAttribute("srcset", rewriteSrcset(raw, url, resolve));
1891
2076
  }
1892
2077
  }
1893
2078
  if (node.hasAttribute("imagesrcset")) {
1894
- const srcSet = node.getAttribute("imagesrcset")?.split(",").map((entry) => {
1895
- const [urlPart, descriptor] = entry.trim().split(/\s+/);
1896
- if (!urlPart)
1897
- return entry;
1898
- const absoluteUrl = new URL(urlPart, url).href;
1899
- const resolvedUrl = resolveClientUrl?.(absoluteUrl) ?? absoluteUrl;
1900
- return descriptor ? `${resolvedUrl} ${descriptor}` : resolvedUrl;
1901
- }).join(", ");
1902
- if (srcSet) {
1903
- node.setAttribute("imagesrcset", srcSet);
2079
+ const raw = node.getAttribute("imagesrcset");
2080
+ if (raw) {
2081
+ const resolve = resolveClientUrl ? (abs) => resolveClientUrl(abs) ?? abs : void 0;
2082
+ node.setAttribute("imagesrcset", rewriteSrcset(raw, url, resolve));
1904
2083
  }
1905
2084
  }
1906
2085
  });
@@ -1946,26 +2125,28 @@ function buildMetadata(attrs, url) {
1946
2125
  // src/runtime/html/parse-remote-html.ts
1947
2126
  init_error();
1948
2127
  function validateSingleComponent(doc, name, url) {
1949
- 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}"]`)) {
2128
+ if (doc.querySelectorAll(`div[${DATA_BUNDLE}][${DATA_ROUTE}]`).length > 1 && !doc.querySelector(
2129
+ `div[${DATA_BUNDLE}][${DATA_ROUTE}][id^="${name}"]`
2130
+ ) || doc.querySelectorAll(`${TAG_REMOTE_COMPONENT}:not([src])`).length > 1 && !doc.querySelector(`${TAG_REMOTE_COMPONENT}[name="${name}"]`)) {
1950
2131
  throw multipleRemoteComponentsError(url);
1951
2132
  }
1952
2133
  }
1953
2134
  function findComponentElement(doc, name) {
1954
- 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])");
2135
+ 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])`);
1955
2136
  }
1956
2137
  function parseNextData(doc) {
1957
2138
  return JSON.parse(
1958
- (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
2139
+ (doc.querySelector(`#${NEXT_DATA_ID}`) ?? doc.querySelector(`#${REMOTE_NEXT_DATA_ID}`))?.textContent ?? "null"
1959
2140
  );
1960
2141
  }
1961
2142
  function resolveComponentName(component, nextData, fallbackName) {
1962
- const isRemoteComponent = component?.tagName.toLowerCase() === "remote-component";
1963
- const name = component?.getAttribute("id")?.replace(/_ssr$/, "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
2143
+ const isRemoteComponent = component?.tagName.toLowerCase() === TAG_REMOTE_COMPONENT;
2144
+ const name = component?.getAttribute("id")?.replace(new RegExp(`${ID_SUFFIX_SSR}$`), "") || isRemoteComponent && component?.getAttribute("name") || (nextData ? "__next" : fallbackName);
1964
2145
  return { name, isRemoteComponent };
1965
2146
  }
1966
2147
  function extractRemoteShared(doc, name, nextData) {
1967
2148
  const remoteSharedEl = doc.querySelector(
1968
- `#${name}_shared[data-remote-components-shared]`
2149
+ `#${name}${ID_SUFFIX_SHARED}[${DATA_REMOTE_COMPONENTS_SHARED}]`
1969
2150
  );
1970
2151
  const remoteShared = nextData?.props.__REMOTE_COMPONENT__?.shared ?? (JSON.parse(remoteSharedEl?.textContent ?? "{}") ?? {});
1971
2152
  remoteSharedEl?.remove();
@@ -1986,7 +2167,7 @@ function extractLinks(doc, component) {
1986
2167
  function extractScripts(doc, component, isRemoteComponent) {
1987
2168
  return Array.from(
1988
2169
  (isRemoteComponent ? component : doc).querySelectorAll(
1989
- "script[src],script[data-src]"
2170
+ `script[src],script[${DATA_SRC}]`
1990
2171
  )
1991
2172
  );
1992
2173
  }
@@ -1999,15 +2180,15 @@ function parseRemoteComponentDocument(doc, name, url) {
1999
2180
  nextData,
2000
2181
  name
2001
2182
  );
2002
- const rsc = doc.querySelector(`#${resolvedName}_rsc`);
2183
+ const rsc = doc.querySelector(`#${resolvedName}${ID_SUFFIX_RSC}`);
2003
2184
  const metadata = buildMetadata(
2004
2185
  {
2005
2186
  name: resolvedName,
2006
- bundle: component?.getAttribute("data-bundle") || nextData?.props.__REMOTE_COMPONENT__?.bundle,
2007
- route: component?.getAttribute("data-route") ?? nextData?.page,
2008
- runtime: component?.getAttribute("data-runtime") ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? RUNTIME_SCRIPT,
2187
+ bundle: component?.getAttribute(DATA_BUNDLE) || nextData?.props.__REMOTE_COMPONENT__?.bundle,
2188
+ route: component?.getAttribute(DATA_ROUTE) ?? nextData?.page,
2189
+ runtime: component?.getAttribute(DATA_RUNTIME) ?? nextData?.props.__REMOTE_COMPONENT__?.runtime ?? RUNTIME_SCRIPT,
2009
2190
  id: component?.getAttribute("id"),
2010
- type: component?.getAttribute("data-type")
2191
+ type: component?.getAttribute(DATA_TYPE)
2011
2192
  },
2012
2193
  url
2013
2194
  );
@@ -2035,6 +2216,9 @@ function parseRemoteComponentDocument(doc, name, url) {
2035
2216
  };
2036
2217
  }
2037
2218
 
2219
+ // src/host/html/index.tsx
2220
+ init_namespace();
2221
+
2038
2222
  // src/runtime/rsc.ts
2039
2223
  var import_web_streams_polyfill = require("web-streams-polyfill");
2040
2224
  function fixPayload(payload) {
@@ -2055,6 +2239,18 @@ function fixPayload(payload) {
2055
2239
  }
2056
2240
  }
2057
2241
  }
2242
+ function buildRSCChunks(rscName, data) {
2243
+ const chunks = [];
2244
+ for (const chunk of data) {
2245
+ for (const line of chunk.split("\n")) {
2246
+ const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
2247
+ if (match?.groups?.rsc) {
2248
+ chunks.push(JSON.parse(`"${match.groups.rsc}"`));
2249
+ }
2250
+ }
2251
+ }
2252
+ return chunks;
2253
+ }
2058
2254
  function createRSCStream(rscName, data) {
2059
2255
  return new import_web_streams_polyfill.ReadableStream({
2060
2256
  type: "bytes",
@@ -2062,16 +2258,11 @@ function createRSCStream(rscName, data) {
2062
2258
  const encoder = new TextEncoder();
2063
2259
  const self = globalThis;
2064
2260
  if (data.length > 0) {
2065
- data.forEach((chunk) => {
2066
- const lines = chunk.split("\n");
2067
- for (const line of lines) {
2068
- const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
2069
- if (match?.groups?.rsc) {
2070
- self[rscName] = self[rscName] ?? [];
2071
- self[rscName].push(JSON.parse(`"${match.groups.rsc}"`));
2072
- }
2073
- }
2074
- });
2261
+ const parsed = buildRSCChunks(rscName, data);
2262
+ if (parsed.length > 0) {
2263
+ self[rscName] = self[rscName] ?? [];
2264
+ self[rscName].push(...parsed);
2265
+ }
2075
2266
  }
2076
2267
  const allChunks = (self[rscName] ?? [`0:[null]
2077
2268
  `]).join("");
@@ -2447,11 +2638,7 @@ if (typeof HTMLElement !== "undefined") {
2447
2638
  this.name = resolvedName;
2448
2639
  this.bundle = parsedMetadata.bundle;
2449
2640
  if (url) {
2450
- const self2 = globalThis;
2451
- if (!self2.__remote_bundle_url__) {
2452
- self2.__remote_bundle_url__ = {};
2453
- }
2454
- self2.__remote_bundle_url__[this.bundle] = url;
2641
+ getNamespace().bundleUrls[this.bundle] = url;
2455
2642
  }
2456
2643
  const metadataEl = document.createElement("script");
2457
2644
  metadataEl.type = "application/json";
@@ -2475,22 +2662,22 @@ if (typeof HTMLElement !== "undefined") {
2475
2662
  if (this.hostState.prevIsRemoteComponent) {
2476
2663
  if (this.hostState.prevUrl) {
2477
2664
  const prevUrl = this.hostState.prevUrl;
2478
- const self2 = globalThis;
2479
- if (self2.__remote_script_entrypoint_unmount__?.[prevUrl.href]) {
2665
+ const nsUnmount = getNamespace();
2666
+ if (nsUnmount.unmountFns[prevUrl.href]) {
2480
2667
  await Promise.all(
2481
- Array.from(
2482
- self2.__remote_script_entrypoint_unmount__[prevUrl.href] ?? []
2483
- ).map(async (unmount) => {
2484
- try {
2485
- await unmount(this.root);
2486
- } catch (e) {
2487
- logError(
2488
- "HtmlHost",
2489
- `Error while calling unmount() for Remote Component from ${prevUrl.href}.`,
2490
- e
2491
- );
2668
+ Array.from(nsUnmount.unmountFns[prevUrl.href] ?? []).map(
2669
+ async (unmount) => {
2670
+ try {
2671
+ await unmount(this.root);
2672
+ } catch (e) {
2673
+ logError(
2674
+ "HtmlHost",
2675
+ `Error while calling unmount() for Remote Component from ${prevUrl.href}.`,
2676
+ e
2677
+ );
2678
+ }
2492
2679
  }
2493
- })
2680
+ )
2494
2681
  );
2495
2682
  if (!isCurrentLoad()) {
2496
2683
  return abandonLoad();
@@ -2781,21 +2968,21 @@ if (typeof HTMLElement !== "undefined") {
2781
2968
  if (this.fouc) {
2782
2969
  this.root.removeChild(this.fouc);
2783
2970
  }
2784
- } else if (self.__remote_script_entrypoint_mount__?.[url.href]) {
2971
+ } else if (getNamespace().mountFns[url.href]) {
2785
2972
  await Promise.all(
2786
- Array.from(
2787
- self.__remote_script_entrypoint_mount__[url.href] ?? []
2788
- ).map(async (mount) => {
2789
- try {
2790
- await mount(this.root);
2791
- } catch (e) {
2792
- logError(
2793
- "HtmlHost",
2794
- `Error while calling mount() for Remote Component from ${url.href}.`,
2795
- e
2796
- );
2973
+ Array.from(getNamespace().mountFns[url.href] ?? []).map(
2974
+ async (mount) => {
2975
+ try {
2976
+ await mount(this.root);
2977
+ } catch (e) {
2978
+ logError(
2979
+ "HtmlHost",
2980
+ `Error while calling mount() for Remote Component from ${url.href}.`,
2981
+ e
2982
+ );
2983
+ }
2797
2984
  }
2798
- })
2985
+ )
2799
2986
  );
2800
2987
  this.dispatchLifecycleEvent("load", { src: this.src });
2801
2988
  } else {
@@ -2809,14 +2996,9 @@ if (typeof HTMLElement !== "undefined") {
2809
2996
  customElements.define("remote-component", RemoteComponent);
2810
2997
  }
2811
2998
  function registerSharedModules(modules = {}) {
2812
- const self = globalThis;
2813
- if (!self.__remote_component_host_shared_modules__) {
2814
- self.__remote_component_host_shared_modules__ = {};
2815
- }
2999
+ const ns = getNamespace();
2816
3000
  Object.entries(modules).forEach(([key, value]) => {
2817
- if (self.__remote_component_host_shared_modules__) {
2818
- self.__remote_component_host_shared_modules__[key] = value;
2819
- }
3001
+ ns.hostSharedModules[key] = value;
2820
3002
  });
2821
3003
  }
2822
3004
  // Annotate the CommonJS export names for ESM import in node: