@module-federation/bridge-react 0.0.0-next-20241115100504 → 0.0.0-next-20241117185051

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