remote-components 0.0.33 → 0.0.35

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 (77) hide show
  1. package/dist/{component-loader-28ad0083.d.ts → component-loader-8951c052.d.ts} +6 -4
  2. package/dist/html/host.cjs +353 -129
  3. package/dist/html/host.cjs.map +1 -1
  4. package/dist/html/host.js +353 -129
  5. package/dist/html/host.js.map +1 -1
  6. package/dist/html/remote.cjs +199 -0
  7. package/dist/html/remote.cjs.map +1 -0
  8. package/dist/html/remote.d.ts +2 -0
  9. package/dist/html/remote.js +198 -0
  10. package/dist/html/remote.js.map +1 -0
  11. package/dist/internal/next/host/app-router-client.cjs +60 -19
  12. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  13. package/dist/internal/next/host/app-router-client.d.ts +3 -3
  14. package/dist/internal/next/host/app-router-client.js +65 -23
  15. package/dist/internal/next/host/app-router-client.js.map +1 -1
  16. package/dist/internal/next/host/app-router-compat.cjs.map +1 -1
  17. package/dist/internal/next/host/app-router-compat.d.ts +1 -1
  18. package/dist/internal/next/host/app-router-compat.js.map +1 -1
  19. package/dist/internal/next/remote/render-server.cjs +1 -1
  20. package/dist/internal/next/remote/render-server.cjs.map +1 -1
  21. package/dist/internal/next/remote/render-server.js +1 -1
  22. package/dist/internal/next/remote/render-server.js.map +1 -1
  23. package/dist/internal/shared/client/polyfill.cjs.map +1 -1
  24. package/dist/internal/shared/client/polyfill.js.map +1 -1
  25. package/dist/internal/shared/client/remote-component.cjs +237 -133
  26. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  27. package/dist/internal/shared/client/remote-component.d.ts +29 -22
  28. package/dist/internal/shared/client/remote-component.js +235 -133
  29. package/dist/internal/shared/client/remote-component.js.map +1 -1
  30. package/dist/internal/shared/error.cjs +1 -1
  31. package/dist/internal/shared/error.cjs.map +1 -1
  32. package/dist/internal/shared/error.d.ts +1 -1
  33. package/dist/internal/shared/error.js +1 -1
  34. package/dist/internal/shared/error.js.map +1 -1
  35. package/dist/internal/shared/ssr/dom-flight.cjs +24 -8
  36. package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
  37. package/dist/internal/shared/ssr/dom-flight.d.ts +1 -1
  38. package/dist/internal/shared/ssr/dom-flight.js +24 -8
  39. package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
  40. package/dist/internal/shared/ssr/fetch-remote-component.cjs +8 -4
  41. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  42. package/dist/internal/shared/ssr/fetch-remote-component.d.ts +2 -1
  43. package/dist/internal/shared/ssr/fetch-remote-component.js +9 -5
  44. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  45. package/dist/next/config.cjs +100 -86
  46. package/dist/next/config.cjs.map +1 -1
  47. package/dist/next/config.js +100 -86
  48. package/dist/next/config.js.map +1 -1
  49. package/dist/next/host/app-router-server.cjs +1 -0
  50. package/dist/next/host/app-router-server.cjs.map +1 -1
  51. package/dist/next/host/app-router-server.js +2 -1
  52. package/dist/next/host/app-router-server.js.map +1 -1
  53. package/dist/next/host/client/index.cjs +1381 -1187
  54. package/dist/next/host/client/index.cjs.map +1 -1
  55. package/dist/next/host/client/index.d.ts +1 -1
  56. package/dist/next/host/client/index.js +682 -490
  57. package/dist/next/host/client/index.js.map +1 -1
  58. package/dist/next/host/pages-router-server.cjs +4 -4
  59. package/dist/next/host/pages-router-server.cjs.map +1 -1
  60. package/dist/next/host/pages-router-server.js +4 -4
  61. package/dist/next/host/pages-router-server.js.map +1 -1
  62. package/dist/next/{middleware.cjs → proxy.cjs} +24 -14
  63. package/dist/next/proxy.cjs.map +1 -0
  64. package/dist/next/proxy.d.ts +29 -0
  65. package/dist/next/{middleware.js → proxy.js} +21 -11
  66. package/dist/next/proxy.js.map +1 -0
  67. package/dist/react/index.cjs +201 -107
  68. package/dist/react/index.cjs.map +1 -1
  69. package/dist/react/index.d.ts +2 -2
  70. package/dist/react/index.js +206 -112
  71. package/dist/react/index.js.map +1 -1
  72. package/dist/{types-7c207455.d.ts → types-4e7dea94.d.ts} +2 -1
  73. package/dist/{types-e4a3fa37.d.ts → types-cbf6c34f.d.ts} +2 -2
  74. package/package.json +22 -9
  75. package/dist/next/middleware.cjs.map +0 -1
  76. package/dist/next/middleware.d.ts +0 -29
  77. package/dist/next/middleware.js.map +0 -1
@@ -1,46 +1,19 @@
1
- // src/shared/client/component-loader.ts
2
- import * as React from "react";
3
- import * as ReactDOM from "react-dom";
4
- import * as ReactDOMClient from "react-dom/client";
5
- import * as JSXDevRuntime from "react/jsx-dev-runtime";
6
- import * as JSXRuntime from "react/jsx-runtime";
7
-
8
- // src/shared/webpack/shared-modules.ts
9
- function applySharedModules(bundle, resolve) {
10
- const self = globalThis;
11
- if (self.__remote_webpack_require__?.[bundle]) {
12
- const modulePaths = Object.keys(
13
- self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
14
- );
15
- for (const [key, value] of Object.entries(resolve)) {
16
- let ids = modulePaths.filter((p) => p === key);
17
- if (ids.length === 0) {
18
- ids = modulePaths.filter((p) => p.includes(key));
19
- }
20
- for (let id of ids) {
21
- const webpackBundle = self.__remote_webpack_require__[bundle];
22
- if (webpackBundle.m) {
23
- if (self.__remote_webpack_module_map__?.[bundle]?.[id]) {
24
- id = `${self.__remote_webpack_module_map__[bundle][id]}`;
25
- }
26
- webpackBundle.m[id] = (module) => {
27
- module.exports = value;
28
- };
29
- }
30
- }
31
- }
32
- }
33
- }
34
-
35
1
  // src/shared/error.ts
36
2
  var RemoteComponentsError = class extends Error {
3
+ code = "REMOTE_COMPONENTS_ERROR";
37
4
  constructor(message, options) {
38
5
  super(message, options);
39
- this.code = "REMOTE_COMPONENTS_ERROR";
40
6
  this.name = "RemoteComponentsError";
41
7
  }
42
8
  };
43
9
 
10
+ // src/shared/client/component-loader.ts
11
+ import * as React from "react";
12
+ import * as JSXDevRuntime from "react/jsx-dev-runtime";
13
+ import * as JSXRuntime from "react/jsx-runtime";
14
+ import * as ReactDOM from "react-dom";
15
+ import * as ReactDOMClient from "react-dom/client";
16
+
44
17
  // src/shared/webpack/next-client-pages-loader.ts
45
18
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
46
19
  const self = globalThis;
@@ -153,6 +126,128 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
153
126
  return { Component: null, App: null };
154
127
  }
155
128
 
129
+ // src/shared/webpack/shared-modules.ts
130
+ function applySharedModules(bundle, resolve) {
131
+ const self = globalThis;
132
+ if (self.__remote_webpack_require__?.[bundle]) {
133
+ const modulePaths = Object.keys(
134
+ self.__remote_webpack_module_map__?.[bundle] ?? self.__remote_webpack_require__[bundle].m ?? {}
135
+ );
136
+ for (const [key, value] of Object.entries(resolve)) {
137
+ let ids = modulePaths.filter((p) => p === key);
138
+ if (ids.length === 0) {
139
+ ids = modulePaths.filter((p) => p.includes(key));
140
+ }
141
+ for (let id of ids) {
142
+ const webpackBundle = self.__remote_webpack_require__[bundle];
143
+ if (webpackBundle.m) {
144
+ if (self.__remote_webpack_module_map__?.[bundle]?.[id]) {
145
+ id = `${self.__remote_webpack_module_map__[bundle][id]}`;
146
+ }
147
+ webpackBundle.m[id] = (module) => {
148
+ module.exports = value;
149
+ };
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ // src/shared/client/rsc.ts
157
+ import { ReadableStream } from "web-streams-polyfill";
158
+ function fixPayload(payload) {
159
+ if (Array.isArray(payload)) {
160
+ if (payload[0] === "$") {
161
+ fixPayload(payload[3]);
162
+ if (payload.length === 4) {
163
+ payload.push(null, null, 1);
164
+ }
165
+ } else {
166
+ for (const item of payload) {
167
+ fixPayload(item);
168
+ }
169
+ }
170
+ } else if (typeof payload === "object" && payload !== null) {
171
+ for (const key in payload) {
172
+ fixPayload(payload[key]);
173
+ }
174
+ }
175
+ }
176
+ function createRSCStream(rscName, data) {
177
+ return new ReadableStream({
178
+ type: "bytes",
179
+ start(controller) {
180
+ const encoder = new TextEncoder();
181
+ const self = globalThis;
182
+ if (data.length > 0) {
183
+ data.forEach((chunk) => {
184
+ const lines = chunk.split("\n");
185
+ for (const line of lines) {
186
+ const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
187
+ if (match?.groups?.rsc) {
188
+ self[rscName] = self[rscName] ?? [];
189
+ self[rscName].push(JSON.parse(`"${match.groups.rsc}"`));
190
+ }
191
+ }
192
+ });
193
+ }
194
+ const allChunks = (self[rscName] ?? [`0:[null]
195
+ `]).join("");
196
+ self[rscName] = null;
197
+ allChunks.split("\n").forEach((chunk) => {
198
+ if (chunk.length > 0) {
199
+ const { before, id, prefix, payload } = /^(?<before>[^:]*?)?(?<id>[0-9a-zA-Z]+):(?<prefix>[A-Z])?(?<payload>\[.*\])/.exec(
200
+ chunk
201
+ )?.groups ?? {};
202
+ if (payload) {
203
+ const jsonPayload = JSON.parse(payload);
204
+ fixPayload(jsonPayload);
205
+ const reconstruct = `${before ?? ""}${id}:${prefix ?? ""}${JSON.stringify(jsonPayload)}`;
206
+ controller.enqueue(encoder.encode(`${reconstruct}
207
+ `));
208
+ } else {
209
+ controller.enqueue(encoder.encode(`${chunk}
210
+ `));
211
+ }
212
+ } else {
213
+ controller.enqueue(encoder.encode(`${chunk}
214
+ `));
215
+ }
216
+ });
217
+ controller.close();
218
+ }
219
+ });
220
+ }
221
+
222
+ // src/shared/client/script-loader.ts
223
+ async function loadScripts(scripts) {
224
+ await Promise.all(
225
+ scripts.map((script) => {
226
+ return new Promise((resolve, reject) => {
227
+ const newSrc = new URL(
228
+ // remove the remote component bundle name identifier from the script src
229
+ script.src.replace(/\/_next\/\[.+\](?<whitespace>%20| )/, "/_next/"),
230
+ location.origin
231
+ ).href;
232
+ const newScript = document.createElement("script");
233
+ newScript.onload = () => {
234
+ resolve();
235
+ };
236
+ newScript.onerror = () => {
237
+ reject(
238
+ new RemoteComponentsError(
239
+ `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
240
+ )
241
+ );
242
+ };
243
+ newScript.src = newSrc;
244
+ newScript.async = true;
245
+ document.head.appendChild(newScript);
246
+ });
247
+ })
248
+ );
249
+ }
250
+
156
251
  // src/shared/utils/index.ts
157
252
  function escapeString(str) {
158
253
  return str.replace(/[^a-z0-9]/g, "_");
@@ -162,6 +257,7 @@ function escapeString(str) {
162
257
  var DEFAULT_ROUTE = "/";
163
258
  var RUNTIME_WEBPACK = "webpack";
164
259
  var RUNTIME_TURBOPACK = "turbopack";
260
+ var RUNTIME_SCRIPT = "script";
165
261
  var REMOTE_COMPONENT_REGEX = /(?<prefix>.*?)\[(?<bundle>[^\]]+)\](?:%20| )(?<id>.+)/;
166
262
  function getBundleKey(bundle) {
167
263
  return escapeString(bundle);
@@ -330,7 +426,10 @@ async function handleTurbopackChunk(code, bundle, url) {
330
426
  function createModuleRequire(runtime) {
331
427
  return (id) => {
332
428
  const self = globalThis;
333
- const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? { bundle: "default", id };
429
+ const { bundle, id: moduleId } = id.match(REMOTE_COMPONENT_REGEX)?.groups ?? {
430
+ bundle: "default",
431
+ id
432
+ };
334
433
  const remoteRuntime = self.__remote_webpack_require__?.[bundle ?? "default"] ? self.__remote_webpack_require__[bundle ?? "default"]?.type || "webpack" : runtime;
335
434
  try {
336
435
  if (remoteRuntime === RUNTIME_WEBPACK && bundle && moduleId) {
@@ -666,101 +765,6 @@ function handleTurbopackModule(bundle, moduleId, id) {
666
765
  return moduleExports.exports;
667
766
  }
668
767
 
669
- // src/shared/client/script-loader.ts
670
- async function loadScripts(scripts) {
671
- await Promise.all(
672
- scripts.map((script) => {
673
- return new Promise((resolve, reject) => {
674
- const newSrc = new URL(
675
- // remove the remote component bundle name identifier from the script src
676
- script.src.replace(/\/_next\/\[.+\](?<whitespace>%20| )/, "/_next/"),
677
- location.origin
678
- ).href;
679
- const newScript = document.createElement("script");
680
- newScript.onload = () => {
681
- resolve();
682
- };
683
- newScript.onerror = () => {
684
- reject(
685
- new RemoteComponentsError(
686
- `Failed to load <script src="${script.src}"> for Remote Component. Check the URL is correct.`
687
- )
688
- );
689
- };
690
- newScript.src = newSrc;
691
- newScript.async = true;
692
- document.head.appendChild(newScript);
693
- });
694
- })
695
- );
696
- }
697
-
698
- // src/shared/client/rsc.ts
699
- import { ReadableStream } from "web-streams-polyfill";
700
- function fixPayload(payload) {
701
- if (Array.isArray(payload)) {
702
- if (payload[0] === "$") {
703
- fixPayload(payload[3]);
704
- if (payload.length === 4) {
705
- payload.push(null, null, 1);
706
- }
707
- } else {
708
- for (const item of payload) {
709
- fixPayload(item);
710
- }
711
- }
712
- } else if (typeof payload === "object" && payload !== null) {
713
- for (const key in payload) {
714
- fixPayload(payload[key]);
715
- }
716
- }
717
- }
718
- function createRSCStream(rscName, data) {
719
- return new ReadableStream({
720
- type: "bytes",
721
- start(controller) {
722
- const encoder = new TextEncoder();
723
- const self = globalThis;
724
- if (data.length > 0) {
725
- data.forEach((chunk) => {
726
- const lines = chunk.split("\n");
727
- for (const line of lines) {
728
- const match = /\.push\("(?<rsc>.*)"\);$/.exec(line);
729
- if (match?.groups?.rsc) {
730
- self[rscName] = self[rscName] ?? [];
731
- self[rscName].push(JSON.parse(`"${match.groups.rsc}"`));
732
- }
733
- }
734
- });
735
- }
736
- const allChunks = (self[rscName] ?? [`0:[null]
737
- `]).join("");
738
- self[rscName] = null;
739
- allChunks.split("\n").forEach((chunk) => {
740
- if (chunk.length > 0) {
741
- const { before, id, prefix, payload } = /^(?<before>[^:]*?)?(?<id>[0-9a-zA-Z]+):(?<prefix>[A-Z])?(?<payload>\[.*\])/.exec(
742
- chunk
743
- )?.groups ?? {};
744
- if (payload) {
745
- const jsonPayload = JSON.parse(payload);
746
- fixPayload(jsonPayload);
747
- const reconstruct = `${before ?? ""}${id}:${prefix ?? ""}${JSON.stringify(jsonPayload)}`;
748
- controller.enqueue(encoder.encode(`${reconstruct}
749
- `));
750
- } else {
751
- controller.enqueue(encoder.encode(`${chunk}
752
- `));
753
- }
754
- } else {
755
- controller.enqueue(encoder.encode(`${chunk}
756
- `));
757
- }
758
- });
759
- controller.close();
760
- }
761
- });
762
- }
763
-
764
768
  // src/shared/client/component-loader.ts
765
769
  async function loadRemoteComponent({
766
770
  url,
@@ -833,7 +837,9 @@ async function loadRemoteComponent({
833
837
  component: null,
834
838
  error: new RemoteComponentsError(
835
839
  `Failed to load remote component "${name}".`,
836
- { cause: error instanceof Error ? error : new Error(String(error)) }
840
+ {
841
+ cause: error instanceof Error ? error : new Error(String(error))
842
+ }
837
843
  )
838
844
  };
839
845
  }
@@ -891,7 +897,7 @@ function isBooleanScriptAttribute(attr) {
891
897
  }
892
898
  function setAttributesFromProps(el, props) {
893
899
  for (const [p, value] of Object.entries(props)) {
894
- if (!Object.prototype.hasOwnProperty.call(props, p))
900
+ if (!Object.hasOwn(props, p))
895
901
  continue;
896
902
  if (ignoreProps.includes(p))
897
903
  continue;
@@ -910,9 +916,104 @@ function setAttributesFromProps(el, props) {
910
916
  }
911
917
  }
912
918
  }
919
+
920
+ // src/shared/client/static-loader.ts
921
+ async function loadStaticRemoteComponent(scripts, url) {
922
+ const self = globalThis;
923
+ if (self.__remote_script_entrypoint_mount__?.[url.href]) {
924
+ self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
925
+ }
926
+ if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
927
+ self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
928
+ }
929
+ const mountUnmountSets = await Promise.all(
930
+ scripts.map(async (script) => {
931
+ try {
932
+ let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
933
+ if (!src && script.textContent) {
934
+ const blob = new Blob(
935
+ [
936
+ script.textContent.replace(
937
+ /import\.meta\.url/g,
938
+ JSON.stringify(url)
939
+ )
940
+ ],
941
+ {
942
+ type: "text/javascript"
943
+ }
944
+ );
945
+ src = URL.createObjectURL(blob);
946
+ }
947
+ const mod = await import(
948
+ /* @vite-ignore */
949
+ /* webpackIgnore: true */
950
+ new URL(src, url).href
951
+ );
952
+ if (src.startsWith("blob:")) {
953
+ URL.revokeObjectURL(src);
954
+ }
955
+ if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
956
+ if (!self.__remote_script_entrypoint_mount__) {
957
+ self.__remote_script_entrypoint_mount__ = {};
958
+ }
959
+ if (!self.__remote_script_entrypoint_mount__[url.href]) {
960
+ self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
961
+ }
962
+ self.__remote_script_entrypoint_mount__[url.href]?.add(
963
+ mod.mount || mod.default?.mount || (() => {
964
+ })
965
+ );
966
+ }
967
+ if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
968
+ if (!self.__remote_script_entrypoint_unmount__) {
969
+ self.__remote_script_entrypoint_unmount__ = {};
970
+ }
971
+ if (!self.__remote_script_entrypoint_unmount__[url.href]) {
972
+ self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
973
+ }
974
+ self.__remote_script_entrypoint_unmount__[url.href]?.add(
975
+ mod.unmount || mod.default?.unmount || (() => {
976
+ })
977
+ );
978
+ }
979
+ return {
980
+ mount: mod.mount || mod.default?.mount,
981
+ unmount: mod.unmount || mod.default?.unmount
982
+ };
983
+ } catch (e) {
984
+ console.error(
985
+ new RemoteComponentsError(
986
+ `Error loading remote component script from "${script.src || url.href}".`,
987
+ { cause: e }
988
+ )
989
+ );
990
+ return {
991
+ mount: void 0,
992
+ unmount: void 0
993
+ };
994
+ }
995
+ })
996
+ );
997
+ return mountUnmountSets.reduce(
998
+ (acc, { mount, unmount }) => {
999
+ if (typeof mount === "function") {
1000
+ acc.mount.add(mount);
1001
+ }
1002
+ if (typeof unmount === "function") {
1003
+ acc.unmount.add(unmount);
1004
+ }
1005
+ return acc;
1006
+ },
1007
+ {
1008
+ mount: /* @__PURE__ */ new Set(),
1009
+ unmount: /* @__PURE__ */ new Set()
1010
+ }
1011
+ );
1012
+ }
913
1013
  export {
914
1014
  DEFAULT_ROUTE,
915
1015
  REMOTE_COMPONENT_REGEX,
1016
+ RUNTIME_SCRIPT,
916
1017
  RUNTIME_TURBOPACK,
917
1018
  RUNTIME_WEBPACK,
918
1019
  RemoteComponentsError,
@@ -921,6 +1022,7 @@ export {
921
1022
  getBundleKey,
922
1023
  loadRemoteComponent,
923
1024
  loadScripts,
1025
+ loadStaticRemoteComponent,
924
1026
  setAttributesFromProps,
925
1027
  setupWebpackRuntime
926
1028
  };