@module-federation/bridge-react 0.0.0-next-20240913094943 → 0.0.0-next-20240918063302

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react",
3
- "version": "0.0.0-next-20240913094943",
3
+ "version": "0.0.0-next-20240918063302",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -35,12 +35,13 @@
35
35
  "dependencies": {
36
36
  "@loadable/component": "^5.16.4",
37
37
  "react-error-boundary": "^4.0.13",
38
- "@module-federation/bridge-shared": "0.0.0-next-20240913094943"
38
+ "@module-federation/bridge-shared": "0.0.0-next-20240918063302"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": ">=16.9.0",
42
42
  "react-dom": ">=16.9.0",
43
- "react-router-dom": ">=4"
43
+ "react-router-dom": ">=4",
44
+ "@module-federation/runtime": "0.0.0-next-20240918063302"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@testing-library/react": "15.0.7",
package/src/create.tsx CHANGED
@@ -1,12 +1,14 @@
1
1
  import React, { forwardRef } from 'react';
2
- import type { ProviderParams } from '@module-federation/bridge-shared';
3
- import { LoggerInstance } from './utils';
4
2
  import {
5
3
  ErrorBoundary,
6
4
  ErrorBoundaryPropsWithComponent,
7
5
  } from 'react-error-boundary';
6
+
7
+ import { LoggerInstance } from './utils';
8
8
  import RemoteApp from './remote';
9
9
 
10
+ import type { ProviderParams } from '@module-federation/bridge-shared';
11
+
10
12
  export interface RenderFnParams extends ProviderParams {
11
13
  dom?: any;
12
14
  }
package/src/index.ts CHANGED
@@ -4,3 +4,4 @@ export type {
4
4
  ProviderParams,
5
5
  RenderFnParams,
6
6
  } from '@module-federation/bridge-shared';
7
+ export type { BridgeRuntimePlugin } from './lifecycle';
@@ -0,0 +1,18 @@
1
+ import { getInstance } from '@module-federation/runtime';
2
+ import helper from '@module-federation/runtime/helpers';
3
+
4
+ const registerPlugin = helper.global.registerPlugins;
5
+ const pluginHelper = helper.global.pluginHelper;
6
+ const host = getInstance()!;
7
+ const pluginSystem = new pluginHelper.PluginSystem({
8
+ beforeBridgeRender: new pluginHelper.SyncHook<[Record<string, any>], void>(),
9
+ beforeBridgeDestroy: new pluginHelper.SyncHook<[Record<string, any>], void>(),
10
+ });
11
+
12
+ registerPlugin<typeof pluginSystem.lifecycle, typeof pluginSystem>(
13
+ host?.options?.plugins,
14
+ [pluginSystem],
15
+ );
16
+
17
+ export default pluginSystem;
18
+ export type BridgeRuntimePlugin = typeof pluginSystem;
package/src/provider.tsx CHANGED
@@ -2,23 +2,32 @@ import { useLayoutEffect, useRef, useState } from 'react';
2
2
  import * as React from 'react';
3
3
  import ReactDOM from 'react-dom';
4
4
  import ReactDOMClient from 'react-dom/client';
5
- import { RouterContext } from './context';
6
5
  import type {
7
6
  ProviderParams,
8
7
  RenderFnParams,
9
8
  } from '@module-federation/bridge-shared';
10
- import { LoggerInstance, atLeastReact18 } from './utils';
11
9
  import { ErrorBoundary } from 'react-error-boundary';
12
10
 
11
+ import { RouterContext } from './context';
12
+ import { LoggerInstance, atLeastReact18 } from './utils';
13
+
14
+ type RenderParams = RenderFnParams & any;
15
+ type DestroyParams = {
16
+ dom: HTMLElement;
17
+ };
13
18
  type RootType = HTMLElement | ReactDOMClient.Root;
19
+
14
20
  type ProviderFnParams<T> = {
15
21
  rootComponent: React.ComponentType<T>;
16
22
  render?: (
17
23
  App: React.ReactElement,
18
24
  id?: HTMLElement | string,
19
25
  ) => RootType | Promise<RootType>;
26
+ hooks?: {
27
+ beforeBridgeRender?: (params: RenderFnParams) => void;
28
+ beforeBridgeDestroy?: (params: DestroyParams) => void;
29
+ };
20
30
  };
21
-
22
31
  export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
23
32
  return () => {
24
33
  const rootMap = new Map<any, RootType>();
@@ -37,7 +46,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
37
46
  };
38
47
 
39
48
  return {
40
- async render(info: RenderFnParams & any) {
49
+ async render(info: RenderParams) {
41
50
  LoggerInstance.log(`createBridgeComponent render Info`, info);
42
51
  const {
43
52
  moduleName,
@@ -61,6 +70,17 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
61
70
  </ErrorBoundary>
62
71
  );
63
72
 
73
+ // call beforeBridgeRender hook
74
+ if (
75
+ bridgeInfo?.hooks &&
76
+ bridgeInfo?.hooks.beforeBridgeRender &&
77
+ typeof bridgeInfo?.hooks.beforeBridgeRender === 'function'
78
+ ) {
79
+ bridgeInfo.hooks.beforeBridgeRender(info);
80
+ // bridgeInfo?.beforeBridgeRender?.(info);
81
+ }
82
+
83
+ // call render function
64
84
  if (atLeastReact18(React)) {
65
85
  if (bridgeInfo?.render) {
66
86
  // in case bridgeInfo?.render is an async function, resolve this to promise
@@ -78,10 +98,21 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
78
98
  renderFn?.(rootComponentWithErrorBoundary, info.dom);
79
99
  }
80
100
  },
81
- async destroy(info: { dom: HTMLElement }) {
101
+ async destroy(info: DestroyParams) {
82
102
  LoggerInstance.log(`createBridgeComponent destroy Info`, {
83
103
  dom: info.dom,
84
104
  });
105
+
106
+ // call beforeBridgeDestroy hook
107
+ if (
108
+ bridgeInfo?.hooks &&
109
+ bridgeInfo?.hooks.beforeBridgeDestroy &&
110
+ typeof bridgeInfo?.hooks.beforeBridgeDestroy === 'function'
111
+ ) {
112
+ bridgeInfo.hooks.beforeBridgeDestroy(info);
113
+ }
114
+
115
+ // call destroy function
85
116
  if (atLeastReact18(React)) {
86
117
  const root = rootMap.get(info.dom);
87
118
  (root as ReactDOMClient.Root)?.unmount();
@@ -7,9 +7,10 @@ import React, {
7
7
  } from 'react';
8
8
  import * as ReactRouterDOM from 'react-router-dom';
9
9
  import type { ProviderParams } from '@module-federation/bridge-shared';
10
- import { LoggerInstance, pathJoin } from '../utils';
11
10
  import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
12
11
  import { ErrorBoundaryPropsWithComponent } from 'react-error-boundary';
12
+ import bridgeHook from '../lifecycle';
13
+ import { LoggerInstance, pathJoin } from '../utils';
13
14
 
14
15
  declare const __APP_VERSION__: string;
15
16
  export interface RenderFnParams extends ProviderParams {
@@ -78,6 +79,9 @@ const RemoteAppWrapper = forwardRef(function (
78
79
  `createRemoteComponent LazyComponent render >>>`,
79
80
  renderProps,
80
81
  );
82
+ bridgeHook.lifecycle.beforeBridgeRender.emit({
83
+ ...renderProps,
84
+ });
81
85
  providerReturn.render(renderProps);
82
86
  });
83
87
 
@@ -89,6 +93,14 @@ const RemoteAppWrapper = forwardRef(function (
89
93
  `createRemoteComponent LazyComponent destroy >>>`,
90
94
  { moduleName, basename, dom: renderDom.current },
91
95
  );
96
+ bridgeHook.lifecycle.beforeBridgeDestroy.emit({
97
+ moduleName,
98
+ dom: renderDom.current,
99
+ basename,
100
+ memoryRoute,
101
+ fallback,
102
+ ...resProps,
103
+ });
92
104
  providerInfoRef.current?.destroy({
93
105
  dom: renderDom.current,
94
106
  });