@module-federation/bridge-react 0.0.0-next-20241118092546 → 0.0.0-next-20241118100312

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.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
  };
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const React = require("react");
4
- const ReactRouterDom$1 = require("react-router-dom/dist/index.js");
5
- const context = require("./context-BcJ-YlNr.cjs");
6
4
  const ReactRouterDom = require("react-router-dom/dist/index.js");
5
+ const context = require("./context-BcJ-YlNr.cjs");
7
6
  function _interopNamespaceDefault(e) {
8
7
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
9
8
  if (e) {
@@ -20,7 +19,7 @@ function _interopNamespaceDefault(e) {
20
19
  n.default = e;
21
20
  return Object.freeze(n);
22
21
  }
23
- const ReactRouterDom__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDom$1);
22
+ const ReactRouterDom__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDom);
24
23
  function WraperRouter(props) {
25
24
  const { basename, ...propsRes } = props;
26
25
  const routerContextProps = React.useContext(context.RouterContext) || {};
@@ -6,6 +6,6 @@ export declare function BrowserRouter(props: Parameters<typeof ReactRouterDom.Br
6
6
  export declare function RouterProvider(props: Parameters<typeof ReactRouterDom.RouterProvider>[0]): default_2.JSX.Element;
7
7
 
8
8
 
9
- export * from "react-router-dom/";
9
+ export * from "react-router-dom/dist/index.js";
10
10
 
11
11
  export { }
@@ -1,7 +1,7 @@
1
1
  import React__default, { useContext } from "react";
2
- import * as ReactRouterDom$1 from "react-router-dom/dist/index.js";
3
- import { R as RouterContext, L as LoggerInstance } from "./context-CUbFnlO5.js";
2
+ import * as ReactRouterDom from "react-router-dom/dist/index.js";
4
3
  export * from "react-router-dom/dist/index.js";
4
+ import { R as RouterContext, L as LoggerInstance } from "./context-CUbFnlO5.js";
5
5
  function WraperRouter(props) {
6
6
  const { basename, ...propsRes } = props;
7
7
  const routerContextProps = useContext(RouterContext) || {};
@@ -12,7 +12,7 @@ function WraperRouter(props) {
12
12
  });
13
13
  if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
14
14
  return /* @__PURE__ */ React__default.createElement(
15
- ReactRouterDom$1.MemoryRouter,
15
+ ReactRouterDom.MemoryRouter,
16
16
  {
17
17
  ...props,
18
18
  initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
@@ -20,7 +20,7 @@ function WraperRouter(props) {
20
20
  );
21
21
  }
22
22
  return /* @__PURE__ */ React__default.createElement(
23
- ReactRouterDom$1.BrowserRouter,
23
+ ReactRouterDom.BrowserRouter,
24
24
  {
25
25
  ...propsRes,
26
26
  basename: (routerContextProps == null ? void 0 : routerContextProps.basename) || basename
@@ -37,9 +37,9 @@ function WraperRouterProvider(props) {
37
37
  WraperRouterProviderProps: props,
38
38
  router
39
39
  });
40
- const RouterProvider = ReactRouterDom$1["RouterProvider"];
41
- const createMemoryRouter = ReactRouterDom$1["createMemoryRouter"];
42
- const createBrowserRouter = ReactRouterDom$1["createBrowserRouter"];
40
+ const RouterProvider = ReactRouterDom["RouterProvider"];
41
+ const createMemoryRouter = ReactRouterDom["createMemoryRouter"];
42
+ const createBrowserRouter = ReactRouterDom["createBrowserRouter"];
43
43
  if (routerContextProps.memoryRoute) {
44
44
  const MemeoryRouterInstance = createMemoryRouter(routers, {
45
45
  initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react",
3
- "version": "0.0.0-next-20241118092546",
3
+ "version": "0.0.0-next-20241118100312",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -26,11 +26,6 @@
26
26
  "import": "./dist/router.es.js",
27
27
  "require": "./dist/router.cjs.js"
28
28
  },
29
- "./plugin": {
30
- "types": "./dist/plugin.d.ts",
31
- "import": "./dist/plugin.es.js",
32
- "require": "./dist/plugin.es.js"
33
- },
34
29
  "./router-v5": {
35
30
  "types": "./dist/router-v5.d.ts",
36
31
  "import": "./dist/router-v5.es.js",
@@ -46,8 +41,9 @@
46
41
  "dependencies": {
47
42
  "@loadable/component": "^5.16.4",
48
43
  "react-error-boundary": "^4.0.13",
49
- "@module-federation/bridge-shared": "0.0.0-next-20241118092546",
50
- "@module-federation/sdk": "0.0.0-next-20241118092546"
44
+ "@module-federation/bridge-shared": "0.0.0-next-20241118100312",
45
+ "@module-federation/sdk": "0.0.0-next-20241118100312",
46
+ "@module-federation/runtime": "0.0.0-next-20241118100312"
51
47
  },
52
48
  "peerDependencies": {
53
49
  "react": ">=16.9.0",
@@ -67,8 +63,7 @@
67
63
  "react-router-dom": "6.22.3",
68
64
  "typescript": "^5.2.2",
69
65
  "vite": "^5.2.14",
70
- "vite-plugin-dts": "^3.9.1",
71
- "@module-federation/runtime": "0.0.0-next-20241118092546"
66
+ "vite-plugin-dts": "^3.9.1"
72
67
  },
73
68
  "scripts": {
74
69
  "dev": "vite",
package/src/create.tsx CHANGED
@@ -1,5 +1,4 @@
1
1
  import React, { forwardRef } from 'react';
2
- import type { FederationHost } from '@module-federation/runtime';
3
2
  import {
4
3
  ErrorBoundary,
5
4
  ErrorBoundaryPropsWithComponent,
@@ -19,17 +18,12 @@ interface RemoteModule {
19
18
  };
20
19
  }
21
20
 
22
- type LazyRemoteComponentInfo<T, E extends keyof T> = {
21
+ function createLazyRemoteComponent<T, E extends keyof T>(info: {
23
22
  loader: () => Promise<T>;
24
23
  loading: React.ReactNode;
25
24
  fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
26
25
  export?: E;
27
- instance?: FederationHost;
28
- };
29
-
30
- function createLazyRemoteComponent<T, E extends keyof T>(
31
- info: LazyRemoteComponentInfo<T, E>,
32
- ) {
26
+ }) {
33
27
  const exportName = info?.export || 'default';
34
28
  return React.lazy(async () => {
35
29
  LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
@@ -64,7 +58,6 @@ function createLazyRemoteComponent<T, E extends keyof T>(
64
58
  exportName={info.export || 'default'}
65
59
  fallback={info.fallback}
66
60
  ref={ref}
67
- instance={info.instance}
68
61
  {...props}
69
62
  />
70
63
  );
@@ -90,9 +83,12 @@ function createLazyRemoteComponent<T, E extends keyof T>(
90
83
  });
91
84
  }
92
85
 
93
- export function createRemoteComponent<T, E extends keyof T>(
94
- info: LazyRemoteComponentInfo<T, E>,
95
- ) {
86
+ export function createRemoteComponent<T, E extends keyof T>(info: {
87
+ loader: () => Promise<T>;
88
+ loading: React.ReactNode;
89
+ fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
90
+ export?: E;
91
+ }) {
96
92
  type ExportType = T[E] extends (...args: any) => any
97
93
  ? ReturnType<T[E]>
98
94
  : never;
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  export { createRemoteComponent } from './create';
2
2
  export { createBridgeComponent } from './provider';
3
- export { default as BridgeReactPlugin } from './plugin';
4
-
5
3
  export type {
6
4
  ProviderParams,
7
5
  RenderFnParams,
package/src/provider.tsx CHANGED
@@ -9,7 +9,7 @@ import type {
9
9
  import { ErrorBoundary } from 'react-error-boundary';
10
10
  import { RouterContext } from './context';
11
11
  import { LoggerInstance, atLeastReact18 } from './utils';
12
- import type { FederationHost } from '@module-federation/runtime';
12
+ import { getInstance } from '@module-federation/runtime';
13
13
 
14
14
  type RenderParams = RenderFnParams & {
15
15
  [key: string]: unknown;
@@ -26,17 +26,13 @@ type ProviderFnParams<T> = {
26
26
  App: React.ReactElement,
27
27
  id?: HTMLElement | string,
28
28
  ) => RootType | Promise<RootType>;
29
- instance?: FederationHost;
30
29
  };
31
30
 
32
31
  export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
33
32
  return () => {
34
33
  const rootMap = new Map<any, RootType>();
35
- const { instance } = bridgeInfo;
36
- LoggerInstance.log(
37
- `createBridgeComponent instance from props >>>`,
38
- instance,
39
- );
34
+ const instance = getInstance();
35
+ LoggerInstance.log(`createBridgeComponent remote instance`, instance);
40
36
 
41
37
  const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
42
38
  const { appInfo, propsInfo, ...restProps } = info;
@@ -99,6 +95,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
99
95
  const renderFn = bridgeInfo?.render || ReactDOM.render;
100
96
  renderFn?.(rootComponentWithErrorBoundary, info.dom);
101
97
  }
98
+
102
99
  instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
103
100
  },
104
101
 
@@ -106,6 +103,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
106
103
  LoggerInstance.log(`createBridgeComponent destroy Info`, {
107
104
  dom: info.dom,
108
105
  });
106
+
109
107
  instance?.bridgeHook?.lifecycle?.beforeBridgeDestroy?.emit(info);
110
108
 
111
109
  // call destroy function
@@ -117,9 +115,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
117
115
  ReactDOM.unmountComponentAtNode(info.dom);
118
116
  }
119
117
 
120
- (instance as any)?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit(
121
- info,
122
- );
118
+ instance?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit(info);
123
119
  },
124
120
  rawComponent: bridgeInfo.rootComponent,
125
121
  __BRIDGE_FN__: (_args: T) => {},