remote-components 0.3.1 → 0.3.2

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 (64) hide show
  1. package/dist/config/nextjs.cjs.map +1 -1
  2. package/dist/config/nextjs.js.map +1 -1
  3. package/dist/host/html.cjs +390 -353
  4. package/dist/host/html.cjs.map +1 -1
  5. package/dist/host/html.js +390 -353
  6. package/dist/host/html.js.map +1 -1
  7. package/dist/host/nextjs/app/client-only.cjs +453 -435
  8. package/dist/host/nextjs/app/client-only.cjs.map +1 -1
  9. package/dist/host/nextjs/app/client-only.js +461 -443
  10. package/dist/host/nextjs/app/client-only.js.map +1 -1
  11. package/dist/host/react.cjs +381 -362
  12. package/dist/host/react.cjs.map +1 -1
  13. package/dist/host/react.js +381 -362
  14. package/dist/host/react.js.map +1 -1
  15. package/dist/internal/host/nextjs/image-shared.cjs +2 -2
  16. package/dist/internal/host/nextjs/image-shared.cjs.map +1 -1
  17. package/dist/internal/host/nextjs/image-shared.js +2 -2
  18. package/dist/internal/host/nextjs/image-shared.js.map +1 -1
  19. package/dist/internal/host/server/fetch-remote-component.cjs +20 -1
  20. package/dist/internal/host/server/fetch-remote-component.cjs.map +1 -1
  21. package/dist/internal/host/server/fetch-remote-component.js +20 -1
  22. package/dist/internal/host/server/fetch-remote-component.js.map +1 -1
  23. package/dist/internal/host/shared/remote-image-loader.cjs +2 -5
  24. package/dist/internal/host/shared/remote-image-loader.cjs.map +1 -1
  25. package/dist/internal/host/shared/remote-image-loader.js +2 -5
  26. package/dist/internal/host/shared/remote-image-loader.js.map +1 -1
  27. package/dist/internal/runtime/turbopack/chunk-loader.cjs +76 -126
  28. package/dist/internal/runtime/turbopack/chunk-loader.cjs.map +1 -1
  29. package/dist/internal/runtime/turbopack/chunk-loader.d.ts +22 -7
  30. package/dist/internal/runtime/turbopack/chunk-loader.js +85 -131
  31. package/dist/internal/runtime/turbopack/chunk-loader.js.map +1 -1
  32. package/dist/internal/runtime/turbopack/module.cjs +56 -57
  33. package/dist/internal/runtime/turbopack/module.cjs.map +1 -1
  34. package/dist/internal/runtime/turbopack/module.d.ts +14 -11
  35. package/dist/internal/runtime/turbopack/module.js +54 -56
  36. package/dist/internal/runtime/turbopack/module.js.map +1 -1
  37. package/dist/internal/runtime/turbopack/remote-scope.cjs +101 -0
  38. package/dist/internal/runtime/turbopack/remote-scope.cjs.map +1 -0
  39. package/dist/internal/runtime/turbopack/remote-scope.d.ts +55 -0
  40. package/dist/internal/runtime/turbopack/remote-scope.js +73 -0
  41. package/dist/internal/runtime/turbopack/remote-scope.js.map +1 -0
  42. package/dist/internal/runtime/turbopack/shared-modules.cjs +62 -56
  43. package/dist/internal/runtime/turbopack/shared-modules.cjs.map +1 -1
  44. package/dist/internal/runtime/turbopack/shared-modules.d.ts +7 -3
  45. package/dist/internal/runtime/turbopack/shared-modules.js +63 -57
  46. package/dist/internal/runtime/turbopack/shared-modules.js.map +1 -1
  47. package/dist/internal/runtime/turbopack/webpack-runtime.cjs +39 -29
  48. package/dist/internal/runtime/turbopack/webpack-runtime.cjs.map +1 -1
  49. package/dist/internal/runtime/turbopack/webpack-runtime.d.ts +5 -2
  50. package/dist/internal/runtime/turbopack/webpack-runtime.js +42 -32
  51. package/dist/internal/runtime/turbopack/webpack-runtime.js.map +1 -1
  52. package/dist/internal/runtime/types.cjs.map +1 -1
  53. package/dist/internal/runtime/types.d.ts +4 -3
  54. package/dist/internal/utils/logger.cjs.map +1 -1
  55. package/dist/internal/utils/logger.d.ts +1 -1
  56. package/dist/internal/utils/logger.js.map +1 -1
  57. package/dist/internal/utils.cjs +5 -0
  58. package/dist/internal/utils.cjs.map +1 -1
  59. package/dist/internal/utils.d.ts +15 -1
  60. package/dist/internal/utils.js +4 -0
  61. package/dist/internal/utils.js.map +1 -1
  62. package/dist/remote/html.cjs.map +1 -1
  63. package/dist/remote/html.js.map +1 -1
  64. package/package.json +1 -1
package/dist/host/html.js CHANGED
@@ -191,6 +191,9 @@ var init_logger = __esm({
191
191
  function escapeString(str) {
192
192
  return str.replace(/[^a-z0-9]/g, "_");
193
193
  }
194
+ function computeScopedName(name, options) {
195
+ return options.isCrossOrigin ? `${name}_${escapeString(options.remoteHost.toLowerCase())}` : name;
196
+ }
194
197
  var init_utils = __esm({
195
198
  "src/utils/index.ts"() {
196
199
  "use strict";
@@ -402,11 +405,93 @@ var init_next_client_pages_loader = __esm({
402
405
  }
403
406
  });
404
407
 
405
- // src/host/shared/remote-image-loader.ts
406
- function getRemoteBundleOrigin(bundle) {
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() {
407
424
  const self = globalThis;
408
- return self.__remote_bundle_url__?.[bundle]?.origin ?? "";
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: "" };
409
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
+ // src/host/shared/remote-image-loader.ts
410
495
  function createRemoteImageLoader(bundle, resolveClientUrl) {
411
496
  const loader = Object.assign(
412
497
  ({
@@ -416,7 +501,7 @@ function createRemoteImageLoader(bundle, resolveClientUrl) {
416
501
  quality
417
502
  }) => {
418
503
  const q = quality ?? 75;
419
- const remoteOrigin = getRemoteBundleOrigin(bundle);
504
+ const remoteOrigin = getScope(bundle)?.url.origin ?? "";
420
505
  const isCrossOrigin = remoteOrigin && remoteOrigin !== location.origin;
421
506
  const basePath = isCrossOrigin ? `${remoteOrigin}${config.path ?? "/_next/image"}` : config.path ?? `${remoteOrigin}/_next/image`;
422
507
  const url = `${basePath}?url=${encodeURIComponent(src)}&w=${width}&q=${q}`;
@@ -431,6 +516,7 @@ function createRemoteImageLoader(bundle, resolveClientUrl) {
431
516
  var init_remote_image_loader = __esm({
432
517
  "src/host/shared/remote-image-loader.ts"() {
433
518
  "use strict";
519
+ init_remote_scope();
434
520
  }
435
521
  });
436
522
 
@@ -584,16 +670,6 @@ var init_polyfill = __esm({
584
670
  }
585
671
  });
586
672
 
587
- // src/runtime/patterns.ts
588
- var REMOTE_COMPONENT_REGEX, NEXT_BUNDLE_PATH_RE;
589
- var init_patterns = __esm({
590
- "src/runtime/patterns.ts"() {
591
- "use strict";
592
- REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
593
- NEXT_BUNDLE_PATH_RE = /\/_next\/\[.+\](?:%20| )/;
594
- }
595
- });
596
-
597
673
  // src/runtime/loaders/script-loader.ts
598
674
  async function loadScripts(scripts, resolveClientUrl) {
599
675
  await Promise.all(
@@ -752,62 +828,39 @@ var init_patterns2 = __esm({
752
828
  });
753
829
 
754
830
  // src/runtime/turbopack/chunk-loader.ts
755
- function createChunkLoader(runtime, resolveClientUrl) {
756
- return function __turbopack_chunk_load__(chunkId, scriptBundle) {
757
- logDebug("ChunkLoader", `Loading chunk: "${chunkId}"`);
758
- const self = globalThis;
759
- const {
760
- bundle,
761
- id: path,
762
- prefix
763
- } = REMOTE_COMPONENT_REGEX.exec(chunkId)?.groups ?? {
764
- bundle: scriptBundle ?? "",
765
- id: chunkId
766
- };
767
- logDebug(
768
- "ChunkLoader",
769
- `Parsed chunk - bundle: "${bundle}", path: "${path}", prefix: "${prefix}"`
770
- );
771
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
772
- logDebug("ChunkLoader", `Remote runtime: "${remoteRuntime}"`);
773
- if (remoteRuntime === RUNTIME_WEBPACK) {
774
- logDebug("ChunkLoader", "Skipping chunk load - webpack runtime detected");
775
- return Promise.resolve(void 0);
776
- }
777
- const url = new URL(
778
- path ? `${prefix ?? ""}${path}`.replace(
779
- /(?<char>[^:])(?<double>\/\/)/g,
780
- "$1/"
781
- ) : "/",
782
- self.__remote_bundle_url__?.[bundle ?? "default"] ?? new URL(location.origin)
783
- ).href;
784
- if (url.endsWith(".css")) {
785
- logDebug("ChunkLoader", `Skipping CSS file: "${url}"`);
786
- return;
787
- }
788
- if (!self.__remote_components_turbopack_chunk_loader_promise__) {
789
- self.__remote_components_turbopack_chunk_loader_promise__ = {};
790
- }
791
- if (self.__remote_components_turbopack_chunk_loader_promise__[url]) {
792
- logDebug("ChunkLoader", `Returning cached promise for: "${url}"`);
793
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
794
- }
795
- const resolvedUrl = resolveClientUrl?.(url) ?? url;
796
- logDebug("ChunkLoader", `Fetching chunk from: "${resolvedUrl}"`);
797
- self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise((resolve, reject) => {
831
+ function loadChunkWithScope(scope, chunkId) {
832
+ logDebug(
833
+ "ChunkLoader",
834
+ `loadChunkWithScope: "${chunkId}" (scope: "${scope.scopedName}")`
835
+ );
836
+ const self = globalThis;
837
+ const { bundle, path, prefix } = parseRemoteId(chunkId);
838
+ const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : scope.runtime;
839
+ if (remoteRuntime === RUNTIME_WEBPACK) {
840
+ return Promise.resolve(void 0);
841
+ }
842
+ const rawPath = path ? collapseDoubleSlashes(`${prefix}${path}`) : "/";
843
+ const url = new URL(rawPath, scope.url).href;
844
+ if (url.endsWith(".css")) {
845
+ return;
846
+ }
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];
852
+ }
853
+ const resolvedUrl = scope.resolveClientUrl?.(url) ?? url;
854
+ if (resolvedUrl !== url) {
855
+ logDebug("ChunkLoader", `Proxied chunk URL: "${url}" \u2192 "${resolvedUrl}"`);
856
+ }
857
+ self.__remote_components_turbopack_chunk_loader_promise__[url] = new Promise(
858
+ (resolve, reject) => {
798
859
  fetch(resolvedUrl).then((res) => res.text()).then((code) => {
799
860
  const hasTurbopack = TURBOPACK_GLOBAL_RE.test(code);
800
861
  if (hasTurbopack) {
801
- return handleTurbopackChunk(code, bundle ?? "", url);
862
+ return handleTurbopackChunk(code, scope, url);
802
863
  }
803
- logDebug(
804
- "ChunkLoader",
805
- `Chunk does not contain globalThis.TURBOPACK or self.TURBOPACK: "${url}"`
806
- );
807
- logDebug(
808
- "ChunkLoader",
809
- `First 500 chars of chunk: ${code.slice(0, 500)}`
810
- );
811
864
  }).then(resolve).catch((error) => {
812
865
  const isProxied = isProxiedUrl(resolvedUrl);
813
866
  if (isProxied) {
@@ -817,17 +870,29 @@ function createChunkLoader(runtime, resolveClientUrl) {
817
870
  reject(error);
818
871
  }
819
872
  });
820
- });
821
- return self.__remote_components_turbopack_chunk_loader_promise__[url];
873
+ }
874
+ );
875
+ return self.__remote_components_turbopack_chunk_loader_promise__[url];
876
+ }
877
+ function createChunkDispatcher() {
878
+ return function __chunk_dispatcher__(chunkId, scriptBundle) {
879
+ logDebug("ChunkDispatcher", `Dispatching chunk: "${chunkId}"`);
880
+ const { bundle } = parseRemoteId(chunkId);
881
+ const bundleName = bundle || scriptBundle || "default";
882
+ const scope = getScope(bundleName);
883
+ logDebug(
884
+ "ChunkDispatcher",
885
+ `Scope resolution: bundle="${bundleName}", scope=${scope?.scopedName ?? "null"}`
886
+ );
887
+ if (!scope) {
888
+ logWarn("ChunkDispatcher", `No scope found for bundle "${bundleName}"`);
889
+ return Promise.resolve(void 0);
890
+ }
891
+ return loadChunkWithScope(scope, chunkId);
822
892
  };
823
893
  }
824
- async function handleTurbopackChunk(code, bundle, url) {
825
- logDebug(
826
- "ChunkLoader",
827
- `Handling Turbopack chunk - bundle: "${bundle}", url: "${url}"`
828
- );
894
+ async function handleTurbopackChunk(code, scope, url) {
829
895
  if (/importScripts\(\.\.\.self.TURBOPACK_NEXT_CHUNK_URLS/.test(code)) {
830
- logDebug("ChunkLoader", `Skipping worker chunk: "${url}"`);
831
896
  const preloadLinks = document.querySelectorAll(
832
897
  `link[rel="preload"][href="${new URL(url).pathname}"]`
833
898
  );
@@ -835,58 +900,41 @@ async function handleTurbopackChunk(code, bundle, url) {
835
900
  return;
836
901
  }
837
902
  const self = globalThis;
838
- const bundleKey = getBundleKey(bundle);
839
- logDebug("ChunkLoader", `Bundle key: "${bundleKey}"`);
903
+ const { globalKey } = scope;
840
904
  const transformedCode = code.replace(
841
905
  /globalThis\[\s*["']TURBOPACK["']\s*\]/g,
842
- `globalThis["TURBOPACK_${bundleKey}"]`
906
+ `globalThis["TURBOPACK_${globalKey}"]`
843
907
  ).replace(
844
908
  /self\[\s*["']TURBOPACK["']\s*\]/g,
845
- `self["TURBOPACK_${bundleKey}"]`
846
- ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${bundleKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${bundleKey}`).replace(
909
+ `self["TURBOPACK_${globalKey}"]`
910
+ ).replace(/globalThis\.TURBOPACK/g, `globalThis.TURBOPACK_${globalKey}`).replace(/self\.TURBOPACK(?!_)/g, `self.TURBOPACK_${globalKey}`).replace(
847
911
  /TURBOPACK_WORKER_LOCATION/g,
848
- `TURBOPACK_WORKER_LOCATION_${bundleKey}`
912
+ `TURBOPACK_WORKER_LOCATION_${globalKey}`
849
913
  ).replace(
850
914
  /TURBOPACK_NEXT_CHUNK_URLS/g,
851
- `TURBOPACK_NEXT_CHUNK_URLS_${bundleKey}`
915
+ `TURBOPACK_NEXT_CHUNK_URLS_${globalKey}`
852
916
  ).replace(
853
917
  /TURBOPACK_CHUNK_UPDATE_LISTENERS/g,
854
- `TURBOPACK_CHUNK_UPDATE_LISTENERS_${bundleKey}`
855
- ).replace(/__next_require__/g, `__${bundleKey}_next_require__`).replace(
918
+ `TURBOPACK_CHUNK_UPDATE_LISTENERS_${globalKey}`
919
+ ).replace(/__next_require__/g, `__${globalKey}_next_require__`).replace(
856
920
  /\/\/# sourceMappingURL=(?<name>.+)(?<optional>\._)?\.js\.map/g,
857
- `//# sourceMappingURL=${new URL(
858
- ".",
859
- new URL(
860
- url,
861
- self.__remote_bundle_url__?.[bundle] ?? new URL(location.origin)
862
- )
863
- ).href}$1$2.js.map`
921
+ `//# sourceMappingURL=${new URL(".", new URL(url, scope.url)).href}$1$2.js.map`
864
922
  );
865
- if (!self[`TURBOPACK_${bundleKey}`]) {
923
+ if (!self[`TURBOPACK_${globalKey}`]) {
866
924
  const chunkData = [];
867
925
  const turbopackObject = {
868
926
  push: (item) => {
869
- logDebug(
870
- "ChunkLoader",
871
- `TURBOPACK_${bundleKey}.push() called with item type: ${Array.isArray(item) ? "array" : typeof item}`
872
- );
873
927
  if (Array.isArray(item)) {
874
928
  chunkData.push(item);
875
- logDebug(
876
- "ChunkLoader",
877
- `TURBOPACK_${bundleKey} now has ${chunkData.length} chunks`
878
- );
879
929
  } else {
880
930
  chunkData.push([item]);
881
931
  }
882
932
  return chunkData.length;
883
933
  },
884
- // Store chunks for later access
885
934
  __chunks__: chunkData
886
935
  };
887
- self[`TURBOPACK_${bundleKey}`] = turbopackObject;
936
+ self[`TURBOPACK_${globalKey}`] = turbopackObject;
888
937
  }
889
- logDebug("ChunkLoader", `Creating blob script for: "${url}"`);
890
938
  await new Promise((scriptResolve, scriptReject) => {
891
939
  const blob = new Blob([transformedCode], {
892
940
  type: "application/javascript; charset=UTF-8"
@@ -898,21 +946,6 @@ async function handleTurbopackChunk(code, bundle, url) {
898
946
  script.async = true;
899
947
  script.onload = () => {
900
948
  URL.revokeObjectURL(scriptUrl);
901
- logDebug(
902
- "ChunkLoader",
903
- `Script loaded successfully for bundle "${bundle}"`
904
- );
905
- const turbopackBundle = self[`TURBOPACK_${bundleKey}`];
906
- logDebug(
907
- "ChunkLoader",
908
- `TURBOPACK_${bundleKey} type: ${typeof turbopackBundle}, isArray: ${Array.isArray(turbopackBundle)}, keys: ${turbopackBundle ? Object.keys(turbopackBundle).slice(0, 10).join(", ") : "none"}`
909
- );
910
- if (turbopackBundle && typeof turbopackBundle === "object") {
911
- logDebug(
912
- "ChunkLoader",
913
- `TURBOPACK_${bundleKey} length/size: ${Array.isArray(turbopackBundle) ? turbopackBundle.length : Object.keys(turbopackBundle).length}`
914
- );
915
- }
916
949
  scriptResolve(void 0);
917
950
  script.remove();
918
951
  };
@@ -927,35 +960,25 @@ async function handleTurbopackChunk(code, bundle, url) {
927
960
  };
928
961
  document.head.appendChild(script);
929
962
  });
930
- const chunkLists = self[`TURBOPACK_${bundleKey}_CHUNK_LISTS`];
931
- logDebug(
932
- "ChunkLoader",
933
- `Processing chunk lists for bundle "${bundle}": ${chunkLists?.length ?? 0} lists`
934
- );
935
- const loadChunkLists = [];
963
+ const chunkLists = self[`TURBOPACK_${globalKey}_CHUNK_LISTS`];
964
+ const loadChunkPromises = [];
936
965
  while (chunkLists?.length) {
937
966
  const { chunks } = chunkLists.shift() ?? { chunks: [] };
938
967
  if (chunks.length > 0) {
939
- logDebug(
940
- "ChunkLoader",
941
- `Loading ${chunks.length} additional chunks for bundle "${bundle}": [${chunks.join(", ")}]`
942
- );
943
- chunks.forEach((id) => {
944
- const chunkLoadResult = self.__webpack_chunk_load__?.(
945
- `[${bundle}] ${url.slice(0, url.indexOf("/_next"))}/_next/${id}`
968
+ for (const id of chunks) {
969
+ const baseUrl = url.slice(0, url.indexOf("/_next"));
970
+ const chunkLoadResult = loadChunkWithScope(
971
+ scope,
972
+ formatRemoteId(scope, `${baseUrl}/_next/${id}`)
946
973
  );
947
974
  if (chunkLoadResult) {
948
- loadChunkLists.push(chunkLoadResult);
975
+ loadChunkPromises.push(chunkLoadResult);
949
976
  }
950
- });
977
+ }
951
978
  }
952
979
  }
953
- if (loadChunkLists.length > 0) {
954
- logDebug(
955
- "ChunkLoader",
956
- `Waiting for ${loadChunkLists.length} additional chunks to load`
957
- );
958
- await Promise.all(loadChunkLists);
980
+ if (loadChunkPromises.length > 0) {
981
+ await Promise.all(loadChunkPromises);
959
982
  }
960
983
  }
961
984
  var init_chunk_loader = __esm({
@@ -967,14 +990,169 @@ var init_chunk_loader = __esm({
967
990
  init_error();
968
991
  init_logger();
969
992
  init_patterns2();
993
+ init_remote_scope();
994
+ }
995
+ });
996
+
997
+ // src/runtime/turbopack/shared-modules.ts
998
+ function getTurbopackModules(scope) {
999
+ const self = globalThis;
1000
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
1001
+ if (!raw)
1002
+ return void 0;
1003
+ if (typeof raw === "object" && "__chunks__" in raw) {
1004
+ return raw.__chunks__.flat();
1005
+ }
1006
+ if (Array.isArray(raw)) {
1007
+ return raw.flat();
1008
+ }
1009
+ return Object.entries(raw).flat();
1010
+ }
1011
+ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}) {
1012
+ const allModules = getTurbopackModules(scope);
1013
+ logDebug(
1014
+ "SharedModules",
1015
+ `initializeSharedModules: scope="${scope.scopedName}", allModules=${allModules ? allModules.length : "null"}, hostShared=[${Object.keys(hostShared).join(", ")}], remoteShared=${JSON.stringify(remoteShared)}`
1016
+ );
1017
+ let sharedModuleInitializer = null;
1018
+ if (allModules) {
1019
+ const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1020
+ if (typeof idOrFunc !== "function") {
1021
+ return false;
1022
+ }
1023
+ const funcCode = idOrFunc.toString();
1024
+ return REMOTE_SHARED_MARKER_RE.test(funcCode);
1025
+ });
1026
+ if (sharedModuleInitializerIndex > 0) {
1027
+ const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1028
+ const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1029
+ const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1030
+ if (sharedModuleId) {
1031
+ const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1032
+ scope,
1033
+ sharedModuleId,
1034
+ formatRemoteId(scope, String(sharedModuleInitializerId))
1035
+ );
1036
+ sharedModuleInitializer = sharedModuleInitializerInstance;
1037
+ }
1038
+ }
1039
+ if (sharedModuleInitializer) {
1040
+ const { shared } = await sharedModuleInitializer;
1041
+ const sharedModuleIds = extractSharedModuleIds(shared, scope);
1042
+ logDebug(
1043
+ "SharedModules",
1044
+ `Resolved shared modules for scope="${scope.scopedName}": ${JSON.stringify(sharedModuleIds)}`
1045
+ );
1046
+ return Promise.all(
1047
+ Object.entries(sharedModuleIds).map(async ([id, module]) => {
1048
+ if (hostShared[module]) {
1049
+ scope.sharedModules[id] = await hostShared[module](scope.name);
1050
+ } else {
1051
+ logError(
1052
+ "SharedModules",
1053
+ `Host shared module "${module}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1054
+ );
1055
+ }
1056
+ })
1057
+ );
1058
+ }
1059
+ logWarn(
1060
+ "SharedModules",
1061
+ `No shared module initializer found in bundle for scope="${scope.scopedName}" \u2014 falling back to remoteShared mapping`
1062
+ );
1063
+ } else {
1064
+ logWarn(
1065
+ "SharedModules",
1066
+ `No TURBOPACK modules found for scope="${scope.scopedName}" (TURBOPACK_${scope.globalKey} is empty)`
1067
+ );
1068
+ }
1069
+ return Promise.all(
1070
+ Object.entries(remoteShared).map(async ([id, module]) => {
1071
+ if (hostShared[module]) {
1072
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
1073
+ scope.sharedModules[normalizedId] = await hostShared[module](
1074
+ scope.name
1075
+ );
1076
+ } else {
1077
+ logError(
1078
+ "SharedModules",
1079
+ `Shared module "${module}" not found for "${scope.name}". ${DEDUPLICATION_WARNING}`
1080
+ );
1081
+ }
1082
+ })
1083
+ );
1084
+ }
1085
+ function extractSharedModuleIds(shared, scope) {
1086
+ return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1087
+ const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1088
+ if (asyncSharedModuleId) {
1089
+ const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1090
+ let asyncSharedModule;
1091
+ const turbopackModules = getTurbopackModules(scope) ?? [];
1092
+ const asyncSharedModuleIdIndex = turbopackModules.indexOf(
1093
+ asyncSharedModuleIdNumber
1094
+ );
1095
+ if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
1096
+ asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
1097
+ }
1098
+ if (asyncSharedModule) {
1099
+ const asyncSharedModuleCode = asyncSharedModule.toString();
1100
+ const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1101
+ acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1102
+ "__remote_shared_module_",
1103
+ ""
1104
+ );
1105
+ }
1106
+ }
1107
+ return acc;
1108
+ }, {});
1109
+ }
1110
+ function getSharedModule(scope, id) {
1111
+ const idStr = String(id);
1112
+ if (scope.sharedModules[idStr] !== void 0) {
1113
+ return scope.sharedModules[idStr];
1114
+ }
1115
+ for (const [key, value] of Object.entries(scope.sharedModules)) {
1116
+ if (typeof value !== "undefined" && idStr !== key && idStr.endsWith(key)) {
1117
+ return value;
1118
+ }
1119
+ }
1120
+ return null;
1121
+ }
1122
+ var DEDUPLICATION_WARNING;
1123
+ var init_shared_modules = __esm({
1124
+ "src/runtime/turbopack/shared-modules.ts"() {
1125
+ "use strict";
1126
+ init_logger();
1127
+ init_module();
1128
+ init_patterns2();
1129
+ init_remote_scope();
1130
+ DEDUPLICATION_WARNING = "This module will not be deduplicated \u2014 the remote may load its own copy, which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).";
970
1131
  }
971
1132
  });
972
1133
 
973
1134
  // src/runtime/turbopack/module.ts
974
- function handleTurbopackModule(bundle, moduleId, id) {
1135
+ function requireModule(scope, moduleId, fullId) {
1136
+ const idStr = String(moduleId);
1137
+ if (scope.moduleCache[idStr])
1138
+ return scope.moduleCache[idStr];
1139
+ const sharedModule = getSharedModule(scope, moduleId);
1140
+ if (sharedModule) {
1141
+ scope.moduleCache[idStr] = sharedModule;
1142
+ return sharedModule;
1143
+ }
1144
+ return handleTurbopackModule(
1145
+ scope,
1146
+ idStr,
1147
+ fullId ?? formatRemoteId(scope, idStr)
1148
+ );
1149
+ }
1150
+ function handleTurbopackModule(scope, moduleId, id) {
975
1151
  const self = globalThis;
976
- const bundleKey = getBundleKey(bundle);
977
- const raw = self[`TURBOPACK_${bundleKey}`];
1152
+ if (scope.moduleCache[moduleId]) {
1153
+ return scope.moduleCache[moduleId];
1154
+ }
1155
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
978
1156
  let modules;
979
1157
  if (raw && typeof raw === "object" && "__chunks__" in raw) {
980
1158
  modules = raw.__chunks__.flat();
@@ -983,48 +1161,35 @@ function handleTurbopackModule(bundle, moduleId, id) {
983
1161
  } else {
984
1162
  modules = raw;
985
1163
  }
986
- if (!self.__remote_components_turbopack_modules__) {
987
- self.__remote_components_turbopack_modules__ = {};
988
- }
989
- if (!self.__remote_components_turbopack_modules__[bundle]) {
990
- self.__remote_components_turbopack_modules__[bundle] = {};
991
- }
992
- if (self.__remote_components_turbopack_modules__[bundle][moduleId]) {
993
- return self.__remote_components_turbopack_modules__[bundle][moduleId];
994
- }
995
1164
  if (!modules) {
996
- logError("TurbopackModule", `TURBOPACK_${bundleKey} is undefined`);
1165
+ logError(
1166
+ "TurbopackModule",
1167
+ `TURBOPACK_${scope.globalKey} is undefined (scope: "${scope.scopedName}")`
1168
+ );
997
1169
  }
998
1170
  const moduleInit = findModuleInit(modules, moduleId);
999
1171
  const exports = {};
1000
1172
  const moduleExports = { exports };
1001
1173
  if (typeof moduleInit !== "function") {
1002
1174
  throw new Error(
1003
- `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
1175
+ `Module ${id} not found in bundle ${scope.name} with id ${moduleId}`
1004
1176
  );
1005
1177
  }
1006
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1007
- if (!self.__remote_components_turbopack_global__) {
1008
- self.__remote_components_turbopack_global__ = {};
1009
- }
1010
- if (!self.__remote_components_turbopack_global__[bundle]) {
1011
- self.__remote_components_turbopack_global__[bundle] = {};
1012
- }
1178
+ scope.moduleCache[moduleId] = moduleExports.exports;
1013
1179
  moduleInit(
1014
1180
  createTurbopackContext(
1015
- bundle,
1181
+ scope,
1016
1182
  exports,
1017
1183
  moduleExports,
1018
1184
  modules,
1019
1185
  moduleInit,
1020
- id,
1021
- self
1186
+ id
1022
1187
  ),
1023
1188
  moduleExports,
1024
1189
  exports
1025
1190
  );
1026
- if (self.__remote_components_turbopack_modules__[bundle][moduleId] !== moduleExports.exports) {
1027
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
1191
+ if (scope.moduleCache[moduleId] !== moduleExports.exports) {
1192
+ scope.moduleCache[moduleId] = moduleExports.exports;
1028
1193
  }
1029
1194
  return moduleExports.exports;
1030
1195
  }
@@ -1049,7 +1214,8 @@ function findModuleInit(modules, moduleId) {
1049
1214
  (e) => Boolean(e && typeof e === "object" && moduleId in e)
1050
1215
  )?.[moduleId];
1051
1216
  }
1052
- function createTurbopackContext(bundle, exports, moduleExports, modules, moduleInit, id, self) {
1217
+ function createTurbopackContext(scope, exports, moduleExports, modules, moduleInit, id) {
1218
+ const scopedRequire = (moduleId) => requireModule(scope, moduleId, formatRemoteId(scope, String(moduleId)));
1053
1219
  return {
1054
1220
  // HMR not implemented for Remote Components
1055
1221
  k: {
@@ -1065,16 +1231,10 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1065
1231
  s(bindings, esmId) {
1066
1232
  let mod = exports;
1067
1233
  if (typeof esmId === "string" || typeof esmId === "number") {
1068
- if (!self.__remote_components_turbopack_modules__) {
1069
- self.__remote_components_turbopack_modules__ = {};
1070
- }
1071
- if (!self.__remote_components_turbopack_modules__[bundle]) {
1072
- self.__remote_components_turbopack_modules__[bundle] = {};
1234
+ if (!scope.moduleCache[esmId]) {
1235
+ scope.moduleCache[esmId] = {};
1073
1236
  }
1074
- if (!self.__remote_components_turbopack_modules__[bundle][esmId]) {
1075
- self.__remote_components_turbopack_modules__[bundle][esmId] = {};
1076
- }
1077
- mod = self.__remote_components_turbopack_modules__[bundle][esmId];
1237
+ mod = scope.moduleCache[esmId];
1078
1238
  }
1079
1239
  Object.defineProperty(mod, "__esModule", { value: true });
1080
1240
  if (Array.isArray(bindings)) {
@@ -1107,7 +1267,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1107
1267
  }
1108
1268
  }
1109
1269
  },
1110
- // import
1270
+ // import — resolves directly via scope, no global dispatch
1111
1271
  i(importId) {
1112
1272
  let mod;
1113
1273
  if (typeof importId === "string") {
@@ -1118,7 +1278,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1118
1278
  /\s+<export(?<specifier>.*)>$/,
1119
1279
  ""
1120
1280
  );
1121
- mod = self.__webpack_require__?.(`[${bundle}] ${normalizedId}`);
1281
+ mod = scopedRequire(normalizedId);
1122
1282
  if (mod && exportSource && exportName && (exportSource === "*" || typeof mod[exportSource] !== "undefined") && typeof mod[exportName] === "undefined") {
1123
1283
  if (exportSource === "*") {
1124
1284
  mod[exportName] = mod;
@@ -1127,7 +1287,7 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1127
1287
  }
1128
1288
  }
1129
1289
  } else {
1130
- mod = self.__webpack_require__?.(`[${bundle}] ${importId}`);
1290
+ mod = scopedRequire(importId);
1131
1291
  }
1132
1292
  if (typeof mod !== "object" || mod === null) {
1133
1293
  mod = { default: mod };
@@ -1139,16 +1299,14 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1139
1299
  }
1140
1300
  return mod;
1141
1301
  },
1142
- // require
1302
+ // require — resolves directly via scope
1143
1303
  r(requireId) {
1144
- return self.__webpack_require__?.(`[${bundle}] ${requireId}`);
1304
+ return scopedRequire(requireId);
1145
1305
  },
1146
1306
  // value exports
1147
1307
  v(value) {
1148
1308
  if (typeof value === "function") {
1149
- exports.default = value((vid) => {
1150
- return self.__webpack_require__?.(`[${bundle}] ${vid}`);
1151
- });
1309
+ exports.default = value((vid) => scopedRequire(vid));
1152
1310
  } else {
1153
1311
  moduleExports.exports = value;
1154
1312
  }
@@ -1163,17 +1321,15 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1163
1321
  );
1164
1322
  exports.default = result;
1165
1323
  },
1166
- // async module loader
1324
+ // async module loader — resolves directly via scope
1167
1325
  async A(Aid) {
1168
- const mod = self.__webpack_require__?.(`[${bundle}] ${Aid}`);
1169
- return mod.default(
1170
- (parentId) => self.__webpack_require__?.(`[${bundle}] ${parentId}`)
1171
- );
1326
+ const mod = scopedRequire(Aid);
1327
+ return mod.default((parentId) => scopedRequire(parentId));
1172
1328
  },
1173
1329
  // dynamic import tracking — no-op for remote components
1174
1330
  j() {
1175
1331
  },
1176
- // chunk loader
1332
+ // chunk loader — loads directly via scope, no global dispatch
1177
1333
  l(url) {
1178
1334
  const flatModules = Array.isArray(modules) ? modules : [];
1179
1335
  const moduleInitIndex = flatModules.indexOf(moduleInit);
@@ -1184,16 +1340,16 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1184
1340
  const scriptSrc = script.getAttribute("data-turbopack-src") || "";
1185
1341
  const nextIndex = scriptSrc.indexOf("/_next");
1186
1342
  const baseUrl = nextIndex !== -1 ? scriptSrc.slice(0, nextIndex) : "";
1187
- const bundleUrl = `[${bundle}] ${baseUrl}/_next/${url}`;
1188
- return self.__webpack_chunk_load__?.(bundleUrl, bundle);
1343
+ const chunkUrl = `${baseUrl}/_next/${url}`;
1344
+ return loadChunkWithScope(scope, formatRemoteId(scope, chunkUrl));
1189
1345
  }
1190
1346
  }
1191
1347
  throw new Error(
1192
1348
  `Failed to load Turbopack chunk "${url}" for module "${id}". Check the URL is correct.`
1193
1349
  );
1194
1350
  },
1195
- // global object for this bundle
1196
- g: self.__remote_components_turbopack_global__?.[bundle],
1351
+ // globalThis substitute shared across all modules in this scope
1352
+ g: scope.moduleGlobal,
1197
1353
  m: moduleExports,
1198
1354
  e: exports
1199
1355
  };
@@ -1201,169 +1357,49 @@ function createTurbopackContext(bundle, exports, moduleExports, modules, moduleI
1201
1357
  var init_module = __esm({
1202
1358
  "src/runtime/turbopack/module.ts"() {
1203
1359
  "use strict";
1204
- init_constants2();
1205
1360
  init_logger();
1206
- }
1207
- });
1208
-
1209
- // src/runtime/turbopack/shared-modules.ts
1210
- async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
1211
- const self = globalThis;
1212
- self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};
1213
- if (!self.__remote_shared_modules__[bundle]) {
1214
- self.__remote_shared_modules__[bundle] = {};
1215
- }
1216
- const bundleKey = getBundleKey(bundle);
1217
- let modules = self[`TURBOPACK_${bundleKey}`];
1218
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
1219
- const chunks = modules.__chunks__;
1220
- modules = chunks.flat();
1221
- }
1222
- let sharedModuleInitializer = null;
1223
- if (modules) {
1224
- const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
1225
- const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
1226
- if (typeof idOrFunc !== "function") {
1227
- return false;
1228
- }
1229
- const funcCode = idOrFunc.toString();
1230
- return REMOTE_SHARED_MARKER_RE.test(funcCode);
1231
- });
1232
- if (sharedModuleInitializerIndex > 0) {
1233
- const sharedModuleInitializerCode = allModules[sharedModuleInitializerIndex].toString();
1234
- const sharedModuleInitializerId = allModules[sharedModuleInitializerIndex - 1];
1235
- const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
1236
- if (sharedModuleId) {
1237
- const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
1238
- bundle,
1239
- sharedModuleId,
1240
- `[${bundle}] ${sharedModuleInitializerId}`
1241
- );
1242
- sharedModuleInitializer = sharedModuleInitializerInstance;
1243
- }
1244
- }
1245
- if (sharedModuleInitializer) {
1246
- const { shared } = await sharedModuleInitializer;
1247
- const sharedModuleIds = extractSharedModuleIds(
1248
- shared,
1249
- bundleKey,
1250
- self
1251
- );
1252
- return Promise.all(
1253
- Object.entries(sharedModuleIds).map(async ([id, module]) => {
1254
- if (self.__remote_shared_modules__?.[bundle]) {
1255
- if (hostShared[module]) {
1256
- self.__remote_shared_modules__[bundle][id] = await hostShared[module](bundle);
1257
- } else {
1258
- logError(
1259
- "SharedModules",
1260
- `Host shared module "${module}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
1261
- );
1262
- }
1263
- }
1264
- })
1265
- );
1266
- }
1267
- }
1268
- return Promise.all(
1269
- Object.entries(remoteShared).map(async ([id, module]) => {
1270
- if (self.__remote_shared_modules__?.[bundle]) {
1271
- if (hostShared[module]) {
1272
- const normalizedId = id.replace("[app-ssr]", "[app-client]");
1273
- self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module](bundle);
1274
- } else {
1275
- logError(
1276
- "SharedModules",
1277
- `Shared module "${module}" not found for "${bundle}". ${DEDUPLICATION_WARNING}`
1278
- );
1279
- }
1280
- }
1281
- })
1282
- );
1283
- }
1284
- function extractSharedModuleIds(shared, bundleKey, self) {
1285
- return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
1286
- const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
1287
- if (asyncSharedModuleId) {
1288
- const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
1289
- let asyncSharedModule;
1290
- let turbopackModules = self[`TURBOPACK_${bundleKey}`];
1291
- if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
1292
- const chunks = turbopackModules.__chunks__;
1293
- turbopackModules = chunks.flat();
1294
- }
1295
- const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
1296
- key2,
1297
- value2
1298
- ]) : [];
1299
- const asyncSharedModuleIdIndex = newAllModules.indexOf(
1300
- asyncSharedModuleIdNumber
1301
- );
1302
- if (asyncSharedModuleIdIndex !== -1 && typeof newAllModules[asyncSharedModuleIdIndex + 1] === "function") {
1303
- asyncSharedModule = newAllModules[asyncSharedModuleIdIndex + 1];
1304
- }
1305
- if (asyncSharedModule) {
1306
- const asyncSharedModuleCode = asyncSharedModule.toString();
1307
- const { sharedModuleId } = ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ?? ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ?? {};
1308
- acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(
1309
- "__remote_shared_module_",
1310
- ""
1311
- );
1312
- }
1313
- }
1314
- return acc;
1315
- }, {});
1316
- }
1317
- function getSharedModule(bundle, id) {
1318
- const self = globalThis;
1319
- for (const [key, value] of Object.entries(
1320
- self.__remote_shared_modules__?.[bundle] ?? {}
1321
- )) {
1322
- if (typeof value !== "undefined" && (typeof id === "string" && id.includes(key) || id === key)) {
1323
- return value;
1324
- }
1325
- }
1326
- return null;
1327
- }
1328
- var DEDUPLICATION_WARNING;
1329
- var init_shared_modules = __esm({
1330
- "src/runtime/turbopack/shared-modules.ts"() {
1331
- "use strict";
1332
- init_constants2();
1333
- init_logger();
1334
- init_module();
1335
- init_patterns2();
1336
- DEDUPLICATION_WARNING = "This module will not be deduplicated \u2014 the remote may load its own copy, which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).";
1361
+ init_chunk_loader();
1362
+ init_remote_scope();
1363
+ init_shared_modules();
1337
1364
  }
1338
1365
  });
1339
1366
 
1340
1367
  // src/runtime/turbopack/webpack-runtime.ts
1341
1368
  async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location.href), bundle, shared = {}, remoteShared = {}, resolveClientUrl) {
1342
1369
  const self = globalThis;
1370
+ const bundleName = bundle ?? "default";
1371
+ const scope = createScope(bundleName, url, runtime, resolveClientUrl);
1372
+ registerScope(scope);
1343
1373
  if (!self.__remote_bundle_url__) {
1344
1374
  self.__remote_bundle_url__ = {};
1345
1375
  }
1346
- self.__remote_bundle_url__[bundle ?? "default"] = url;
1376
+ self.__remote_bundle_url__[bundleName] = url;
1377
+ if (scope.scopedName !== bundleName) {
1378
+ self.__remote_bundle_url__[scope.scopedName] = url;
1379
+ }
1347
1380
  self.__webpack_get_script_filename__ = () => null;
1348
- if (typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack") {
1381
+ const willCreateDispatchers = typeof self.__webpack_require__ !== "function" || self.__webpack_require_type__ !== "turbopack";
1382
+ if (willCreateDispatchers) {
1349
1383
  if (!self.__original_webpack_require__ && !self.__original_webpack_chunk_load__) {
1350
1384
  self.__original_webpack_chunk_load__ = self.__webpack_chunk_load__;
1351
1385
  self.__original_webpack_require__ = self.__webpack_require__;
1352
1386
  }
1353
- self.__webpack_chunk_load__ = createChunkLoader(runtime, resolveClientUrl);
1354
- self.__webpack_require__ = createModuleRequire(runtime);
1387
+ self.__webpack_chunk_load__ = createChunkDispatcher();
1388
+ self.__webpack_require__ = createModuleDispatcher(runtime);
1355
1389
  self.__webpack_require_type__ = runtime;
1356
1390
  if (self.__remote_webpack_require__ && runtime === RUNTIME_TURBOPACK) {
1357
- const remoteBundle = bundle ?? "default";
1358
- self.__remote_webpack_require__[remoteBundle] = self.__webpack_require__;
1359
- self.__remote_webpack_require__[remoteBundle].type = "turbopack";
1391
+ self.__remote_webpack_require__[bundleName] = self.__webpack_require__;
1392
+ self.__remote_webpack_require__[bundleName].type = "turbopack";
1360
1393
  }
1361
1394
  }
1395
+ if (self.__remote_webpack_require__?.[bundleName] && scope.scopedName !== bundleName) {
1396
+ self.__remote_webpack_require__[scope.scopedName] = self.__remote_webpack_require__[bundleName];
1397
+ }
1362
1398
  if (runtime === RUNTIME_TURBOPACK) {
1363
1399
  const results = await Promise.allSettled(
1364
1400
  scripts.map((script) => {
1365
1401
  if (script.src) {
1366
- return self.__webpack_chunk_load__?.(script.src, bundle);
1402
+ return loadChunkWithScope(scope, script.src);
1367
1403
  }
1368
1404
  return Promise.resolve(void 0);
1369
1405
  })
@@ -1385,54 +1421,54 @@ async function setupWebpackRuntime(runtime, scripts = [], url = new URL(location
1385
1421
  "react-dom/client": async () => (await import("react-dom/client")).default,
1386
1422
  ...shared
1387
1423
  };
1388
- await initializeSharedModules(
1389
- bundle ?? "default",
1390
- // include all core modules as shared
1391
- coreShared,
1392
- remoteShared
1393
- );
1424
+ await initializeSharedModules(scope, coreShared, remoteShared);
1394
1425
  }
1395
- function createModuleRequire(runtime) {
1426
+ function createModuleDispatcher(runtime) {
1396
1427
  return (id) => {
1397
1428
  const self = globalThis;
1398
1429
  const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? {
1399
1430
  bundle: "default",
1400
1431
  id
1401
1432
  };
1402
- const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
1403
- logDebug("WebpackRuntime", `remoteRuntime: "${remoteRuntime}"`);
1433
+ const bundleName = bundle ?? "default";
1434
+ const remoteRuntime = self.__remote_webpack_require__?.[bundleName] ? self.__remote_webpack_require__[bundleName]?.type || "webpack" : runtime;
1435
+ logDebug(
1436
+ "ModuleDispatcher",
1437
+ `Resolving "${id}" (bundle: "${bundleName}", runtime: "${remoteRuntime}")`
1438
+ );
1404
1439
  try {
1405
1440
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
1406
1441
  return self.__remote_webpack_require__?.[bundle]?.(moduleId);
1407
1442
  }
1408
- const sharedModuleId = moduleId ?? id;
1409
- const sharedModule = getSharedModule(bundle ?? "default", sharedModuleId);
1410
- if (sharedModule) {
1411
- return sharedModule;
1412
- }
1413
- if (bundle && moduleId) {
1414
- return handleTurbopackModule(bundle, moduleId, id);
1443
+ const scope = getScope(bundleName);
1444
+ if (scope) {
1445
+ return requireModule(scope, moduleId ?? id, id);
1415
1446
  }
1416
- throw new Error(`Module "${id}" not found.`);
1447
+ throw new Error(
1448
+ `Module "${id}" not found \u2014 no scope for bundle "${bundleName}".`
1449
+ );
1417
1450
  } catch (requireError) {
1418
1451
  logWarn(
1419
- "WebpackRuntime",
1452
+ "ModuleDispatcher",
1420
1453
  `Module require failed: ${String(requireError)}`
1421
1454
  );
1422
1455
  if (typeof self.__original_webpack_require__ !== "function") {
1423
1456
  throw new RemoteComponentsError(
1424
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1457
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1425
1458
  {
1426
1459
  cause: requireError instanceof Error ? requireError : void 0
1427
1460
  }
1428
1461
  );
1429
1462
  }
1430
1463
  try {
1431
- logDebug("WebpackRuntime", "Falling back to original webpack require");
1464
+ logDebug(
1465
+ "ModuleDispatcher",
1466
+ "Falling back to original webpack require"
1467
+ );
1432
1468
  return self.__original_webpack_require__(id);
1433
1469
  } catch (originalError) {
1434
1470
  throw new RemoteComponentsError(
1435
- `Module "${id}" not found in remote component bundle "${bundle}".`,
1471
+ `Module "${id}" not found in remote component bundle "${bundleName}".`,
1436
1472
  { cause: originalError instanceof Error ? originalError : void 0 }
1437
1473
  );
1438
1474
  }
@@ -1448,6 +1484,7 @@ var init_webpack_runtime = __esm({
1448
1484
  init_logger();
1449
1485
  init_chunk_loader();
1450
1486
  init_module();
1487
+ init_remote_scope();
1451
1488
  init_shared_modules();
1452
1489
  }
1453
1490
  });