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

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,431 +1,6 @@
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
- }
1
+ import { B, a, c } from "./plugin-qjDRExZS.js";
428
2
  export {
429
- createBridgeComponent,
430
- createRemoteComponent
3
+ B as BridgeReactPlugin,
4
+ a as createBridgeComponent,
5
+ c as createRemoteComponent
431
6
  };