@module-federation/bridge-react 0.15.0 → 0.17.0
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 +21 -0
- package/__tests__/bridge.spec.tsx +7 -7
- package/__tests__/createLazyComponent.spec.tsx +210 -0
- package/__tests__/prefetch.spec.ts +153 -0
- package/__tests__/setupTests.ts +3 -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 +140 -0
- package/dist/index.es.js +38 -12
- package/dist/index.esm-BCeUd-x9.mjs +418 -0
- package/dist/index.esm-j_1sIRzg.js +417 -0
- package/dist/lazy-load-component-plugin-C1tVve-W.js +521 -0
- package/dist/lazy-load-component-plugin-PERjiaFJ.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-CZvoIftg.js +1334 -0
- package/dist/prefetch-Dux8GUpr.mjs +1335 -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-Cy-amYU5.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/package.json +47 -5
- 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 +17 -4
- package/tsconfig.json +1 -1
- 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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @module-federation/bridge-react
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- e874c64: refactor(bridge-react): rename createRemoteComponent as createRemoteAppComponent
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- e874c64: feat(bridge-react): export createLazyCompoent api
|
|
12
|
+
- 3f736b6: chore: rename FederationHost to ModuleFederation
|
|
13
|
+
- @module-federation/sdk@0.17.0
|
|
14
|
+
- @module-federation/bridge-shared@0.17.0
|
|
15
|
+
|
|
16
|
+
## 0.16.0
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- Updated dependencies [1485fcf]
|
|
21
|
+
- @module-federation/sdk@0.16.0
|
|
22
|
+
- @module-federation/bridge-shared@0.16.0
|
|
23
|
+
|
|
3
24
|
## 0.15.0
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { assert, describe, it } from 'vitest';
|
|
3
|
-
import { createBridgeComponent,
|
|
3
|
+
import { createBridgeComponent, createRemoteAppComponent } from '../src';
|
|
4
4
|
import {
|
|
5
5
|
act,
|
|
6
6
|
fireEvent,
|
|
@@ -44,14 +44,14 @@ describe('bridge', () => {
|
|
|
44
44
|
expect(document.querySelector('#container')!.innerHTML).toContain('');
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
it('
|
|
47
|
+
it('createRemoteAppComponent', async () => {
|
|
48
48
|
function Component({ props }: { props?: Record<string, any> }) {
|
|
49
49
|
return <div>life cycle render {props?.msg}</div>;
|
|
50
50
|
}
|
|
51
51
|
const BridgeComponent = createBridgeComponent({
|
|
52
52
|
rootComponent: Component,
|
|
53
53
|
});
|
|
54
|
-
const RemoteComponent =
|
|
54
|
+
const RemoteComponent = createRemoteAppComponent({
|
|
55
55
|
loader: async () => {
|
|
56
56
|
return {
|
|
57
57
|
default: BridgeComponent,
|
|
@@ -71,7 +71,7 @@ describe('bridge', () => {
|
|
|
71
71
|
expect(getHtml(container)).toMatch('hello world');
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
it('
|
|
74
|
+
it('createRemoteAppComponent and obtain ref property', async () => {
|
|
75
75
|
const ref = {
|
|
76
76
|
current: null,
|
|
77
77
|
};
|
|
@@ -82,7 +82,7 @@ describe('bridge', () => {
|
|
|
82
82
|
const BridgeComponent = createBridgeComponent({
|
|
83
83
|
rootComponent: Component,
|
|
84
84
|
});
|
|
85
|
-
const RemoteComponent =
|
|
85
|
+
const RemoteComponent = createRemoteAppComponent({
|
|
86
86
|
loader: async () => {
|
|
87
87
|
return {
|
|
88
88
|
default: BridgeComponent,
|
|
@@ -103,7 +103,7 @@ describe('bridge', () => {
|
|
|
103
103
|
expect(ref.current).not.toBeNull();
|
|
104
104
|
});
|
|
105
105
|
|
|
106
|
-
it('
|
|
106
|
+
it('createRemoteAppComponent with custom createRoot prop', async () => {
|
|
107
107
|
const renderMock = vi.fn();
|
|
108
108
|
|
|
109
109
|
function Component({ props }: { props?: Record<string, any> }) {
|
|
@@ -118,7 +118,7 @@ describe('bridge', () => {
|
|
|
118
118
|
};
|
|
119
119
|
},
|
|
120
120
|
});
|
|
121
|
-
const RemoteComponent =
|
|
121
|
+
const RemoteComponent = createRemoteAppComponent({
|
|
122
122
|
loader: async () => {
|
|
123
123
|
return {
|
|
124
124
|
default: BridgeComponent,
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import React, { Suspense } from 'react';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
4
|
+
import {
|
|
5
|
+
createLazyComponent,
|
|
6
|
+
collectSSRAssets,
|
|
7
|
+
} from '../src/lazy/createLazyComponent';
|
|
8
|
+
import * as runtime from '@module-federation/runtime';
|
|
9
|
+
import * as utils from '../src/lazy/utils';
|
|
10
|
+
|
|
11
|
+
// Mocking dependencies
|
|
12
|
+
vi.mock('@module-federation/runtime');
|
|
13
|
+
vi.mock('../src/lazy/utils');
|
|
14
|
+
|
|
15
|
+
const mockGetInstance = runtime.getInstance as vi.Mock;
|
|
16
|
+
const mockGetLoadedRemoteInfos = utils.getLoadedRemoteInfos as vi.Mock;
|
|
17
|
+
const mockGetDataFetchMapKey = utils.getDataFetchMapKey as vi.Mock;
|
|
18
|
+
const mockFetchData = utils.fetchData as vi.Mock;
|
|
19
|
+
|
|
20
|
+
const MockComponent = () => <div>Mock Component</div>;
|
|
21
|
+
const LoadingComponent = () => <div>Loading...</div>;
|
|
22
|
+
const ErrorComponent = () => <div>Error!</div>;
|
|
23
|
+
|
|
24
|
+
describe('createLazyComponent', () => {
|
|
25
|
+
let mockInstance: any;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
vi.clearAllMocks();
|
|
29
|
+
mockInstance = {
|
|
30
|
+
name: 'host-app',
|
|
31
|
+
options: { version: '1.0.0' },
|
|
32
|
+
getModuleInfo: vi.fn(),
|
|
33
|
+
};
|
|
34
|
+
mockGetInstance.mockReturnValue(mockInstance);
|
|
35
|
+
mockGetLoadedRemoteInfos.mockReturnValue({
|
|
36
|
+
name: 'remoteApp',
|
|
37
|
+
alias: 'remote',
|
|
38
|
+
expose: './Component',
|
|
39
|
+
version: '1.0.0',
|
|
40
|
+
snapshot: {
|
|
41
|
+
modules: [
|
|
42
|
+
{
|
|
43
|
+
modulePath: './Component',
|
|
44
|
+
assets: {
|
|
45
|
+
css: { sync: [], async: [] },
|
|
46
|
+
js: { sync: [], async: [] },
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
publicPath: 'http://localhost:3001/',
|
|
51
|
+
remoteEntry: 'remoteEntry.js',
|
|
52
|
+
},
|
|
53
|
+
entryGlobalName: 'remoteApp',
|
|
54
|
+
});
|
|
55
|
+
mockGetDataFetchMapKey.mockReturnValue('data-fetch-key');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should render loading component then the actual component', async () => {
|
|
59
|
+
const loader = vi.fn().mockResolvedValue({
|
|
60
|
+
default: MockComponent,
|
|
61
|
+
[Symbol.for('mf_module_id')]: 'remoteApp/Component',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const LazyComponent = createLazyComponent({
|
|
65
|
+
loader,
|
|
66
|
+
instance: mockInstance,
|
|
67
|
+
loading: <LoadingComponent />,
|
|
68
|
+
fallback: <ErrorComponent />,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
render(
|
|
72
|
+
<Suspense fallback={<LoadingComponent />}>
|
|
73
|
+
<LazyComponent />
|
|
74
|
+
</Suspense>,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
78
|
+
|
|
79
|
+
await waitFor(() => {
|
|
80
|
+
expect(screen.getByText('Mock Component')).toBeInTheDocument();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should render fallback component on data fetch error', async () => {
|
|
85
|
+
mockFetchData.mockRejectedValue(new Error('Data fetch failed'));
|
|
86
|
+
const LazyComponentWithDataFetch = createLazyComponent({
|
|
87
|
+
loader: vi.fn().mockResolvedValue({
|
|
88
|
+
default: MockComponent,
|
|
89
|
+
[Symbol.for('mf_module_id')]: 'remoteApp/Component',
|
|
90
|
+
}),
|
|
91
|
+
instance: mockInstance,
|
|
92
|
+
loading: <LoadingComponent />,
|
|
93
|
+
fallback: <ErrorComponent />,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
render(<LazyComponentWithDataFetch />);
|
|
97
|
+
|
|
98
|
+
await waitFor(() => {
|
|
99
|
+
expect(screen.getByText('Error!')).toBeInTheDocument();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should fetch data and pass it to the component', async () => {
|
|
104
|
+
const loader = vi.fn().mockResolvedValue({
|
|
105
|
+
default: (props: { mfData: any }) => (
|
|
106
|
+
<div>Data: {JSON.stringify(props.mfData)}</div>
|
|
107
|
+
),
|
|
108
|
+
[Symbol.for('mf_module_id')]: 'remoteApp/Component',
|
|
109
|
+
});
|
|
110
|
+
const mockData = { message: 'Hello' };
|
|
111
|
+
mockFetchData.mockResolvedValue(mockData);
|
|
112
|
+
|
|
113
|
+
const LazyComponent = createLazyComponent({
|
|
114
|
+
loader,
|
|
115
|
+
instance: mockInstance,
|
|
116
|
+
loading: <LoadingComponent />,
|
|
117
|
+
fallback: <ErrorComponent />,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
render(<LazyComponent />);
|
|
121
|
+
|
|
122
|
+
await waitFor(() => {
|
|
123
|
+
expect(
|
|
124
|
+
screen.getByText(`Data: ${JSON.stringify(mockData)}`),
|
|
125
|
+
).toBeInTheDocument();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('collectSSRAssets', () => {
|
|
131
|
+
let mockInstance: any;
|
|
132
|
+
|
|
133
|
+
beforeEach(() => {
|
|
134
|
+
vi.clearAllMocks();
|
|
135
|
+
mockInstance = {
|
|
136
|
+
name: 'host-app',
|
|
137
|
+
options: { version: '1.0.0' },
|
|
138
|
+
};
|
|
139
|
+
mockGetInstance.mockReturnValue(mockInstance);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should return an empty array if instance is not available', () => {
|
|
143
|
+
const assets = collectSSRAssets({
|
|
144
|
+
id: 'test/expose',
|
|
145
|
+
instance: undefined as any,
|
|
146
|
+
});
|
|
147
|
+
expect(assets).toEqual([]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should return an empty array if module info is not found', () => {
|
|
151
|
+
mockGetLoadedRemoteInfos.mockReturnValue(undefined);
|
|
152
|
+
const assets = collectSSRAssets({
|
|
153
|
+
id: 'test/expose',
|
|
154
|
+
instance: mockInstance,
|
|
155
|
+
});
|
|
156
|
+
expect(assets).toEqual([]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should collect CSS and JS assets for SSR', () => {
|
|
160
|
+
mockGetLoadedRemoteInfos.mockReturnValue({
|
|
161
|
+
name: 'remoteApp',
|
|
162
|
+
expose: './Component',
|
|
163
|
+
snapshot: {
|
|
164
|
+
publicPath: 'http://localhost:3001/',
|
|
165
|
+
remoteEntry: 'remoteEntry.js',
|
|
166
|
+
modules: [
|
|
167
|
+
{
|
|
168
|
+
modulePath: './Component',
|
|
169
|
+
assets: {
|
|
170
|
+
css: { sync: ['main.css'], async: ['extra.css'] },
|
|
171
|
+
js: { sync: ['main.js'], async: [] },
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const assets = collectSSRAssets({
|
|
179
|
+
id: 'remoteApp/Component',
|
|
180
|
+
instance: mockInstance,
|
|
181
|
+
injectScript: true,
|
|
182
|
+
injectLink: true,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(assets).toHaveLength(4); // 2 links, 2 scripts
|
|
186
|
+
|
|
187
|
+
const links = assets.filter(
|
|
188
|
+
(asset) => (asset as React.ReactElement).type === 'link',
|
|
189
|
+
);
|
|
190
|
+
const scripts = assets.filter(
|
|
191
|
+
(asset) => (asset as React.ReactElement).type === 'script',
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(links).toHaveLength(2);
|
|
195
|
+
expect((links[0] as React.ReactElement).props.href).toBe(
|
|
196
|
+
'http://localhost:3001/extra.css',
|
|
197
|
+
);
|
|
198
|
+
expect((links[1] as React.ReactElement).props.href).toBe(
|
|
199
|
+
'http://localhost:3001/main.css',
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
expect(scripts).toHaveLength(2);
|
|
203
|
+
expect((scripts[0] as React.ReactElement).props.src).toBe(
|
|
204
|
+
'http://localhost:3001/remoteEntry.js',
|
|
205
|
+
);
|
|
206
|
+
expect((scripts[1] as React.ReactElement).props.src).toBe(
|
|
207
|
+
'http://localhost:3001/main.js',
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { prefetch } from '../src/lazy/data-fetch/prefetch';
|
|
3
|
+
import * as utils from '../src/lazy/utils';
|
|
4
|
+
import logger from '../src/lazy/logger';
|
|
5
|
+
import helpers from '@module-federation/runtime/helpers';
|
|
6
|
+
|
|
7
|
+
// Mock dependencies
|
|
8
|
+
vi.mock('../src/lazy/logger');
|
|
9
|
+
vi.mock('../src/lazy/utils');
|
|
10
|
+
vi.mock('@module-federation/runtime/helpers', () => ({
|
|
11
|
+
default: {
|
|
12
|
+
utils: {
|
|
13
|
+
matchRemoteWithNameAndExpose: vi.fn(),
|
|
14
|
+
getRemoteInfo: vi.fn(),
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
describe('prefetch', () => {
|
|
20
|
+
let mockInstance: any;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.clearAllMocks();
|
|
24
|
+
mockInstance = {
|
|
25
|
+
name: 'host',
|
|
26
|
+
options: {
|
|
27
|
+
version: '1.0.0',
|
|
28
|
+
remotes: [
|
|
29
|
+
{
|
|
30
|
+
name: 'remote1',
|
|
31
|
+
alias: 'remote1_alias',
|
|
32
|
+
entry: 'http://localhost:3001/remoteEntry.js',
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
snapshotHandler: {
|
|
37
|
+
loadRemoteSnapshotInfo: vi.fn(),
|
|
38
|
+
},
|
|
39
|
+
remoteHandler: {
|
|
40
|
+
hooks: {
|
|
41
|
+
lifecycle: {
|
|
42
|
+
generatePreloadAssets: {
|
|
43
|
+
emit: vi.fn(),
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should log an error if id is not provided', async () => {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
await prefetch({ instance: mockInstance });
|
|
54
|
+
expect(logger.error).toHaveBeenCalledWith('id is required for prefetch!');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should log an error if instance is not provided', async () => {
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
await prefetch({ id: 'remote1/component1' });
|
|
60
|
+
expect(logger.error).toHaveBeenCalledWith(
|
|
61
|
+
'instance is required for prefetch!',
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should log an error if remote is not found', async () => {
|
|
66
|
+
(helpers.utils.matchRemoteWithNameAndExpose as vi.Mock).mockReturnValue(
|
|
67
|
+
undefined,
|
|
68
|
+
);
|
|
69
|
+
await prefetch({ id: 'nonexistent/component', instance: mockInstance });
|
|
70
|
+
expect(logger.error).toHaveBeenCalledWith(
|
|
71
|
+
`Can not found 'nonexistent/component' in instance.options.remotes!`,
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should successfully prefetch data and component resources', async () => {
|
|
76
|
+
const mockRemoteInfo = {
|
|
77
|
+
remote: { name: 'remote1', alias: 'remote1_alias' },
|
|
78
|
+
expose: './component1',
|
|
79
|
+
};
|
|
80
|
+
(helpers.utils.matchRemoteWithNameAndExpose as vi.Mock).mockReturnValue(
|
|
81
|
+
mockRemoteInfo,
|
|
82
|
+
);
|
|
83
|
+
(
|
|
84
|
+
mockInstance.snapshotHandler.loadRemoteSnapshotInfo as vi.Mock
|
|
85
|
+
).mockResolvedValue({
|
|
86
|
+
remoteSnapshot: {},
|
|
87
|
+
globalSnapshot: {},
|
|
88
|
+
});
|
|
89
|
+
(helpers.utils.getRemoteInfo as vi.Mock).mockReturnValue({});
|
|
90
|
+
|
|
91
|
+
const mockDataFetchFn = vi
|
|
92
|
+
.fn()
|
|
93
|
+
.mockResolvedValue({ data: 'prefetched data' });
|
|
94
|
+
const mockGetDataFetchGetter = vi.fn().mockResolvedValue(mockDataFetchFn);
|
|
95
|
+
const mockDataFetchMap = {
|
|
96
|
+
'remote1_alias@remote1/component1': [
|
|
97
|
+
[mockGetDataFetchGetter, 'GET', undefined],
|
|
98
|
+
],
|
|
99
|
+
};
|
|
100
|
+
(utils.getDataFetchMap as vi.Mock).mockReturnValue(mockDataFetchMap);
|
|
101
|
+
(utils.getDataFetchInfo as vi.Mock).mockReturnValue({
|
|
102
|
+
name: 'remote1',
|
|
103
|
+
alias: 'remote1_alias',
|
|
104
|
+
id: 'remote1/component1',
|
|
105
|
+
});
|
|
106
|
+
(utils.getDataFetchMapKey as vi.Mock).mockReturnValue(
|
|
107
|
+
'remote1_alias@remote1/component1',
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
await prefetch({
|
|
111
|
+
id: 'remote1/component1',
|
|
112
|
+
instance: mockInstance,
|
|
113
|
+
dataFetchParams: { some: 'param' },
|
|
114
|
+
preloadComponentResource: true,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(
|
|
118
|
+
mockInstance.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit,
|
|
119
|
+
).toHaveBeenCalled();
|
|
120
|
+
|
|
121
|
+
expect(mockGetDataFetchGetter).toHaveBeenCalled();
|
|
122
|
+
await new Promise(process.nextTick);
|
|
123
|
+
expect(mockDataFetchFn).toHaveBeenCalledWith({
|
|
124
|
+
some: 'param',
|
|
125
|
+
_id: 'remote1_alias@remote1/component1',
|
|
126
|
+
isDowngrade: false,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should handle cases where data fetch info is not available', async () => {
|
|
131
|
+
const mockRemoteInfo = {
|
|
132
|
+
remote: { name: 'remote1', alias: 'remote1_alias' },
|
|
133
|
+
expose: './component1',
|
|
134
|
+
};
|
|
135
|
+
(helpers.utils.matchRemoteWithNameAndExpose as vi.Mock).mockReturnValue(
|
|
136
|
+
mockRemoteInfo,
|
|
137
|
+
);
|
|
138
|
+
(
|
|
139
|
+
mockInstance.snapshotHandler.loadRemoteSnapshotInfo as vi.Mock
|
|
140
|
+
).mockResolvedValue({
|
|
141
|
+
remoteSnapshot: {},
|
|
142
|
+
globalSnapshot: {},
|
|
143
|
+
});
|
|
144
|
+
(utils.getDataFetchMap as vi.Mock).mockReturnValue(undefined);
|
|
145
|
+
|
|
146
|
+
await prefetch({
|
|
147
|
+
id: 'remote1/component1',
|
|
148
|
+
instance: mockInstance,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(utils.getDataFetchInfo).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// In vitest, you can use the setupFiles option in your configuration file to import any necessary setup files for your tests.
|
|
2
|
+
// For example, if you want to use testing-library's custom matchers, you can import them in a setup file like this:
|
|
3
|
+
import '@testing-library/jest-dom';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { Component, createElement, createContext } from "react";
|
|
3
|
-
import { L as LoggerInstance, R as RouterContext } from "./index-
|
|
3
|
+
import { L as LoggerInstance, R as RouterContext } from "./index-CqxytsLY.mjs";
|
|
4
4
|
import { federationRuntime } from "./plugin.es.js";
|
|
5
5
|
const ErrorBoundaryContext = createContext(null);
|
|
6
6
|
const initialState = {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const React = require("react");
|
|
3
|
-
const index = require("./index-
|
|
3
|
+
const index = require("./index-C0fDZB5b.js");
|
|
4
4
|
const plugin = require("./plugin.cjs.js");
|
|
5
5
|
function _interopNamespaceDefault(e) {
|
|
6
6
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const lazyUtils = require("./utils-iEVlDmyk.js");
|
|
3
|
+
const index_esm = require("./index.esm-j_1sIRzg.js");
|
|
4
|
+
function wrapSetTimeout(targetPromise, delay = 2e4, id) {
|
|
5
|
+
if (targetPromise && typeof targetPromise.then === "function") {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const timeoutId = setTimeout(() => {
|
|
8
|
+
lazyUtils.logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
|
|
9
|
+
reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
|
|
10
|
+
}, delay);
|
|
11
|
+
targetPromise.then((value) => {
|
|
12
|
+
clearTimeout(timeoutId);
|
|
13
|
+
resolve(value);
|
|
14
|
+
}).catch((err) => {
|
|
15
|
+
clearTimeout(timeoutId);
|
|
16
|
+
reject(err);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function addProtocol(url) {
|
|
22
|
+
if (url.startsWith("//")) {
|
|
23
|
+
return "https:" + url;
|
|
24
|
+
}
|
|
25
|
+
return url;
|
|
26
|
+
}
|
|
27
|
+
const getDecodeQuery = (url, name) => {
|
|
28
|
+
const res = url.searchParams.get(name);
|
|
29
|
+
if (!res) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return decodeURIComponent(res);
|
|
33
|
+
};
|
|
34
|
+
const dataFetchServerMiddleware = async (ctx, next) => {
|
|
35
|
+
var _a, _b, _c;
|
|
36
|
+
let url;
|
|
37
|
+
let dataFetchKey;
|
|
38
|
+
let params;
|
|
39
|
+
let remoteInfo;
|
|
40
|
+
try {
|
|
41
|
+
url = new URL(ctx.req.url);
|
|
42
|
+
dataFetchKey = getDecodeQuery(url, lazyUtils.DATA_FETCH_QUERY);
|
|
43
|
+
params = JSON.parse(getDecodeQuery(url, "params") || "{}");
|
|
44
|
+
const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
|
|
45
|
+
remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
|
|
46
|
+
} catch (e) {
|
|
47
|
+
lazyUtils.logger.error("fetch data from server, error: ", e);
|
|
48
|
+
return next();
|
|
49
|
+
}
|
|
50
|
+
if (!dataFetchKey) {
|
|
51
|
+
return next();
|
|
52
|
+
}
|
|
53
|
+
lazyUtils.logger.log("fetch data from server, dataFetchKey: ", dataFetchKey);
|
|
54
|
+
lazyUtils.logger.debug(
|
|
55
|
+
"fetch data from server, moduleInfo: ",
|
|
56
|
+
(_a = globalThis.__FEDERATION__) == null ? void 0 : _a.moduleInfo
|
|
57
|
+
);
|
|
58
|
+
try {
|
|
59
|
+
const dataFetchMap = lazyUtils.getDataFetchMap();
|
|
60
|
+
if (!dataFetchMap) {
|
|
61
|
+
lazyUtils.initDataFetchMap();
|
|
62
|
+
}
|
|
63
|
+
const fetchDataPromise = (_b = dataFetchMap[dataFetchKey]) == null ? void 0 : _b[1];
|
|
64
|
+
lazyUtils.logger.debug(
|
|
65
|
+
"fetch data from server, fetchDataPromise: ",
|
|
66
|
+
fetchDataPromise
|
|
67
|
+
);
|
|
68
|
+
if (fetchDataPromise && ((_c = dataFetchMap[dataFetchKey]) == null ? void 0 : _c[2]) !== lazyUtils.MF_DATA_FETCH_STATUS.ERROR) {
|
|
69
|
+
const targetPromise = fetchDataPromise[0];
|
|
70
|
+
const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchKey);
|
|
71
|
+
if (wrappedPromise) {
|
|
72
|
+
const res = await wrappedPromise;
|
|
73
|
+
lazyUtils.logger.log("fetch data from server, fetchDataPromise res: ", res);
|
|
74
|
+
return ctx.json(res);
|
|
75
|
+
}
|
|
76
|
+
lazyUtils.logger.error(
|
|
77
|
+
`Expected a Promise from fetchDataPromise[0] for dataFetchKey ${dataFetchKey}, but received:`,
|
|
78
|
+
targetPromise,
|
|
79
|
+
"Will try call new dataFetch again..."
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (remoteInfo) {
|
|
83
|
+
try {
|
|
84
|
+
const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
85
|
+
const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
|
|
86
|
+
if (!hostInstance2) {
|
|
87
|
+
throw new Error("host instance not found!");
|
|
88
|
+
}
|
|
89
|
+
const remote = hostInstance2.options.remotes.find(
|
|
90
|
+
(remote2) => remote2.name === remoteInfo.name
|
|
91
|
+
);
|
|
92
|
+
lazyUtils.logger.debug("find remote: ", JSON.stringify(remote));
|
|
93
|
+
if (!remote) {
|
|
94
|
+
hostInstance2.registerRemotes([
|
|
95
|
+
{
|
|
96
|
+
name: remoteInfo.name,
|
|
97
|
+
entry: remoteEntry,
|
|
98
|
+
entryGlobalName: remoteInfo.globalName
|
|
99
|
+
}
|
|
100
|
+
]);
|
|
101
|
+
} else if (!("entry" in remote) || !remote.entry.includes(index_esm.MANIFEST_EXT)) {
|
|
102
|
+
const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
|
|
103
|
+
lazyUtils.logger.debug(
|
|
104
|
+
"find hostGlobalSnapshot: ",
|
|
105
|
+
JSON.stringify(hostGlobalSnapshot)
|
|
106
|
+
);
|
|
107
|
+
lazyUtils.logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
|
|
108
|
+
if (!hostGlobalSnapshot || !remoteSnapshot) {
|
|
109
|
+
if ("version" in remote) {
|
|
110
|
+
delete remote.version;
|
|
111
|
+
}
|
|
112
|
+
remote.entry = remoteEntry;
|
|
113
|
+
remote.entryGlobalName = remoteInfo.globalName;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} catch (e) {
|
|
117
|
+
ctx.status(500);
|
|
118
|
+
return ctx.text(
|
|
119
|
+
`failed to fetch ${remoteInfo.name} data, error:
|
|
120
|
+
${e}`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const dataFetchItem = dataFetchMap[dataFetchKey];
|
|
125
|
+
lazyUtils.logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
|
|
126
|
+
if (dataFetchItem) {
|
|
127
|
+
const callFetchDataPromise = lazyUtils.fetchData(dataFetchKey, {
|
|
128
|
+
...params,
|
|
129
|
+
isDowngrade: !remoteInfo,
|
|
130
|
+
_id: dataFetchKey
|
|
131
|
+
});
|
|
132
|
+
const wrappedPromise = wrapSetTimeout(
|
|
133
|
+
callFetchDataPromise,
|
|
134
|
+
2e4,
|
|
135
|
+
dataFetchKey
|
|
136
|
+
);
|
|
137
|
+
if (wrappedPromise) {
|
|
138
|
+
const res = await wrappedPromise;
|
|
139
|
+
lazyUtils.logger.log("fetch data from server, dataFetchItem res: ", res);
|
|
140
|
+
return ctx.json(res);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const remoteId = dataFetchKey.split(index_esm.SEPARATOR)[0];
|
|
144
|
+
const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
145
|
+
if (!hostInstance) {
|
|
146
|
+
throw new Error("host instance not found!");
|
|
147
|
+
}
|
|
148
|
+
const dataFetchFn = await lazyUtils.loadDataFetchModule(hostInstance, remoteId);
|
|
149
|
+
const data = await dataFetchFn({
|
|
150
|
+
...params,
|
|
151
|
+
isDowngrade: !remoteInfo,
|
|
152
|
+
_id: dataFetchKey
|
|
153
|
+
});
|
|
154
|
+
lazyUtils.logger.log("fetch data from server, loadDataFetchModule res: ", data);
|
|
155
|
+
return ctx.json(data);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
lazyUtils.logger.error("server plugin data fetch error: ", e);
|
|
158
|
+
ctx.status(500);
|
|
159
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
160
|
+
${e}`);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
module.exports = dataFetchServerMiddleware;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MiddlewareHandler } from 'hono';
|
|
2
|
+
|
|
3
|
+
declare const dataFetchServerMiddleware: MiddlewareHandler;
|
|
4
|
+
export default dataFetchServerMiddleware;
|
|
5
|
+
|
|
6
|
+
export { }
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
declare module '@module-federation/runtime-core' {
|
|
10
|
+
interface ModuleFederation {
|
|
11
|
+
createLazyComponent<T, E extends keyof T>(options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>): ReturnType<typeof createLazyComponent<T, E>>;
|
|
12
|
+
prefetch(options: Omit<PrefetchOptions, 'instance'>): ReturnType<typeof prefetch>;
|
|
13
|
+
collectSSRAssets(options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>): ReturnType<typeof collectSSRAssets>;
|
|
14
|
+
}
|
|
15
|
+
}
|