@kaena1/mm-ui-shared-utils 1.25.0-f-poc-EC-3301.1 → 1.25.0-f-poc-EC-3301.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.
- package/dist/components/ImportMapScripts.js +35 -0
- package/dist/components/RemoteLinks.js +10 -0
- package/dist/components/RemoteRenderer.js +27 -0
- package/dist/hooks/useRemoteComponent.js +67 -0
- package/dist/index.d.ts +1 -4
- package/dist/index.js +15 -297
- package/dist/services/fetchManifest.js +31 -0
- package/dist/services/generateImportMap.js +14 -0
- package/dist/services/moduleLoader.js +155 -0
- package/package.json +2 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import "react";
|
|
3
|
+
import "react-dom";
|
|
4
|
+
import { generateImportMapJson } from "../services/generateImportMap.js";
|
|
5
|
+
const ImportMapScripts = () => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
6
|
+
/* @__PURE__ */ jsx(
|
|
7
|
+
"script",
|
|
8
|
+
{
|
|
9
|
+
defer: true,
|
|
10
|
+
src: "https://ga.jspm.io/npm:es-module-shims@1.10.0/dist/es-module-shims.js",
|
|
11
|
+
crossOrigin: "anonymous"
|
|
12
|
+
}
|
|
13
|
+
),
|
|
14
|
+
/* @__PURE__ */ jsx(
|
|
15
|
+
"link",
|
|
16
|
+
{
|
|
17
|
+
rel: "preconnect",
|
|
18
|
+
href: "https://assets-qa.mintmobile.com",
|
|
19
|
+
crossOrigin: "anonymous"
|
|
20
|
+
}
|
|
21
|
+
),
|
|
22
|
+
/* @__PURE__ */ jsx("link", { rel: "dns-prefetch", href: "https://assets-qa.mintmobile.com" }),
|
|
23
|
+
/* @__PURE__ */ jsx("link", { rel: "preconnect", href: "https://esm.sh", crossOrigin: "anonymous" }),
|
|
24
|
+
/* @__PURE__ */ jsx("link", { rel: "dns-prefetch", href: "https://esm.sh" }),
|
|
25
|
+
/* @__PURE__ */ jsx(
|
|
26
|
+
"script",
|
|
27
|
+
{
|
|
28
|
+
type: "importmap-shim",
|
|
29
|
+
dangerouslySetInnerHTML: { __html: generateImportMapJson() }
|
|
30
|
+
}
|
|
31
|
+
)
|
|
32
|
+
] });
|
|
33
|
+
export {
|
|
34
|
+
ImportMapScripts
|
|
35
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
const RemoteLinks = ({ manifest }) => {
|
|
3
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
4
|
+
manifest.styleUrl && /* @__PURE__ */ jsx("link", { rel: "stylesheet", href: manifest.styleUrl }),
|
|
5
|
+
/* @__PURE__ */ jsx("link", { rel: "modulepreload", href: manifest.bundleUrl })
|
|
6
|
+
] });
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
RemoteLinks
|
|
10
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useRemoteComponent } from "../hooks/useRemoteComponent.js";
|
|
3
|
+
const RemoteRenderer = ({
|
|
4
|
+
manifest,
|
|
5
|
+
componentName,
|
|
6
|
+
componentProps,
|
|
7
|
+
autoLoad = true,
|
|
8
|
+
placeholder = null,
|
|
9
|
+
children,
|
|
10
|
+
errorFallback
|
|
11
|
+
}) => {
|
|
12
|
+
const {
|
|
13
|
+
component: Comp,
|
|
14
|
+
error,
|
|
15
|
+
loaded
|
|
16
|
+
} = useRemoteComponent({
|
|
17
|
+
manifest,
|
|
18
|
+
componentName,
|
|
19
|
+
autoLoad
|
|
20
|
+
});
|
|
21
|
+
if (error) return errorFallback ? errorFallback(error) : null;
|
|
22
|
+
if (!loaded || !Comp) return /* @__PURE__ */ jsx(Fragment, { children: placeholder });
|
|
23
|
+
return /* @__PURE__ */ jsx(Comp, { ...componentProps, children });
|
|
24
|
+
};
|
|
25
|
+
export {
|
|
26
|
+
RemoteRenderer
|
|
27
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from "react";
|
|
2
|
+
import { moduleLoader } from "../services/moduleLoader.js";
|
|
3
|
+
function useRemoteComponent(options) {
|
|
4
|
+
const {
|
|
5
|
+
manifest: manifestFromProps,
|
|
6
|
+
componentName,
|
|
7
|
+
autoLoad = true
|
|
8
|
+
} = options;
|
|
9
|
+
const [state, setState] = useState({
|
|
10
|
+
component: null,
|
|
11
|
+
manifest: null,
|
|
12
|
+
loading: false,
|
|
13
|
+
error: null,
|
|
14
|
+
loaded: false
|
|
15
|
+
});
|
|
16
|
+
const loadComponent = useCallback(async () => {
|
|
17
|
+
try {
|
|
18
|
+
setState((prev) => ({
|
|
19
|
+
...prev,
|
|
20
|
+
loading: true,
|
|
21
|
+
error: null,
|
|
22
|
+
manifest: manifestFromProps
|
|
23
|
+
}));
|
|
24
|
+
const loadOptions = {
|
|
25
|
+
bundleUrl: manifestFromProps.bundleUrl,
|
|
26
|
+
componentName
|
|
27
|
+
};
|
|
28
|
+
const component = await moduleLoader.loadComponent(loadOptions);
|
|
29
|
+
setState((prev) => ({
|
|
30
|
+
...prev,
|
|
31
|
+
component,
|
|
32
|
+
loading: false,
|
|
33
|
+
loaded: true
|
|
34
|
+
}));
|
|
35
|
+
} catch (err) {
|
|
36
|
+
console.error("Error loading remote component:", err);
|
|
37
|
+
setState((prev) => ({
|
|
38
|
+
...prev,
|
|
39
|
+
error: err.message,
|
|
40
|
+
loading: false,
|
|
41
|
+
loaded: false
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
}, [manifestFromProps, componentName]);
|
|
45
|
+
const reload = useCallback(async () => {
|
|
46
|
+
await loadComponent();
|
|
47
|
+
}, [loadComponent]);
|
|
48
|
+
const clearError = useCallback(() => {
|
|
49
|
+
setState((prev) => ({ ...prev, error: null }));
|
|
50
|
+
}, []);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (autoLoad) {
|
|
53
|
+
loadComponent().then(() => {
|
|
54
|
+
setState((prev) => ({ ...prev, loaded: true }));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}, [autoLoad, loadComponent]);
|
|
58
|
+
return {
|
|
59
|
+
...state,
|
|
60
|
+
loadComponent,
|
|
61
|
+
reload,
|
|
62
|
+
clearError
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
useRemoteComponent
|
|
67
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -18,10 +18,7 @@ export declare const ImportMapScripts: default_2.FC;
|
|
|
18
18
|
export declare class ModuleLoader {
|
|
19
19
|
private loadedModules;
|
|
20
20
|
private importMapInjected;
|
|
21
|
-
|
|
22
|
-
* Ensure React and ReactDOM are available globally
|
|
23
|
-
* Remote components may expect these to be on window object.
|
|
24
|
-
*/
|
|
21
|
+
private prepareGlobalDependencies;
|
|
25
22
|
/**
|
|
26
23
|
* Ensure import map is injected before loading any modules
|
|
27
24
|
* Import maps MUST be added before any module scripts
|
package/dist/index.js
CHANGED
|
@@ -1,299 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const e = await fetch(n, {
|
|
9
|
-
headers: {
|
|
10
|
-
"Content-Type": "application/json"
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
if (!e.ok)
|
|
14
|
-
throw new Error(
|
|
15
|
-
`Failed to fetch manifest: ${e.status} ${e.statusText}`
|
|
16
|
-
);
|
|
17
|
-
const t = await e.json();
|
|
18
|
-
if (!t || typeof t != "object" || !t.components || typeof t.components != "object")
|
|
19
|
-
throw new Error("Invalid manifest: missing 'components' object");
|
|
20
|
-
return t;
|
|
21
|
-
} catch (e) {
|
|
22
|
-
throw e instanceof Error ? new Error(
|
|
23
|
-
`Manifest loading failed from ${n}: ${e.message}`
|
|
24
|
-
) : new Error(
|
|
25
|
-
`Manifest loading failed from ${n}: Unknown error`
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
class R {
|
|
30
|
-
constructor() {
|
|
31
|
-
u(this, "loadedModules", /* @__PURE__ */ new Map());
|
|
32
|
-
u(this, "importMapInjected", !1);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Ensure React and ReactDOM are available globally
|
|
36
|
-
* Remote components may expect these to be on window object.
|
|
37
|
-
*/
|
|
38
|
-
// private prepareGlobalDependencies(): void {
|
|
39
|
-
// if (typeof window === 'undefined') return;
|
|
40
|
-
// if (!(window as any).React) {
|
|
41
|
-
// (window as any).React = require('react');
|
|
42
|
-
// }
|
|
43
|
-
// if (!(window as any).ReactDOM) {
|
|
44
|
-
// (window as any).ReactDOM = require('react-dom');
|
|
45
|
-
// }
|
|
46
|
-
// }
|
|
47
|
-
/**
|
|
48
|
-
* Ensure import map is injected before loading any modules
|
|
49
|
-
* Import maps MUST be added before any module scripts
|
|
50
|
-
*/
|
|
51
|
-
ensureImportMap() {
|
|
52
|
-
if (typeof window > "u" || this.importMapInjected) return;
|
|
53
|
-
if (document.querySelector(
|
|
54
|
-
'script[type="importmap"], script[type="importmap-shim"]'
|
|
55
|
-
)) {
|
|
56
|
-
this.importMapInjected = !0;
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
console.warn(
|
|
60
|
-
`Import map not found. Ensure an import map is included early in:
|
|
61
|
-
|
|
62
|
-
- pages router: the document head (e.g. in pages/_document.tsx).
|
|
63
|
-
|
|
64
|
-
- app router: the root layout head (e.g. in app/layout.tsx).`
|
|
65
|
-
), this.importMapInjected = !0;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Load remote React component from ES module URL
|
|
69
|
-
*
|
|
70
|
-
* @param options - Configuration object with bundleUrl and componentName
|
|
71
|
-
* @returns Promise that resolves to the loaded React component
|
|
72
|
-
* @throws Error if component loading fails or times out (10s)
|
|
73
|
-
*/
|
|
74
|
-
async loadComponent(e) {
|
|
75
|
-
const { bundleUrl: t, componentName: a } = e;
|
|
76
|
-
if (this.loadedModules.has(t)) {
|
|
77
|
-
console.log(
|
|
78
|
-
`%c📦 LOADER: Returning module from INTERNAL cache for ${t}`,
|
|
79
|
-
"color: green; font-weight: bold;"
|
|
80
|
-
);
|
|
81
|
-
const o = this.loadedModules.get(t), s = (o == null ? void 0 : o[a]) || (o == null ? void 0 : o.default);
|
|
82
|
-
if (s) return s;
|
|
83
|
-
}
|
|
84
|
-
this.ensureImportMap();
|
|
85
|
-
try {
|
|
86
|
-
const o = await this.loadModuleViaScript(t), s = o[a] || o.default;
|
|
87
|
-
if (!s)
|
|
88
|
-
throw new Error(`Component ${a} not found in module`);
|
|
89
|
-
return this.loadedModules.set(t, o), s;
|
|
90
|
-
} catch (o) {
|
|
91
|
-
throw console.error("🔍 LOADER: Failed to load component:", o), o;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Load ES module from external URL using dynamic script injection
|
|
96
|
-
* This bypasses Webpack's module resolution and works with import maps
|
|
97
|
-
*/
|
|
98
|
-
loadModuleViaScript(e) {
|
|
99
|
-
return new Promise((t, a) => {
|
|
100
|
-
const o = `__moduleCallback_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, s = 50, i = 200;
|
|
101
|
-
let c = 0;
|
|
102
|
-
const m = `
|
|
103
|
-
import * as loadedModule from '${e}';
|
|
104
|
-
window.${o} = loadedModule;
|
|
105
|
-
`, r = document.createElement("script");
|
|
106
|
-
r.type = "module-shim", r.textContent = m;
|
|
107
|
-
const d = setInterval(() => {
|
|
108
|
-
if (c++, window[o]) {
|
|
109
|
-
clearInterval(d);
|
|
110
|
-
const p = window[o];
|
|
111
|
-
delete window[o], r.parentNode && document.head.removeChild(r), t(p);
|
|
112
|
-
} else c >= i && (clearInterval(d), delete window[o], r.parentNode && document.head.removeChild(r), a(new Error(`Timeout loading module from ${e}`)));
|
|
113
|
-
}, s);
|
|
114
|
-
r.onerror = (p) => {
|
|
115
|
-
clearInterval(d), delete window[o], r.parentNode && document.head.removeChild(r), a(new Error(`Failed to load module from ${e}`));
|
|
116
|
-
}, document.head.appendChild(r);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Preload a module without extracting components
|
|
121
|
-
* Useful for warming up cache
|
|
122
|
-
*
|
|
123
|
-
* @param bundleUrl - URL of the module to preload
|
|
124
|
-
*/
|
|
125
|
-
async preloadModule(e) {
|
|
126
|
-
if (!this.loadedModules.has(e)) {
|
|
127
|
-
this.ensureImportMap();
|
|
128
|
-
try {
|
|
129
|
-
const t = await this.loadModuleViaScript(e);
|
|
130
|
-
this.loadedModules.set(e, t);
|
|
131
|
-
} catch (t) {
|
|
132
|
-
console.error(`Failed to preload module ${e}:`, t);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Check if a module has already been loaded
|
|
138
|
-
*
|
|
139
|
-
* @param bundleUrl - URL of the module to check
|
|
140
|
-
* @returns true if module is already loaded, false otherwise
|
|
141
|
-
*/
|
|
142
|
-
isLoaded(e) {
|
|
143
|
-
return this.loadedModules.has(e);
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Clear all loaded modules from cache
|
|
147
|
-
* Useful for development/testing
|
|
148
|
-
*/
|
|
149
|
-
clearCache() {
|
|
150
|
-
this.loadedModules.clear();
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const j = new R();
|
|
154
|
-
function I() {
|
|
155
|
-
const n = `data:text/javascript;charset=utf-8,
|
|
156
|
-
const React = window.React;
|
|
157
|
-
export default React;
|
|
158
|
-
export const {
|
|
159
|
-
Component, PureComponent, createContext, useContext, useState, useEffect,
|
|
160
|
-
useCallback, useMemo, useRef, forwardRef, createElement, Fragment, memo, lazy, Suspense,
|
|
161
|
-
useLayoutEffect, useId, useReducer, useImperativeHandle
|
|
162
|
-
} = React;`, e = `data:text/javascript;charset=utf-8,
|
|
163
|
-
const React = window.React;
|
|
164
|
-
export const jsx = React.createElement;
|
|
165
|
-
export const jsxs = React.createElement;
|
|
166
|
-
export const Fragment = React.Fragment;`;
|
|
167
|
-
return JSON.stringify({
|
|
168
|
-
imports: {
|
|
169
|
-
react: n,
|
|
170
|
-
"react/jsx-runtime": e,
|
|
171
|
-
"react/jsx-dev-runtime": e,
|
|
172
|
-
"react/jsx-runtime.js": e,
|
|
173
|
-
"react/jsx-dev-runtime.js": e,
|
|
174
|
-
"react-dom": "https://esm.sh/react-dom@18.3.1",
|
|
175
|
-
"react-dom/client": "https://esm.sh/react-dom@18.3.1/client"
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
function C(n) {
|
|
180
|
-
const {
|
|
181
|
-
manifest: e,
|
|
182
|
-
componentName: t,
|
|
183
|
-
autoLoad: a = !0
|
|
184
|
-
} = n, [o, s] = x({
|
|
185
|
-
component: null,
|
|
186
|
-
manifest: null,
|
|
187
|
-
loading: !1,
|
|
188
|
-
error: null,
|
|
189
|
-
loaded: !1
|
|
190
|
-
});
|
|
191
|
-
w(() => {
|
|
192
|
-
var r;
|
|
193
|
-
typeof window < "u" && console.log("🧩 Remote sees React:", {
|
|
194
|
-
react: window.React,
|
|
195
|
-
useState: (r = window.React) == null ? void 0 : r.useState
|
|
196
|
-
});
|
|
197
|
-
}, []);
|
|
198
|
-
const i = f(async () => {
|
|
199
|
-
try {
|
|
200
|
-
s((p) => ({
|
|
201
|
-
...p,
|
|
202
|
-
loading: !0,
|
|
203
|
-
error: null,
|
|
204
|
-
manifest: e
|
|
205
|
-
}));
|
|
206
|
-
const r = {
|
|
207
|
-
bundleUrl: e.bundleUrl,
|
|
208
|
-
componentName: t
|
|
209
|
-
}, d = await j.loadComponent(r);
|
|
210
|
-
s((p) => ({
|
|
211
|
-
...p,
|
|
212
|
-
component: d,
|
|
213
|
-
loading: !1,
|
|
214
|
-
loaded: !0
|
|
215
|
-
}));
|
|
216
|
-
} catch (r) {
|
|
217
|
-
console.error("Error loading remote component:", r), s((d) => ({
|
|
218
|
-
...d,
|
|
219
|
-
error: r.message,
|
|
220
|
-
loading: !1,
|
|
221
|
-
loaded: !1
|
|
222
|
-
}));
|
|
223
|
-
}
|
|
224
|
-
}, [e, t]), c = f(async () => {
|
|
225
|
-
await i();
|
|
226
|
-
}, [i]), m = f(() => {
|
|
227
|
-
s((r) => ({ ...r, error: null }));
|
|
228
|
-
}, []);
|
|
229
|
-
return w(() => {
|
|
230
|
-
a && i().then(() => {
|
|
231
|
-
s((r) => ({ ...r, loaded: !0 }));
|
|
232
|
-
});
|
|
233
|
-
}, [a, i]), {
|
|
234
|
-
...o,
|
|
235
|
-
loadComponent: i,
|
|
236
|
-
reload: c,
|
|
237
|
-
clearError: m
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
const k = ({
|
|
241
|
-
manifest: n,
|
|
242
|
-
componentName: e,
|
|
243
|
-
componentProps: t,
|
|
244
|
-
autoLoad: a = !0,
|
|
245
|
-
placeholder: o = null,
|
|
246
|
-
children: s,
|
|
247
|
-
errorFallback: i
|
|
248
|
-
}) => {
|
|
249
|
-
const {
|
|
250
|
-
component: c,
|
|
251
|
-
error: m,
|
|
252
|
-
loaded: r
|
|
253
|
-
} = C({
|
|
254
|
-
manifest: n,
|
|
255
|
-
componentName: e,
|
|
256
|
-
autoLoad: a
|
|
257
|
-
});
|
|
258
|
-
return m ? i ? i(m) : null : !r || !c ? /* @__PURE__ */ l(h, { children: o }) : /* @__PURE__ */ l(c, { ...t, children: s });
|
|
259
|
-
}, b = ({ manifest: n }) => /* @__PURE__ */ g(h, { children: [
|
|
260
|
-
n.styleUrl && /* @__PURE__ */ l("link", { rel: "stylesheet", href: n.styleUrl }),
|
|
261
|
-
/* @__PURE__ */ l("link", { rel: "modulepreload", href: n.bundleUrl })
|
|
262
|
-
] }), L = () => /* @__PURE__ */ g(h, { children: [
|
|
263
|
-
/* @__PURE__ */ l(
|
|
264
|
-
"script",
|
|
265
|
-
{
|
|
266
|
-
defer: !0,
|
|
267
|
-
src: "https://ga.jspm.io/npm:es-module-shims@1.10.0/dist/es-module-shims.js",
|
|
268
|
-
crossOrigin: "anonymous"
|
|
269
|
-
}
|
|
270
|
-
),
|
|
271
|
-
/* @__PURE__ */ l(
|
|
272
|
-
"link",
|
|
273
|
-
{
|
|
274
|
-
rel: "preconnect",
|
|
275
|
-
href: "https://assets-qa.mintmobile.com",
|
|
276
|
-
crossOrigin: "anonymous"
|
|
277
|
-
}
|
|
278
|
-
),
|
|
279
|
-
/* @__PURE__ */ l("link", { rel: "dns-prefetch", href: "https://assets-qa.mintmobile.com" }),
|
|
280
|
-
/* @__PURE__ */ l("link", { rel: "preconnect", href: "https://esm.sh", crossOrigin: "anonymous" }),
|
|
281
|
-
/* @__PURE__ */ l("link", { rel: "dns-prefetch", href: "https://esm.sh" }),
|
|
282
|
-
/* @__PURE__ */ l(
|
|
283
|
-
"script",
|
|
284
|
-
{
|
|
285
|
-
type: "importmap-shim",
|
|
286
|
-
dangerouslySetInnerHTML: { __html: I() }
|
|
287
|
-
}
|
|
288
|
-
)
|
|
289
|
-
] });
|
|
1
|
+
import { fetchManifest } from "./services/fetchManifest.js";
|
|
2
|
+
import { ModuleLoader, moduleLoader } from "./services/moduleLoader.js";
|
|
3
|
+
import { generateImportMapJson } from "./services/generateImportMap.js";
|
|
4
|
+
import { useRemoteComponent } from "./hooks/useRemoteComponent.js";
|
|
5
|
+
import { RemoteRenderer } from "./components/RemoteRenderer.js";
|
|
6
|
+
import { RemoteLinks } from "./components/RemoteLinks.js";
|
|
7
|
+
import { ImportMapScripts } from "./components/ImportMapScripts.js";
|
|
290
8
|
export {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
9
|
+
ImportMapScripts,
|
|
10
|
+
ModuleLoader,
|
|
11
|
+
RemoteLinks,
|
|
12
|
+
RemoteRenderer,
|
|
13
|
+
fetchManifest,
|
|
14
|
+
generateImportMapJson,
|
|
15
|
+
moduleLoader,
|
|
16
|
+
useRemoteComponent
|
|
299
17
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
async function fetchManifest(manifestUrl) {
|
|
2
|
+
try {
|
|
3
|
+
const response = await fetch(manifestUrl, {
|
|
4
|
+
headers: {
|
|
5
|
+
"Content-Type": "application/json"
|
|
6
|
+
}
|
|
7
|
+
});
|
|
8
|
+
if (!response.ok) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
`Failed to fetch manifest: ${response.status} ${response.statusText}`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
const data = await response.json();
|
|
14
|
+
if (!data || typeof data !== "object" || !data.components || typeof data.components !== "object") {
|
|
15
|
+
throw new Error("Invalid manifest: missing 'components' object");
|
|
16
|
+
}
|
|
17
|
+
return data;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (error instanceof Error) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Manifest loading failed from ${manifestUrl}: ${error.message}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
throw new Error(
|
|
25
|
+
`Manifest loading failed from ${manifestUrl}: Unknown error`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
fetchManifest
|
|
31
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function generateImportMapJson() {
|
|
2
|
+
const importMap = {
|
|
3
|
+
imports: {
|
|
4
|
+
react: "react",
|
|
5
|
+
"react-dom": "react-dom",
|
|
6
|
+
"react/jsx-runtime": "react/jsx-runtime",
|
|
7
|
+
"react/jsx-dev-runtime": "react/jsx-dev-runtime"
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
return JSON.stringify(importMap);
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
generateImportMapJson
|
|
14
|
+
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import React from "react";
|
|
5
|
+
import ReactDOM from "react-dom";
|
|
6
|
+
class ModuleLoader {
|
|
7
|
+
constructor() {
|
|
8
|
+
__publicField(this, "loadedModules", /* @__PURE__ */ new Map());
|
|
9
|
+
__publicField(this, "importMapInjected", false);
|
|
10
|
+
}
|
|
11
|
+
prepareGlobalDependencies() {
|
|
12
|
+
if (!window.React) {
|
|
13
|
+
window.React = React;
|
|
14
|
+
}
|
|
15
|
+
if (!window.ReactDOM) {
|
|
16
|
+
window.ReactDOM = ReactDOM;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Ensure import map is injected before loading any modules
|
|
21
|
+
* Import maps MUST be added before any module scripts
|
|
22
|
+
*/
|
|
23
|
+
ensureImportMap() {
|
|
24
|
+
if (typeof window === "undefined") return;
|
|
25
|
+
if (this.importMapInjected) return;
|
|
26
|
+
const existingImportMap = document.querySelector(
|
|
27
|
+
'script[type="importmap"], script[type="importmap-shim"]'
|
|
28
|
+
);
|
|
29
|
+
if (existingImportMap) {
|
|
30
|
+
this.importMapInjected = true;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
console.warn(
|
|
34
|
+
`Import map not found. Ensure an import map is included early in:
|
|
35
|
+
|
|
36
|
+
- pages router: the document head (e.g. in pages/_document.tsx).
|
|
37
|
+
|
|
38
|
+
- app router: the root layout head (e.g. in app/layout.tsx).`
|
|
39
|
+
);
|
|
40
|
+
this.importMapInjected = true;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Load remote React component from ES module URL
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration object with bundleUrl and componentName
|
|
46
|
+
* @returns Promise that resolves to the loaded React component
|
|
47
|
+
* @throws Error if component loading fails or times out (10s)
|
|
48
|
+
*/
|
|
49
|
+
async loadComponent(options) {
|
|
50
|
+
const { bundleUrl, componentName } = options;
|
|
51
|
+
if (this.loadedModules.has(bundleUrl)) {
|
|
52
|
+
const cachedModule = this.loadedModules.get(bundleUrl);
|
|
53
|
+
const component = (cachedModule == null ? void 0 : cachedModule[componentName]) || (cachedModule == null ? void 0 : cachedModule.default);
|
|
54
|
+
if (component) return component;
|
|
55
|
+
}
|
|
56
|
+
this.prepareGlobalDependencies();
|
|
57
|
+
this.ensureImportMap();
|
|
58
|
+
try {
|
|
59
|
+
const esm = await this.loadModuleViaScript(bundleUrl);
|
|
60
|
+
const component = esm[componentName] || esm.default;
|
|
61
|
+
if (!component) {
|
|
62
|
+
throw new Error(`Component ${componentName} not found in module`);
|
|
63
|
+
}
|
|
64
|
+
this.loadedModules.set(bundleUrl, esm);
|
|
65
|
+
return component;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error("🔍 LOADER: Failed to load component:", error);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Load ES module from external URL using dynamic script injection
|
|
73
|
+
* This bypasses Webpack's module resolution and works with import maps
|
|
74
|
+
*/
|
|
75
|
+
loadModuleViaScript(url) {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
const callbackName = `__moduleCallback_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
78
|
+
const checkInterval = 50;
|
|
79
|
+
const maxAttempts = 200;
|
|
80
|
+
let attempts = 0;
|
|
81
|
+
const scriptContent = `
|
|
82
|
+
import * as loadedModule from '${url}';
|
|
83
|
+
window.${callbackName} = loadedModule;
|
|
84
|
+
`;
|
|
85
|
+
const script = document.createElement("script");
|
|
86
|
+
script.type = "module-shim";
|
|
87
|
+
script.textContent = scriptContent;
|
|
88
|
+
const pollForModule = setInterval(() => {
|
|
89
|
+
attempts++;
|
|
90
|
+
if (window[callbackName]) {
|
|
91
|
+
clearInterval(pollForModule);
|
|
92
|
+
const loadedModule = window[callbackName];
|
|
93
|
+
delete window[callbackName];
|
|
94
|
+
if (script.parentNode) {
|
|
95
|
+
document.head.removeChild(script);
|
|
96
|
+
}
|
|
97
|
+
resolve(loadedModule);
|
|
98
|
+
} else if (attempts >= maxAttempts) {
|
|
99
|
+
clearInterval(pollForModule);
|
|
100
|
+
delete window[callbackName];
|
|
101
|
+
if (script.parentNode) {
|
|
102
|
+
document.head.removeChild(script);
|
|
103
|
+
}
|
|
104
|
+
reject(new Error(`Timeout loading module from ${url}`));
|
|
105
|
+
}
|
|
106
|
+
}, checkInterval);
|
|
107
|
+
script.onerror = (_err) => {
|
|
108
|
+
clearInterval(pollForModule);
|
|
109
|
+
delete window[callbackName];
|
|
110
|
+
if (script.parentNode) {
|
|
111
|
+
document.head.removeChild(script);
|
|
112
|
+
}
|
|
113
|
+
reject(new Error(`Failed to load module from ${url}`));
|
|
114
|
+
};
|
|
115
|
+
document.head.appendChild(script);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Preload a module without extracting components
|
|
120
|
+
* Useful for warming up cache
|
|
121
|
+
*
|
|
122
|
+
* @param bundleUrl - URL of the module to preload
|
|
123
|
+
*/
|
|
124
|
+
async preloadModule(bundleUrl) {
|
|
125
|
+
if (this.loadedModules.has(bundleUrl)) return;
|
|
126
|
+
this.ensureImportMap();
|
|
127
|
+
try {
|
|
128
|
+
const esm = await this.loadModuleViaScript(bundleUrl);
|
|
129
|
+
this.loadedModules.set(bundleUrl, esm);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error(`Failed to preload module ${bundleUrl}:`, error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if a module has already been loaded
|
|
136
|
+
*
|
|
137
|
+
* @param bundleUrl - URL of the module to check
|
|
138
|
+
* @returns true if module is already loaded, false otherwise
|
|
139
|
+
*/
|
|
140
|
+
isLoaded(bundleUrl) {
|
|
141
|
+
return this.loadedModules.has(bundleUrl);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Clear all loaded modules from cache
|
|
145
|
+
* Useful for development/testing
|
|
146
|
+
*/
|
|
147
|
+
clearCache() {
|
|
148
|
+
this.loadedModules.clear();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const moduleLoader = new ModuleLoader();
|
|
152
|
+
export {
|
|
153
|
+
ModuleLoader,
|
|
154
|
+
moduleLoader
|
|
155
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaena1/mm-ui-shared-utils",
|
|
3
|
-
"version": "1.25.0-f-poc-EC-3301.
|
|
3
|
+
"version": "1.25.0-f-poc-EC-3301.2",
|
|
4
4
|
"description": "Shared utilities for loading remote React components in Mint Mobile microfrontends",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"eslint": "^9.39.2",
|
|
55
55
|
"react": "^18.3.1",
|
|
56
56
|
"react-dom": "^18.3.1",
|
|
57
|
+
"read-pkg-up": "^11.0.0",
|
|
57
58
|
"rollup-preserve-directives": "^1.1.3",
|
|
58
59
|
"semantic-release": "^23.0.0",
|
|
59
60
|
"semantic-release-slack-bot": "^4.0.2",
|