@module-federation/bridge-react 0.0.0-next-20241118060105 → 0.0.0-next-20241118092546

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.
@@ -0,0 +1,473 @@
1
+ "use strict";
2
+ const React = require("react");
3
+ const context = require("./context-BcJ-YlNr.cjs");
4
+ const ReactRouterDOM = require("react-router-dom");
5
+ const ReactDOM = require("react-dom");
6
+ function _interopNamespaceDefault(e2) {
7
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
8
+ if (e2) {
9
+ for (const k in e2) {
10
+ if (k !== "default") {
11
+ const d = Object.getOwnPropertyDescriptor(e2, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: () => e2[k]
15
+ });
16
+ }
17
+ }
18
+ }
19
+ n.default = e2;
20
+ return Object.freeze(n);
21
+ }
22
+ const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
23
+ const ReactRouterDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDOM);
24
+ const ErrorBoundaryContext = React.createContext(null);
25
+ const initialState = {
26
+ didCatch: false,
27
+ error: null
28
+ };
29
+ class ErrorBoundary extends React.Component {
30
+ constructor(props) {
31
+ super(props);
32
+ this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
33
+ this.state = initialState;
34
+ }
35
+ static getDerivedStateFromError(error) {
36
+ return {
37
+ didCatch: true,
38
+ error
39
+ };
40
+ }
41
+ resetErrorBoundary() {
42
+ const {
43
+ error
44
+ } = this.state;
45
+ if (error !== null) {
46
+ var _this$props$onReset, _this$props;
47
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
48
+ args[_key] = arguments[_key];
49
+ }
50
+ (_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
51
+ args,
52
+ reason: "imperative-api"
53
+ });
54
+ this.setState(initialState);
55
+ }
56
+ }
57
+ componentDidCatch(error, info) {
58
+ var _this$props$onError, _this$props2;
59
+ (_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
60
+ }
61
+ componentDidUpdate(prevProps, prevState) {
62
+ const {
63
+ didCatch
64
+ } = this.state;
65
+ const {
66
+ resetKeys
67
+ } = this.props;
68
+ if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
69
+ var _this$props$onReset2, _this$props3;
70
+ (_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
71
+ next: resetKeys,
72
+ prev: prevProps.resetKeys,
73
+ reason: "keys"
74
+ });
75
+ this.setState(initialState);
76
+ }
77
+ }
78
+ render() {
79
+ const {
80
+ children,
81
+ fallbackRender,
82
+ FallbackComponent,
83
+ fallback
84
+ } = this.props;
85
+ const {
86
+ didCatch,
87
+ error
88
+ } = this.state;
89
+ let childToRender = children;
90
+ if (didCatch) {
91
+ const props = {
92
+ error,
93
+ resetErrorBoundary: this.resetErrorBoundary
94
+ };
95
+ if (typeof fallbackRender === "function") {
96
+ childToRender = fallbackRender(props);
97
+ } else if (FallbackComponent) {
98
+ childToRender = React.createElement(FallbackComponent, props);
99
+ } else if (fallback === null || React.isValidElement(fallback)) {
100
+ childToRender = fallback;
101
+ } else {
102
+ throw error;
103
+ }
104
+ }
105
+ return React.createElement(ErrorBoundaryContext.Provider, {
106
+ value: {
107
+ didCatch,
108
+ error,
109
+ resetErrorBoundary: this.resetErrorBoundary
110
+ }
111
+ }, childToRender);
112
+ }
113
+ }
114
+ function hasArrayChanged() {
115
+ let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
116
+ let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
117
+ return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
118
+ }
119
+ function e() {
120
+ const t = new PopStateEvent("popstate", { state: window.history.state });
121
+ window.dispatchEvent(t);
122
+ }
123
+ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
124
+ const RemoteApp2 = () => {
125
+ context.LoggerInstance.log(`RemoteAppWrapper RemoteApp props >>>`, { props });
126
+ const {
127
+ moduleName,
128
+ memoryRoute,
129
+ basename,
130
+ providerInfo,
131
+ className,
132
+ style,
133
+ fallback,
134
+ instance,
135
+ ...resProps
136
+ } = props;
137
+ const rootRef = ref && "current" in ref ? ref : React.useRef(null);
138
+ const renderDom = React.useRef(null);
139
+ const providerInfoRef = React.useRef(null);
140
+ context.LoggerInstance.log(`RemoteAppWrapper instance from props >>>`, instance);
141
+ React.useEffect(() => {
142
+ const renderTimeout = setTimeout(() => {
143
+ var _a, _b, _c, _d, _e, _f;
144
+ const providerReturn = providerInfo();
145
+ providerInfoRef.current = providerReturn;
146
+ let renderProps = {
147
+ moduleName,
148
+ dom: rootRef.current,
149
+ basename,
150
+ memoryRoute,
151
+ fallback,
152
+ ...resProps
153
+ };
154
+ renderDom.current = rootRef.current;
155
+ context.LoggerInstance.log(
156
+ `createRemoteComponent LazyComponent render >>>`,
157
+ renderProps
158
+ );
159
+ context.LoggerInstance.log(
160
+ `createRemoteComponent LazyComponent hostInstance >>>`,
161
+ instance
162
+ );
163
+ const beforeBridgeRenderRes = ((_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(
164
+ renderProps
165
+ )) || {};
166
+ renderProps = { ...renderProps, ...beforeBridgeRenderRes.extraProps };
167
+ providerReturn.render(renderProps);
168
+ (_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeRender) == null ? void 0 : _f.emit(renderProps);
169
+ });
170
+ return () => {
171
+ clearTimeout(renderTimeout);
172
+ setTimeout(() => {
173
+ var _a, _b, _c, _d, _e, _f, _g, _h;
174
+ if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
175
+ context.LoggerInstance.log(
176
+ `createRemoteComponent LazyComponent destroy >>>`,
177
+ { moduleName, basename, dom: renderDom.current }
178
+ );
179
+ (_d = (_c = (_b = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _b.lifecycle) == null ? void 0 : _c.beforeBridgeDestroy) == null ? void 0 : _d.emit({
180
+ moduleName,
181
+ dom: renderDom.current,
182
+ basename,
183
+ memoryRoute,
184
+ fallback,
185
+ ...resProps
186
+ });
187
+ (_e = providerInfoRef.current) == null ? void 0 : _e.destroy({
188
+ moduleName,
189
+ dom: renderDom.current
190
+ });
191
+ (_h = (_g = (_f = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _f.lifecycle) == null ? void 0 : _g.afterBridgeDestroy) == null ? void 0 : _h.emit({
192
+ moduleName,
193
+ dom: renderDom.current,
194
+ basename,
195
+ memoryRoute,
196
+ fallback,
197
+ ...resProps
198
+ });
199
+ }
200
+ });
201
+ };
202
+ }, []);
203
+ const rootComponentClassName = `${context.getRootDomDefaultClassName(moduleName)} ${props == null ? void 0 : props.className}`;
204
+ return /* @__PURE__ */ React.createElement(
205
+ "div",
206
+ {
207
+ className: rootComponentClassName,
208
+ style: props == null ? void 0 : props.style,
209
+ ref: rootRef
210
+ }
211
+ );
212
+ };
213
+ RemoteApp2["__APP_VERSION__"] = "0.7.4";
214
+ return /* @__PURE__ */ React.createElement(RemoteApp2, null);
215
+ });
216
+ function withRouterData(WrappedComponent) {
217
+ const Component = React.forwardRef(function(props, ref) {
218
+ var _a;
219
+ let enableDispathPopstate = false;
220
+ let routerContextVal;
221
+ try {
222
+ ReactRouterDOM__namespace.useLocation();
223
+ enableDispathPopstate = true;
224
+ } catch {
225
+ enableDispathPopstate = false;
226
+ }
227
+ let basename = "/";
228
+ if (!props.basename && enableDispathPopstate) {
229
+ const ReactRouterDOMAny = ReactRouterDOM__namespace;
230
+ const useRouteMatch = ReactRouterDOMAny["useRouteMatch"];
231
+ const useHistory = ReactRouterDOMAny["useHistory"];
232
+ const useHref = ReactRouterDOMAny["useHref"];
233
+ const UNSAFE_RouteContext = ReactRouterDOMAny["UNSAFE_RouteContext"];
234
+ if (UNSAFE_RouteContext) {
235
+ if (useHref) {
236
+ basename = useHref == null ? void 0 : useHref("/");
237
+ }
238
+ routerContextVal = React.useContext(UNSAFE_RouteContext);
239
+ if (routerContextVal && routerContextVal.matches && routerContextVal.matches.length > 0) {
240
+ const matchIndex = routerContextVal.matches.length - 1;
241
+ const pathnameBase = routerContextVal.matches[matchIndex].pathnameBase;
242
+ basename = context.pathJoin(basename, pathnameBase || "/");
243
+ }
244
+ } else {
245
+ const match = useRouteMatch == null ? void 0 : useRouteMatch();
246
+ if (useHistory) {
247
+ const history = useHistory == null ? void 0 : useHistory();
248
+ basename = (_a = history == null ? void 0 : history.createHref) == null ? void 0 : _a.call(history, { pathname: "/" });
249
+ }
250
+ if (match) {
251
+ basename = context.pathJoin(basename, (match == null ? void 0 : match.path) || "/");
252
+ }
253
+ }
254
+ }
255
+ context.LoggerInstance.log(`createRemoteComponent withRouterData >>>`, {
256
+ ...props,
257
+ basename,
258
+ routerContextVal,
259
+ enableDispathPopstate
260
+ });
261
+ if (enableDispathPopstate) {
262
+ const location = ReactRouterDOM__namespace.useLocation();
263
+ const [pathname, setPathname] = React.useState(location.pathname);
264
+ React.useEffect(() => {
265
+ if (pathname !== "" && pathname !== location.pathname) {
266
+ context.LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
267
+ name: props.name,
268
+ pathname: location.pathname
269
+ });
270
+ e();
271
+ }
272
+ setPathname(location.pathname);
273
+ }, [location]);
274
+ }
275
+ return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename, ref });
276
+ });
277
+ return React.forwardRef(function(props, ref) {
278
+ return /* @__PURE__ */ React.createElement(Component, { ...props, ref });
279
+ });
280
+ }
281
+ const RemoteApp = withRouterData(RemoteAppWrapper);
282
+ function createLazyRemoteComponent(info) {
283
+ const exportName = (info == null ? void 0 : info.export) || "default";
284
+ return React.lazy(async () => {
285
+ context.LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
286
+ lazyComponent: info.loader,
287
+ exportName
288
+ });
289
+ try {
290
+ const m2 = await info.loader();
291
+ const moduleName = m2 && m2[Symbol.for("mf_module_id")];
292
+ context.LoggerInstance.log(
293
+ `createRemoteComponent LazyComponent loadRemote info >>>`,
294
+ { name: moduleName, module: m2, exportName }
295
+ );
296
+ const exportFn = m2[exportName];
297
+ if (exportName in m2 && typeof exportFn === "function") {
298
+ const RemoteAppComponent = React.forwardRef((props, ref) => {
299
+ return /* @__PURE__ */ React.createElement(
300
+ RemoteApp,
301
+ {
302
+ moduleName,
303
+ providerInfo: exportFn,
304
+ exportName: info.export || "default",
305
+ fallback: info.fallback,
306
+ ref,
307
+ instance: info.instance,
308
+ ...props
309
+ }
310
+ );
311
+ });
312
+ return {
313
+ default: RemoteAppComponent
314
+ };
315
+ } else {
316
+ context.LoggerInstance.log(
317
+ `createRemoteComponent LazyComponent module not found >>>`,
318
+ { name: moduleName, module: m2, exportName }
319
+ );
320
+ throw Error(
321
+ `Make sure that ${moduleName} has the correct export when export is ${String(
322
+ exportName
323
+ )}`
324
+ );
325
+ }
326
+ } catch (error) {
327
+ throw error;
328
+ }
329
+ });
330
+ }
331
+ function createRemoteComponent(info) {
332
+ return React.forwardRef(
333
+ (props, ref) => {
334
+ const LazyComponent = createLazyRemoteComponent(info);
335
+ return /* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props, ref })));
336
+ }
337
+ );
338
+ }
339
+ var client = {};
340
+ var m = ReactDOM;
341
+ if (process.env.NODE_ENV === "production") {
342
+ client.createRoot = m.createRoot;
343
+ client.hydrateRoot = m.hydrateRoot;
344
+ } else {
345
+ var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
346
+ client.createRoot = function(c, o) {
347
+ i.usingClientEntryPoint = true;
348
+ try {
349
+ return m.createRoot(c, o);
350
+ } finally {
351
+ i.usingClientEntryPoint = false;
352
+ }
353
+ };
354
+ client.hydrateRoot = function(c, h, o) {
355
+ i.usingClientEntryPoint = true;
356
+ try {
357
+ return m.hydrateRoot(c, h, o);
358
+ } finally {
359
+ i.usingClientEntryPoint = false;
360
+ }
361
+ };
362
+ }
363
+ function createBridgeComponent(bridgeInfo) {
364
+ return () => {
365
+ const rootMap = /* @__PURE__ */ new Map();
366
+ const { instance } = bridgeInfo;
367
+ context.LoggerInstance.log(
368
+ `createBridgeComponent instance from props >>>`,
369
+ instance
370
+ );
371
+ const RawComponent = (info) => {
372
+ const { appInfo, propsInfo, ...restProps } = info;
373
+ const { moduleName, memoryRoute, basename = "/" } = appInfo;
374
+ return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { moduleName, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(
375
+ bridgeInfo.rootComponent,
376
+ {
377
+ ...propsInfo,
378
+ basename,
379
+ ...restProps
380
+ }
381
+ ));
382
+ };
383
+ return {
384
+ async render(info) {
385
+ var _a, _b, _c, _d, _e, _f;
386
+ context.LoggerInstance.log(`createBridgeComponent render Info`, info);
387
+ const {
388
+ moduleName,
389
+ dom,
390
+ basename,
391
+ memoryRoute,
392
+ fallback,
393
+ ...propsInfo
394
+ } = info;
395
+ const beforeBridgeRenderRes = ((_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(info)) || {};
396
+ const rootComponentWithErrorBoundary = (
397
+ // set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
398
+ /* @__PURE__ */ React__namespace.createElement(ErrorBoundary, { FallbackComponent: fallback }, /* @__PURE__ */ React__namespace.createElement(
399
+ RawComponent,
400
+ {
401
+ appInfo: {
402
+ moduleName,
403
+ basename,
404
+ memoryRoute
405
+ },
406
+ propsInfo: { ...propsInfo, ...beforeBridgeRenderRes == null ? void 0 : beforeBridgeRenderRes.extraProps }
407
+ }
408
+ ))
409
+ );
410
+ if (context.atLeastReact18(React__namespace)) {
411
+ if (bridgeInfo == null ? void 0 : bridgeInfo.render) {
412
+ Promise.resolve(
413
+ bridgeInfo == null ? void 0 : bridgeInfo.render(rootComponentWithErrorBoundary, dom)
414
+ ).then((root) => rootMap.set(info.dom, root));
415
+ } else {
416
+ const root = client.createRoot(info.dom);
417
+ root.render(rootComponentWithErrorBoundary);
418
+ rootMap.set(info.dom, root);
419
+ }
420
+ } else {
421
+ const renderFn = (bridgeInfo == null ? void 0 : bridgeInfo.render) || ReactDOM.render;
422
+ renderFn == null ? void 0 : renderFn(rootComponentWithErrorBoundary, info.dom);
423
+ }
424
+ ((_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeRender) == null ? void 0 : _f.emit(info)) || {};
425
+ },
426
+ async destroy(info) {
427
+ var _a, _b, _c, _d, _e, _f;
428
+ context.LoggerInstance.log(`createBridgeComponent destroy Info`, {
429
+ dom: info.dom
430
+ });
431
+ (_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeDestroy) == null ? void 0 : _c.emit(info);
432
+ if (context.atLeastReact18(React__namespace)) {
433
+ const root = rootMap.get(info.dom);
434
+ root == null ? void 0 : root.unmount();
435
+ rootMap.delete(info.dom);
436
+ } else {
437
+ ReactDOM.unmountComponentAtNode(info.dom);
438
+ }
439
+ (_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeDestroy) == null ? void 0 : _f.emit(
440
+ info
441
+ );
442
+ },
443
+ rawComponent: bridgeInfo.rootComponent,
444
+ __BRIDGE_FN__: (_args) => {
445
+ }
446
+ };
447
+ };
448
+ }
449
+ function BridgeReactPlugin() {
450
+ return {
451
+ name: "bridge-react-plugin",
452
+ beforeInit(args) {
453
+ const originalCreateRemoteComponent = createRemoteComponent;
454
+ args.origin.createRemoteComponent = function(info) {
455
+ return originalCreateRemoteComponent({
456
+ ...info,
457
+ instance: args.origin
458
+ });
459
+ };
460
+ const originalCreateBridgeComponentt = createBridgeComponent;
461
+ args.origin.createBridgeComponent = function(info) {
462
+ return originalCreateBridgeComponentt({
463
+ ...info,
464
+ instance: args.origin
465
+ });
466
+ };
467
+ return args;
468
+ }
469
+ };
470
+ }
471
+ exports.BridgeReactPlugin = BridgeReactPlugin;
472
+ exports.createBridgeComponent = createBridgeComponent;
473
+ exports.createRemoteComponent = createRemoteComponent;