@module-federation/bridge-react 0.11.4 → 0.13.0

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/v19.es.js ADDED
@@ -0,0 +1,15 @@
1
+ import { createRoot } from "react-dom/client";
2
+ import { c as createBaseBridgeComponent } from "./bridge-base-0CS6p-6-.js";
3
+ function createReact19Root(container, options) {
4
+ return createRoot(container, options);
5
+ }
6
+ function createBridgeComponent(bridgeInfo) {
7
+ const fullBridgeInfo = {
8
+ createRoot: createReact19Root,
9
+ ...bridgeInfo
10
+ };
11
+ return createBaseBridgeComponent(fullBridgeInfo);
12
+ }
13
+ export {
14
+ createBridgeComponent
15
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react",
3
- "version": "0.11.4",
3
+ "version": "0.13.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -21,6 +21,16 @@
21
21
  "import": "./dist/index.es.js",
22
22
  "require": "./dist/index.cjs.js"
23
23
  },
24
+ "./v18": {
25
+ "types": "./dist/v18.d.ts",
26
+ "import": "./dist/v18.es.js",
27
+ "require": "./dist/v18.cjs.js"
28
+ },
29
+ "./v19": {
30
+ "types": "./dist/v19.d.ts",
31
+ "import": "./dist/v19.es.js",
32
+ "require": "./dist/v19.cjs.js"
33
+ },
24
34
  "./router": {
25
35
  "types": "./dist/router.d.ts",
26
36
  "import": "./dist/router.es.js",
@@ -45,8 +55,8 @@
45
55
  },
46
56
  "dependencies": {
47
57
  "react-error-boundary": "^4.1.2",
48
- "@module-federation/bridge-shared": "0.11.4",
49
- "@module-federation/sdk": "0.11.4"
58
+ "@module-federation/bridge-shared": "0.13.0",
59
+ "@module-federation/sdk": "0.13.0"
50
60
  },
51
61
  "peerDependencies": {
52
62
  "react": ">=16.9.0",
@@ -65,9 +75,9 @@
65
75
  "react-dom": "18.3.1",
66
76
  "react-router-dom": "6.22.3",
67
77
  "typescript": "^5.2.2",
68
- "vite": "^5.4.12",
78
+ "vite": "^5.4.18",
69
79
  "vite-plugin-dts": "^4.3.0",
70
- "@module-federation/runtime": "0.11.4"
80
+ "@module-federation/runtime": "0.13.0"
71
81
  },
72
82
  "scripts": {
73
83
  "dev": "vite",
package/src/index.ts CHANGED
@@ -1,8 +1,14 @@
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';
1
6
  export { createRemoteComponent } from './remote/create';
2
- export { createBridgeComponent } from './provider/create';
7
+ export type { CreateRootOptions, Root } from './provider/versions/legacy';
3
8
  export type {
4
9
  ProviderParams,
5
- RenderFnParams,
10
+ ProviderFnParams,
11
+ RootType,
6
12
  DestroyParams,
7
13
  RenderParams,
8
14
  } from './types';
@@ -1,20 +1,24 @@
1
+ /**
2
+ * Base bridge component implementation
3
+ * This file contains bridge component logic shared across all React versions
4
+ */
1
5
  import * as React from 'react';
2
- import ReactDOM from 'react-dom';
3
6
  import type {
4
7
  ProviderParams,
5
8
  ProviderFnParams,
6
9
  RootType,
7
10
  DestroyParams,
8
11
  RenderParams,
9
- } from '../types';
10
- import { ErrorBoundary } from 'react-error-boundary';
11
- import { RouterContext } from './context';
12
- import { LoggerInstance } from '../utils';
13
- import { federationRuntime } from './plugin';
14
- import { createRoot as defaultCreateRoot } from './compat';
12
+ CreateRootOptions,
13
+ } from '../../types';
14
+ import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
15
+ import { RouterContext } from '../context';
16
+ import { LoggerInstance } from '../../utils';
17
+ import { federationRuntime } from '../plugin';
15
18
 
16
- export function createBridgeComponent<T>({
17
- createRoot = defaultCreateRoot,
19
+ export function createBaseBridgeComponent<T>({
20
+ createRoot,
21
+ defaultRootOptions,
18
22
  ...bridgeInfo
19
23
  }: ProviderFnParams<T>) {
20
24
  return () => {
@@ -48,14 +52,22 @@ export function createBridgeComponent<T>({
48
52
  basename,
49
53
  memoryRoute,
50
54
  fallback,
55
+ rootOptions,
51
56
  ...propsInfo
52
57
  } = info;
53
58
 
59
+ const mergedRootOptions: CreateRootOptions | undefined = {
60
+ ...defaultRootOptions,
61
+ ...(rootOptions as CreateRootOptions),
62
+ };
63
+
54
64
  const beforeBridgeRenderRes =
55
65
  instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info) || {};
56
66
 
57
67
  const rootComponentWithErrorBoundary = (
58
- <ErrorBoundary FallbackComponent={fallback}>
68
+ <ErrorBoundary
69
+ FallbackComponent={fallback as React.ComponentType<FallbackProps>}
70
+ >
59
71
  <RawComponent
60
72
  appInfo={{
61
73
  moduleName,
@@ -63,7 +75,10 @@ export function createBridgeComponent<T>({
63
75
  memoryRoute,
64
76
  }}
65
77
  propsInfo={
66
- { ...propsInfo, ...beforeBridgeRenderRes?.extraProps } as T
78
+ {
79
+ ...propsInfo,
80
+ ...(beforeBridgeRenderRes as any)?.extraProps,
81
+ } as T
67
82
  }
68
83
  />
69
84
  </ErrorBoundary>
@@ -75,17 +90,16 @@ export function createBridgeComponent<T>({
75
90
  ).then((root: RootType) => rootMap.set(dom, root));
76
91
  } else {
77
92
  let root = rootMap.get(dom);
78
- // do not call createRoot multiple times
79
- if (!root) {
80
- root = createRoot(dom);
81
- rootMap.set(dom, root);
93
+ // Do not call createRoot multiple times
94
+ if (!root && createRoot) {
95
+ root = createRoot(dom, mergedRootOptions);
96
+ rootMap.set(dom, root as any);
82
97
  }
83
98
 
84
- if ('render' in root) {
99
+ if (root && 'render' in root) {
85
100
  root.render(rootComponentWithErrorBoundary);
86
101
  }
87
102
  }
88
-
89
103
  instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
90
104
  },
91
105
 
@@ -97,7 +111,7 @@ export function createBridgeComponent<T>({
97
111
  if ('unmount' in root) {
98
112
  root.unmount();
99
113
  } else {
100
- ReactDOM.unmountComponentAtNode(root as HTMLElement);
114
+ console.warn('Root does not have unmount method');
101
115
  }
102
116
  rootMap.delete(dom);
103
117
  }
@@ -0,0 +1,86 @@
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
+ import type { ProviderFnParams } from '../../types';
6
+ import { createBaseBridgeComponent } from './bridge-base';
7
+ import ReactDOM from 'react-dom';
8
+
9
+ export interface CreateRootOptions {
10
+ identifierPrefix?: string;
11
+ onRecoverableError?: (error: unknown, errorInfo: unknown) => void;
12
+ }
13
+
14
+ export interface Root {
15
+ render(children: React.ReactNode): void;
16
+ unmount(): void;
17
+ }
18
+
19
+ /**
20
+ * Default createRoot function that automatically detects React version and uses the appropriate API(only support React 16/17, 18)
21
+ *
22
+ * Note: Users can also directly import version-specific bridge components:
23
+ * - import { createBridgeComponent } from '@module-federation/bridge-react'
24
+ * - import { createBridgeComponent } from '@module-federation/bridge-react/v18'
25
+ * - import { createBridgeComponent } from '@module-federation/bridge-react/v19'
26
+ */
27
+
28
+ export function createReact16Or17Root(
29
+ container: Element | DocumentFragment,
30
+ ): Root {
31
+ return {
32
+ render(children: React.ReactNode) {
33
+ /**
34
+ * Detect React version
35
+ */
36
+ const reactVersion = ReactDOM.version || '';
37
+ const isReact18 = reactVersion.startsWith('18');
38
+ const isReact19 = reactVersion.startsWith('19');
39
+
40
+ /**
41
+ * Throw error for React 19
42
+ *
43
+ * Note: Due to Module Federation sharing mechanism, the actual version detected here
44
+ * might be 18 or 19, even if the application itself uses React 16/17.
45
+ * This happens because in MF environments, different remote modules may share different React versions.
46
+ * The console may throw warnings about version and API mismatches. If you need to resolve these issues,
47
+ * consider disabling the shared configuration for React.
48
+ */
49
+ if (isReact19) {
50
+ throw new Error(
51
+ `React 19 detected in legacy mode. This is not supported. ` +
52
+ `Please use the version-specific import: ` +
53
+ `import { createBridgeComponent } from '@module-federation/bridge-react/v19'`,
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Provide warning for React 18
59
+ */
60
+ if (isReact18) {
61
+ console.warn(
62
+ `[Bridge-React] React 18 detected in legacy mode. ` +
63
+ `For better compatibility, please use the version-specific import: ` +
64
+ `import { createBridgeComponent } from '@module-federation/bridge-react/v18'`,
65
+ );
66
+ }
67
+
68
+ // @ts-ignore - React 17's render method is deprecated but still functional
69
+ ReactDOM.render(children, container);
70
+ },
71
+ unmount() {
72
+ ReactDOM.unmountComponentAtNode(container as Element);
73
+ },
74
+ };
75
+ }
76
+
77
+ export function createBridgeComponent<T = any>(
78
+ bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>,
79
+ ) {
80
+ const fullBridgeInfo = {
81
+ createRoot: createReact16Or17Root,
82
+ ...bridgeInfo,
83
+ } as unknown as ProviderFnParams<T>;
84
+
85
+ return createBaseBridgeComponent(fullBridgeInfo);
86
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Entry point for React 18 specific bridge components
3
+ */
4
+ import React from 'react';
5
+ import { createRoot as createReactRoot, hydrateRoot } from 'react-dom/client';
6
+ import { createBaseBridgeComponent } from './bridge-base';
7
+ import type { ProviderFnParams } from '../../types';
8
+
9
+ export interface CreateRootOptions {
10
+ identifierPrefix?: string;
11
+ onRecoverableError?: (error: unknown, errorInfo: unknown) => void;
12
+ }
13
+
14
+ export interface Root {
15
+ render(children: React.ReactNode): void;
16
+ unmount(): void;
17
+ }
18
+
19
+ export function createReact18Root(
20
+ container: Element | DocumentFragment,
21
+ options?: CreateRootOptions,
22
+ ): Root {
23
+ return createReactRoot(container, options);
24
+ }
25
+
26
+ export function hydrateReact18Root(
27
+ container: Element | DocumentFragment,
28
+ initialChildren: React.ReactNode,
29
+ options?: CreateRootOptions,
30
+ ) {
31
+ return hydrateRoot(
32
+ container as Element,
33
+ initialChildren as React.ReactElement,
34
+ options,
35
+ );
36
+ }
37
+
38
+ export function createBridgeComponent<T = any>(
39
+ bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>,
40
+ ) {
41
+ const fullBridgeInfo = {
42
+ createRoot: createReact18Root,
43
+ ...bridgeInfo,
44
+ } as unknown as ProviderFnParams<T>;
45
+
46
+ return createBaseBridgeComponent(fullBridgeInfo);
47
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Entry point for React 19 specific bridge components
3
+ * This file provides support for React 19 version, using the new ReactDOM.createRoot API
4
+ */
5
+ import React from 'react';
6
+ import { createRoot, hydrateRoot } from 'react-dom/client';
7
+ import { createBaseBridgeComponent } from './bridge-base';
8
+ import type { ProviderFnParams } from '../../types';
9
+ export interface CreateRootOptions {
10
+ identifierPrefix?: string;
11
+ onRecoverableError?: (error: unknown, errorInfo: unknown) => void;
12
+ transitionCallbacks?: unknown;
13
+ }
14
+
15
+ export interface Root {
16
+ render(children: React.ReactNode): void;
17
+ unmount(): void;
18
+ }
19
+
20
+ export function createReact19Root(
21
+ container: Element | DocumentFragment,
22
+ options?: CreateRootOptions,
23
+ ): Root {
24
+ return createRoot(container as Element, options);
25
+ }
26
+
27
+ export function hydrateReact19Root(
28
+ container: Element | DocumentFragment,
29
+ initialChildren: React.ReactNode,
30
+ options?: CreateRootOptions,
31
+ ): Root {
32
+ return hydrateRoot(
33
+ container as Element,
34
+ initialChildren as React.ReactElement,
35
+ options,
36
+ );
37
+ }
38
+
39
+ export function createBridgeComponent<T = any>(
40
+ bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>,
41
+ ) {
42
+ const fullBridgeInfo = {
43
+ createRoot: createReact19Root,
44
+ ...bridgeInfo,
45
+ } as unknown as ProviderFnParams<T>;
46
+
47
+ return createBaseBridgeComponent(fullBridgeInfo);
48
+ }
@@ -1,5 +1,5 @@
1
1
  import React, { forwardRef } from 'react';
2
- import { ErrorBoundary } from 'react-error-boundary';
2
+ import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
3
3
  import { LoggerInstance } from '../utils';
4
4
  import RemoteApp from './component';
5
5
  import {
@@ -77,7 +77,9 @@ export function createRemoteComponent<
77
77
  const LazyComponent = createLazyRemoteComponent(info);
78
78
  return forwardRef<HTMLDivElement, RemoteComponentProps>((props, ref) => {
79
79
  return (
80
- <ErrorBoundary FallbackComponent={info.fallback}>
80
+ <ErrorBoundary
81
+ FallbackComponent={info.fallback as React.ComponentType<FallbackProps>}
82
+ >
81
83
  <React.Suspense fallback={info.loading}>
82
84
  <LazyComponent {...props} ref={ref} />
83
85
  </React.Suspense>
package/src/types.ts CHANGED
@@ -34,6 +34,15 @@ export interface RenderParams {
34
34
  initialState?: Record<string, unknown>;
35
35
  };
36
36
  dom: HTMLElement;
37
+ /**
38
+ * Options to pass to createRoot for React 18 and 19
39
+ * @example
40
+ * {
41
+ * identifierPrefix: 'app-',
42
+ * onRecoverableError: (err) => console.error(err)
43
+ * }
44
+ */
45
+ rootOptions?: CreateRootOptions;
37
46
  [key: string]: unknown;
38
47
  }
39
48
 
@@ -81,6 +90,16 @@ export interface ProviderFnParams<T> {
81
90
  container: Element | DocumentFragment,
82
91
  options?: CreateRootOptions,
83
92
  ) => Root;
93
+ /**
94
+ * Default options to pass to createRoot for React 18 and 19
95
+ * These options will be used when creating a root unless overridden by rootOptions in render params
96
+ * @example
97
+ * {
98
+ * identifierPrefix: 'app-',
99
+ * onRecoverableError: (err) => console.error(err)
100
+ * }
101
+ */
102
+ defaultRootOptions?: CreateRootOptions;
84
103
  }
85
104
 
86
105
  /**
@@ -1,29 +1,9 @@
1
- import React from 'react';
2
1
  import { createLogger } from '@module-federation/sdk';
3
2
 
4
3
  export const LoggerInstance = createLogger(
5
4
  '[ Module Federation Bridge React ]',
6
5
  );
7
6
 
8
- type typeReact = typeof React;
9
-
10
- export function atLeastReact18(React: typeReact) {
11
- if (
12
- React &&
13
- typeof React.version === 'string' &&
14
- React.version.indexOf('.') >= 0
15
- ) {
16
- const majorVersionString = React.version.split('.')[0];
17
- try {
18
- return Number(majorVersionString) >= 18;
19
- } catch (err) {
20
- return false;
21
- }
22
- } else {
23
- return false;
24
- }
25
- }
26
-
27
7
  export function pathJoin(...args: string[]) {
28
8
  const res = args.reduce((res, path: string) => {
29
9
  let nPath = path;
package/src/v18.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { createBridgeComponent } from './provider/versions/v18';
2
+ export type { CreateRootOptions, Root } from './provider/versions/v18';
3
+ export type {
4
+ ProviderParams,
5
+ ProviderFnParams,
6
+ RootType,
7
+ DestroyParams,
8
+ RenderParams,
9
+ } from './types';
package/src/v19.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { createBridgeComponent } from './provider/versions/v19';
2
+ export type { CreateRootOptions, Root } from './provider/versions/v19';
3
+ export type {
4
+ ProviderParams,
5
+ ProviderFnParams,
6
+ RootType,
7
+ DestroyParams,
8
+ RenderParams,
9
+ } from './types';
package/vite.config.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import { defineConfig } from 'vite';
2
- // import vue from '@vitejs/plugin-vue';
3
2
  import path from 'path';
4
3
  import dts from 'vite-plugin-dts';
5
- // import react from '@vitejs/plugin-react';
6
4
  import packageJson from './package.json';
7
5
 
8
6
  const perDepsKeys = Object.keys(packageJson.peerDependencies);
9
7
 
10
8
  export default defineConfig({
11
9
  plugins: [
10
+ // 添加我们的自定义插件
12
11
  dts({
13
12
  rollupTypes: true,
14
13
  bundledPackages: [
@@ -25,6 +24,8 @@ export default defineConfig({
25
24
  router: path.resolve(__dirname, 'src/router/default.tsx'),
26
25
  'router-v5': path.resolve(__dirname, 'src/router/v5.tsx'),
27
26
  'router-v6': path.resolve(__dirname, 'src/router/v6.tsx'),
27
+ v18: path.resolve(__dirname, 'src/v18.ts'),
28
+ v19: path.resolve(__dirname, 'src/v19.ts'),
28
29
  },
29
30
  formats: ['cjs', 'es'],
30
31
  fileName: (format, entryName) => `${entryName}.${format}.js`,
@@ -1,60 +0,0 @@
1
- import ReactDOM from 'react-dom';
2
- import { CreateRootOptions, Root } from '../types';
3
-
4
- // ReactDOM.version is only available in React 16.13.0 and later
5
- const isReact18 = ReactDOM.version?.startsWith('18');
6
-
7
- /**
8
- * Creates a root for a container element compatible with both React 16 and 18
9
- */
10
- export function createRoot(
11
- container: Element | DocumentFragment,
12
- options?: CreateRootOptions,
13
- ): Root {
14
- if (isReact18) {
15
- // For React 18, use the new createRoot API
16
- // @ts-ignore - Types will be available in React 18
17
- return (ReactDOM as any).createRoot(container, options);
18
- }
19
-
20
- // For React 16/17, simulate the new root API using render/unmountComponentAtNode
21
- return {
22
- render(children: React.ReactNode) {
23
- ReactDOM.render(children, container);
24
- },
25
- unmount() {
26
- ReactDOM.unmountComponentAtNode(container);
27
- },
28
- };
29
- }
30
-
31
- /**
32
- * Hydrates a container compatible with both React 16 and 18
33
- */
34
- export function hydrateRoot(
35
- container: Element | DocumentFragment,
36
- initialChildren: React.ReactNode,
37
- options?: CreateRootOptions,
38
- ): Root {
39
- if (isReact18) {
40
- // For React 18, use the new hydrateRoot API
41
- // @ts-ignore - Types will be available in React 18
42
- return (ReactDOM as any).hydrateRoot(container, initialChildren, options);
43
- }
44
-
45
- // For React 16/17, simulate the new root API using hydrate/unmountComponentAtNode
46
- return {
47
- render(children: React.ReactNode) {
48
- // For the initial render, use hydrate
49
- if (children === initialChildren) {
50
- ReactDOM.hydrate(children, container);
51
- } else {
52
- // For subsequent renders, use regular render
53
- ReactDOM.render(children, container);
54
- }
55
- },
56
- unmount() {
57
- ReactDOM.unmountComponentAtNode(container);
58
- },
59
- };
60
- }
@@ -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,