@module-federation/bridge-react 0.12.0 → 0.13.1

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/index.cjs.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const bridgeBase = require("./bridge-base-DIOcSEVT.cjs");
4
+ const ReactDOM = require("react-dom");
4
5
  const React = require("react");
5
- const context = require("./context-C79iMWYD.cjs");
6
+ const index = require("./index-BYDWxFmi.cjs");
6
7
  const ReactRouterDOM = require("react-router-dom");
7
8
  const plugin = require("./plugin.cjs.js");
8
- const ReactDOM = require("react-dom");
9
9
  function _interopNamespaceDefault(e2) {
10
10
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
11
11
  if (e2) {
@@ -22,102 +22,36 @@ function _interopNamespaceDefault(e2) {
22
22
  n.default = e2;
23
23
  return Object.freeze(n);
24
24
  }
25
- const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
26
25
  const ReactRouterDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDOM);
27
- const ErrorBoundaryContext = React.createContext(null);
28
- const initialState = {
29
- didCatch: false,
30
- error: null
31
- };
32
- class ErrorBoundary extends React.Component {
33
- constructor(props) {
34
- super(props);
35
- this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
36
- this.state = initialState;
37
- }
38
- static getDerivedStateFromError(error) {
39
- return {
40
- didCatch: true,
41
- error
42
- };
43
- }
44
- resetErrorBoundary() {
45
- const {
46
- error
47
- } = this.state;
48
- if (error !== null) {
49
- var _this$props$onReset, _this$props;
50
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
51
- args[_key] = arguments[_key];
26
+ function createReact16Or17Root(container) {
27
+ return {
28
+ render(children) {
29
+ const reactVersion = ReactDOM.version || "";
30
+ const isReact18 = reactVersion.startsWith("18");
31
+ const isReact19 = reactVersion.startsWith("19");
32
+ if (isReact19) {
33
+ throw new Error(
34
+ `React 19 detected in legacy mode. This is not supported. Please use the version-specific import: import { createBridgeComponent } from '@module-federation/bridge-react/v19'`
35
+ );
52
36
  }
53
- (_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
54
- args,
55
- reason: "imperative-api"
56
- });
57
- this.setState(initialState);
58
- }
59
- }
60
- componentDidCatch(error, info) {
61
- var _this$props$onError, _this$props2;
62
- (_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
63
- }
64
- componentDidUpdate(prevProps, prevState) {
65
- const {
66
- didCatch
67
- } = this.state;
68
- const {
69
- resetKeys
70
- } = this.props;
71
- if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
72
- var _this$props$onReset2, _this$props3;
73
- (_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
74
- next: resetKeys,
75
- prev: prevProps.resetKeys,
76
- reason: "keys"
77
- });
78
- this.setState(initialState);
79
- }
80
- }
81
- render() {
82
- const {
83
- children,
84
- fallbackRender,
85
- FallbackComponent,
86
- fallback
87
- } = this.props;
88
- const {
89
- didCatch,
90
- error
91
- } = this.state;
92
- let childToRender = children;
93
- if (didCatch) {
94
- const props = {
95
- error,
96
- resetErrorBoundary: this.resetErrorBoundary
97
- };
98
- if (typeof fallbackRender === "function") {
99
- childToRender = fallbackRender(props);
100
- } else if (FallbackComponent) {
101
- childToRender = React.createElement(FallbackComponent, props);
102
- } else if (fallback !== void 0) {
103
- childToRender = fallback;
104
- } else {
105
- throw error;
37
+ if (isReact18) {
38
+ console.warn(
39
+ `[Bridge-React] React 18 detected in legacy mode. For better compatibility, please use the version-specific import: import { createBridgeComponent } from '@module-federation/bridge-react/v18'`
40
+ );
106
41
  }
42
+ ReactDOM.render(children, container);
43
+ },
44
+ unmount() {
45
+ ReactDOM.unmountComponentAtNode(container);
107
46
  }
108
- return React.createElement(ErrorBoundaryContext.Provider, {
109
- value: {
110
- didCatch,
111
- error,
112
- resetErrorBoundary: this.resetErrorBoundary
113
- }
114
- }, childToRender);
115
- }
47
+ };
116
48
  }
117
- function hasArrayChanged() {
118
- let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
119
- let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
120
- return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
49
+ function createBridgeComponent(bridgeInfo) {
50
+ const fullBridgeInfo = {
51
+ createRoot: createReact16Or17Root,
52
+ ...bridgeInfo
53
+ };
54
+ return bridgeBase.createBaseBridgeComponent(fullBridgeInfo);
121
55
  }
122
56
  function e() {
123
57
  const t = new PopStateEvent("popstate", { state: window.history.state });
@@ -139,16 +73,16 @@ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
139
73
  const renderDom = React.useRef(null);
140
74
  const providerInfoRef = React.useRef(null);
141
75
  const [initialized, setInitialized] = React.useState(false);
142
- context.LoggerInstance.debug(`RemoteAppWrapper instance from props >>>`, instance);
76
+ index.LoggerInstance.debug(`RemoteAppWrapper instance from props >>>`, instance);
143
77
  React.useEffect(() => {
144
78
  if (initialized) return;
145
79
  const providerReturn = providerInfo();
146
80
  providerInfoRef.current = providerReturn;
147
81
  setInitialized(true);
148
82
  return () => {
149
- var _a2, _b, _c, _d, _e, _f, _g, _h;
150
- if ((_a2 = providerInfoRef.current) == null ? void 0 : _a2.destroy) {
151
- context.LoggerInstance.debug(
83
+ var _a, _b, _c, _d, _e, _f, _g, _h;
84
+ if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
85
+ index.LoggerInstance.debug(
152
86
  `createRemoteComponent LazyComponent destroy >>>`,
153
87
  { moduleName, basename, dom: renderDom.current }
154
88
  );
@@ -176,7 +110,7 @@ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
176
110
  };
177
111
  }, [moduleName]);
178
112
  React.useEffect(() => {
179
- var _a2, _b, _c, _d, _e, _f;
113
+ var _a, _b, _c, _d, _e, _f;
180
114
  if (!initialized || !providerInfoRef.current) return;
181
115
  let renderProps = {
182
116
  moduleName,
@@ -187,17 +121,17 @@ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
187
121
  ...resProps
188
122
  };
189
123
  renderDom.current = rootRef.current;
190
- const beforeBridgeRenderRes = ((_c = (_b = (_a2 = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a2.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(renderProps)) || {};
124
+ 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(renderProps)) || {};
191
125
  renderProps = { ...renderProps, ...beforeBridgeRenderRes.extraProps };
192
126
  providerInfoRef.current.render(renderProps);
193
127
  (_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);
194
128
  }, [initialized, ...Object.values(props)]);
195
- const rootComponentClassName = `${context.getRootDomDefaultClassName(moduleName)} ${className || ""}`;
129
+ const rootComponentClassName = `${index.getRootDomDefaultClassName(moduleName)} ${className || ""}`;
196
130
  return /* @__PURE__ */ React.createElement("div", { className: rootComponentClassName, style, ref: rootRef });
197
131
  });
198
132
  function withRouterData(WrappedComponent) {
199
133
  const Component = React.forwardRef(function(props, ref) {
200
- var _a2;
134
+ var _a;
201
135
  if (props == null ? void 0 : props.basename) {
202
136
  return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename: props.basename, ref });
203
137
  }
@@ -224,20 +158,20 @@ function withRouterData(WrappedComponent) {
224
158
  if (routerContextVal && routerContextVal.matches && routerContextVal.matches.length > 0) {
225
159
  const matchIndex = routerContextVal.matches.length - 1;
226
160
  const pathnameBase = routerContextVal.matches[matchIndex].pathnameBase;
227
- basename = context.pathJoin(basename, pathnameBase || "/");
161
+ basename = index.pathJoin(basename, pathnameBase || "/");
228
162
  }
229
163
  } else {
230
164
  const match = useRouteMatch == null ? void 0 : useRouteMatch();
231
165
  if (useHistory) {
232
166
  const history = useHistory == null ? void 0 : useHistory();
233
- basename = (_a2 = history == null ? void 0 : history.createHref) == null ? void 0 : _a2.call(history, { pathname: "/" });
167
+ basename = (_a = history == null ? void 0 : history.createHref) == null ? void 0 : _a.call(history, { pathname: "/" });
234
168
  }
235
169
  if (match) {
236
- basename = context.pathJoin(basename, (match == null ? void 0 : match.path) || "/");
170
+ basename = index.pathJoin(basename, (match == null ? void 0 : match.path) || "/");
237
171
  }
238
172
  }
239
173
  }
240
- context.LoggerInstance.debug(`createRemoteComponent withRouterData >>>`, {
174
+ index.LoggerInstance.debug(`createRemoteComponent withRouterData >>>`, {
241
175
  ...props,
242
176
  basename,
243
177
  routerContextVal,
@@ -248,7 +182,7 @@ function withRouterData(WrappedComponent) {
248
182
  const [pathname, setPathname] = React.useState(location.pathname);
249
183
  React.useEffect(() => {
250
184
  if (pathname !== "" && pathname !== location.pathname) {
251
- context.LoggerInstance.debug(
185
+ index.LoggerInstance.debug(
252
186
  `createRemoteComponent dispatchPopstateEnv >>>`,
253
187
  {
254
188
  name: props.name,
@@ -270,14 +204,14 @@ const RemoteApp = withRouterData(RemoteAppWrapper);
270
204
  function createLazyRemoteComponent(info) {
271
205
  const exportName = (info == null ? void 0 : info.export) || "default";
272
206
  return React.lazy(async () => {
273
- context.LoggerInstance.debug(`createRemoteComponent LazyComponent create >>>`, {
207
+ index.LoggerInstance.debug(`createRemoteComponent LazyComponent create >>>`, {
274
208
  lazyComponent: info.loader,
275
209
  exportName
276
210
  });
277
211
  try {
278
212
  const m = await info.loader();
279
213
  const moduleName = m && m[Symbol.for("mf_module_id")];
280
- context.LoggerInstance.debug(
214
+ index.LoggerInstance.debug(
281
215
  `createRemoteComponent LazyComponent loadRemote info >>>`,
282
216
  { name: moduleName, module: m, exportName }
283
217
  );
@@ -300,7 +234,7 @@ function createLazyRemoteComponent(info) {
300
234
  default: RemoteAppComponent
301
235
  };
302
236
  } else {
303
- context.LoggerInstance.debug(
237
+ index.LoggerInstance.debug(
304
238
  `createRemoteComponent LazyComponent module not found >>>`,
305
239
  { name: moduleName, module: m, exportName }
306
240
  );
@@ -318,103 +252,14 @@ function createLazyRemoteComponent(info) {
318
252
  function createRemoteComponent(info) {
319
253
  const LazyComponent = createLazyRemoteComponent(info);
320
254
  return React.forwardRef((props, ref) => {
321
- return /* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props, ref })));
322
- });
323
- }
324
- const isReact18 = (_a = ReactDOM.version) == null ? void 0 : _a.startsWith("18");
325
- function createRoot(container, options) {
326
- if (isReact18) {
327
- return ReactDOM.createRoot(container, options);
328
- }
329
- return {
330
- render(children) {
331
- ReactDOM.render(children, container);
332
- },
333
- unmount() {
334
- ReactDOM.unmountComponentAtNode(container);
335
- }
336
- };
337
- }
338
- function createBridgeComponent({
339
- createRoot: createRoot$1 = createRoot,
340
- ...bridgeInfo
341
- }) {
342
- return () => {
343
- const rootMap = /* @__PURE__ */ new Map();
344
- const instance = plugin.federationRuntime.instance;
345
- context.LoggerInstance.debug(
346
- `createBridgeComponent instance from props >>>`,
347
- instance
348
- );
349
- const RawComponent = (info) => {
350
- const { appInfo, propsInfo, ...restProps } = info;
351
- const { moduleName, memoryRoute, basename = "/" } = appInfo;
352
- return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { moduleName, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(
353
- bridgeInfo.rootComponent,
354
- {
355
- ...propsInfo,
356
- basename,
357
- ...restProps
358
- }
359
- ));
360
- };
361
- return {
362
- async render(info) {
363
- var _a2, _b, _c, _d, _e, _f;
364
- context.LoggerInstance.debug(`createBridgeComponent render Info`, info);
365
- const {
366
- moduleName,
367
- dom,
368
- basename,
369
- memoryRoute,
370
- fallback,
371
- ...propsInfo
372
- } = info;
373
- const beforeBridgeRenderRes = ((_c = (_b = (_a2 = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a2.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(info)) || {};
374
- const rootComponentWithErrorBoundary = /* @__PURE__ */ React__namespace.createElement(ErrorBoundary, { FallbackComponent: fallback }, /* @__PURE__ */ React__namespace.createElement(
375
- RawComponent,
376
- {
377
- appInfo: {
378
- moduleName,
379
- basename,
380
- memoryRoute
381
- },
382
- propsInfo: { ...propsInfo, ...beforeBridgeRenderRes == null ? void 0 : beforeBridgeRenderRes.extraProps }
383
- }
384
- ));
385
- if (bridgeInfo.render) {
386
- await Promise.resolve(
387
- bridgeInfo.render(rootComponentWithErrorBoundary, dom)
388
- ).then((root) => rootMap.set(dom, root));
389
- } else {
390
- let root = rootMap.get(dom);
391
- if (!root) {
392
- root = createRoot$1(dom);
393
- rootMap.set(dom, root);
394
- }
395
- if ("render" in root) {
396
- root.render(rootComponentWithErrorBoundary);
397
- }
398
- }
399
- ((_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)) || {};
255
+ return /* @__PURE__ */ React.createElement(
256
+ bridgeBase.ErrorBoundary,
257
+ {
258
+ FallbackComponent: info.fallback
400
259
  },
401
- destroy(info) {
402
- var _a2, _b, _c;
403
- const { dom } = info;
404
- context.LoggerInstance.debug(`createBridgeComponent destroy Info`, info);
405
- const root = rootMap.get(dom);
406
- if (root) {
407
- if ("unmount" in root) {
408
- root.unmount();
409
- } else {
410
- ReactDOM.unmountComponentAtNode(root);
411
- }
412
- rootMap.delete(dom);
413
- }
414
- (_c = (_b = (_a2 = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a2.lifecycle) == null ? void 0 : _b.afterBridgeDestroy) == null ? void 0 : _c.emit(info);
415
- }
416
- };
417
- };
260
+ /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props, ref }))
261
+ );
262
+ });
418
263
  }
419
264
  exports.createBridgeComponent = createBridgeComponent;
420
265
  exports.createRemoteComponent = createRemoteComponent;
package/dist/index.d.ts CHANGED
@@ -1,17 +1,22 @@
1
1
  import { default as default_2 } from 'react';
2
2
  import * as React_2 from 'react';
3
3
 
4
- export declare function createBridgeComponent<T>({ createRoot, ...bridgeInfo }: ProviderFnParams<T>): () => {
4
+ export declare function createBridgeComponent<T = any>(bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>): () => {
5
5
  render(info: RenderParams): Promise<void>;
6
6
  destroy(info: DestroyParams): void;
7
7
  };
8
8
 
9
9
  export declare function createRemoteComponent<T = Record<string, unknown>, E extends keyof T = keyof T>(info: LazyRemoteComponentInfo<T, E>): default_2.ForwardRefExoticComponent<Omit<RemoteComponentProps<Record<string, unknown>>, "ref"> & default_2.RefAttributes<HTMLDivElement>>;
10
10
 
11
+ export declare interface CreateRootOptions {
12
+ identifierPrefix?: string;
13
+ onRecoverableError?: (error: unknown, errorInfo: unknown) => void;
14
+ }
15
+
11
16
  /**
12
17
  * Options for creating a React root
13
18
  */
14
- declare interface CreateRootOptions {
19
+ declare interface CreateRootOptions_2 {
15
20
  identifierPrefix?: string;
16
21
  onRecoverableError?: (error: unknown) => void;
17
22
  transitionCallbacks?: unknown;
@@ -30,10 +35,20 @@ declare type LazyRemoteComponentInfo<T, E extends keyof T> = RemoteComponentPara
30
35
  /**
31
36
  * Parameters for the provider function
32
37
  */
33
- declare interface ProviderFnParams<T> {
38
+ export declare interface ProviderFnParams<T> {
34
39
  rootComponent: React_2.ComponentType<T>;
35
40
  render?: (App: React_2.ReactElement, id?: HTMLElement | string) => RootType | Promise<RootType>;
36
- createRoot?: (container: Element | DocumentFragment, options?: CreateRootOptions) => Root;
41
+ createRoot?: (container: Element | DocumentFragment, options?: CreateRootOptions_2) => Root_2;
42
+ /**
43
+ * Default options to pass to createRoot for React 18 and 19
44
+ * These options will be used when creating a root unless overridden by rootOptions in render params
45
+ * @example
46
+ * {
47
+ * identifierPrefix: 'app-',
48
+ * onRecoverableError: (err) => console.error(err)
49
+ * }
50
+ */
51
+ defaultRootOptions?: CreateRootOptions_2;
37
52
  }
38
53
 
39
54
  /**
@@ -75,17 +90,6 @@ declare interface RemoteComponentProps<T = Record<string, unknown>> {
75
90
  [key: string]: unknown;
76
91
  }
77
92
 
78
- /**
79
- * Parameters for the render function, extending ProviderParams
80
- */
81
- export declare interface RenderFnParams extends ProviderParams {
82
- dom: HTMLElement;
83
- fallback?: React_2.ComponentType<{
84
- error: Error;
85
- }>;
86
- [key: string]: unknown;
87
- }
88
-
89
93
  /**
90
94
  * Parameters for the render function
91
95
  */
@@ -97,13 +101,27 @@ export declare interface RenderParams {
97
101
  initialState?: Record<string, unknown>;
98
102
  };
99
103
  dom: HTMLElement;
104
+ /**
105
+ * Options to pass to createRoot for React 18 and 19
106
+ * @example
107
+ * {
108
+ * identifierPrefix: 'app-',
109
+ * onRecoverableError: (err) => console.error(err)
110
+ * }
111
+ */
112
+ rootOptions?: CreateRootOptions_2;
100
113
  [key: string]: unknown;
101
114
  }
102
115
 
116
+ export declare interface Root {
117
+ render(children: React.ReactNode): void;
118
+ unmount(): void;
119
+ }
120
+
103
121
  /**
104
122
  * Interface for a React root object
105
123
  */
106
- declare interface Root {
124
+ declare interface Root_2 {
107
125
  render(children: React_2.ReactNode): void;
108
126
  unmount(): void;
109
127
  }
@@ -111,6 +129,6 @@ declare interface Root {
111
129
  /**
112
130
  * Type for a root element, which can be either an HTMLElement or a React root
113
131
  */
114
- declare type RootType = HTMLElement | Root;
132
+ export declare type RootType = HTMLElement | Root_2;
115
133
 
116
134
  export { }