remote-components 0.3.0 → 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 (105) 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 +467 -395
  4. package/dist/host/html.cjs.map +1 -1
  5. package/dist/host/html.js +467 -395
  6. package/dist/host/html.js.map +1 -1
  7. package/dist/host/nextjs/app/client-only.cjs +507 -447
  8. package/dist/host/nextjs/app/client-only.cjs.map +1 -1
  9. package/dist/host/nextjs/app/client-only.js +515 -455
  10. package/dist/host/nextjs/app/client-only.js.map +1 -1
  11. package/dist/host/nextjs/app.cjs +5 -6
  12. package/dist/host/nextjs/app.cjs.map +1 -1
  13. package/dist/host/nextjs/app.js +5 -6
  14. package/dist/host/nextjs/app.js.map +1 -1
  15. package/dist/host/nextjs/pages.cjs +6 -11
  16. package/dist/host/nextjs/pages.cjs.map +1 -1
  17. package/dist/host/nextjs/pages.js +9 -11
  18. package/dist/host/nextjs/pages.js.map +1 -1
  19. package/dist/host/react.cjs +435 -374
  20. package/dist/host/react.cjs.map +1 -1
  21. package/dist/host/react.js +435 -374
  22. package/dist/host/react.js.map +1 -1
  23. package/dist/internal/host/nextjs/dom-flight.cjs +16 -7
  24. package/dist/internal/host/nextjs/dom-flight.cjs.map +1 -1
  25. package/dist/internal/host/nextjs/dom-flight.d.ts +2 -2
  26. package/dist/internal/host/nextjs/dom-flight.js +16 -7
  27. package/dist/internal/host/nextjs/dom-flight.js.map +1 -1
  28. package/dist/internal/host/nextjs/image-shared.cjs +2 -2
  29. package/dist/internal/host/nextjs/image-shared.cjs.map +1 -1
  30. package/dist/internal/host/nextjs/image-shared.js +2 -2
  31. package/dist/internal/host/nextjs/image-shared.js.map +1 -1
  32. package/dist/internal/host/server/fetch-remote-component.cjs +183 -149
  33. package/dist/internal/host/server/fetch-remote-component.cjs.map +1 -1
  34. package/dist/internal/host/server/fetch-remote-component.js +185 -149
  35. package/dist/internal/host/server/fetch-remote-component.js.map +1 -1
  36. package/dist/internal/host/shared/remote-image-loader.cjs +2 -5
  37. package/dist/internal/host/shared/remote-image-loader.cjs.map +1 -1
  38. package/dist/internal/host/shared/remote-image-loader.js +2 -5
  39. package/dist/internal/host/shared/remote-image-loader.js.map +1 -1
  40. package/dist/internal/runtime/constants.cjs +6 -6
  41. package/dist/internal/runtime/constants.cjs.map +1 -1
  42. package/dist/internal/runtime/constants.d.ts +3 -3
  43. package/dist/internal/runtime/constants.js +4 -4
  44. package/dist/internal/runtime/constants.js.map +1 -1
  45. package/dist/internal/runtime/html/parse-remote-html.cjs +11 -15
  46. package/dist/internal/runtime/html/parse-remote-html.cjs.map +1 -1
  47. package/dist/internal/runtime/html/parse-remote-html.d.ts +2 -12
  48. package/dist/internal/runtime/html/parse-remote-html.js +17 -15
  49. package/dist/internal/runtime/html/parse-remote-html.js.map +1 -1
  50. package/dist/internal/runtime/loaders/script-loader.cjs +2 -2
  51. package/dist/internal/runtime/loaders/script-loader.cjs.map +1 -1
  52. package/dist/internal/runtime/loaders/script-loader.js +1 -1
  53. package/dist/internal/runtime/loaders/script-loader.js.map +1 -1
  54. package/dist/internal/runtime/metadata.cjs +42 -0
  55. package/dist/internal/runtime/metadata.cjs.map +1 -1
  56. package/dist/internal/runtime/metadata.d.ts +21 -1
  57. package/dist/internal/runtime/metadata.js +38 -0
  58. package/dist/internal/runtime/metadata.js.map +1 -1
  59. package/dist/internal/runtime/patterns.cjs +38 -0
  60. package/dist/internal/runtime/patterns.cjs.map +1 -0
  61. package/dist/internal/runtime/patterns.d.ts +5 -0
  62. package/dist/internal/runtime/patterns.js +12 -0
  63. package/dist/internal/runtime/patterns.js.map +1 -0
  64. package/dist/internal/runtime/turbopack/chunk-loader.cjs +79 -128
  65. package/dist/internal/runtime/turbopack/chunk-loader.cjs.map +1 -1
  66. package/dist/internal/runtime/turbopack/chunk-loader.d.ts +22 -7
  67. package/dist/internal/runtime/turbopack/chunk-loader.js +85 -131
  68. package/dist/internal/runtime/turbopack/chunk-loader.js.map +1 -1
  69. package/dist/internal/runtime/turbopack/module.cjs +56 -57
  70. package/dist/internal/runtime/turbopack/module.cjs.map +1 -1
  71. package/dist/internal/runtime/turbopack/module.d.ts +14 -11
  72. package/dist/internal/runtime/turbopack/module.js +54 -56
  73. package/dist/internal/runtime/turbopack/module.js.map +1 -1
  74. package/dist/internal/runtime/turbopack/remote-scope.cjs +101 -0
  75. package/dist/internal/runtime/turbopack/remote-scope.cjs.map +1 -0
  76. package/dist/internal/runtime/turbopack/remote-scope.d.ts +55 -0
  77. package/dist/internal/runtime/turbopack/remote-scope.js +73 -0
  78. package/dist/internal/runtime/turbopack/remote-scope.js.map +1 -0
  79. package/dist/internal/runtime/turbopack/shared-modules.cjs +62 -56
  80. package/dist/internal/runtime/turbopack/shared-modules.cjs.map +1 -1
  81. package/dist/internal/runtime/turbopack/shared-modules.d.ts +7 -3
  82. package/dist/internal/runtime/turbopack/shared-modules.js +63 -57
  83. package/dist/internal/runtime/turbopack/shared-modules.js.map +1 -1
  84. package/dist/internal/runtime/turbopack/webpack-runtime.cjs +50 -31
  85. package/dist/internal/runtime/turbopack/webpack-runtime.cjs.map +1 -1
  86. package/dist/internal/runtime/turbopack/webpack-runtime.d.ts +5 -2
  87. package/dist/internal/runtime/turbopack/webpack-runtime.js +52 -34
  88. package/dist/internal/runtime/turbopack/webpack-runtime.js.map +1 -1
  89. package/dist/internal/runtime/types.cjs.map +1 -1
  90. package/dist/internal/runtime/types.d.ts +4 -3
  91. package/dist/internal/utils/logger.cjs.map +1 -1
  92. package/dist/internal/utils/logger.d.ts +1 -1
  93. package/dist/internal/utils/logger.js.map +1 -1
  94. package/dist/internal/utils.cjs +5 -0
  95. package/dist/internal/utils.cjs.map +1 -1
  96. package/dist/internal/utils.d.ts +15 -1
  97. package/dist/internal/utils.js +4 -0
  98. package/dist/internal/utils.js.map +1 -1
  99. package/dist/remote/html.cjs.map +1 -1
  100. package/dist/remote/html.js.map +1 -1
  101. package/dist/remote/nextjs/app.cjs +2 -1
  102. package/dist/remote/nextjs/app.cjs.map +1 -1
  103. package/dist/remote/nextjs/app.js +2 -1
  104. package/dist/remote/nextjs/app.js.map +1 -1
  105. package/package.json +1 -1
@@ -0,0 +1,73 @@
1
+ import { getBundleKey } from "#internal/runtime/constants";
2
+ import { REMOTE_COMPONENT_REGEX } from "#internal/runtime/patterns";
3
+ import { computeScopedName } from "#internal/utils";
4
+ import { logDebug, logWarn } from "#internal/utils/logger";
5
+ function getRegistry() {
6
+ const self = globalThis;
7
+ if (!self.__remote_component_scopes__) {
8
+ self.__remote_component_scopes__ = /* @__PURE__ */ new Map();
9
+ }
10
+ return self.__remote_component_scopes__;
11
+ }
12
+ function createScope(name, url, runtime, resolveClientUrl) {
13
+ const isCrossOrigin = url.origin !== location.origin;
14
+ const scopedName = computeScopedName(name, {
15
+ remoteHost: url.host,
16
+ isCrossOrigin
17
+ });
18
+ const globalKey = getBundleKey(scopedName);
19
+ return {
20
+ name,
21
+ scopedName,
22
+ globalKey,
23
+ url,
24
+ runtime,
25
+ resolveClientUrl,
26
+ moduleCache: {},
27
+ sharedModules: {},
28
+ moduleGlobal: {}
29
+ };
30
+ }
31
+ function registerScope(scope) {
32
+ const registry = getRegistry();
33
+ registry.set(scope.scopedName, scope);
34
+ if (scope.scopedName !== scope.name) {
35
+ const existing = registry.get(scope.name);
36
+ if (existing && existing.scopedName !== scope.scopedName) {
37
+ logWarn(
38
+ "RemoteScope",
39
+ `Plain name "${scope.name}" already registered by scope "${existing.scopedName}" \u2014 overwriting with "${scope.scopedName}". Static hosts will only resolve the latest one.`
40
+ );
41
+ }
42
+ registry.set(scope.name, scope);
43
+ }
44
+ logDebug(
45
+ "RemoteScope",
46
+ `Registered scope "${scope.scopedName}" (${registry.size} total)`
47
+ );
48
+ }
49
+ function getScope(name) {
50
+ return getRegistry().get(name);
51
+ }
52
+ function formatRemoteId(scope, path) {
53
+ return `[${scope.scopedName}] ${path}`;
54
+ }
55
+ function parseRemoteId(id) {
56
+ const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;
57
+ if (groups?.bundle && groups.id) {
58
+ return {
59
+ bundle: groups.bundle,
60
+ path: groups.id,
61
+ prefix: groups.prefix ?? ""
62
+ };
63
+ }
64
+ return { bundle: "default", path: id, prefix: "" };
65
+ }
66
+ export {
67
+ createScope,
68
+ formatRemoteId,
69
+ getScope,
70
+ parseRemoteId,
71
+ registerScope
72
+ };
73
+ //# sourceMappingURL=remote-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/runtime/turbopack/remote-scope.ts"],"sourcesContent":["import type { InternalResolveClientUrl } from '#internal/host/server/types';\nimport { getBundleKey, type Runtime } from '#internal/runtime/constants';\nimport { REMOTE_COMPONENT_REGEX } from '#internal/runtime/patterns';\nimport type { GlobalScope } from '#internal/runtime/types';\nimport { computeScopedName } from '#internal/utils';\nimport { logDebug, logWarn } from '#internal/utils/logger';\n\n/**\n * Encapsulates all per-remote state so that two remotes sharing the same\n * bundle name but served from different origins never collide.\n *\n * A scope is created once per `setupWebpackRuntime` call and threaded\n * directly through internal code paths (chunk loading, module resolution,\n * shared modules). Global dispatchers (`__webpack_require__`,\n * `__webpack_chunk_load__`) only exist for external callers (RSC runtime)\n * and resolve the correct scope via the registry.\n */\nexport interface RemoteScope {\n /** Plain bundle name (e.g. \"remote-component-registry\") */\n name: string;\n /** Origin-qualified key — unique even when two remotes share a name */\n scopedName: string;\n /** Escaped key used as suffix for TURBOPACK_ globals */\n globalKey: string;\n /** Base URL for resolving relative chunk paths */\n url: URL;\n /** Bundler runtime type */\n runtime: Runtime;\n /** Proxy callback for routing chunks through the host */\n resolveClientUrl?: InternalResolveClientUrl;\n /** Per-scope cache of executed Turbopack module exports */\n moduleCache: Record<string, unknown>;\n /** Per-scope shared modules provided by the host (React, etc.) */\n sharedModules: Record<string, unknown>;\n /** Per-scope globalThis substitute passed to module initializers as `g` */\n moduleGlobal: Record<string, unknown>;\n}\n\nfunction getRegistry(): Map<string, RemoteScope> {\n const self = globalThis as GlobalScope;\n if (!self.__remote_component_scopes__) {\n self.__remote_component_scopes__ = new Map<string, unknown>();\n }\n return self.__remote_component_scopes__ as unknown as Map<\n string,\n RemoteScope\n >;\n}\n\nexport function createScope(\n name: string,\n url: URL,\n runtime: Runtime,\n resolveClientUrl?: InternalResolveClientUrl,\n): RemoteScope {\n const isCrossOrigin = url.origin !== location.origin;\n const scopedName = computeScopedName(name, {\n remoteHost: url.host,\n isCrossOrigin,\n });\n const globalKey = getBundleKey(scopedName);\n return {\n name,\n scopedName,\n globalKey,\n url,\n runtime,\n resolveClientUrl,\n moduleCache: {},\n sharedModules: {},\n moduleGlobal: {},\n };\n}\n\nexport function registerScope(scope: RemoteScope): void {\n const registry = getRegistry();\n registry.set(scope.scopedName, scope);\n\n // Also register under the plain name so hosts without server-side RSC\n // rewriting (static/HTML hosts) can look up cross-origin scopes by\n // the bundle name that appears in unrewritten chunk/module IDs.\n if (scope.scopedName !== scope.name) {\n const existing = registry.get(scope.name);\n if (existing && existing.scopedName !== scope.scopedName) {\n logWarn(\n 'RemoteScope',\n `Plain name \"${scope.name}\" already registered by scope \"${existing.scopedName}\" — ` +\n `overwriting with \"${scope.scopedName}\". Static hosts will only resolve the latest one.`,\n );\n }\n registry.set(scope.name, scope);\n }\n\n logDebug(\n 'RemoteScope',\n `Registered scope \"${scope.scopedName}\" (${registry.size} total)`,\n );\n}\n\n/**\n * Resolves a scope by name. Accepts either the scoped name (used by Next.js\n * hosts that rewrite RSC data server-side) or the plain bundle name (used by\n * static/HTML hosts where chunks arrive unrewritten).\n */\nexport function getScope(name: string): RemoteScope | undefined {\n return getRegistry().get(name);\n}\n\n/** Formats a `[scopedName] path` string for use in chunk/module IDs. */\nexport function formatRemoteId(scope: RemoteScope, path: string): string {\n return `[${scope.scopedName}] ${path}`;\n}\n\n/**\n * Parses the `[bundle] path` format used in chunk/module IDs.\n * Uses REMOTE_COMPONENT_REGEX (the same pattern used by the module dispatcher\n * and script loader) so the two parsing paths can never diverge.\n */\nexport function parseRemoteId(id: string): {\n bundle: string;\n path: string;\n prefix: string;\n} {\n const groups = REMOTE_COMPONENT_REGEX.exec(id)?.groups;\n if (groups?.bundle && groups.id) {\n return {\n bundle: groups.bundle,\n path: groups.id,\n prefix: groups.prefix ?? '',\n };\n }\n return { bundle: 'default', path: id, prefix: '' };\n}\n"],"mappings":"AACA,SAAS,oBAAkC;AAC3C,SAAS,8BAA8B;AAEvC,SAAS,yBAAyB;AAClC,SAAS,UAAU,eAAe;AAiClC,SAAS,cAAwC;AAC/C,QAAM,OAAO;AACb,MAAI,CAAC,KAAK,6BAA6B;AACrC,SAAK,8BAA8B,oBAAI,IAAqB;AAAA,EAC9D;AACA,SAAO,KAAK;AAId;AAEO,SAAS,YACd,MACA,KACA,SACA,kBACa;AACb,QAAM,gBAAgB,IAAI,WAAW,SAAS;AAC9C,QAAM,aAAa,kBAAkB,MAAM;AAAA,IACzC,YAAY,IAAI;AAAA,IAChB;AAAA,EACF,CAAC;AACD,QAAM,YAAY,aAAa,UAAU;AACzC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,aAAa,CAAC;AAAA,IACd,eAAe,CAAC;AAAA,IAChB,cAAc,CAAC;AAAA,EACjB;AACF;AAEO,SAAS,cAAc,OAA0B;AACtD,QAAM,WAAW,YAAY;AAC7B,WAAS,IAAI,MAAM,YAAY,KAAK;AAKpC,MAAI,MAAM,eAAe,MAAM,MAAM;AACnC,UAAM,WAAW,SAAS,IAAI,MAAM,IAAI;AACxC,QAAI,YAAY,SAAS,eAAe,MAAM,YAAY;AACxD;AAAA,QACE;AAAA,QACA,eAAe,MAAM,sCAAsC,SAAS,wCAC7C,MAAM;AAAA,MAC/B;AAAA,IACF;AACA,aAAS,IAAI,MAAM,MAAM,KAAK;AAAA,EAChC;AAEA;AAAA,IACE;AAAA,IACA,qBAAqB,MAAM,gBAAgB,SAAS;AAAA,EACtD;AACF;AAOO,SAAS,SAAS,MAAuC;AAC9D,SAAO,YAAY,EAAE,IAAI,IAAI;AAC/B;AAGO,SAAS,eAAe,OAAoB,MAAsB;AACvE,SAAO,IAAI,MAAM,eAAe;AAClC;AAOO,SAAS,cAAc,IAI5B;AACA,QAAM,SAAS,uBAAuB,KAAK,EAAE,GAAG;AAChD,MAAI,QAAQ,UAAU,OAAO,IAAI;AAC/B,WAAO;AAAA,MACL,QAAQ,OAAO;AAAA,MACf,MAAM,OAAO;AAAA,MACb,QAAQ,OAAO,UAAU;AAAA,IAC3B;AAAA,EACF;AACA,SAAO,EAAE,QAAQ,WAAW,MAAM,IAAI,QAAQ,GAAG;AACnD;","names":[]}
@@ -22,26 +22,32 @@ __export(shared_modules_exports, {
22
22
  initializeSharedModules: () => initializeSharedModules
23
23
  });
24
24
  module.exports = __toCommonJS(shared_modules_exports);
25
- var import_constants = require("#internal/runtime/constants");
26
25
  var import_logger = require("#internal/utils/logger");
27
26
  var import_module = require("./module");
28
27
  var import_patterns = require("./patterns");
28
+ var import_remote_scope = require("./remote-scope");
29
29
  const 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).";
30
- async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
30
+ function getTurbopackModules(scope) {
31
31
  const self = globalThis;
32
- self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};
33
- if (!self.__remote_shared_modules__[bundle]) {
34
- self.__remote_shared_modules__[bundle] = {};
32
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
33
+ if (!raw)
34
+ return void 0;
35
+ if (typeof raw === "object" && "__chunks__" in raw) {
36
+ return raw.__chunks__.flat();
35
37
  }
36
- const bundleKey = (0, import_constants.getBundleKey)(bundle);
37
- let modules = self[`TURBOPACK_${bundleKey}`];
38
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
39
- const chunks = modules.__chunks__;
40
- modules = chunks.flat();
38
+ if (Array.isArray(raw)) {
39
+ return raw.flat();
41
40
  }
41
+ return Object.entries(raw).flat();
42
+ }
43
+ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}) {
44
+ const allModules = getTurbopackModules(scope);
45
+ (0, import_logger.logDebug)(
46
+ "SharedModules",
47
+ `initializeSharedModules: scope="${scope.scopedName}", allModules=${allModules ? allModules.length : "null"}, hostShared=[${Object.keys(hostShared).join(", ")}], remoteShared=${JSON.stringify(remoteShared)}`
48
+ );
42
49
  let sharedModuleInitializer = null;
43
- if (modules) {
44
- const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
50
+ if (allModules) {
45
51
  const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
46
52
  if (typeof idOrFunc !== "function") {
47
53
  return false;
@@ -55,72 +61,71 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
55
61
  const { sharedModuleId } = import_patterns.REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
56
62
  if (sharedModuleId) {
57
63
  const { default: sharedModuleInitializerInstance } = (0, import_module.handleTurbopackModule)(
58
- bundle,
64
+ scope,
59
65
  sharedModuleId,
60
- `[${bundle}] ${sharedModuleInitializerId}`
66
+ (0, import_remote_scope.formatRemoteId)(scope, String(sharedModuleInitializerId))
61
67
  );
62
68
  sharedModuleInitializer = sharedModuleInitializerInstance;
63
69
  }
64
70
  }
65
71
  if (sharedModuleInitializer) {
66
72
  const { shared } = await sharedModuleInitializer;
67
- const sharedModuleIds = extractSharedModuleIds(
68
- shared,
69
- bundleKey,
70
- self
73
+ const sharedModuleIds = extractSharedModuleIds(shared, scope);
74
+ (0, import_logger.logDebug)(
75
+ "SharedModules",
76
+ `Resolved shared modules for scope="${scope.scopedName}": ${JSON.stringify(sharedModuleIds)}`
71
77
  );
72
78
  return Promise.all(
73
79
  Object.entries(sharedModuleIds).map(async ([id, module2]) => {
74
- if (self.__remote_shared_modules__?.[bundle]) {
75
- if (hostShared[module2]) {
76
- self.__remote_shared_modules__[bundle][id] = await hostShared[module2](bundle);
77
- } else {
78
- (0, import_logger.logError)(
79
- "SharedModules",
80
- `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
81
- );
82
- }
80
+ if (hostShared[module2]) {
81
+ scope.sharedModules[id] = await hostShared[module2](scope.name);
82
+ } else {
83
+ (0, import_logger.logError)(
84
+ "SharedModules",
85
+ `Host shared module "${module2}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
86
+ );
83
87
  }
84
88
  })
85
89
  );
86
90
  }
91
+ (0, import_logger.logWarn)(
92
+ "SharedModules",
93
+ `No shared module initializer found in bundle for scope="${scope.scopedName}" \u2014 falling back to remoteShared mapping`
94
+ );
95
+ } else {
96
+ (0, import_logger.logWarn)(
97
+ "SharedModules",
98
+ `No TURBOPACK modules found for scope="${scope.scopedName}" (TURBOPACK_${scope.globalKey} is empty)`
99
+ );
87
100
  }
88
101
  return Promise.all(
89
102
  Object.entries(remoteShared).map(async ([id, module2]) => {
90
- if (self.__remote_shared_modules__?.[bundle]) {
91
- if (hostShared[module2]) {
92
- const normalizedId = id.replace("[app-ssr]", "[app-client]");
93
- self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module2](bundle);
94
- } else {
95
- (0, import_logger.logError)(
96
- "SharedModules",
97
- `Shared module "${module2}" not found for "${bundle}". ${DEDUPLICATION_WARNING}`
98
- );
99
- }
103
+ if (hostShared[module2]) {
104
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
105
+ scope.sharedModules[normalizedId] = await hostShared[module2](
106
+ scope.name
107
+ );
108
+ } else {
109
+ (0, import_logger.logError)(
110
+ "SharedModules",
111
+ `Shared module "${module2}" not found for "${scope.name}". ${DEDUPLICATION_WARNING}`
112
+ );
100
113
  }
101
114
  })
102
115
  );
103
116
  }
104
- function extractSharedModuleIds(shared, bundleKey, self) {
117
+ function extractSharedModuleIds(shared, scope) {
105
118
  return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
106
119
  const { asyncSharedModuleId } = import_patterns.ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
107
120
  if (asyncSharedModuleId) {
108
121
  const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
109
122
  let asyncSharedModule;
110
- let turbopackModules = self[`TURBOPACK_${bundleKey}`];
111
- if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
112
- const chunks = turbopackModules.__chunks__;
113
- turbopackModules = chunks.flat();
114
- }
115
- const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
116
- key2,
117
- value2
118
- ]) : [];
119
- const asyncSharedModuleIdIndex = newAllModules.indexOf(
123
+ const turbopackModules = getTurbopackModules(scope) ?? [];
124
+ const asyncSharedModuleIdIndex = turbopackModules.indexOf(
120
125
  asyncSharedModuleIdNumber
121
126
  );
122
- if (asyncSharedModuleIdIndex !== -1 && typeof newAllModules[asyncSharedModuleIdIndex + 1] === "function") {
123
- asyncSharedModule = newAllModules[asyncSharedModuleIdIndex + 1];
127
+ if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
128
+ asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
124
129
  }
125
130
  if (asyncSharedModule) {
126
131
  const asyncSharedModuleCode = asyncSharedModule.toString();
@@ -134,12 +139,13 @@ function extractSharedModuleIds(shared, bundleKey, self) {
134
139
  return acc;
135
140
  }, {});
136
141
  }
137
- function getSharedModule(bundle, id) {
138
- const self = globalThis;
139
- for (const [key, value] of Object.entries(
140
- self.__remote_shared_modules__?.[bundle] ?? {}
141
- )) {
142
- if (typeof value !== "undefined" && (typeof id === "string" && id.includes(key) || id === key)) {
142
+ function getSharedModule(scope, id) {
143
+ const idStr = String(id);
144
+ if (scope.sharedModules[idStr] !== void 0) {
145
+ return scope.sharedModules[idStr];
146
+ }
147
+ for (const [key, value] of Object.entries(scope.sharedModules)) {
148
+ if (typeof value !== "undefined" && idStr !== key && idStr.endsWith(key)) {
143
149
  return value;
144
150
  }
145
151
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/runtime/turbopack/shared-modules.ts"],"sourcesContent":["import { getBundleKey } from '#internal/runtime/constants';\nimport type { GlobalScope } from '#internal/runtime/types';\nimport { logError } from '#internal/utils/logger';\nimport { handleTurbopackModule } from './module';\nimport {\n ASYNC_MODULE_ALL_RE,\n ASYNC_MODULE_LOADER_RE,\n ASYNC_MODULE_RESOLVE_RE,\n REMOTE_SHARED_ASSIGNMENT_RE,\n REMOTE_SHARED_MARKER_RE,\n} from './patterns';\n\nconst DEDUPLICATION_WARNING =\n 'This module will not be deduplicated — the remote may load its own copy, ' +\n 'which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).';\n\n/**\n * Initializes shared modules between the host application and remote components.\n * This enables sharing of common dependencies like React to avoid duplicate instances.\n *\n * The function works by:\n * 1. Looking for a shared module initializer in the Turbopack bundle\n * 2. Extracting module IDs from the initializer\n * 3. Loading the corresponding host modules and mapping them to the remote's expected IDs\n */\nexport async function initializeSharedModules(\n bundle: string,\n hostShared: Record<string, (bundle?: string) => Promise<unknown>> = {},\n remoteShared: Record<string, string> = {},\n // biome-ignore lint/suspicious/noConfusingVoidType: Runtime is undefined but in TS land service it is void\n): Promise<void[]> {\n const self = globalThis as GlobalScope & {\n __next_f?: unknown[];\n };\n self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};\n\n if (!self.__remote_shared_modules__[bundle]) {\n self.__remote_shared_modules__[bundle] = {};\n }\n\n // prepare to load shared module initializers from the Turbopack bundle\n const bundleKey = getBundleKey(bundle);\n let modules = self[`TURBOPACK_${bundleKey}`] as\n | [unknown, Record<string, unknown>][]\n | Record<string, unknown>\n | undefined;\n\n // Check for new push-based format with __chunks__\n if (modules && typeof modules === 'object' && '__chunks__' in modules) {\n const chunks = (modules as { __chunks__: unknown[][] }).__chunks__;\n // Flatten chunks into module array format\n modules = chunks.flat() as [unknown, Record<string, unknown>][];\n }\n let sharedModuleInitializer: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }> | null = null;\n\n // find the shared module initializer in the Turbopack bundle\n if (modules) {\n // Handle both array and object formats for modules\n const allModules = Array.isArray(modules)\n ? modules.flat()\n : Object.entries(modules).flat();\n // find the shared module initializer module id by looking for\n // a function that contains the TURBOPACK_REMOTE_SHARED pattern\n const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {\n if (typeof idOrFunc !== 'function') {\n return false;\n }\n const funcCode = idOrFunc.toString();\n return REMOTE_SHARED_MARKER_RE.test(funcCode);\n });\n\n // if found, extract the shared module initializer\n // first element in the array is always the source script element\n if (sharedModuleInitializerIndex > 0) {\n const sharedModuleInitializerCode = (\n allModules[sharedModuleInitializerIndex] as () => unknown\n ).toString();\n const sharedModuleInitializerId = allModules[\n sharedModuleInitializerIndex - 1\n ] as string | number;\n // extract the shared module id from the function code\n const { sharedModuleId } =\n REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ??\n {};\n // load the shared module initializer using the extracted module id\n if (sharedModuleId) {\n const { default: sharedModuleInitializerInstance } =\n handleTurbopackModule(\n bundle,\n sharedModuleId,\n `[${bundle}] ${sharedModuleInitializerId}`,\n ) as {\n default: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }>;\n };\n sharedModuleInitializer = sharedModuleInitializerInstance;\n }\n }\n\n // if we have a shared module initializer, load the shared modules from the host application\n if (sharedModuleInitializer) {\n const { shared } = await sharedModuleInitializer;\n // map shared module ids to their initializer functions\n const sharedModuleIds = extractSharedModuleIds(\n shared,\n bundleKey,\n self as GlobalScope,\n );\n\n // load shared modules from the host application\n return Promise.all(\n Object.entries(sharedModuleIds).map(async ([id, module]) => {\n if (self.__remote_shared_modules__?.[bundle]) {\n if (hostShared[module]) {\n self.__remote_shared_modules__[bundle][id] =\n await hostShared[module](bundle);\n } else {\n logError(\n 'SharedModules',\n `Host shared module \"${module}\" not found for ID ${id}. ${DEDUPLICATION_WARNING}`,\n );\n }\n }\n }),\n );\n }\n }\n\n // fallback: ensure that the shared modules are initialized using remoteShared mapping\n return Promise.all(\n Object.entries(remoteShared).map(async ([id, module]) => {\n if (self.__remote_shared_modules__?.[bundle]) {\n if (hostShared[module]) {\n const normalizedId = id.replace('[app-ssr]', '[app-client]');\n self.__remote_shared_modules__[bundle][normalizedId] =\n await hostShared[module](bundle);\n } else {\n logError(\n 'SharedModules',\n `Shared module \"${module}\" not found for \"${bundle}\". ${DEDUPLICATION_WARNING}`,\n );\n }\n }\n }),\n );\n}\n\n/**\n * Extracts shared module IDs from the shared module initializer functions.\n * This parses the minified Turbopack code to find the module ID mappings.\n */\nfunction extractSharedModuleIds(\n shared: Record<string, string | (() => Promise<unknown>)>,\n bundleKey: string,\n self: GlobalScope,\n): Record<string, string> {\n return Object.entries(shared)\n .filter(([, value]) => typeof value === 'function')\n .reduce<Record<string, string>>((acc, [key, value]) => {\n const { asyncSharedModuleId } =\n ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};\n\n if (asyncSharedModuleId) {\n const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);\n\n let asyncSharedModule;\n let turbopackModules = self[`TURBOPACK_${bundleKey}`] as\n | unknown[]\n | Record<string, unknown>\n | undefined;\n\n // Check for new push-based format with __chunks__\n if (\n turbopackModules &&\n typeof turbopackModules === 'object' &&\n '__chunks__' in turbopackModules\n ) {\n const chunks = (turbopackModules as { __chunks__: unknown[][] })\n .__chunks__;\n turbopackModules = chunks.flat() as unknown[];\n }\n\n const newAllModules = Array.isArray(turbopackModules)\n ? turbopackModules.flat()\n : turbopackModules\n ? Object.entries(turbopackModules).flatMap(([key, value]) => [\n key,\n value,\n ])\n : [];\n const asyncSharedModuleIdIndex = newAllModules.indexOf(\n asyncSharedModuleIdNumber,\n );\n if (\n asyncSharedModuleIdIndex !== -1 &&\n typeof newAllModules[asyncSharedModuleIdIndex + 1] === 'function'\n ) {\n asyncSharedModule = newAllModules[\n asyncSharedModuleIdIndex + 1\n ] as () => unknown;\n }\n if (asyncSharedModule) {\n const asyncSharedModuleCode = asyncSharedModule.toString();\n // Try both Promise.resolve and Promise.all patterns\n const { sharedModuleId } =\n ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ??\n ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ??\n {};\n // map the shared module id to the actual module name\n acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(\n '__remote_shared_module_',\n '',\n );\n }\n }\n return acc;\n }, {});\n}\n\n/**\n * Returns a shared module for the given bundle and module ID.\n * Shared modules are common dependencies like React that are provided by the host.\n */\nexport function getSharedModule(bundle: string, id: string | number): unknown {\n const self = globalThis as {\n __remote_shared_modules__?: Record<string, Record<string, unknown>>;\n };\n\n for (const [key, value] of Object.entries(\n self.__remote_shared_modules__?.[bundle] ?? {},\n )) {\n if (\n typeof value !== 'undefined' &&\n ((typeof id === 'string' && id.includes(key)) || id === key)\n ) {\n return value;\n }\n }\n return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAA6B;AAE7B,oBAAyB;AACzB,oBAAsC;AACtC,sBAMO;AAEP,MAAM,wBACJ;AAYF,eAAsB,wBACpB,QACA,aAAoE,CAAC,GACrE,eAAuC,CAAC,GAEvB;AACjB,QAAM,OAAO;AAGb,OAAK,4BAA4B,KAAK,6BAA6B,CAAC;AAEpE,MAAI,CAAC,KAAK,0BAA0B,MAAM,GAAG;AAC3C,SAAK,0BAA0B,MAAM,IAAI,CAAC;AAAA,EAC5C;AAGA,QAAM,gBAAY,+BAAa,MAAM;AACrC,MAAI,UAAU,KAAK,aAAa,WAAW;AAM3C,MAAI,WAAW,OAAO,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YACvD;AACA,4BAAoB,cAClB,2BAA2B,CAC7B;AAAA,MACF;AACA,UAAI,mBAAmB;AACrB,cAAM,wBAAwB,kBAAkB,SAAS;AAEzD,cAAM,EAAE,eAAe,IACrB,wCAAwB,KAAK,qBAAqB,GAAG,UACrD,oCAAoB,KAAK,qBAAqB,GAAG,UACjD,CAAC;AAEH,YAAI,kBAAkB,mBAAmB,IAAI,IAAI;AAAA,UAC/C;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACT;AAMO,SAAS,gBAAgB,QAAgB,IAA8B;AAC5E,QAAM,OAAO;AAIb,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO;AAAA,IAChC,KAAK,4BAA4B,MAAM,KAAK,CAAC;AAAA,EAC/C,GAAG;AACD,QACE,OAAO,UAAU,gBACf,OAAO,OAAO,YAAY,GAAG,SAAS,GAAG,KAAM,OAAO,MACxD;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["module","key","value"]}
1
+ {"version":3,"sources":["../../../../src/runtime/turbopack/shared-modules.ts"],"sourcesContent":["import type { GlobalScope } from '#internal/runtime/types';\nimport { logDebug, logError, logWarn } from '#internal/utils/logger';\nimport { handleTurbopackModule } from './module';\nimport {\n ASYNC_MODULE_ALL_RE,\n ASYNC_MODULE_LOADER_RE,\n ASYNC_MODULE_RESOLVE_RE,\n REMOTE_SHARED_ASSIGNMENT_RE,\n REMOTE_SHARED_MARKER_RE,\n} from './patterns';\nimport { formatRemoteId, type RemoteScope } from './remote-scope';\n\nconst DEDUPLICATION_WARNING =\n 'This module will not be deduplicated — the remote may load its own copy, ' +\n 'which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).';\n\n/**\n * Reads the TURBOPACK global for a scope, normalizing the push-based\n * `__chunks__` format into a flat module list when needed.\n */\nfunction getTurbopackModules(scope: RemoteScope): unknown[] | undefined {\n const self = globalThis as GlobalScope;\n const raw = self[`TURBOPACK_${scope.globalKey}`];\n if (!raw) return undefined;\n\n if (typeof raw === 'object' && '__chunks__' in (raw as object)) {\n return (raw as { __chunks__: unknown[][] }).__chunks__.flat();\n }\n if (Array.isArray(raw)) {\n return (raw as unknown[]).flat();\n }\n return Object.entries(raw as Record<string, unknown>).flat();\n}\n\n/**\n * Initializes shared modules between the host application and remote components.\n * This enables sharing of common dependencies like React to avoid duplicate instances.\n *\n * The function works by:\n * 1. Looking for a shared module initializer in the Turbopack bundle\n * 2. Extracting module IDs from the initializer\n * 3. Loading the corresponding host modules and mapping them to the remote's expected IDs\n */\nexport async function initializeSharedModules(\n scope: RemoteScope,\n hostShared: Record<string, (bundle?: string) => Promise<unknown>> = {},\n remoteShared: Record<string, string> = {},\n // biome-ignore lint/suspicious/noConfusingVoidType: Runtime is undefined but in TS land service it is void\n): Promise<void[]> {\n const allModules = getTurbopackModules(scope);\n\n logDebug(\n 'SharedModules',\n `initializeSharedModules: scope=\"${scope.scopedName}\", ` +\n `allModules=${allModules ? allModules.length : 'null'}, ` +\n `hostShared=[${Object.keys(hostShared).join(', ')}], ` +\n `remoteShared=${JSON.stringify(remoteShared)}`,\n );\n\n let sharedModuleInitializer: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }> | null = null;\n\n // find the shared module initializer in the Turbopack bundle\n if (allModules) {\n // find the shared module initializer module id by looking for\n // a function that contains the TURBOPACK_REMOTE_SHARED pattern\n const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {\n if (typeof idOrFunc !== 'function') {\n return false;\n }\n const funcCode = idOrFunc.toString();\n return REMOTE_SHARED_MARKER_RE.test(funcCode);\n });\n\n // if found, extract the shared module initializer\n // first element in the array is always the source script element\n if (sharedModuleInitializerIndex > 0) {\n const sharedModuleInitializerCode = (\n allModules[sharedModuleInitializerIndex] as () => unknown\n ).toString();\n const sharedModuleInitializerId = allModules[\n sharedModuleInitializerIndex - 1\n ] as string | number;\n // extract the shared module id from the function code\n const { sharedModuleId } =\n REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ??\n {};\n // load the shared module initializer using the extracted module id\n if (sharedModuleId) {\n const { default: sharedModuleInitializerInstance } =\n handleTurbopackModule(\n scope,\n sharedModuleId,\n formatRemoteId(scope, String(sharedModuleInitializerId)),\n ) as {\n default: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }>;\n };\n sharedModuleInitializer = sharedModuleInitializerInstance;\n }\n }\n\n // if we have a shared module initializer, load the shared modules from the host application\n if (sharedModuleInitializer) {\n const { shared } = await sharedModuleInitializer;\n // map shared module ids to their initializer functions\n const sharedModuleIds = extractSharedModuleIds(shared, scope);\n logDebug(\n 'SharedModules',\n `Resolved shared modules for scope=\"${scope.scopedName}\": ${JSON.stringify(sharedModuleIds)}`,\n );\n\n // load shared modules from the host application\n return Promise.all(\n Object.entries(sharedModuleIds).map(async ([id, module]) => {\n if (hostShared[module]) {\n scope.sharedModules[id] = await hostShared[module](scope.name);\n } else {\n logError(\n 'SharedModules',\n `Host shared module \"${module}\" not found for ID ${id}. ${DEDUPLICATION_WARNING}`,\n );\n }\n }),\n );\n }\n\n logWarn(\n 'SharedModules',\n `No shared module initializer found in bundle for scope=\"${scope.scopedName}\" — falling back to remoteShared mapping`,\n );\n } else {\n logWarn(\n 'SharedModules',\n `No TURBOPACK modules found for scope=\"${scope.scopedName}\" (TURBOPACK_${scope.globalKey} is empty)`,\n );\n }\n\n // fallback: ensure that the shared modules are initialized using remoteShared mapping\n return Promise.all(\n Object.entries(remoteShared).map(async ([id, module]) => {\n if (hostShared[module]) {\n const normalizedId = id.replace('[app-ssr]', '[app-client]');\n scope.sharedModules[normalizedId] = await hostShared[module](\n scope.name,\n );\n } else {\n logError(\n 'SharedModules',\n `Shared module \"${module}\" not found for \"${scope.name}\". ${DEDUPLICATION_WARNING}`,\n );\n }\n }),\n );\n}\n\n/**\n * Extracts shared module IDs from the shared module initializer functions.\n * This parses the minified Turbopack code to find the module ID mappings.\n */\nfunction extractSharedModuleIds(\n shared: Record<string, string | (() => Promise<unknown>)>,\n scope: RemoteScope,\n): Record<string, string> {\n return Object.entries(shared)\n .filter(([, value]) => typeof value === 'function')\n .reduce<Record<string, string>>((acc, [key, value]) => {\n const { asyncSharedModuleId } =\n ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};\n\n if (asyncSharedModuleId) {\n const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);\n\n let asyncSharedModule: (() => unknown) | undefined;\n const turbopackModules = getTurbopackModules(scope) ?? [];\n const asyncSharedModuleIdIndex = turbopackModules.indexOf(\n asyncSharedModuleIdNumber,\n );\n if (\n asyncSharedModuleIdIndex !== -1 &&\n typeof turbopackModules[asyncSharedModuleIdIndex + 1] === 'function'\n ) {\n asyncSharedModule = turbopackModules[\n asyncSharedModuleIdIndex + 1\n ] as () => unknown;\n }\n if (asyncSharedModule) {\n const asyncSharedModuleCode = asyncSharedModule.toString();\n // Try both Promise.resolve and Promise.all patterns\n const { sharedModuleId } =\n ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ??\n ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ??\n {};\n // map the shared module id to the actual module name\n acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(\n '__remote_shared_module_',\n '',\n );\n }\n }\n return acc;\n }, {});\n}\n\n/**\n * Returns a shared module for the given scope and module ID.\n * Shared modules are common dependencies like React that are provided by the host.\n */\nexport function getSharedModule(\n scope: RemoteScope,\n id: string | number,\n): unknown {\n const idStr = String(id);\n\n // Exact match first (covers both string and numeric IDs)\n if (scope.sharedModules[idStr] !== undefined) {\n return scope.sharedModules[idStr];\n }\n\n // Fallback: the id may be a bundle-prefixed string like \"[bundle] /react/index.js\"\n // that contains the shared module key as a suffix. Only match when the key\n // appears as a complete suffix segment to avoid \"react\" matching \"react-dom\".\n for (const [key, value] of Object.entries(scope.sharedModules)) {\n if (typeof value !== 'undefined' && idStr !==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C,CAAC,EAAE,KAAK,MAAM,OAAO,UAAU,UAAU,EACjD,OAA+B,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AACrD,UAAM,EAAE,oBAAoB,IAC1B,uCAAuB,KAAK,MAAM,SAAS,CAAC,GAAG,UAAU,CAAC;AAE5D,QAAI,qBAAqB;AACvB,YAAM,4BAA4B,OAAO,mBAAmB;AAE5D,UAAI;AACJ,YAAM,mBAAmB,oBAAoB,KAAK,KAAK,CAAC;AACxD,YAAM,2BAA2B,iBAAiB;AAAA,QAChD;AAAA,MACF;AACA,UACE,6BAA6B,MAC7B,OAAO,iBAAiB,2BAA2B,CAAC,MAAM,YAC1D;AACA,4BAAoB,iBAClB,2BAA2B,CAC7B;AAAA,MACF;AACA,UAAI,mBAAmB;AACrB,cAAM,wBAAwB,kBAAkB,SAAS;AAEzD,cAAM,EAAE,eAAe,IACrB,wCAAwB,KAAK,qBAAqB,GAAG,UACrD,oCAAoB,KAAK,qBAAqB,GAAG,UACjD,CAAC;AAEH,YAAI,kBAAkB,mBAAmB,IAAI,IAAI;AAAA,UAC/C;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACT;AAMO,SAAS,gBACd,OACA,IACS;AACT,QAAM,QAAQ,OAAO,EAAE;AAGvB,MAAI,MAAM,cAAc,KAAK,MAAM,QAAW;AAC5C,WAAO,MAAM,cAAc,KAAK;AAAA,EAClC;AAKA,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,aAAa,GAAG;AAC9D,QAAI,OAAO,UAAU,eAAe,UAAU,OAAO,MAAM,SAAS,GAAG,GAAG;AACxE,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":["module"]}
@@ -1,3 +1,7 @@
1
+ import { RemoteScope } from './remote-scope.js';
2
+ import '../url/resolve-client-url.js';
3
+ import '../constants.js';
4
+
1
5
  /**
2
6
  * Initializes shared modules between the host application and remote components.
3
7
  * This enables sharing of common dependencies like React to avoid duplicate instances.
@@ -7,11 +11,11 @@
7
11
  * 2. Extracting module IDs from the initializer
8
12
  * 3. Loading the corresponding host modules and mapping them to the remote's expected IDs
9
13
  */
10
- declare function initializeSharedModules(bundle: string, hostShared?: Record<string, (bundle?: string) => Promise<unknown>>, remoteShared?: Record<string, string>): Promise<void[]>;
14
+ declare function initializeSharedModules(scope: RemoteScope, hostShared?: Record<string, (bundle?: string) => Promise<unknown>>, remoteShared?: Record<string, string>): Promise<void[]>;
11
15
  /**
12
- * Returns a shared module for the given bundle and module ID.
16
+ * Returns a shared module for the given scope and module ID.
13
17
  * Shared modules are common dependencies like React that are provided by the host.
14
18
  */
15
- declare function getSharedModule(bundle: string, id: string | number): unknown;
19
+ declare function getSharedModule(scope: RemoteScope, id: string | number): unknown;
16
20
 
17
21
  export { getSharedModule, initializeSharedModules };
@@ -1,5 +1,4 @@
1
- import { getBundleKey } from "#internal/runtime/constants";
2
- import { logError } from "#internal/utils/logger";
1
+ import { logDebug, logError, logWarn } from "#internal/utils/logger";
3
2
  import { handleTurbopackModule } from "./module";
4
3
  import {
5
4
  ASYNC_MODULE_ALL_RE,
@@ -8,22 +7,29 @@ import {
8
7
  REMOTE_SHARED_ASSIGNMENT_RE,
9
8
  REMOTE_SHARED_MARKER_RE
10
9
  } from "./patterns";
10
+ import { formatRemoteId } from "./remote-scope";
11
11
  const 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).";
12
- async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {}) {
12
+ function getTurbopackModules(scope) {
13
13
  const self = globalThis;
14
- self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};
15
- if (!self.__remote_shared_modules__[bundle]) {
16
- self.__remote_shared_modules__[bundle] = {};
14
+ const raw = self[`TURBOPACK_${scope.globalKey}`];
15
+ if (!raw)
16
+ return void 0;
17
+ if (typeof raw === "object" && "__chunks__" in raw) {
18
+ return raw.__chunks__.flat();
17
19
  }
18
- const bundleKey = getBundleKey(bundle);
19
- let modules = self[`TURBOPACK_${bundleKey}`];
20
- if (modules && typeof modules === "object" && "__chunks__" in modules) {
21
- const chunks = modules.__chunks__;
22
- modules = chunks.flat();
20
+ if (Array.isArray(raw)) {
21
+ return raw.flat();
23
22
  }
23
+ return Object.entries(raw).flat();
24
+ }
25
+ async function initializeSharedModules(scope, hostShared = {}, remoteShared = {}) {
26
+ const allModules = getTurbopackModules(scope);
27
+ logDebug(
28
+ "SharedModules",
29
+ `initializeSharedModules: scope="${scope.scopedName}", allModules=${allModules ? allModules.length : "null"}, hostShared=[${Object.keys(hostShared).join(", ")}], remoteShared=${JSON.stringify(remoteShared)}`
30
+ );
24
31
  let sharedModuleInitializer = null;
25
- if (modules) {
26
- const allModules = Array.isArray(modules) ? modules.flat() : Object.entries(modules).flat();
32
+ if (allModules) {
27
33
  const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {
28
34
  if (typeof idOrFunc !== "function") {
29
35
  return false;
@@ -37,72 +43,71 @@ async function initializeSharedModules(bundle, hostShared = {}, remoteShared = {
37
43
  const { sharedModuleId } = REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ?? {};
38
44
  if (sharedModuleId) {
39
45
  const { default: sharedModuleInitializerInstance } = handleTurbopackModule(
40
- bundle,
46
+ scope,
41
47
  sharedModuleId,
42
- `[${bundle}] ${sharedModuleInitializerId}`
48
+ formatRemoteId(scope, String(sharedModuleInitializerId))
43
49
  );
44
50
  sharedModuleInitializer = sharedModuleInitializerInstance;
45
51
  }
46
52
  }
47
53
  if (sharedModuleInitializer) {
48
54
  const { shared } = await sharedModuleInitializer;
49
- const sharedModuleIds = extractSharedModuleIds(
50
- shared,
51
- bundleKey,
52
- self
55
+ const sharedModuleIds = extractSharedModuleIds(shared, scope);
56
+ logDebug(
57
+ "SharedModules",
58
+ `Resolved shared modules for scope="${scope.scopedName}": ${JSON.stringify(sharedModuleIds)}`
53
59
  );
54
60
  return Promise.all(
55
61
  Object.entries(sharedModuleIds).map(async ([id, module]) => {
56
- if (self.__remote_shared_modules__?.[bundle]) {
57
- if (hostShared[module]) {
58
- self.__remote_shared_modules__[bundle][id] = await hostShared[module](bundle);
59
- } else {
60
- logError(
61
- "SharedModules",
62
- `Host shared module "${module}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
63
- );
64
- }
62
+ if (hostShared[module]) {
63
+ scope.sharedModules[id] = await hostShared[module](scope.name);
64
+ } else {
65
+ logError(
66
+ "SharedModules",
67
+ `Host shared module "${module}" not found for ID ${id}. ${DEDUPLICATION_WARNING}`
68
+ );
65
69
  }
66
70
  })
67
71
  );
68
72
  }
73
+ logWarn(
74
+ "SharedModules",
75
+ `No shared module initializer found in bundle for scope="${scope.scopedName}" \u2014 falling back to remoteShared mapping`
76
+ );
77
+ } else {
78
+ logWarn(
79
+ "SharedModules",
80
+ `No TURBOPACK modules found for scope="${scope.scopedName}" (TURBOPACK_${scope.globalKey} is empty)`
81
+ );
69
82
  }
70
83
  return Promise.all(
71
84
  Object.entries(remoteShared).map(async ([id, module]) => {
72
- if (self.__remote_shared_modules__?.[bundle]) {
73
- if (hostShared[module]) {
74
- const normalizedId = id.replace("[app-ssr]", "[app-client]");
75
- self.__remote_shared_modules__[bundle][normalizedId] = await hostShared[module](bundle);
76
- } else {
77
- logError(
78
- "SharedModules",
79
- `Shared module "${module}" not found for "${bundle}". ${DEDUPLICATION_WARNING}`
80
- );
81
- }
85
+ if (hostShared[module]) {
86
+ const normalizedId = id.replace("[app-ssr]", "[app-client]");
87
+ scope.sharedModules[normalizedId] = await hostShared[module](
88
+ scope.name
89
+ );
90
+ } else {
91
+ logError(
92
+ "SharedModules",
93
+ `Shared module "${module}" not found for "${scope.name}". ${DEDUPLICATION_WARNING}`
94
+ );
82
95
  }
83
96
  })
84
97
  );
85
98
  }
86
- function extractSharedModuleIds(shared, bundleKey, self) {
99
+ function extractSharedModuleIds(shared, scope) {
87
100
  return Object.entries(shared).filter(([, value]) => typeof value === "function").reduce((acc, [key, value]) => {
88
101
  const { asyncSharedModuleId } = ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};
89
102
  if (asyncSharedModuleId) {
90
103
  const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);
91
104
  let asyncSharedModule;
92
- let turbopackModules = self[`TURBOPACK_${bundleKey}`];
93
- if (turbopackModules && typeof turbopackModules === "object" && "__chunks__" in turbopackModules) {
94
- const chunks = turbopackModules.__chunks__;
95
- turbopackModules = chunks.flat();
96
- }
97
- const newAllModules = Array.isArray(turbopackModules) ? turbopackModules.flat() : turbopackModules ? Object.entries(turbopackModules).flatMap(([key2, value2]) => [
98
- key2,
99
- value2
100
- ]) : [];
101
- const asyncSharedModuleIdIndex = newAllModules.indexOf(
105
+ const turbopackModules = getTurbopackModules(scope) ?? [];
106
+ const asyncSharedModuleIdIndex = turbopackModules.indexOf(
102
107
  asyncSharedModuleIdNumber
103
108
  );
104
- if (asyncSharedModuleIdIndex !== -1 && typeof newAllModules[asyncSharedModuleIdIndex + 1] === "function") {
105
- asyncSharedModule = newAllModules[asyncSharedModuleIdIndex + 1];
109
+ if (asyncSharedModuleIdIndex !== -1 && typeof turbopackModules[asyncSharedModuleIdIndex + 1] === "function") {
110
+ asyncSharedModule = turbopackModules[asyncSharedModuleIdIndex + 1];
106
111
  }
107
112
  if (asyncSharedModule) {
108
113
  const asyncSharedModuleCode = asyncSharedModule.toString();
@@ -116,12 +121,13 @@ function extractSharedModuleIds(shared, bundleKey, self) {
116
121
  return acc;
117
122
  }, {});
118
123
  }
119
- function getSharedModule(bundle, id) {
120
- const self = globalThis;
121
- for (const [key, value] of Object.entries(
122
- self.__remote_shared_modules__?.[bundle] ?? {}
123
- )) {
124
- if (typeof value !== "undefined" && (typeof id === "string" && id.includes(key) || id === key)) {
124
+ function getSharedModule(scope, id) {
125
+ const idStr = String(id);
126
+ if (scope.sharedModules[idStr] !== void 0) {
127
+ return scope.sharedModules[idStr];
128
+ }
129
+ for (const [key, value] of Object.entries(scope.sharedModules)) {
130
+ if (typeof value !== "undefined" && idStr !== key && idStr.endsWith(key)) {
125
131
  return value;
126
132
  }
127
133
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/runtime/turbopack/shared-modules.ts"],"sourcesContent":["import { getBundleKey } from '#internal/runtime/constants';\nimport type { GlobalScope } from '#internal/runtime/types';\nimport { logError } from '#internal/utils/logger';\nimport { handleTurbopackModule } from './module';\nimport {\n ASYNC_MODULE_ALL_RE,\n ASYNC_MODULE_LOADER_RE,\n ASYNC_MODULE_RESOLVE_RE,\n REMOTE_SHARED_ASSIGNMENT_RE,\n REMOTE_SHARED_MARKER_RE,\n} from './patterns';\n\nconst DEDUPLICATION_WARNING =\n 'This module will not be deduplicated — the remote may load its own copy, ' +\n 'which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).';\n\n/**\n * Initializes shared modules between the host application and remote components.\n * This enables sharing of common dependencies like React to avoid duplicate instances.\n *\n * The function works by:\n * 1. Looking for a shared module initializer in the Turbopack bundle\n * 2. Extracting module IDs from the initializer\n * 3. Loading the corresponding host modules and mapping them to the remote's expected IDs\n */\nexport async function initializeSharedModules(\n bundle: string,\n hostShared: Record<string, (bundle?: string) => Promise<unknown>> = {},\n remoteShared: Record<string, string> = {},\n // biome-ignore lint/suspicious/noConfusingVoidType: Runtime is undefined but in TS land service it is void\n): Promise<void[]> {\n const self = globalThis as GlobalScope & {\n __next_f?: unknown[];\n };\n self.__remote_shared_modules__ = self.__remote_shared_modules__ ?? {};\n\n if (!self.__remote_shared_modules__[bundle]) {\n self.__remote_shared_modules__[bundle] = {};\n }\n\n // prepare to load shared module initializers from the Turbopack bundle\n const bundleKey = getBundleKey(bundle);\n let modules = self[`TURBOPACK_${bundleKey}`] as\n | [unknown, Record<string, unknown>][]\n | Record<string, unknown>\n | undefined;\n\n // Check for new push-based format with __chunks__\n if (modules && typeof modules === 'object' && '__chunks__' in modules) {\n const chunks = (modules as { __chunks__: unknown[][] }).__chunks__;\n // Flatten chunks into module array format\n modules = chunks.flat() as [unknown, Record<string, unknown>][];\n }\n let sharedModuleInitializer: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }> | null = null;\n\n // find the shared module initializer in the Turbopack bundle\n if (modules) {\n // Handle both array and object formats for modules\n const allModules = Array.isArray(modules)\n ? modules.flat()\n : Object.entries(modules).flat();\n // find the shared module initializer module id by looking for\n // a function that contains the TURBOPACK_REMOTE_SHARED pattern\n const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {\n if (typeof idOrFunc !== 'function') {\n return false;\n }\n const funcCode = idOrFunc.toString();\n return REMOTE_SHARED_MARKER_RE.test(funcCode);\n });\n\n // if found, extract the shared module initializer\n // first element in the array is always the source script element\n if (sharedModuleInitializerIndex > 0) {\n const sharedModuleInitializerCode = (\n allModules[sharedModuleInitializerIndex] as () => unknown\n ).toString();\n const sharedModuleInitializerId = allModules[\n sharedModuleInitializerIndex - 1\n ] as string | number;\n // extract the shared module id from the function code\n const { sharedModuleId } =\n REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ??\n {};\n // load the shared module initializer using the extracted module id\n if (sharedModuleId) {\n const { default: sharedModuleInitializerInstance } =\n handleTurbopackModule(\n bundle,\n sharedModuleId,\n `[${bundle}] ${sharedModuleInitializerId}`,\n ) as {\n default: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }>;\n };\n sharedModuleInitializer = sharedModuleInitializerInstance;\n }\n }\n\n // if we have a shared module initializer, load the shared modules from the host application\n if (sharedModuleInitializer) {\n const { shared } = await sharedModuleInitializer;\n // map shared module ids to their initializer functions\n const sharedModuleIds = extractSharedModuleIds(\n shared,\n bundleKey,\n self as GlobalScope,\n );\n\n // load shared modules from the host application\n return Promise.all(\n Object.entries(sharedModuleIds).map(async ([id, module]) => {\n if (self.__remote_shared_modules__?.[bundle]) {\n if (hostShared[module]) {\n self.__remote_shared_modules__[bundle][id] =\n await hostShared[module](bundle);\n } else {\n logError(\n 'SharedModules',\n `Host shared module \"${module}\" not found for ID ${id}. ${DEDUPLICATION_WARNING}`,\n );\n }\n }\n }),\n );\n }\n }\n\n // fallback: ensure that the shared modules are initialized using remoteShared mapping\n return Promise.all(\n Object.entries(remoteShared).map(async ([id, module]) => {\n if (self.__remote_shared_modules__?.[bundle]) {\n if (hostShared[module]) {\n const normalizedId = id.replace('[app-ssr]', '[app-client]');\n self.__remote_shared_modules__[bundle][normalizedId] =\n await hostShared[module](bundle);\n } else {\n logError(\n 'SharedModules',\n `Shared module \"${module}\" not found for \"${bundle}\". ${DEDUPLICATION_WARNING}`,\n );\n }\n }\n }),\n );\n}\n\n/**\n * Extracts shared module IDs from the shared module initializer functions.\n * This parses the minified Turbopack code to find the module ID mappings.\n */\nfunction extractSharedModuleIds(\n shared: Record<string, string | (() => Promise<unknown>)>,\n bundleKey: string,\n self: GlobalScope,\n): Record<string, string> {\n return Object.entries(shared)\n .filter(([, value]) => typeof value === 'function')\n .reduce<Record<string, string>>((acc, [key, value]) => {\n const { asyncSharedModuleId } =\n ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};\n\n if (asyncSharedModuleId) {\n const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);\n\n let asyncSharedModule;\n let turbopackModules = self[`TURBOPACK_${bundleKey}`] as\n | unknown[]\n | Record<string, unknown>\n | undefined;\n\n // Check for new push-based format with __chunks__\n if (\n turbopackModules &&\n typeof turbopackModules === 'object' &&\n '__chunks__' in turbopackModules\n ) {\n const chunks = (turbopackModules as { __chunks__: unknown[][] })\n .__chunks__;\n turbopackModules = chunks.flat() as unknown[];\n }\n\n const newAllModules = Array.isArray(turbopackModules)\n ? turbopackModules.flat()\n : turbopackModules\n ? Object.entries(turbopackModules).flatMap(([key, value]) => [\n key,\n value,\n ])\n : [];\n const asyncSharedModuleIdIndex = newAllModules.indexOf(\n asyncSharedModuleIdNumber,\n );\n if (\n asyncSharedModuleIdIndex !== -1 &&\n typeof newAllModules[asyncSharedModuleIdIndex + 1] === 'function'\n ) {\n asyncSharedModule = newAllModules[\n asyncSharedModuleIdIndex + 1\n ] as () => unknown;\n }\n if (asyncSharedModule) {\n const asyncSharedModuleCode = asyncSharedModule.toString();\n // Try both Promise.resolve and Promise.all patterns\n const { sharedModuleId } =\n ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ??\n ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ??\n {};\n // map the shared module id to the actual module name\n acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(\n '__remote_shared_module_',\n '',\n );\n }\n }\n return acc;\n }, {});\n}\n\n/**\n * Returns a shared module for the given bundle and module ID.\n * Shared modules are common dependencies like React that are provided by the host.\n */\nexport function getSharedModule(bundle: string, id: string | number): unknown {\n const self = globalThis as {\n __remote_shared_modules__?: Record<string, Record<string, unknown>>;\n };\n\n for (const [key, value] of Object.entries(\n self.__remote_shared_modules__?.[bundle] ?? {},\n )) {\n if (\n typeof value !== 'undefined' &&\n ((typeof id === 'string' && id.includes(key)) || id === key)\n ) {\n return value;\n }\n }\n return null;\n}\n"],"mappings":"AAAA,SAAS,oBAAoB;AAE7B,SAAS,gBAAgB;AACzB,SAAS,6BAA6B;AACtC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,MAAM,wBACJ;AAYF,eAAsB,wBACpB,QACA,aAAoE,CAAC,GACrE,eAAuC,CAAC,GAEvB;AACjB,QAAM,OAAO;AAGb,OAAK,4BAA4B,KAAK,6BAA6B,CAAC;AAEpE,MAAI,CAAC,KAAK,0BAA0B,MAAM,GAAG;AAC3C,SAAK,0BAA0B,MAAM,IAAI,CAAC;AAAA,EAC5C;AAGA,QAAM,YAAY,aAAa,MAAM;AACrC,MAAI,UAAU,KAAK,aAAa,WAAW;AAM3C,MAAI,WAAW,O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;AACA,4BAAoB,cAClB,2BAA2B,CAC7B;AAAA,MACF;AACA,UAAI,mBAAmB;AACrB,cAAM,wBAAwB,kBAAkB,SAAS;AAEzD,cAAM,EAAE,eAAe,IACrB,wBAAwB,KAAK,qBAAqB,GAAG,UACrD,oBAAoB,KAAK,qBAAqB,GAAG,UACjD,CAAC;AAEH,YAAI,kBAAkB,mBAAmB,IAAI,IAAI;AAAA,UAC/C;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACT;AAMO,SAAS,gBAAgB,QAAgB,IAA8B;AAC5E,QAAM,OAAO;AAIb,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO;AAAA,IAChC,KAAK,4BAA4B,MAAM,KAAK,CAAC;AAAA,EAC/C,GAAG;AACD,QACE,OAAO,UAAU,gBACf,OAAO,OAAO,YAAY,GAAG,SAAS,GAAG,KAAM,OAAO,MACxD;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["key","value"]}
1
+ {"version":3,"sources":["../../../../src/runtime/turbopack/shared-modules.ts"],"sourcesContent":["import type { GlobalScope } from '#internal/runtime/types';\nimport { logDebug, logError, logWarn } from '#internal/utils/logger';\nimport { handleTurbopackModule } from './module';\nimport {\n ASYNC_MODULE_ALL_RE,\n ASYNC_MODULE_LOADER_RE,\n ASYNC_MODULE_RESOLVE_RE,\n REMOTE_SHARED_ASSIGNMENT_RE,\n REMOTE_SHARED_MARKER_RE,\n} from './patterns';\nimport { formatRemoteId, type RemoteScope } from './remote-scope';\n\nconst DEDUPLICATION_WARNING =\n 'This module will not be deduplicated — the remote may load its own copy, ' +\n 'which can cause duplicate instance errors (e.g. invalid hook calls if React is loaded twice).';\n\n/**\n * Reads the TURBOPACK global for a scope, normalizing the push-based\n * `__chunks__` format into a flat module list when needed.\n */\nfunction getTurbopackModules(scope: RemoteScope): unknown[] | undefined {\n const self = globalThis as GlobalScope;\n const raw = self[`TURBOPACK_${scope.globalKey}`];\n if (!raw) return undefined;\n\n if (typeof raw === 'object' && '__chunks__' in (raw as object)) {\n return (raw as { __chunks__: unknown[][] }).__chunks__.flat();\n }\n if (Array.isArray(raw)) {\n return (raw as unknown[]).flat();\n }\n return Object.entries(raw as Record<string, unknown>).flat();\n}\n\n/**\n * Initializes shared modules between the host application and remote components.\n * This enables sharing of common dependencies like React to avoid duplicate instances.\n *\n * The function works by:\n * 1. Looking for a shared module initializer in the Turbopack bundle\n * 2. Extracting module IDs from the initializer\n * 3. Loading the corresponding host modules and mapping them to the remote's expected IDs\n */\nexport async function initializeSharedModules(\n scope: RemoteScope,\n hostShared: Record<string, (bundle?: string) => Promise<unknown>> = {},\n remoteShared: Record<string, string> = {},\n // biome-ignore lint/suspicious/noConfusingVoidType: Runtime is undefined but in TS land service it is void\n): Promise<void[]> {\n const allModules = getTurbopackModules(scope);\n\n logDebug(\n 'SharedModules',\n `initializeSharedModules: scope=\"${scope.scopedName}\", ` +\n `allModules=${allModules ? allModules.length : 'null'}, ` +\n `hostShared=[${Object.keys(hostShared).join(', ')}], ` +\n `remoteShared=${JSON.stringify(remoteShared)}`,\n );\n\n let sharedModuleInitializer: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }> | null = null;\n\n // find the shared module initializer in the Turbopack bundle\n if (allModules) {\n // find the shared module initializer module id by looking for\n // a function that contains the TURBOPACK_REMOTE_SHARED pattern\n const sharedModuleInitializerIndex = allModules.findIndex((idOrFunc) => {\n if (typeof idOrFunc !== 'function') {\n return false;\n }\n const funcCode = idOrFunc.toString();\n return REMOTE_SHARED_MARKER_RE.test(funcCode);\n });\n\n // if found, extract the shared module initializer\n // first element in the array is always the source script element\n if (sharedModuleInitializerIndex > 0) {\n const sharedModuleInitializerCode = (\n allModules[sharedModuleInitializerIndex] as () => unknown\n ).toString();\n const sharedModuleInitializerId = allModules[\n sharedModuleInitializerIndex - 1\n ] as string | number;\n // extract the shared module id from the function code\n const { sharedModuleId } =\n REMOTE_SHARED_ASSIGNMENT_RE.exec(sharedModuleInitializerCode)?.groups ??\n {};\n // load the shared module initializer using the extracted module id\n if (sharedModuleId) {\n const { default: sharedModuleInitializerInstance } =\n handleTurbopackModule(\n scope,\n sharedModuleId,\n formatRemoteId(scope, String(sharedModuleInitializerId)),\n ) as {\n default: Promise<{\n shared: Record<string, string | (() => Promise<unknown>)>;\n }>;\n };\n sharedModuleInitializer = sharedModuleInitializerInstance;\n }\n }\n\n // if we have a shared module initializer, load the shared modules from the host application\n if (sharedModuleInitializer) {\n const { shared } = await sharedModuleInitializer;\n // map shared module ids to their initializer functions\n const sharedModuleIds = extractSharedModuleIds(shared, scope);\n logDebug(\n 'SharedModules',\n `Resolved shared modules for scope=\"${scope.scopedName}\": ${JSON.stringify(sharedModuleIds)}`,\n );\n\n // load shared modules from the host application\n return Promise.all(\n Object.entries(sharedModuleIds).map(async ([id, module]) => {\n if (hostShared[module]) {\n scope.sharedModules[id] = await hostShared[module](scope.name);\n } else {\n logError(\n 'SharedModules',\n `Host shared module \"${module}\" not found for ID ${id}. ${DEDUPLICATION_WARNING}`,\n );\n }\n }),\n );\n }\n\n logWarn(\n 'SharedModules',\n `No shared module initializer found in bundle for scope=\"${scope.scopedName}\" — falling back to remoteShared mapping`,\n );\n } else {\n logWarn(\n 'SharedModules',\n `No TURBOPACK modules found for scope=\"${scope.scopedName}\" (TURBOPACK_${scope.globalKey} is empty)`,\n );\n }\n\n // fallback: ensure that the shared modules are initialized using remoteShared mapping\n return Promise.all(\n Object.entries(remoteShared).map(async ([id, module]) => {\n if (hostShared[module]) {\n const normalizedId = id.replace('[app-ssr]', '[app-client]');\n scope.sharedModules[normalizedId] = await hostShared[module](\n scope.name,\n );\n } else {\n logError(\n 'SharedModules',\n `Shared module \"${module}\" not found for \"${scope.name}\". ${DEDUPLICATION_WARNING}`,\n );\n }\n }),\n );\n}\n\n/**\n * Extracts shared module IDs from the shared module initializer functions.\n * This parses the minified Turbopack code to find the module ID mappings.\n */\nfunction extractSharedModuleIds(\n shared: Record<string, string | (() => Promise<unknown>)>,\n scope: RemoteScope,\n): Record<string, string> {\n return Object.entries(shared)\n .filter(([, value]) => typeof value === 'function')\n .reduce<Record<string, string>>((acc, [key, value]) => {\n const { asyncSharedModuleId } =\n ASYNC_MODULE_LOADER_RE.exec(value.toString())?.groups ?? {};\n\n if (asyncSharedModuleId) {\n const asyncSharedModuleIdNumber = Number(asyncSharedModuleId);\n\n let asyncSharedModule: (() => unknown) | undefined;\n const turbopackModules = getTurbopackModules(scope) ?? [];\n const asyncSharedModuleIdIndex = turbopackModules.indexOf(\n asyncSharedModuleIdNumber,\n );\n if (\n asyncSharedModuleIdIndex !== -1 &&\n typeof turbopackModules[asyncSharedModuleIdIndex + 1] === 'function'\n ) {\n asyncSharedModule = turbopackModules[\n asyncSharedModuleIdIndex + 1\n ] as () => unknown;\n }\n if (asyncSharedModule) {\n const asyncSharedModuleCode = asyncSharedModule.toString();\n // Try both Promise.resolve and Promise.all patterns\n const { sharedModuleId } =\n ASYNC_MODULE_RESOLVE_RE.exec(asyncSharedModuleCode)?.groups ??\n ASYNC_MODULE_ALL_RE.exec(asyncSharedModuleCode)?.groups ??\n {};\n // map the shared module id to the actual module name\n acc[sharedModuleId ?? asyncSharedModuleId] = key.replace(\n '__remote_shared_module_',\n '',\n );\n }\n }\n return acc;\n }, {});\n}\n\n/**\n * Returns a shared module for the given scope and module ID.\n * Shared modules are common dependencies like React that are provided by the host.\n */\nexport function getSharedModule(\n scope: RemoteScope,\n id: string | number,\n): unknown {\n const idStr = String(id);\n\n // Exact match first (covers both string and numeric IDs)\n if (scope.sharedModules[idStr] !== undefined) {\n return scope.sharedModules[idStr];\n }\n\n // Fallback: the id may be a bundle-prefixed string like \"[bundle] /react/index.js\"\n // that contains the shared module key as a suffix. Only match when the key\n // appears as a complete suffix segment to avoid \"react\" matching \"react-dom\".\n for (const [key, value] of Object.entries(scope.sharedModules)) {\n if (typeof value !== 'undefined' && idStr !== key && idStr.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