remote-components 0.0.16 → 0.0.18

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 (47) hide show
  1. package/dist/html/host.cjs +185 -79
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +184 -79
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/next/config.cjs +53 -12
  6. package/dist/next/config.cjs.map +1 -1
  7. package/dist/next/config.js +53 -12
  8. package/dist/next/config.js.map +1 -1
  9. package/dist/next/host/app-router-client.cjs +1 -1
  10. package/dist/next/host/app-router-client.cjs.map +1 -1
  11. package/dist/next/host/app-router-client.js +1 -1
  12. package/dist/next/host/app-router-client.js.map +1 -1
  13. package/dist/next/host/app-router-server.cjs +5 -1
  14. package/dist/next/host/app-router-server.cjs.map +1 -1
  15. package/dist/next/host/app-router-server.d.ts +3 -1
  16. package/dist/next/host/app-router-server.js +5 -1
  17. package/dist/next/host/app-router-server.js.map +1 -1
  18. package/dist/next/host/client/index.cjs +1 -1
  19. package/dist/next/host/client/index.cjs.map +1 -1
  20. package/dist/next/host/client/index.js +1 -1
  21. package/dist/next/host/client/index.js.map +1 -1
  22. package/dist/next/host/pages-router-client.cjs +2 -2
  23. package/dist/next/host/pages-router-client.cjs.map +1 -1
  24. package/dist/next/host/pages-router-client.js +1 -1
  25. package/dist/next/host/pages-router-client.js.map +1 -1
  26. package/dist/next/host/pages-router-server.cjs +99 -18
  27. package/dist/next/host/pages-router-server.cjs.map +1 -1
  28. package/dist/next/host/pages-router-server.js +90 -19
  29. package/dist/next/host/pages-router-server.js.map +1 -1
  30. package/dist/next/index.cjs.map +1 -1
  31. package/dist/next/index.js.map +1 -1
  32. package/dist/react/index.cjs +91 -6
  33. package/dist/react/index.cjs.map +1 -1
  34. package/dist/react/index.d.ts +2 -2
  35. package/dist/react/index.js +92 -6
  36. package/dist/react/index.js.map +1 -1
  37. package/dist/shared/client/remote-component.cjs +128 -60
  38. package/dist/shared/client/remote-component.cjs.map +1 -1
  39. package/dist/shared/client/remote-component.d.ts +5 -2
  40. package/dist/shared/client/remote-component.js +127 -60
  41. package/dist/shared/client/remote-component.js.map +1 -1
  42. package/dist/shared/ssr/fetch-remote-component.cjs +5 -2
  43. package/dist/shared/ssr/fetch-remote-component.cjs.map +1 -1
  44. package/dist/shared/ssr/fetch-remote-component.d.ts +4 -1
  45. package/dist/shared/ssr/fetch-remote-component.js +5 -2
  46. package/dist/shared/ssr/fetch-remote-component.js.map +1 -1
  47. package/package.json +1 -1
@@ -152,27 +152,102 @@ var init_next_client_pages_loader = __esm({
152
152
  }
153
153
  });
154
154
 
155
+ // src/shared/client/polyfill.tsx
156
+ function sharedPolyfills(shared) {
157
+ const self = globalThis;
158
+ const polyfill = {
159
+ "next/dist/client/components/navigation": self.__remote_component_host_shared_modules__?.["next/navigation"] ?? shared?.["next/navigation"] ?? (() => Promise.resolve({
160
+ useRouter() {
161
+ return {
162
+ push: (routerUrl) => {
163
+ history.pushState({}, "", routerUrl);
164
+ },
165
+ replace: (routerUrl) => {
166
+ history.replaceState({}, "", routerUrl);
167
+ },
168
+ back: () => {
169
+ history.back();
170
+ }
171
+ };
172
+ },
173
+ useSearchParams() {
174
+ return new URLSearchParams(location.search);
175
+ },
176
+ __esModule: true
177
+ })),
178
+ "next/dist/client/app-dir/link": self.__remote_component_host_shared_modules__?.["next/link"] ?? shared?.["next/link"] ?? (() => Promise.resolve({
179
+ default: ({
180
+ children,
181
+ ...props
182
+ }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { ...props, href: props.href, suppressHydrationWarning: true, children: children ?? null }),
183
+ __esModule: true
184
+ })),
185
+ "next/dist/client/app-dir/form": self.__remote_component_host_shared_modules__?.["next/form"] ?? shared?.["next/form"] ?? (() => Promise.resolve({
186
+ default: () => {
187
+ throw new Error("Next.js <Form> component not implemented");
188
+ },
189
+ __esModule: true
190
+ })),
191
+ "next/dist/client/image-component": self.__remote_component_host_shared_modules__?.["next/image"] ?? shared?.["next/image"] ?? (() => Promise.resolve({
192
+ Image: (props) => (
193
+ // eslint-disable-next-line @next/next/no-img-element, jsx-a11y/alt-text
194
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
195
+ "img",
196
+ {
197
+ ...props,
198
+ src: props.src,
199
+ suppressHydrationWarning: true
200
+ }
201
+ )
202
+ ),
203
+ __esModule: true
204
+ })),
205
+ "next/dist/client/script": self.__remote_component_host_shared_modules__?.["next/script"] ?? shared?.["next/script"] ?? (() => Promise.resolve({
206
+ // TODO: implement <Script> component for non-Next.js host applications
207
+ // do not throw an error for now
208
+ default: () => null,
209
+ __esModule: true
210
+ }))
211
+ };
212
+ polyfill["next/link"] = polyfill["next/dist/client/app-dir/link"];
213
+ polyfill["next/form"] = polyfill["next/dist/client/app-dir/form"];
214
+ polyfill["next/image"] = polyfill["next/dist/client/image-component"];
215
+ polyfill["next/dist/api/image"] = polyfill["next/dist/client/image-component"];
216
+ polyfill["next/script"] = polyfill["next/dist/client/script"];
217
+ return polyfill;
218
+ }
219
+ var import_jsx_runtime;
220
+ var init_polyfill = __esm({
221
+ "src/shared/client/polyfill.tsx"() {
222
+ "use strict";
223
+ import_jsx_runtime = require("react/jsx-runtime");
224
+ }
225
+ });
226
+
155
227
  // src/html/host/runtime/webpack.ts
156
228
  var webpack_exports = {};
157
229
  __export(webpack_exports, {
158
230
  webpackRuntime: () => webpackRuntime
159
231
  });
160
- async function webpackRuntime(shared, remoteShared) {
232
+ async function webpackRuntime(bundle, shared, remoteShared) {
161
233
  const self = globalThis;
162
- self.__DISABLE_WEBPACK_EXEC__ = true;
234
+ if (!self.__DISABLE_WEBPACK_EXEC__) {
235
+ self.__DISABLE_WEBPACK_EXEC__ = {};
236
+ }
237
+ self.__DISABLE_WEBPACK_EXEC__[bundle] = true;
163
238
  if (typeof self.__webpack_require__ !== "function" && self.__webpack_require_type__ !== "turbopack") {
164
239
  self.__webpack_require__ = (remoteId) => {
165
240
  const re = /\[(?<bundle>[^\]]+)\] (?<id>.*)/;
166
241
  const match = re.exec(remoteId);
167
- const bundle = match?.groups?.bundle;
242
+ const remoteBundle = match?.groups?.bundle;
168
243
  const id = match?.groups?.id;
169
- if (!(id && bundle)) {
244
+ if (!(id && remoteBundle)) {
170
245
  throw new Error(`Module not found: "${remoteId}"`);
171
246
  }
172
- if (typeof self.__remote_webpack_require__?.[bundle] !== "function") {
173
- throw new Error(`Remote component loading error "${bundle}"`);
247
+ if (typeof self.__remote_webpack_require__?.[remoteBundle] !== "function") {
248
+ throw new Error(`Remote component loading error "${remoteBundle}"`);
174
249
  }
175
- return self.__remote_webpack_require__[bundle](id);
250
+ return self.__remote_webpack_require__[remoteBundle](id);
176
251
  };
177
252
  self.__webpack_chunk_load__ = () => {
178
253
  return Promise.resolve([]);
@@ -181,7 +256,7 @@ async function webpackRuntime(shared, remoteShared) {
181
256
  const {
182
257
  default: { createFromReadableStream }
183
258
  } = await import("react-server-dom-webpack/client.browser");
184
- async function preloadScripts(scripts, url, bundle, _) {
259
+ async function preloadScripts(scripts, url, remoteBundle, _) {
185
260
  await Promise.all(
186
261
  scripts.map((script) => {
187
262
  return new Promise((resolve2, reject) => {
@@ -210,6 +285,7 @@ async function webpackRuntime(shared, remoteShared) {
210
285
  })
211
286
  );
212
287
  const hostShared = {
288
+ ...sharedPolyfills(shared),
213
289
  ...self.__remote_component_host_shared_modules__,
214
290
  ...shared
215
291
  };
@@ -237,7 +313,7 @@ async function webpackRuntime(shared, remoteShared) {
237
313
  return Promise.resolve(value);
238
314
  })
239
315
  );
240
- applySharedModules(bundle, resolve);
316
+ applySharedModules(remoteBundle, resolve);
241
317
  }
242
318
  return {
243
319
  self,
@@ -252,6 +328,7 @@ var init_webpack = __esm({
252
328
  "use strict";
253
329
  init_shared_modules();
254
330
  init_next_client_pages_loader();
331
+ init_polyfill();
255
332
  }
256
333
  });
257
334
 
@@ -396,6 +473,7 @@ async function handleTurbopackChunk(code, bundle, url) {
396
473
  script.onload = () => {
397
474
  URL.revokeObjectURL(scriptUrl);
398
475
  scriptResolve(void 0);
476
+ script.remove();
399
477
  };
400
478
  script.onerror = (error) => {
401
479
  URL.revokeObjectURL(scriptUrl);
@@ -404,6 +482,7 @@ async function handleTurbopackChunk(code, bundle, url) {
404
482
  `Failed to load script: ${error instanceof Error ? error.message : String(error)}`
405
483
  )
406
484
  );
485
+ script.remove();
407
486
  };
408
487
  document.head.appendChild(script);
409
488
  });
@@ -468,20 +547,11 @@ function initializeSharedModules(bundle, shared = {}, remoteShared = {}) {
468
547
  return Promise.all(
469
548
  Object.entries(remoteShared).map(async ([id, module2]) => {
470
549
  if (self.__remote_shared_modules__?.[bundle]) {
471
- self.__remote_shared_modules__[bundle][id.replace("[app-ssr]", "[app-client]")] = await (shared[module2] ?? (() => Promise.resolve(
472
- new Proxy(
473
- {},
474
- {
475
- get(_, prop) {
476
- if (prop !== "then") {
477
- console.warn(
478
- `Shared dependency "${module2}" not found for "${bundle}" when trying to import "${prop}".`
479
- );
480
- }
481
- }
482
- }
483
- )
484
- )))();
550
+ if (shared[module2]) {
551
+ self.__remote_shared_modules__[bundle][id.replace("[app-ssr]", "[app-client]")] = await shared[module2]();
552
+ } else {
553
+ console.error(`Shared module "${module2}" not found for "${bundle}".`);
554
+ }
485
555
  }
486
556
  })
487
557
  );
@@ -504,9 +574,17 @@ function handleTurbopackModule(bundle, moduleId, id) {
504
574
  let moduleInit;
505
575
  for (const mod of modules ?? []) {
506
576
  if (typeof mod[1] === "string") {
507
- const index = mod.indexOf(moduleId);
577
+ let index = mod.indexOf(moduleId);
578
+ if (index === -1) {
579
+ index = mod.findIndex(
580
+ (m) => typeof m === "string" && m.startsWith(moduleId)
581
+ );
582
+ }
508
583
  if (index !== -1) {
509
- moduleInit = mod[index + 1];
584
+ while (typeof mod[index] !== "function" && index < mod.length) {
585
+ index++;
586
+ }
587
+ moduleInit = mod[index];
510
588
  break;
511
589
  }
512
590
  } else {
@@ -534,58 +612,78 @@ function handleTurbopackModule(bundle, moduleId, id) {
534
612
  `Module ${id} not found in bundle ${bundle} with id ${moduleId}`
535
613
  );
536
614
  }
537
- moduleInit({
538
- // HMR not implemented for Remote Components
539
- k: {
540
- register() {
541
- },
542
- registerExports() {
615
+ self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
616
+ if (!self.__remote_components_turbopack_global__) {
617
+ self.__remote_components_turbopack_global__ = {};
618
+ }
619
+ if (!self.__remote_components_turbopack_global__[bundle]) {
620
+ self.__remote_components_turbopack_global__[bundle] = {};
621
+ }
622
+ moduleInit(
623
+ {
624
+ // HMR not implemented for Remote Components
625
+ k: {
626
+ register() {
627
+ },
628
+ registerExports() {
629
+ },
630
+ signature() {
631
+ return () => {
632
+ };
633
+ }
543
634
  },
544
- signature() {
545
- return () => {
546
- };
547
- }
548
- },
549
- s(m) {
550
- let mod = m;
551
- if (Array.isArray(m)) {
552
- mod = {};
553
- for (let i = 0; i < m.length; i += 2) {
554
- const key = m[i];
555
- const value = m[i + 1];
556
- if (typeof key === "string" && typeof value === "function") {
557
- mod[key] = value;
635
+ s(m) {
636
+ let mod = m;
637
+ if (Array.isArray(m)) {
638
+ mod = {};
639
+ const keys = [];
640
+ for (const current of m) {
641
+ if (typeof current === "string") {
642
+ keys.push(current);
643
+ } else if (typeof current === "function") {
644
+ while (keys.length > 0) {
645
+ const key = keys.shift();
646
+ if (key) {
647
+ mod[key] = current;
648
+ }
649
+ }
650
+ }
558
651
  }
559
652
  }
560
- }
561
- for (const [key, value] of Object.entries(mod)) {
562
- exports[key] = value;
563
- exportNames.add(key);
564
- }
565
- },
566
- i(iid) {
567
- const { exportSource, exportName } = /\s+<export (?<exportSource>.*?) as (?<exportName>.*?)>$/.exec(iid)?.groups ?? {};
568
- const normalizedId = iid.replace(/\s+<export(?<specifier>.*)>$/, "");
569
- const mod = self.__webpack_require__?.(
570
- `[${bundle}] ${normalizedId}`
571
- );
572
- if (exportSource && exportName) {
573
- mod[exportName] = mod[exportSource];
574
- }
575
- return mod;
576
- },
577
- r(rid) {
578
- return self.__webpack_require__?.(`[${bundle}] ${rid}`);
653
+ for (const [key, value] of Object.entries(mod)) {
654
+ exports[key] = value;
655
+ exportNames.add(key);
656
+ }
657
+ },
658
+ i(iid) {
659
+ const { exportSource, exportName } = /\s+<export (?<exportSource>.*?) as (?<exportName>.*?)>$/.exec(iid)?.groups ?? {};
660
+ const normalizedId = iid.replace(/\s+<export(?<specifier>.*)>$/, "");
661
+ const mod = self.__webpack_require__?.(
662
+ `[${bundle}] ${normalizedId}`
663
+ );
664
+ if (exportSource && exportName && typeof mod[exportSource] !== "undefined" && typeof mod[exportName] === "undefined") {
665
+ mod[exportName] = mod[exportSource];
666
+ }
667
+ return mod;
668
+ },
669
+ r(rid) {
670
+ return self.__webpack_require__?.(`[${bundle}] ${rid}`);
671
+ },
672
+ v(value) {
673
+ exports.default = value;
674
+ },
675
+ g: self.__remote_components_turbopack_global__[bundle],
676
+ m: moduleExports,
677
+ e: exports
579
678
  },
580
- m: moduleExports,
581
- e: exports
582
- });
679
+ moduleExports,
680
+ exports
681
+ );
583
682
  for (const name of exportNames) {
584
683
  if (typeof exports[name] === "function") {
585
684
  exports[name] = exports[name]();
586
685
  }
587
686
  }
588
- self.__remote_components_turbopack_modules__[bundle][moduleId] = moduleExports.exports;
589
687
  return moduleExports.exports;
590
688
  }
591
689
  var init_webpack_adapter = __esm({
@@ -602,12 +700,18 @@ __export(turbopack_exports, {
602
700
  });
603
701
  async function turbopackRuntime(url, bundle, shared, remoteShared) {
604
702
  const self = globalThis;
703
+ const hostShared = {
704
+ ...sharedPolyfills(shared),
705
+ ...self.__remote_component_host_shared_modules__,
706
+ ...shared,
707
+ ...self.__remote_component_shared__
708
+ };
605
709
  await setupWebpackRuntime(
606
710
  "turbopack",
607
711
  [],
608
712
  url,
609
713
  bundle,
610
- { ...shared, ...self.__remote_component_shared__ },
714
+ hostShared,
611
715
  remoteShared
612
716
  );
613
717
  const {
@@ -621,11 +725,7 @@ async function turbopackRuntime(url, bundle, shared, remoteShared) {
621
725
  })),
622
726
  url,
623
727
  bundle,
624
- {
625
- ...self.__remote_component_host_shared_modules__,
626
- ...shared,
627
- ...self.__remote_component_shared__
628
- },
728
+ hostShared,
629
729
  remoteShared
630
730
  );
631
731
  }
@@ -643,6 +743,7 @@ var init_turbopack = __esm({
643
743
  init_shared_modules();
644
744
  init_next_client_pages_loader();
645
745
  init_webpack_adapter();
746
+ init_polyfill();
646
747
  }
647
748
  });
648
749
 
@@ -728,7 +829,7 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
728
829
  }
729
830
  if (type === "webpack") {
730
831
  const { webpackRuntime: webpackRuntime2 } = await Promise.resolve().then(() => (init_webpack(), webpack_exports));
731
- return webpackRuntime2(shared, remoteShared);
832
+ return webpackRuntime2(bundle ?? "default", shared, remoteShared);
732
833
  } else if (type === "turbopack") {
733
834
  const { turbopackRuntime: turbopackRuntime2 } = await Promise.resolve().then(() => (init_turbopack(), turbopack_exports));
734
835
  return turbopackRuntime2(url, bundle, shared, remoteShared);
@@ -737,7 +838,7 @@ async function getRuntime(type, url, bundle, shared, remoteShared) {
737
838
  }
738
839
 
739
840
  // src/html/host/index.tsx
740
- var import_jsx_runtime = require("react/jsx-runtime");
841
+ var import_jsx_runtime2 = require("react/jsx-runtime");
741
842
  if (typeof HTMLElement !== "undefined") {
742
843
  class RemoteComponent extends HTMLElement {
743
844
  constructor() {
@@ -783,8 +884,13 @@ if (typeof HTMLElement !== "undefined") {
783
884
  }
784
885
  let url = null;
785
886
  let html = this.innerHTML;
786
- if (!remoteComponentChild && src) {
887
+ if (src) {
787
888
  url = new URL(src, window.location.href);
889
+ if (url.hash) {
890
+ this.name = url.hash.slice(1);
891
+ }
892
+ }
893
+ if (!remoteComponentChild && url) {
788
894
  const fetchInit = {
789
895
  method: "GET",
790
896
  headers: {
@@ -923,7 +1029,7 @@ if (typeof HTMLElement !== "undefined") {
923
1029
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
924
1030
  // @ts-expect-error support for shadow DOM
925
1031
  this.shadowRoot,
926
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RemoteComponentFromReadableStream, { name: this.name })
1032
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RemoteComponentFromReadableStream, { name: this.name })
927
1033
  );
928
1034
  } else if (nextData) {
929
1035
  const { Component, App } = nextClientPagesLoader2(
@@ -936,7 +1042,7 @@ if (typeof HTMLElement !== "undefined") {
936
1042
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
937
1043
  // @ts-expect-error support for shadow DOM
938
1044
  this.shadowRoot,
939
- App ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, { Component, ...nextData.props }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...nextData.props })
1045
+ App ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(App, { Component, ...nextData.props }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { ...nextData.props })
940
1046
  );
941
1047
  }
942
1048
  if (this.fouc) {