@module-federation/bridge-react 0.16.0 → 0.17.1
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/CHANGELOG.md +39 -0
- package/__tests__/bridge.spec.tsx +10 -10
- package/__tests__/createLazyComponent.spec.tsx +209 -0
- package/__tests__/prefetch.spec.ts +156 -0
- package/__tests__/router.spec.tsx +3 -3
- package/__tests__/setupTests.ts +8 -0
- package/dist/{bridge-base-P6pEjY1q.js → bridge-base-BoshEggF.mjs} +1 -1
- package/dist/{bridge-base-BBH982Tz.cjs → bridge-base-UGCwcMnG.js} +1 -1
- package/dist/data-fetch-server-middleware.cjs.js +163 -0
- package/dist/data-fetch-server-middleware.d.ts +15 -0
- package/dist/data-fetch-server-middleware.es.js +164 -0
- package/dist/data-fetch-utils.cjs.js +24 -0
- package/dist/data-fetch-utils.d.ts +81 -0
- package/dist/data-fetch-utils.es.js +26 -0
- package/dist/index-C0fDZB5b.js +45 -0
- package/dist/index-CqxytsLY.mjs +46 -0
- package/dist/index.cjs.js +35 -9
- package/dist/index.d.ts +141 -1
- package/dist/index.es.js +39 -13
- package/dist/index.esm-BCeUd-x9.mjs +418 -0
- package/dist/index.esm-j_1sIRzg.js +417 -0
- package/dist/lazy-load-component-plugin-B80Ud11k.js +521 -0
- package/dist/lazy-load-component-plugin-_UbR2mWQ.mjs +522 -0
- package/dist/lazy-load-component-plugin.cjs.js +6 -0
- package/dist/lazy-load-component-plugin.d.ts +16 -0
- package/dist/lazy-load-component-plugin.es.js +6 -0
- package/dist/lazy-utils.cjs.js +24 -0
- package/dist/lazy-utils.d.ts +149 -0
- package/dist/lazy-utils.es.js +24 -0
- package/dist/plugin.d.ts +13 -4
- package/dist/prefetch-BaKIdUwP.js +1338 -0
- package/dist/prefetch-YpJYjpWC.mjs +1339 -0
- package/dist/router-v5.cjs.js +1 -1
- package/dist/router-v5.d.ts +9 -0
- package/dist/router-v5.es.js +1 -1
- package/dist/router-v6.cjs.js +1 -1
- package/dist/router-v6.d.ts +9 -0
- package/dist/router-v6.es.js +1 -1
- package/dist/router.cjs.js +1 -1
- package/dist/router.d.ts +9 -0
- package/dist/router.es.js +1 -1
- package/dist/utils-C4oPJV34.mjs +2016 -0
- package/dist/utils-iEVlDmyk.js +2015 -0
- package/dist/v18.cjs.js +1 -1
- package/dist/v18.d.ts +9 -0
- package/dist/v18.es.js +1 -1
- package/dist/v19.cjs.js +1 -1
- package/dist/v19.d.ts +9 -0
- package/dist/v19.es.js +1 -1
- package/jest.config.ts +21 -0
- package/package.json +48 -6
- package/project.json +4 -8
- package/src/index.ts +32 -1
- package/src/lazy/AwaitDataFetch.tsx +215 -0
- package/src/lazy/constant.ts +30 -0
- package/src/lazy/createLazyComponent.tsx +411 -0
- package/src/lazy/data-fetch/cache.ts +291 -0
- package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
- package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
- package/src/lazy/data-fetch/index.ts +16 -0
- package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
- package/src/lazy/data-fetch/prefetch.ts +106 -0
- package/src/lazy/data-fetch/runtime-plugin.ts +115 -0
- package/src/lazy/index.ts +35 -0
- package/src/lazy/logger.ts +6 -0
- package/src/lazy/types.ts +75 -0
- package/src/lazy/utils.ts +372 -0
- package/src/lazy/wrapNoSSR.tsx +10 -0
- package/src/plugins/lazy-load-component-plugin.spec.ts +21 -0
- package/src/plugins/lazy-load-component-plugin.ts +57 -0
- package/src/provider/plugin.ts +4 -4
- package/src/remote/component.tsx +3 -3
- package/src/remote/create.tsx +18 -5
- package/tsconfig.json +1 -1
- package/tsconfig.spec.json +26 -0
- package/vite.config.ts +13 -0
- package/vitest.config.ts +6 -1
- package/dist/index-Cv3p6r66.cjs +0 -235
- package/dist/index-D4yt7Udv.js +0 -236
- package/src/.eslintrc.js +0 -9
package/dist/v18.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const client = require("react-dom/client");
|
|
4
|
-
const bridgeBase = require("./bridge-base-
|
|
4
|
+
const bridgeBase = require("./bridge-base-UGCwcMnG.js");
|
|
5
5
|
function createReact18Root(container, options) {
|
|
6
6
|
return client.createRoot(container, options);
|
|
7
7
|
}
|
package/dist/v18.d.ts
CHANGED
|
@@ -103,3 +103,12 @@ declare interface Root_2 {
|
|
|
103
103
|
export declare type RootType = HTMLElement | Root_2;
|
|
104
104
|
|
|
105
105
|
export { }
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
declare module '@module-federation/runtime-core' {
|
|
109
|
+
interface ModuleFederation {
|
|
110
|
+
createLazyComponent<T, E extends keyof T>(options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>): ReturnType<typeof createLazyComponent<T, E>>;
|
|
111
|
+
prefetch(options: Omit<PrefetchOptions, 'instance'>): ReturnType<typeof prefetch>;
|
|
112
|
+
collectSSRAssets(options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>): ReturnType<typeof collectSSRAssets>;
|
|
113
|
+
}
|
|
114
|
+
}
|
package/dist/v18.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRoot } from "react-dom/client";
|
|
2
|
-
import { c as createBaseBridgeComponent } from "./bridge-base-
|
|
2
|
+
import { c as createBaseBridgeComponent } from "./bridge-base-BoshEggF.mjs";
|
|
3
3
|
function createReact18Root(container, options) {
|
|
4
4
|
return createRoot(container, options);
|
|
5
5
|
}
|
package/dist/v19.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const client = require("react-dom/client");
|
|
4
|
-
const bridgeBase = require("./bridge-base-
|
|
4
|
+
const bridgeBase = require("./bridge-base-UGCwcMnG.js");
|
|
5
5
|
function createReact19Root(container, options) {
|
|
6
6
|
return client.createRoot(container, options);
|
|
7
7
|
}
|
package/dist/v19.d.ts
CHANGED
|
@@ -104,3 +104,12 @@ declare interface Root_2 {
|
|
|
104
104
|
export declare type RootType = HTMLElement | Root_2;
|
|
105
105
|
|
|
106
106
|
export { }
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
declare module '@module-federation/runtime-core' {
|
|
110
|
+
interface ModuleFederation {
|
|
111
|
+
createLazyComponent<T, E extends keyof T>(options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>): ReturnType<typeof createLazyComponent<T, E>>;
|
|
112
|
+
prefetch(options: Omit<PrefetchOptions, 'instance'>): ReturnType<typeof prefetch>;
|
|
113
|
+
collectSSRAssets(options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>): ReturnType<typeof collectSSRAssets>;
|
|
114
|
+
}
|
|
115
|
+
}
|
package/dist/v19.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRoot } from "react-dom/client";
|
|
2
|
-
import { c as createBaseBridgeComponent } from "./bridge-base-
|
|
2
|
+
import { c as createBaseBridgeComponent } from "./bridge-base-BoshEggF.mjs";
|
|
3
3
|
function createReact19Root(container, options) {
|
|
4
4
|
return createRoot(container, options);
|
|
5
5
|
}
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
displayName: 'bridge-react',
|
|
3
|
+
preset: '../../../jest.preset.js',
|
|
4
|
+
transform: {
|
|
5
|
+
'^.+\\.[tj]sx?$': [
|
|
6
|
+
'ts-jest',
|
|
7
|
+
{
|
|
8
|
+
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
9
|
+
isolatedModules: true,
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
},
|
|
13
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
14
|
+
coverageDirectory: '../../../coverage/packages/bridge/bridge-react',
|
|
15
|
+
testEnvironment: 'jsdom',
|
|
16
|
+
setupFilesAfterEnv: ['<rootDir>/__tests__/setupTests.ts'],
|
|
17
|
+
testMatch: [
|
|
18
|
+
'<rootDir>/__tests__/**/*.spec.ts',
|
|
19
|
+
'<rootDir>/__tests__/**/*.spec.tsx',
|
|
20
|
+
],
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/bridge-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.1",
|
|
4
|
+
"sideEffects": false,
|
|
4
5
|
"publishConfig": {
|
|
5
6
|
"access": "public"
|
|
6
7
|
},
|
|
@@ -11,7 +12,6 @@
|
|
|
11
12
|
"url": "https://github.com/module-federation/core",
|
|
12
13
|
"directory": "packages/bridge-react"
|
|
13
14
|
},
|
|
14
|
-
"type": "module",
|
|
15
15
|
"main": "./dist/index.cjs.js",
|
|
16
16
|
"module": "./dist/index.es.js",
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
@@ -51,12 +51,52 @@
|
|
|
51
51
|
"import": "./dist/router-v6.es.js",
|
|
52
52
|
"require": "./dist/router-v6.cjs.js"
|
|
53
53
|
},
|
|
54
|
+
"./lazy-utils": {
|
|
55
|
+
"types": "./dist/lazy-utils.d.ts",
|
|
56
|
+
"import": "./dist/lazy-utils.es.js",
|
|
57
|
+
"require": "./dist/lazy-utils.cjs.js"
|
|
58
|
+
},
|
|
59
|
+
"./data-fetch-utils": {
|
|
60
|
+
"types": "./dist/data-fetch-utils.d.ts",
|
|
61
|
+
"import": "./dist/data-fetch-utils.es.js",
|
|
62
|
+
"require": "./dist/data-fetch-utils.cjs.js"
|
|
63
|
+
},
|
|
64
|
+
"./data-fetch-server-middleware": {
|
|
65
|
+
"types": "./dist/data-fetch-server-middleware.d.ts",
|
|
66
|
+
"import": "./dist/data-fetch-server-middleware.es.js",
|
|
67
|
+
"require": "./dist/data-fetch-server-middleware.cjs.js"
|
|
68
|
+
},
|
|
69
|
+
"./lazy-load-component-plugin": {
|
|
70
|
+
"types": "./dist/lazy-load-component-plugin.d.ts",
|
|
71
|
+
"import": "./dist/lazy-load-component-plugin.es.js",
|
|
72
|
+
"require": "./dist/lazy-load-component-plugin.cjs.js"
|
|
73
|
+
},
|
|
54
74
|
"./*": "./*"
|
|
55
75
|
},
|
|
76
|
+
"typesVersions": {
|
|
77
|
+
"*": {
|
|
78
|
+
".": [
|
|
79
|
+
"./dist/types/index.d.ts"
|
|
80
|
+
],
|
|
81
|
+
"lazy-load-component-plugin": [
|
|
82
|
+
"./dist/lazy-load-component-plugin.d.ts"
|
|
83
|
+
],
|
|
84
|
+
"lazy-utils": [
|
|
85
|
+
"./dist/lazy-utils.d.ts"
|
|
86
|
+
],
|
|
87
|
+
"data-fetch-utils": [
|
|
88
|
+
"./dist/data-fetch-utils.d.ts"
|
|
89
|
+
],
|
|
90
|
+
"data-fetch-server-middleware": [
|
|
91
|
+
"./dist/data-fetch-server-middleware.d.ts"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
},
|
|
56
95
|
"dependencies": {
|
|
57
96
|
"react-error-boundary": "^4.1.2",
|
|
58
|
-
"
|
|
59
|
-
"@module-federation/
|
|
97
|
+
"lru-cache": "^10.4.3",
|
|
98
|
+
"@module-federation/bridge-shared": "0.17.1",
|
|
99
|
+
"@module-federation/sdk": "0.17.1"
|
|
60
100
|
},
|
|
61
101
|
"peerDependencies": {
|
|
62
102
|
"react": ">=16.9.0",
|
|
@@ -65,19 +105,21 @@
|
|
|
65
105
|
},
|
|
66
106
|
"devDependencies": {
|
|
67
107
|
"@testing-library/react": "15.0.7",
|
|
108
|
+
"@testing-library/jest-dom": "6.6.3",
|
|
68
109
|
"@types/react": "18.2.79",
|
|
69
110
|
"@types/react-dom": "18.3.0",
|
|
70
111
|
"@vitejs/plugin-react": "^4.3.3",
|
|
71
112
|
"@vitejs/plugin-vue": "^5.0.4",
|
|
72
113
|
"@vitejs/plugin-vue-jsx": "^4.0.0",
|
|
73
|
-
"jsdom": "^24.1.0",
|
|
74
114
|
"react": "18.3.1",
|
|
75
115
|
"react-dom": "18.3.1",
|
|
76
116
|
"react-router-dom": "6.22.3",
|
|
77
117
|
"typescript": "^5.2.2",
|
|
78
118
|
"vite": "^5.4.18",
|
|
79
119
|
"vite-plugin-dts": "^4.3.0",
|
|
80
|
-
"
|
|
120
|
+
"hono": "3.12.12",
|
|
121
|
+
"@module-federation/runtime": "0.17.1",
|
|
122
|
+
"@module-federation/runtime-core": "0.17.1"
|
|
81
123
|
},
|
|
82
124
|
"scripts": {
|
|
83
125
|
"dev": "vite",
|
package/project.json
CHANGED
|
@@ -12,15 +12,11 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"test": {
|
|
15
|
-
"executor": "nx:
|
|
15
|
+
"executor": "@nx/jest:jest",
|
|
16
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
16
17
|
"options": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
{
|
|
20
|
-
"command": "vitest run -c packages/bridge/bridge-react/vitest.config.ts",
|
|
21
|
-
"forwardAllArgs": false
|
|
22
|
-
}
|
|
23
|
-
]
|
|
18
|
+
"jestConfig": "packages/bridge/bridge-react/jest.config.ts",
|
|
19
|
+
"passWithNoTests": true
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
22
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,30 @@
|
|
|
3
3
|
* This file provides support for React 16 and 17 versions, using the traditional ReactDOM.render API
|
|
4
4
|
*/
|
|
5
5
|
export { createBridgeComponent } from './provider/versions/legacy';
|
|
6
|
-
export {
|
|
6
|
+
export {
|
|
7
|
+
createRemoteComponent,
|
|
8
|
+
createRemoteAppComponent,
|
|
9
|
+
} from './remote/create';
|
|
10
|
+
export {
|
|
11
|
+
ERROR_TYPE,
|
|
12
|
+
createLazyComponent,
|
|
13
|
+
collectSSRAssets,
|
|
14
|
+
// wrapNoSSR,
|
|
15
|
+
callDataFetch,
|
|
16
|
+
setSSREnv,
|
|
17
|
+
autoFetchDataPlugin,
|
|
18
|
+
CacheSize,
|
|
19
|
+
CacheTime,
|
|
20
|
+
configureCache,
|
|
21
|
+
generateKey,
|
|
22
|
+
cache,
|
|
23
|
+
revalidateTag,
|
|
24
|
+
clearStore,
|
|
25
|
+
prefetch,
|
|
26
|
+
} from './lazy';
|
|
27
|
+
|
|
28
|
+
export { lazyLoadComponentPlugin } from './plugins/lazy-load-component-plugin';
|
|
29
|
+
|
|
7
30
|
export type { CreateRootOptions, Root } from './provider/versions/legacy';
|
|
8
31
|
export type {
|
|
9
32
|
ProviderParams,
|
|
@@ -12,3 +35,11 @@ export type {
|
|
|
12
35
|
DestroyParams,
|
|
13
36
|
RenderParams,
|
|
14
37
|
} from './types';
|
|
38
|
+
export type {
|
|
39
|
+
DataFetchParams,
|
|
40
|
+
NoSSRRemoteInfo,
|
|
41
|
+
CollectSSRAssetsOptions,
|
|
42
|
+
CreateLazyComponentOptions,
|
|
43
|
+
CacheStatus,
|
|
44
|
+
CacheStatsInfo,
|
|
45
|
+
} from './lazy';
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
MutableRefObject,
|
|
3
|
+
ReactNode,
|
|
4
|
+
Suspense,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import logger from './logger';
|
|
9
|
+
import {
|
|
10
|
+
DATA_FETCH_ERROR_PREFIX,
|
|
11
|
+
LOAD_REMOTE_ERROR_PREFIX,
|
|
12
|
+
ERROR_TYPE,
|
|
13
|
+
DATA_FETCH_FUNCTION,
|
|
14
|
+
} from './constant';
|
|
15
|
+
|
|
16
|
+
import { getDataFetchIdWithErrorMsgs, wrapDataFetchId } from './utils';
|
|
17
|
+
import type { DataFetchParams } from './types';
|
|
18
|
+
|
|
19
|
+
function isPromise<T>(obj: any): obj is PromiseLike<T> {
|
|
20
|
+
return (
|
|
21
|
+
!!obj &&
|
|
22
|
+
(typeof obj === 'object' || typeof obj === 'function') &&
|
|
23
|
+
typeof obj.then === 'function'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
const AWAIT_ERROR_PREFIX =
|
|
27
|
+
'<Await /> caught the following error during render: ';
|
|
28
|
+
|
|
29
|
+
export type ErrorInfo = {
|
|
30
|
+
error: Error;
|
|
31
|
+
errorType: number;
|
|
32
|
+
dataFetchMapKey?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const transformError = (err: string | Error): ErrorInfo => {
|
|
36
|
+
const errMsg = err instanceof Error ? err.message : err;
|
|
37
|
+
const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, '');
|
|
38
|
+
const dataFetchMapKey = getDataFetchIdWithErrorMsgs(originalMsg);
|
|
39
|
+
if (originalMsg.indexOf(DATA_FETCH_ERROR_PREFIX) === 0) {
|
|
40
|
+
return {
|
|
41
|
+
error: new Error(
|
|
42
|
+
originalMsg
|
|
43
|
+
.replace(DATA_FETCH_ERROR_PREFIX, '')
|
|
44
|
+
.replace(wrapDataFetchId(dataFetchMapKey), ''),
|
|
45
|
+
),
|
|
46
|
+
errorType: ERROR_TYPE.DATA_FETCH,
|
|
47
|
+
dataFetchMapKey,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (originalMsg.indexOf(LOAD_REMOTE_ERROR_PREFIX) === 0) {
|
|
51
|
+
return {
|
|
52
|
+
error: new Error(
|
|
53
|
+
originalMsg
|
|
54
|
+
.replace(LOAD_REMOTE_ERROR_PREFIX, '')
|
|
55
|
+
.replace(wrapDataFetchId(dataFetchMapKey), ''),
|
|
56
|
+
),
|
|
57
|
+
errorType: ERROR_TYPE.LOAD_REMOTE,
|
|
58
|
+
dataFetchMapKey,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
error: new Error(originalMsg.replace(wrapDataFetchId(dataFetchMapKey), '')),
|
|
64
|
+
errorType: ERROR_TYPE.UNKNOWN,
|
|
65
|
+
dataFetchMapKey,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export interface AwaitProps<T> {
|
|
70
|
+
resolve: T | Promise<T>;
|
|
71
|
+
loading?: ReactNode;
|
|
72
|
+
delayLoading?: number;
|
|
73
|
+
errorElement?: ReactNode | ((errorInfo: ErrorInfo) => ReactNode);
|
|
74
|
+
children: (data: T) => ReactNode;
|
|
75
|
+
params?: DataFetchParams;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface AwaitErrorHandlerProps<T = any>
|
|
79
|
+
extends Omit<AwaitProps<T>, 'resolve'> {
|
|
80
|
+
resolve: () => T | string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const DefaultLoading = <></>;
|
|
84
|
+
const DefaultErrorElement = (_data: any) => <div>Error</div>;
|
|
85
|
+
|
|
86
|
+
export function AwaitDataFetch<T>({
|
|
87
|
+
resolve,
|
|
88
|
+
loading = DefaultLoading,
|
|
89
|
+
errorElement = DefaultErrorElement,
|
|
90
|
+
children,
|
|
91
|
+
params,
|
|
92
|
+
delayLoading,
|
|
93
|
+
}: AwaitProps<T>) {
|
|
94
|
+
const dataRef = useRef<T>();
|
|
95
|
+
const data = dataRef.current || resolve;
|
|
96
|
+
const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<AwaitSuspense
|
|
100
|
+
params={params}
|
|
101
|
+
loading={loading}
|
|
102
|
+
errorElement={errorElement}
|
|
103
|
+
delayLoading={delayLoading}
|
|
104
|
+
// @ts-expect-error
|
|
105
|
+
resolve={getData}
|
|
106
|
+
>
|
|
107
|
+
{children}
|
|
108
|
+
</AwaitSuspense>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const DelayedLoading = ({
|
|
113
|
+
delayLoading,
|
|
114
|
+
children,
|
|
115
|
+
}: {
|
|
116
|
+
delayLoading?: number;
|
|
117
|
+
children: ReactNode;
|
|
118
|
+
}) => {
|
|
119
|
+
const [show, setShow] = useState(false);
|
|
120
|
+
const timerSet = useRef(false);
|
|
121
|
+
|
|
122
|
+
if (!delayLoading) {
|
|
123
|
+
return children;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (typeof window !== 'undefined' && !show && !timerSet.current) {
|
|
127
|
+
timerSet.current = true;
|
|
128
|
+
setTimeout(() => {
|
|
129
|
+
setShow(true);
|
|
130
|
+
}, delayLoading);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return show ? children : null;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
function AwaitSuspense<T>({
|
|
137
|
+
resolve,
|
|
138
|
+
children,
|
|
139
|
+
loading = DefaultLoading,
|
|
140
|
+
errorElement = DefaultErrorElement,
|
|
141
|
+
delayLoading,
|
|
142
|
+
}: AwaitErrorHandlerProps<T>) {
|
|
143
|
+
return (
|
|
144
|
+
<Suspense
|
|
145
|
+
fallback={
|
|
146
|
+
<DelayedLoading delayLoading={delayLoading}>{loading}</DelayedLoading>
|
|
147
|
+
}
|
|
148
|
+
>
|
|
149
|
+
<ResolveAwait resolve={resolve} errorElement={errorElement}>
|
|
150
|
+
{children}
|
|
151
|
+
</ResolveAwait>
|
|
152
|
+
</Suspense>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function ResolveAwait<T>({
|
|
157
|
+
children,
|
|
158
|
+
resolve,
|
|
159
|
+
errorElement,
|
|
160
|
+
params,
|
|
161
|
+
}: AwaitErrorHandlerProps<T>) {
|
|
162
|
+
const data = resolve();
|
|
163
|
+
logger.debug('resolve data: ', data);
|
|
164
|
+
if (typeof data === 'string' && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
|
|
165
|
+
const transformedError = transformError(data);
|
|
166
|
+
return (
|
|
167
|
+
<>
|
|
168
|
+
{typeof errorElement === 'function' ? (
|
|
169
|
+
<>
|
|
170
|
+
{globalThis.FEDERATION_SSR && (
|
|
171
|
+
<script
|
|
172
|
+
suppressHydrationWarning
|
|
173
|
+
// eslint-disable-next-line react/no-danger
|
|
174
|
+
dangerouslySetInnerHTML={{
|
|
175
|
+
__html: String.raw`
|
|
176
|
+
globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || []
|
|
177
|
+
globalThis['${DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ''},${params ? JSON.stringify(params) : null},true]);`,
|
|
178
|
+
}}
|
|
179
|
+
></script>
|
|
180
|
+
)}
|
|
181
|
+
{errorElement(transformedError)}
|
|
182
|
+
</>
|
|
183
|
+
) : (
|
|
184
|
+
errorElement
|
|
185
|
+
)}
|
|
186
|
+
</>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
const toRender =
|
|
190
|
+
typeof children === 'function' ? children(data as T) : children;
|
|
191
|
+
return <>{toRender}</>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// return string when promise is rejected
|
|
195
|
+
const fetchData = <T,>(promise: Promise<T>, ref: MutableRefObject<T>) => {
|
|
196
|
+
let data: T | string;
|
|
197
|
+
let status: 'pending' | 'success' = 'pending';
|
|
198
|
+
const suspender = promise
|
|
199
|
+
.then((res) => {
|
|
200
|
+
status = 'success';
|
|
201
|
+
data = res;
|
|
202
|
+
ref.current = res;
|
|
203
|
+
})
|
|
204
|
+
.catch((e) => {
|
|
205
|
+
status = 'success';
|
|
206
|
+
console.warn(e);
|
|
207
|
+
data = AWAIT_ERROR_PREFIX + e;
|
|
208
|
+
});
|
|
209
|
+
return () => {
|
|
210
|
+
if (status === 'pending') {
|
|
211
|
+
throw suspender;
|
|
212
|
+
}
|
|
213
|
+
return data;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const PLUGIN_IDENTIFIER = '[ Module Federation React ]';
|
|
2
|
+
export const DOWNGRADE_KEY = '_mfSSRDowngrade';
|
|
3
|
+
export const DATA_FETCH_MAP_KEY = '__MF_DATA_FETCH_MAP__';
|
|
4
|
+
export const DATA_FETCH_FUNCTION = '_mfDataFetch';
|
|
5
|
+
export const FS_HREF = '_mfFSHref';
|
|
6
|
+
export const ERROR_TYPE = {
|
|
7
|
+
DATA_FETCH: 1,
|
|
8
|
+
LOAD_REMOTE: 2,
|
|
9
|
+
UNKNOWN: 3,
|
|
10
|
+
};
|
|
11
|
+
export const WRAP_DATA_FETCH_ID_IDENTIFIER = 'wrap_dfip_identifier';
|
|
12
|
+
export const enum MF_DATA_FETCH_TYPE {
|
|
13
|
+
FETCH_SERVER = 1,
|
|
14
|
+
FETCH_CLIENT = 2,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const enum MF_DATA_FETCH_STATUS {
|
|
18
|
+
LOADED = 1,
|
|
19
|
+
LOADING = 2,
|
|
20
|
+
AWAIT = 0,
|
|
21
|
+
ERROR = 3,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const DATA_FETCH_IDENTIFIER = 'data';
|
|
25
|
+
export const DATA_FETCH_CLIENT_SUFFIX = '.client';
|
|
26
|
+
export const DATA_FETCH_QUERY = 'x-mf-data-fetch';
|
|
27
|
+
export const DATA_FETCH_ERROR_PREFIX =
|
|
28
|
+
'caught the following error during dataFetch: ';
|
|
29
|
+
export const LOAD_REMOTE_ERROR_PREFIX =
|
|
30
|
+
'caught the following error during loadRemote: ';
|