@module-federation/bridge-react 0.0.0-next-20250708033956 → 0.0.0-next-20250708104625

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/dist/data-fetch-server-middleware.d.ts +9 -0
  3. package/dist/data-fetch-server-middleware.es.js +1 -1
  4. package/dist/data-fetch-utils.cjs.js +4 -1253
  5. package/dist/data-fetch-utils.d.ts +10 -1
  6. package/dist/data-fetch-utils.es.js +11 -1260
  7. package/dist/index.cjs.js +7 -433
  8. package/dist/index.d.ts +11 -11
  9. package/dist/index.es.js +20 -447
  10. package/dist/lazy-load-component-plugin-CttAWi8N.js +500 -0
  11. package/dist/lazy-load-component-plugin-hOC-Yus_.mjs +501 -0
  12. package/dist/lazy-load-component-plugin.cjs.js +6 -0
  13. package/dist/lazy-load-component-plugin.d.ts +16 -0
  14. package/dist/lazy-load-component-plugin.es.js +6 -0
  15. package/dist/lazy-utils.d.ts +9 -0
  16. package/dist/lazy-utils.es.js +19 -19
  17. package/dist/plugin.d.ts +9 -0
  18. package/dist/prefetch-Cx6MrkdU.mjs +1330 -0
  19. package/dist/prefetch-cGDaDkgV.js +1329 -0
  20. package/dist/router-v5.d.ts +9 -0
  21. package/dist/router-v6.d.ts +9 -0
  22. package/dist/router.d.ts +9 -0
  23. package/dist/{utils-Bk8hGjjF.mjs → utils-BTYYwZcb.mjs} +31 -31
  24. package/dist/v18.d.ts +9 -0
  25. package/dist/v19.d.ts +9 -0
  26. package/package.json +11 -11
  27. package/src/index.ts +3 -1
  28. package/src/lazy/createLazyComponent.tsx +88 -74
  29. package/src/lazy/data-fetch/index.ts +1 -0
  30. package/src/lazy/data-fetch/prefetch.ts +1 -1
  31. package/src/lazy/data-fetch/runtime-plugin.ts +80 -81
  32. package/src/lazy/index.ts +6 -2
  33. package/src/plugins/lazy-load-component-plugin.ts +72 -0
  34. package/vite.config.ts +2 -2
  35. package/dist/data-fetch-runtime-plugin.cjs.js +0 -73
  36. package/dist/data-fetch-runtime-plugin.d.ts +0 -6
  37. package/dist/data-fetch-runtime-plugin.es.js +0 -74
  38. package/dist/inject-data-fetch-CAvi-gSf.js +0 -79
  39. package/dist/inject-data-fetch-errCdqBS.mjs +0 -80
  40. package/src/.eslintrc.js +0 -9
package/dist/index.es.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { c as createBaseBridgeComponent, E as ErrorBoundary } from "./bridge-base-BoshEggF.mjs";
2
2
  import ReactDOM from "react-dom";
3
- import React__default, { forwardRef, useRef, useState, useEffect, useContext, Suspense } from "react";
3
+ import React__default, { forwardRef, useRef, useState, useEffect, useContext } from "react";
4
4
  import { L as LoggerInstance, g as getRootDomDefaultClassName, p as pathJoin } from "./index-CqxytsLY.mjs";
5
5
  import * as ReactRouterDOM from "react-router-dom";
6
6
  import { federationRuntime } from "./plugin.es.js";
7
- import { default as default2 } from "./data-fetch-runtime-plugin.es.js";
8
- import { s as getDataFetchIdWithErrorMsgs, t as DATA_FETCH_ERROR_PREFIX, E as ERROR_TYPE, w as wrapDataFetchId, L as LOAD_REMOTE_ERROR_PREFIX, l as logger, n as DATA_FETCH_FUNCTION, u as getLoadedRemoteInfos, a as getDataFetchMapKey, g as getDataFetchInfo, m as fetchData$1, v as setDataFetchItemLoadedStatus, F as FS_HREF, b as getDataFetchItem, M as MF_DATA_FETCH_TYPE } from "./utils-Bk8hGjjF.mjs";
9
- import { C, y, B, H, z, A, G, x } from "./utils-Bk8hGjjF.mjs";
10
- import { callDataFetch, prefetch } from "./data-fetch-utils.es.js";
7
+ import { b, a, c, l } from "./lazy-load-component-plugin-hOC-Yus_.mjs";
8
+ import { C, b as b2, E, e, h, c as c2, d, r, s } from "./utils-BTYYwZcb.mjs";
9
+ import { callDataFetch } from "./data-fetch-utils.es.js";
10
+ import { p } from "./prefetch-Cx6MrkdU.mjs";
11
11
  function createReact16Or17Root(container) {
12
12
  return {
13
13
  render(children) {
@@ -38,7 +38,7 @@ function createBridgeComponent(bridgeInfo) {
38
38
  };
39
39
  return createBaseBridgeComponent(fullBridgeInfo);
40
40
  }
41
- function e() {
41
+ function e2() {
42
42
  const t = new PopStateEvent("popstate", { state: window.history.state });
43
43
  window.dispatchEvent(t);
44
44
  }
@@ -174,7 +174,7 @@ function withRouterData(WrappedComponent) {
174
174
  pathname: location.pathname
175
175
  }
176
176
  );
177
- e();
177
+ e2();
178
178
  }
179
179
  setPathname(location.pathname);
180
180
  }, [location]);
@@ -252,450 +252,23 @@ function createRemoteComponent(info) {
252
252
  );
253
253
  return createRemoteAppComponent(info);
254
254
  }
255
- function isPromise(obj) {
256
- return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
257
- }
258
- const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
259
- const transformError = (err) => {
260
- const errMsg = err instanceof Error ? err.message : err;
261
- const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
262
- const dataFetchMapKey = getDataFetchIdWithErrorMsgs(originalMsg);
263
- if (originalMsg.indexOf(DATA_FETCH_ERROR_PREFIX) === 0) {
264
- return {
265
- error: new Error(
266
- originalMsg.replace(DATA_FETCH_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")
267
- ),
268
- errorType: ERROR_TYPE.DATA_FETCH,
269
- dataFetchMapKey
270
- };
271
- }
272
- if (originalMsg.indexOf(LOAD_REMOTE_ERROR_PREFIX) === 0) {
273
- return {
274
- error: new Error(
275
- originalMsg.replace(LOAD_REMOTE_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")
276
- ),
277
- errorType: ERROR_TYPE.LOAD_REMOTE,
278
- dataFetchMapKey
279
- };
280
- }
281
- return {
282
- error: new Error(originalMsg.replace(wrapDataFetchId(dataFetchMapKey), "")),
283
- errorType: ERROR_TYPE.UNKNOWN,
284
- dataFetchMapKey
285
- };
286
- };
287
- const DefaultLoading = /* @__PURE__ */ React__default.createElement(React__default.Fragment, null);
288
- const DefaultErrorElement = (_data) => /* @__PURE__ */ React__default.createElement("div", null, "Error");
289
- function AwaitDataFetch({
290
- resolve,
291
- loading = DefaultLoading,
292
- errorElement = DefaultErrorElement,
293
- children,
294
- params,
295
- delayLoading
296
- }) {
297
- const dataRef = useRef();
298
- const data = dataRef.current || resolve;
299
- const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
300
- return /* @__PURE__ */ React__default.createElement(
301
- AwaitSuspense,
302
- {
303
- params,
304
- loading,
305
- errorElement,
306
- delayLoading,
307
- resolve: getData
308
- },
309
- children
310
- );
311
- }
312
- const DelayedLoading = ({
313
- delayLoading,
314
- children
315
- }) => {
316
- const [show, setShow] = useState(false);
317
- const timerSet = useRef(false);
318
- if (!delayLoading) {
319
- return children;
320
- }
321
- if (typeof window !== "undefined" && !show && !timerSet.current) {
322
- timerSet.current = true;
323
- setTimeout(() => {
324
- setShow(true);
325
- }, delayLoading);
326
- }
327
- return show ? children : null;
328
- };
329
- function AwaitSuspense({
330
- resolve,
331
- children,
332
- loading = DefaultLoading,
333
- errorElement = DefaultErrorElement,
334
- delayLoading
335
- }) {
336
- return /* @__PURE__ */ React__default.createElement(
337
- Suspense,
338
- {
339
- fallback: /* @__PURE__ */ React__default.createElement(DelayedLoading, { delayLoading }, loading)
340
- },
341
- /* @__PURE__ */ React__default.createElement(ResolveAwait, { resolve, errorElement }, children)
342
- );
343
- }
344
- function ResolveAwait({
345
- children,
346
- resolve,
347
- errorElement,
348
- params
349
- }) {
350
- const data = resolve();
351
- logger.debug("resolve data: ", data);
352
- if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
353
- const transformedError = transformError(data);
354
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, typeof errorElement === "function" ? /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, globalThis.FEDERATION_SSR && /* @__PURE__ */ React__default.createElement(
355
- "script",
356
- {
357
- suppressHydrationWarning: true,
358
- dangerouslySetInnerHTML: {
359
- __html: String.raw`
360
- globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || []
361
- globalThis['${DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
362
- }
363
- }
364
- ), errorElement(transformedError)) : errorElement);
365
- }
366
- const toRender = typeof children === "function" ? children(data) : children;
367
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, toRender);
368
- }
369
- const fetchData = (promise, ref) => {
370
- let data;
371
- let status = "pending";
372
- const suspender = promise.then((res) => {
373
- status = "success";
374
- data = res;
375
- ref.current = res;
376
- }).catch((e2) => {
377
- status = "success";
378
- console.warn(e2);
379
- data = AWAIT_ERROR_PREFIX + e2;
380
- });
381
- return () => {
382
- if (status === "pending") {
383
- throw suspender;
384
- }
385
- return data;
386
- };
387
- };
388
- function getTargetModuleInfo(id, instance) {
389
- if (!instance) {
390
- return;
391
- }
392
- const loadedRemoteInfo = getLoadedRemoteInfos(id, instance);
393
- if (!loadedRemoteInfo) {
394
- return;
395
- }
396
- const snapshot = loadedRemoteInfo.snapshot;
397
- if (!snapshot) {
398
- return;
399
- }
400
- const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
401
- if (!publicPath) {
402
- return;
403
- }
404
- const modules = "modules" in snapshot ? snapshot.modules : [];
405
- const targetModule = modules.find(
406
- (m) => m.modulePath === loadedRemoteInfo.expose
407
- );
408
- if (!targetModule) {
409
- return;
410
- }
411
- const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
412
- if (!remoteEntry) {
413
- return;
414
- }
415
- return {
416
- module: targetModule,
417
- publicPath,
418
- remoteEntry
419
- };
420
- }
421
- function collectSSRAssets(options) {
422
- const {
423
- id,
424
- injectLink = true,
425
- injectScript = false
426
- } = typeof options === "string" ? { id: options } : options;
427
- const links = [];
428
- const scripts = [];
429
- const instance = options.instance;
430
- if (!instance || !injectLink && !injectScript) {
431
- return [...scripts, ...links];
432
- }
433
- const moduleAndPublicPath = getTargetModuleInfo(id, instance);
434
- if (!moduleAndPublicPath) {
435
- return [...scripts, ...links];
436
- }
437
- const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
438
- if (injectLink) {
439
- [...targetModule.assets.css.sync, ...targetModule.assets.css.async].sort().forEach((file, index) => {
440
- links.push(
441
- /* @__PURE__ */ React__default.createElement(
442
- "link",
443
- {
444
- key: `${file.split(".")[0]}_${index}`,
445
- href: `${publicPath}${file}`,
446
- rel: "stylesheet",
447
- type: "text/css"
448
- }
449
- )
450
- );
451
- });
452
- }
453
- if (injectScript) {
454
- scripts.push(
455
- /* @__PURE__ */ React__default.createElement(
456
- "script",
457
- {
458
- async: true,
459
- key: remoteEntry.split(".")[0],
460
- src: `${publicPath}${remoteEntry}`,
461
- crossOrigin: "anonymous"
462
- }
463
- )
464
- );
465
- [...targetModule.assets.js.sync].sort().forEach((file, index) => {
466
- scripts.push(
467
- /* @__PURE__ */ React__default.createElement(
468
- "script",
469
- {
470
- key: `${file.split(".")[0]}_${index}`,
471
- async: true,
472
- src: `${publicPath}${file}`,
473
- crossOrigin: "anonymous"
474
- }
475
- )
476
- );
477
- });
478
- }
479
- return [...scripts, ...links];
480
- }
481
- function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
482
- var _a;
483
- if (noSSR || typeof window !== "undefined" && window.location.href !== window[FS_HREF]) {
484
- if (!(loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.version)) {
485
- throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} version is empty`);
486
- }
487
- const { snapshot } = loadedRemoteInfo;
488
- if (!snapshot) {
489
- throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} snapshot is empty`);
490
- }
491
- const dataFetchItem = getDataFetchItem(id);
492
- const isFetchServer = ((_a = dataFetchItem == null ? void 0 : dataFetchItem[0]) == null ? void 0 : _a[1]) === MF_DATA_FETCH_TYPE.FETCH_SERVER;
493
- if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
494
- throw new Error(
495
- `ssrPublicPath is required while fetching ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data in SSR project!`
496
- );
497
- }
498
- if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
499
- throw new Error(
500
- `ssrRemoteEntry is required while loading ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data loader in SSR project!`
501
- );
502
- }
503
- return {
504
- name: loadedRemoteInfo.name,
505
- version: loadedRemoteInfo.version,
506
- ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
507
- ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
508
- globalName: loadedRemoteInfo.entryGlobalName
509
- };
510
- }
511
- return void 0;
512
- }
513
- function createLazyComponent(options) {
514
- const { instance, cacheData } = options;
515
- if (!instance) {
516
- throw new Error("instance is required for createLazyComponent!");
517
- }
518
- let dataCache = null;
519
- const exportName = (options == null ? void 0 : options.export) || "default";
520
- const callLoader = async () => {
521
- logger.debug("callLoader start", Date.now());
522
- const m = await options.loader();
523
- logger.debug("callLoader end", Date.now());
524
- if (!m) {
525
- throw new Error("load remote failed");
526
- }
527
- return m;
528
- };
529
- const getData = async (noSSR) => {
530
- let loadedRemoteInfo;
531
- let moduleId;
532
- try {
533
- const m = await callLoader();
534
- moduleId = m && m[Symbol.for("mf_module_id")];
535
- if (!moduleId) {
536
- throw new Error("moduleId is empty");
537
- }
538
- loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
539
- if (!loadedRemoteInfo) {
540
- throw new Error(`can not find loaded remote('${moduleId}') info!`);
541
- }
542
- } catch (e2) {
543
- const errMsg = `${LOAD_REMOTE_ERROR_PREFIX}${e2}`;
544
- logger.debug(e2);
545
- throw new Error(errMsg);
546
- }
547
- let dataFetchMapKey;
548
- try {
549
- dataFetchMapKey = getDataFetchMapKey(
550
- getDataFetchInfo({
551
- name: loadedRemoteInfo.name,
552
- alias: loadedRemoteInfo.alias,
553
- id: moduleId,
554
- remoteSnapshot: loadedRemoteInfo.snapshot
555
- }),
556
- { name: instance.name, version: instance.options.version }
557
- );
558
- logger.debug("getData dataFetchMapKey: ", dataFetchMapKey);
559
- if (!dataFetchMapKey) {
560
- return;
561
- }
562
- const data = await fetchData$1(
563
- dataFetchMapKey,
564
- {
565
- ...options.dataFetchParams,
566
- isDowngrade: false
567
- },
568
- getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR)
569
- );
570
- setDataFetchItemLoadedStatus(dataFetchMapKey);
571
- logger.debug("get data res: \n", data);
572
- dataCache = data;
573
- return data;
574
- } catch (err) {
575
- const errMsg = `${DATA_FETCH_ERROR_PREFIX}${wrapDataFetchId(dataFetchMapKey)}${err}`;
576
- logger.debug(errMsg);
577
- throw new Error(errMsg);
578
- }
579
- };
580
- const LazyComponent = React__default.lazy(async () => {
581
- const m = await callLoader();
582
- const moduleId = m && m[Symbol.for("mf_module_id")];
583
- const loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
584
- loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.snapshot;
585
- const dataFetchMapKey = loadedRemoteInfo ? getDataFetchMapKey(
586
- getDataFetchInfo({
587
- name: loadedRemoteInfo.name,
588
- alias: loadedRemoteInfo.alias,
589
- id: moduleId,
590
- remoteSnapshot: loadedRemoteInfo.snapshot
591
- }),
592
- { name: instance.name, version: instance == null ? void 0 : instance.options.version }
593
- ) : void 0;
594
- logger.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
595
- const assets = collectSSRAssets({
596
- id: moduleId,
597
- instance
598
- });
599
- const Com = m[exportName];
600
- if (exportName in m && typeof Com === "function") {
601
- return {
602
- default: (props) => /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ React__default.createElement(
603
- "script",
604
- {
605
- suppressHydrationWarning: true,
606
- dangerouslySetInnerHTML: {
607
- __html: String.raw`
608
- globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || [];
609
- globalThis['${DATA_FETCH_FUNCTION}'].push(['${dataFetchMapKey}',${JSON.stringify(props.mfData)}]);
610
- `
611
- }
612
- }
613
- ), globalThis.FEDERATION_SSR && assets, /* @__PURE__ */ React__default.createElement(Com, { ...props }))
614
- };
615
- } else {
616
- throw Error(
617
- `Make sure that ${moduleId} has the correct export when export is ${String(
618
- exportName
619
- )}`
620
- );
621
- }
622
- });
623
- return (props) => {
624
- const { key, ...args } = props;
625
- if (cacheData && dataCache) {
626
- return /* @__PURE__ */ React__default.createElement(LazyComponent, { ...args, mfData: dataCache });
627
- }
628
- if (!options.noSSR) {
629
- return /* @__PURE__ */ React__default.createElement(
630
- AwaitDataFetch,
631
- {
632
- resolve: getData(options.noSSR),
633
- loading: options.loading,
634
- delayLoading: options.delayLoading,
635
- errorElement: options.fallback
636
- },
637
- (data) => /* @__PURE__ */ React__default.createElement(LazyComponent, { ...args, mfData: data })
638
- );
639
- } else {
640
- const [data, setData] = useState(null);
641
- const [loading, setLoading] = useState(true);
642
- const [error, setError] = useState(null);
643
- useEffect(() => {
644
- let isMounted = true;
645
- const fetchDataAsync = async () => {
646
- try {
647
- setLoading(true);
648
- const result = await getData(options.noSSR);
649
- if (isMounted) {
650
- setData(result);
651
- }
652
- } catch (e2) {
653
- if (isMounted) {
654
- setError(transformError(e2));
655
- }
656
- } finally {
657
- if (isMounted) {
658
- setLoading(false);
659
- }
660
- }
661
- };
662
- fetchDataAsync();
663
- return () => {
664
- isMounted = false;
665
- };
666
- }, []);
667
- if (loading) {
668
- return /* @__PURE__ */ React__default.createElement(DelayedLoading, { delayLoading: options.delayLoading }, options.loading);
669
- }
670
- if (error) {
671
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, typeof options.fallback === "function" ? options.fallback(error) : options.fallback);
672
- }
673
- return /* @__PURE__ */ React__default.createElement(LazyComponent, { ...args, mfData: data });
674
- }
675
- };
676
- }
677
- function wrapNoSSR(createLazyComponentFn) {
678
- return (options) => {
679
- return createLazyComponentFn({ ...options, noSSR: true });
680
- };
681
- }
682
255
  export {
683
256
  C as CacheSize,
684
- y as CacheTime,
685
- ERROR_TYPE,
686
- default2 as autoFetchDataPlugin,
687
- B as cache,
257
+ b2 as CacheTime,
258
+ E as ERROR_TYPE,
259
+ b as autoFetchDataPlugin,
260
+ e as cache,
688
261
  callDataFetch,
689
- H as clearStore,
690
- collectSSRAssets,
691
- z as configureCache,
262
+ h as clearStore,
263
+ a as collectSSRAssets,
264
+ c2 as configureCache,
692
265
  createBridgeComponent,
693
- createLazyComponent,
266
+ c as createLazyComponent,
694
267
  createRemoteAppComponent,
695
268
  createRemoteComponent,
696
- A as generateKey,
697
- prefetch,
698
- G as revalidateTag,
699
- x as setSSREnv,
700
- wrapNoSSR
269
+ d as generateKey,
270
+ l as lazyLoadComponentPlugin,
271
+ p as prefetch,
272
+ r as revalidateTag,
273
+ s as setSSREnv
701
274
  };