@module-federation/bridge-react 0.0.0-next-20250416061914 → 0.0.0-next-20250421062338

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.
@@ -1,99 +0,0 @@
1
- import { createContext, Component, createElement } from "react";
2
- const ErrorBoundaryContext = createContext(null);
3
- const initialState = {
4
- didCatch: false,
5
- error: null
6
- };
7
- class ErrorBoundary extends Component {
8
- constructor(props) {
9
- super(props);
10
- this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
11
- this.state = initialState;
12
- }
13
- static getDerivedStateFromError(error) {
14
- return {
15
- didCatch: true,
16
- error
17
- };
18
- }
19
- resetErrorBoundary() {
20
- const {
21
- error
22
- } = this.state;
23
- if (error !== null) {
24
- var _this$props$onReset, _this$props;
25
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26
- args[_key] = arguments[_key];
27
- }
28
- (_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
29
- args,
30
- reason: "imperative-api"
31
- });
32
- this.setState(initialState);
33
- }
34
- }
35
- componentDidCatch(error, info) {
36
- var _this$props$onError, _this$props2;
37
- (_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
38
- }
39
- componentDidUpdate(prevProps, prevState) {
40
- const {
41
- didCatch
42
- } = this.state;
43
- const {
44
- resetKeys
45
- } = this.props;
46
- if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
47
- var _this$props$onReset2, _this$props3;
48
- (_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
49
- next: resetKeys,
50
- prev: prevProps.resetKeys,
51
- reason: "keys"
52
- });
53
- this.setState(initialState);
54
- }
55
- }
56
- render() {
57
- const {
58
- children,
59
- fallbackRender,
60
- FallbackComponent,
61
- fallback
62
- } = this.props;
63
- const {
64
- didCatch,
65
- error
66
- } = this.state;
67
- let childToRender = children;
68
- if (didCatch) {
69
- const props = {
70
- error,
71
- resetErrorBoundary: this.resetErrorBoundary
72
- };
73
- if (typeof fallbackRender === "function") {
74
- childToRender = fallbackRender(props);
75
- } else if (FallbackComponent) {
76
- childToRender = createElement(FallbackComponent, props);
77
- } else if (fallback !== void 0) {
78
- childToRender = fallback;
79
- } else {
80
- throw error;
81
- }
82
- }
83
- return createElement(ErrorBoundaryContext.Provider, {
84
- value: {
85
- didCatch,
86
- error,
87
- resetErrorBoundary: this.resetErrorBoundary
88
- }
89
- }, childToRender);
90
- }
91
- }
92
- function hasArrayChanged() {
93
- let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
94
- let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
95
- return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
96
- }
97
- export {
98
- ErrorBoundary as E
99
- };
@@ -1,98 +0,0 @@
1
- "use strict";
2
- const React = require("react");
3
- const ErrorBoundaryContext = React.createContext(null);
4
- const initialState = {
5
- didCatch: false,
6
- error: null
7
- };
8
- class ErrorBoundary extends React.Component {
9
- constructor(props) {
10
- super(props);
11
- this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
12
- this.state = initialState;
13
- }
14
- static getDerivedStateFromError(error) {
15
- return {
16
- didCatch: true,
17
- error
18
- };
19
- }
20
- resetErrorBoundary() {
21
- const {
22
- error
23
- } = this.state;
24
- if (error !== null) {
25
- var _this$props$onReset, _this$props;
26
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
27
- args[_key] = arguments[_key];
28
- }
29
- (_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
30
- args,
31
- reason: "imperative-api"
32
- });
33
- this.setState(initialState);
34
- }
35
- }
36
- componentDidCatch(error, info) {
37
- var _this$props$onError, _this$props2;
38
- (_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
39
- }
40
- componentDidUpdate(prevProps, prevState) {
41
- const {
42
- didCatch
43
- } = this.state;
44
- const {
45
- resetKeys
46
- } = this.props;
47
- if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
48
- var _this$props$onReset2, _this$props3;
49
- (_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
50
- next: resetKeys,
51
- prev: prevProps.resetKeys,
52
- reason: "keys"
53
- });
54
- this.setState(initialState);
55
- }
56
- }
57
- render() {
58
- const {
59
- children,
60
- fallbackRender,
61
- FallbackComponent,
62
- fallback
63
- } = this.props;
64
- const {
65
- didCatch,
66
- error
67
- } = this.state;
68
- let childToRender = children;
69
- if (didCatch) {
70
- const props = {
71
- error,
72
- resetErrorBoundary: this.resetErrorBoundary
73
- };
74
- if (typeof fallbackRender === "function") {
75
- childToRender = fallbackRender(props);
76
- } else if (FallbackComponent) {
77
- childToRender = React.createElement(FallbackComponent, props);
78
- } else if (fallback !== void 0) {
79
- childToRender = fallback;
80
- } else {
81
- throw error;
82
- }
83
- }
84
- return React.createElement(ErrorBoundaryContext.Provider, {
85
- value: {
86
- didCatch,
87
- error,
88
- resetErrorBoundary: this.resetErrorBoundary
89
- }
90
- }, childToRender);
91
- }
92
- }
93
- function hasArrayChanged() {
94
- let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
95
- let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
96
- return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
97
- }
98
- exports.ErrorBoundary = ErrorBoundary;
package/dist/v16.cjs.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const bridgeBase = require("./bridge-base-BQcdOEoU.cjs");
4
- const ReactDOM = require("react-dom");
5
- function createReact16Or17Root(container) {
6
- return {
7
- render(children) {
8
- ReactDOM.render(children, container);
9
- },
10
- unmount() {
11
- ReactDOM.unmountComponentAtNode(container);
12
- }
13
- };
14
- }
15
- function createBridgeComponent(bridgeInfo) {
16
- const fullBridgeInfo = {
17
- ...bridgeInfo,
18
- createRoot: createReact16Or17Root
19
- };
20
- return bridgeBase.createBaseBridgeComponent(fullBridgeInfo);
21
- }
22
- exports.createBridgeComponent = createBridgeComponent;
package/dist/v16.d.ts DELETED
@@ -1,104 +0,0 @@
1
- import * as React_2 from 'react';
2
-
3
- export declare function createBridgeComponent<T = any>(bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>): () => {
4
- render(info: RenderParams): Promise<void>;
5
- destroy(info: DestroyParams): void;
6
- };
7
-
8
- export declare interface CreateRootOptions {
9
- identifierPrefix?: string;
10
- onRecoverableError?: (error: unknown, errorInfo: unknown) => void;
11
- }
12
-
13
- /**
14
- * Options for creating a React root
15
- */
16
- declare interface CreateRootOptions_2 {
17
- identifierPrefix?: string;
18
- onRecoverableError?: (error: unknown) => void;
19
- transitionCallbacks?: unknown;
20
- }
21
-
22
- /**
23
- * Parameters for the destroy function
24
- */
25
- export declare interface DestroyParams {
26
- moduleName: string;
27
- dom: HTMLElement;
28
- }
29
-
30
- /**
31
- * Parameters for the provider function
32
- */
33
- export declare interface ProviderFnParams<T> {
34
- rootComponent: React_2.ComponentType<T>;
35
- render?: (App: React_2.ReactElement, id?: HTMLElement | string) => RootType | Promise<RootType>;
36
- createRoot?: (container: Element | DocumentFragment, options?: CreateRootOptions_2) => Root_2;
37
- /**
38
- * Default options to pass to createRoot for React 18 and 19
39
- * These options will be used when creating a root unless overridden by rootOptions in render params
40
- * @example
41
- * {
42
- * identifierPrefix: 'app-',
43
- * onRecoverableError: (err) => console.error(err)
44
- * }
45
- */
46
- defaultRootOptions?: CreateRootOptions_2;
47
- }
48
-
49
- /**
50
- * Parameters for the provider function
51
- */
52
- export declare interface ProviderParams {
53
- moduleName?: string;
54
- basename?: string;
55
- memoryRoute?: {
56
- entryPath: string;
57
- initialState?: Record<string, unknown>;
58
- };
59
- style?: React_2.CSSProperties;
60
- className?: string;
61
- }
62
-
63
- /**
64
- * Parameters for the render function
65
- */
66
- export declare interface RenderParams {
67
- moduleName?: string;
68
- basename?: string;
69
- memoryRoute?: {
70
- entryPath: string;
71
- initialState?: Record<string, unknown>;
72
- };
73
- dom: HTMLElement;
74
- /**
75
- * Options to pass to createRoot for React 18 and 19
76
- * @example
77
- * {
78
- * identifierPrefix: 'app-',
79
- * onRecoverableError: (err) => console.error(err)
80
- * }
81
- */
82
- rootOptions?: CreateRootOptions_2;
83
- [key: string]: unknown;
84
- }
85
-
86
- export declare interface Root {
87
- render(children: React.ReactNode): void;
88
- unmount(): void;
89
- }
90
-
91
- /**
92
- * Interface for a React root object
93
- */
94
- declare interface Root_2 {
95
- render(children: React_2.ReactNode): void;
96
- unmount(): void;
97
- }
98
-
99
- /**
100
- * Type for a root element, which can be either an HTMLElement or a React root
101
- */
102
- export declare type RootType = HTMLElement | Root_2;
103
-
104
- export { }
package/dist/v16.es.js DELETED
@@ -1,22 +0,0 @@
1
- import { c as createBaseBridgeComponent } from "./bridge-base-BbzPi965.js";
2
- import ReactDOM from "react-dom";
3
- function createReact16Or17Root(container) {
4
- return {
5
- render(children) {
6
- ReactDOM.render(children, container);
7
- },
8
- unmount() {
9
- ReactDOM.unmountComponentAtNode(container);
10
- }
11
- };
12
- }
13
- function createBridgeComponent(bridgeInfo) {
14
- const fullBridgeInfo = {
15
- ...bridgeInfo,
16
- createRoot: createReact16Or17Root
17
- };
18
- return createBaseBridgeComponent(fullBridgeInfo);
19
- }
20
- export {
21
- createBridgeComponent
22
- };
package/src/legacy.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * Entry point for React 16/17 (legacy) specific bridge components
3
- * This file provides support for React 16 and 17 versions, using the traditional ReactDOM.render API
4
- */
5
- export { createBridgeComponent } from './provider/versions/legacy';
6
- export type { CreateRootOptions, Root } from './provider/versions/legacy';
7
- export type {
8
- ProviderParams,
9
- ProviderFnParams,
10
- RootType,
11
- DestroyParams,
12
- RenderParams,
13
- } from './types';
@@ -1,161 +0,0 @@
1
- import * as React from 'react';
2
- import ReactDOM from 'react-dom';
3
- import type {
4
- ProviderParams,
5
- ProviderFnParams,
6
- RootType,
7
- DestroyParams,
8
- RenderParams,
9
- CreateRootOptions,
10
- } from '../types';
11
- import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
12
- import { RouterContext } from './context';
13
- import { LoggerInstance } from '../utils';
14
- import { federationRuntime } from './plugin';
15
-
16
- /**
17
- * Default createRoot function that automatically detects React version and uses the appropriate API(only support React 16/17, 18)
18
- *
19
- * Note: Users can also directly import version-specific bridge components:
20
- * - import { createBridgeComponent } from '@module-federation/bridge-react/legacy'
21
- * - import { createBridgeComponent } from '@module-federation/bridge-react/v18'
22
- * - import { createBridgeComponent } from '@module-federation/bridge-react/v19'
23
- */
24
- function defaultCreateRoot(
25
- container: Element | DocumentFragment,
26
- options?: CreateRootOptions,
27
- ): {
28
- render(children: React.ReactNode): void;
29
- unmount(): void;
30
- } {
31
- const reactVersion = ReactDOM.version || '';
32
- const isReact18 = reactVersion.startsWith('18');
33
-
34
- // For React 18, use createRoot API
35
- if (isReact18) {
36
- try {
37
- // @ts-ignore - Types will be available in React 18
38
- return (ReactDOM as any).createRoot(container, options);
39
- } catch (e) {
40
- console.warn(
41
- 'Failed to use React 18 createRoot API, falling back to legacy API',
42
- e,
43
- );
44
- }
45
- }
46
-
47
- // For React 16/17, use legacy API
48
- return {
49
- render(children: React.ReactNode) {
50
- // @ts-ignore - React 17's render method is deprecated but still functional
51
- ReactDOM.render(children, container);
52
- },
53
- unmount() {
54
- ReactDOM.unmountComponentAtNode(container as Element);
55
- },
56
- };
57
- }
58
-
59
- /**
60
- * Creates a bridge component factory that automatically detects and uses
61
- * the appropriate React version (16/17, 18, or 19)
62
- */
63
- export function createBridgeComponent<T>({
64
- createRoot = defaultCreateRoot,
65
- defaultRootOptions,
66
- ...bridgeInfo
67
- }: ProviderFnParams<T>) {
68
- return () => {
69
- const rootMap = new Map<any, RootType>();
70
- const instance = federationRuntime.instance;
71
- LoggerInstance.debug(
72
- `createBridgeComponent instance from props >>>`,
73
- instance,
74
- );
75
-
76
- const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
77
- const { appInfo, propsInfo, ...restProps } = info;
78
- const { moduleName, memoryRoute, basename = '/' } = appInfo;
79
- return (
80
- <RouterContext.Provider value={{ moduleName, basename, memoryRoute }}>
81
- <bridgeInfo.rootComponent
82
- {...propsInfo}
83
- basename={basename}
84
- {...restProps}
85
- />
86
- </RouterContext.Provider>
87
- );
88
- };
89
-
90
- return {
91
- async render(info: RenderParams) {
92
- LoggerInstance.debug(`createBridgeComponent render Info`, info);
93
- const {
94
- moduleName,
95
- dom,
96
- basename,
97
- memoryRoute,
98
- fallback,
99
- rootOptions,
100
- ...propsInfo
101
- } = info;
102
-
103
- // Merge default root options with render-specific root options
104
- const mergedRootOptions: CreateRootOptions | undefined = {
105
- ...defaultRootOptions,
106
- ...(rootOptions as CreateRootOptions),
107
- };
108
-
109
- const beforeBridgeRenderRes =
110
- instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info) || {};
111
-
112
- const rootComponentWithErrorBoundary = (
113
- <ErrorBoundary
114
- FallbackComponent={fallback as React.ComponentType<FallbackProps>}
115
- >
116
- <RawComponent
117
- appInfo={{
118
- moduleName,
119
- basename,
120
- memoryRoute,
121
- }}
122
- propsInfo={
123
- {
124
- ...propsInfo,
125
- ...(beforeBridgeRenderRes as any)?.extraProps,
126
- } as T
127
- }
128
- />
129
- </ErrorBoundary>
130
- );
131
-
132
- let root = rootMap.get(dom);
133
- // Do not call createRoot multiple times
134
- if (!root) {
135
- root = createRoot(dom, mergedRootOptions);
136
- rootMap.set(dom, root);
137
- }
138
-
139
- if ('render' in root) {
140
- root.render(rootComponentWithErrorBoundary);
141
- }
142
- instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
143
- },
144
-
145
- destroy(info: DestroyParams) {
146
- const { dom } = info;
147
- LoggerInstance.debug(`createBridgeComponent destroy Info`, info);
148
- const root = rootMap.get(dom);
149
- if (root) {
150
- if ('unmount' in root) {
151
- root.unmount();
152
- } else {
153
- ReactDOM.unmountComponentAtNode(dom as HTMLElement);
154
- }
155
- rootMap.delete(dom);
156
- }
157
- instance?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit(info);
158
- },
159
- };
160
- };
161
- }
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  const React = require("react");
3
+ const RouterContext = React.createContext(null);
3
4
  const BROWSER_LOG_KEY = "FEDERATION_DEBUG";
4
5
  const BROWSER_LOG_VALUE = "1";
5
6
  function isBrowserEnv() {
@@ -95,7 +96,6 @@ const getRootDomDefaultClassName = (moduleName) => {
95
96
  const name = getModuleName(moduleName).replace(/\@/, "").replace(/\//, "-");
96
97
  return `bridge-root-component-${name}`;
97
98
  };
98
- const RouterContext = React.createContext(null);
99
99
  exports.LoggerInstance = LoggerInstance;
100
100
  exports.RouterContext = RouterContext;
101
101
  exports.getRootDomDefaultClassName = getRootDomDefaultClassName;
@@ -1,4 +1,5 @@
1
1
  import React__default from "react";
2
+ const RouterContext = React__default.createContext(null);
2
3
  const BROWSER_LOG_KEY = "FEDERATION_DEBUG";
3
4
  const BROWSER_LOG_VALUE = "1";
4
5
  function isBrowserEnv() {
@@ -94,7 +95,6 @@ const getRootDomDefaultClassName = (moduleName) => {
94
95
  const name = getModuleName(moduleName).replace(/\@/, "").replace(/\//, "-");
95
96
  return `bridge-root-component-${name}`;
96
97
  };
97
- const RouterContext = React__default.createContext(null);
98
98
  export {
99
99
  LoggerInstance as L,
100
100
  RouterContext as R,