@module-federation/data-prefetch 0.0.0-next-20241231075249 → 0.0.0-next-20250103032148
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/package.json +19 -14
- package/.swcrc +0 -29
- package/CHANGELOG.md +0 -246
- package/__tests__/babel.spec.ts +0 -75
- package/__tests__/prefetch.spec.ts +0 -139
- package/__tests__/react.spec.ts +0 -119
- package/jest.config.js +0 -29
- package/project.json +0 -64
- package/rollup.config.js +0 -63
- package/src/cli/babel.ts +0 -94
- package/src/cli/index.ts +0 -176
- package/src/common/constant.ts +0 -1
- package/src/common/index.ts +0 -1
- package/src/common/node-utils.ts +0 -24
- package/src/common/runtime-utils.ts +0 -34
- package/src/constant.ts +0 -1
- package/src/index.ts +0 -2
- package/src/logger/index.ts +0 -5
- package/src/plugin.ts +0 -191
- package/src/prefetch.ts +0 -216
- package/src/react/hooks.ts +0 -95
- package/src/react/index.ts +0 -1
- package/src/react/utils.ts +0 -11
- package/src/universal/index.ts +0 -27
- package/tsconfig.json +0 -27
- package/tsconfig.lib.json +0 -10
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/data-prefetch",
|
|
3
3
|
"description": "Module Federation Data Prefetch",
|
|
4
|
-
"version": "0.0.0-next-
|
|
4
|
+
"version": "0.0.0-next-20250103032148",
|
|
5
5
|
"author": "nieyan <nyqykk@foxmail.com>",
|
|
6
6
|
"homepage": "https://github.com/module-federation/core",
|
|
7
7
|
"license": "MIT",
|
|
@@ -13,31 +13,36 @@
|
|
|
13
13
|
"publishConfig": {
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
16
21
|
"exports": {
|
|
17
22
|
".": {
|
|
23
|
+
"types": "./dist/index.cjs.d.ts",
|
|
18
24
|
"import": "./dist/index.esm.mjs",
|
|
19
|
-
"require": "./dist/index.cjs"
|
|
20
|
-
"types": "./dist/index.cjs.d.ts"
|
|
25
|
+
"require": "./dist/index.cjs.js"
|
|
21
26
|
},
|
|
22
27
|
"./react": {
|
|
28
|
+
"types": "./dist/react.cjs.d.ts",
|
|
23
29
|
"import": "./dist/react.esm.mjs",
|
|
24
|
-
"require": "./dist/react.cjs.js"
|
|
25
|
-
"types": "./dist/react.cjs.d.ts"
|
|
30
|
+
"require": "./dist/react.cjs.js"
|
|
26
31
|
},
|
|
27
32
|
"./cli": {
|
|
33
|
+
"types": "./dist/cli.cjs.d.ts",
|
|
28
34
|
"import": "./dist/cli.esm.mjs",
|
|
29
|
-
"require": "./dist/cli.cjs.js"
|
|
30
|
-
"types": "./dist/cli.cjs.d.ts"
|
|
35
|
+
"require": "./dist/cli.cjs.js"
|
|
31
36
|
},
|
|
32
37
|
"./babel-plugin": {
|
|
38
|
+
"types": "./dist/babel.cjs.d.ts",
|
|
33
39
|
"import": "./dist/babel.esm.mjs",
|
|
34
|
-
"require": "./dist/babel.cjs.js"
|
|
35
|
-
"types": "./dist/babel.cjs.d.ts"
|
|
40
|
+
"require": "./dist/babel.cjs.js"
|
|
36
41
|
},
|
|
37
42
|
"./universal": {
|
|
43
|
+
"types": "./dist/universal.cjs.d.ts",
|
|
38
44
|
"import": "./dist/universal.esm.mjs",
|
|
39
|
-
"require": "./dist/universal.cjs.js"
|
|
40
|
-
"types": "./dist/universal.cjs.d.ts"
|
|
45
|
+
"require": "./dist/universal.cjs.js"
|
|
41
46
|
}
|
|
42
47
|
},
|
|
43
48
|
"typesVersions": {
|
|
@@ -59,7 +64,7 @@
|
|
|
59
64
|
]
|
|
60
65
|
}
|
|
61
66
|
},
|
|
62
|
-
"main": "dist/index.cjs",
|
|
67
|
+
"main": "dist/index.cjs.js",
|
|
63
68
|
"module": "dist/index.esm.mjs",
|
|
64
69
|
"peerDependencies": {
|
|
65
70
|
"react": ">=16.9.0",
|
|
@@ -83,8 +88,8 @@
|
|
|
83
88
|
},
|
|
84
89
|
"dependencies": {
|
|
85
90
|
"fs-extra": "9.1.0",
|
|
86
|
-
"@module-federation/runtime": "0.0.0-next-
|
|
87
|
-
"@module-federation/sdk": "0.0.0-next-
|
|
91
|
+
"@module-federation/runtime": "0.0.0-next-20250103032148",
|
|
92
|
+
"@module-federation/sdk": "0.0.0-next-20250103032148"
|
|
88
93
|
},
|
|
89
94
|
"scripts": {
|
|
90
95
|
"test": "jest"
|
package/.swcrc
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"jsc": {
|
|
3
|
-
"target": "es2017",
|
|
4
|
-
"parser": {
|
|
5
|
-
"syntax": "typescript",
|
|
6
|
-
"decorators": true,
|
|
7
|
-
"dynamicImport": true
|
|
8
|
-
},
|
|
9
|
-
"transform": {
|
|
10
|
-
"decoratorMetadata": true,
|
|
11
|
-
"legacyDecorator": true
|
|
12
|
-
},
|
|
13
|
-
"keepClassNames": true,
|
|
14
|
-
"externalHelpers": true,
|
|
15
|
-
"loose": true
|
|
16
|
-
},
|
|
17
|
-
"module": {
|
|
18
|
-
"type": "es6"
|
|
19
|
-
},
|
|
20
|
-
"sourceMaps": true,
|
|
21
|
-
"exclude": [
|
|
22
|
-
"jest.config.ts",
|
|
23
|
-
".*\\.spec.tsx?$",
|
|
24
|
-
".*\\.test.tsx?$",
|
|
25
|
-
"./src/jest-setup.ts$",
|
|
26
|
-
"./**/jest-setup.ts$",
|
|
27
|
-
".*.js$"
|
|
28
|
-
]
|
|
29
|
-
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
# @module-federation/data-prefetch
|
|
2
|
-
|
|
3
|
-
## 0.0.0-next-20241231075249
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- @module-federation/runtime@0.0.0-next-20241231075249
|
|
8
|
-
- @module-federation/sdk@0.0.0-next-20241231075249
|
|
9
|
-
|
|
10
|
-
## 0.8.7
|
|
11
|
-
|
|
12
|
-
### Patch Changes
|
|
13
|
-
|
|
14
|
-
- Updated dependencies [835b09c]
|
|
15
|
-
- Updated dependencies [f573ad0]
|
|
16
|
-
- Updated dependencies [336f3d8]
|
|
17
|
-
- Updated dependencies [4fd33fb]
|
|
18
|
-
- @module-federation/sdk@0.8.7
|
|
19
|
-
- @module-federation/runtime@0.8.7
|
|
20
|
-
|
|
21
|
-
## 0.8.6
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- @module-federation/runtime@0.8.6
|
|
26
|
-
- @module-federation/sdk@0.8.6
|
|
27
|
-
|
|
28
|
-
## 0.8.5
|
|
29
|
-
|
|
30
|
-
### Patch Changes
|
|
31
|
-
|
|
32
|
-
- @module-federation/runtime@0.8.5
|
|
33
|
-
- @module-federation/sdk@0.8.5
|
|
34
|
-
|
|
35
|
-
## 0.8.4
|
|
36
|
-
|
|
37
|
-
### Patch Changes
|
|
38
|
-
|
|
39
|
-
- @module-federation/runtime@0.8.4
|
|
40
|
-
- @module-federation/sdk@0.8.4
|
|
41
|
-
|
|
42
|
-
## 0.8.3
|
|
43
|
-
|
|
44
|
-
### Patch Changes
|
|
45
|
-
|
|
46
|
-
- 5b637c3: fix(data-prefetch): the prefetch exports type is promise or function
|
|
47
|
-
- Updated dependencies [f817674]
|
|
48
|
-
- Updated dependencies [8e172c8]
|
|
49
|
-
- @module-federation/runtime@0.8.3
|
|
50
|
-
- @module-federation/sdk@0.8.3
|
|
51
|
-
|
|
52
|
-
## 0.8.2
|
|
53
|
-
|
|
54
|
-
### Patch Changes
|
|
55
|
-
|
|
56
|
-
- Updated dependencies [fa7a0bd]
|
|
57
|
-
- @module-federation/runtime@0.8.2
|
|
58
|
-
- @module-federation/sdk@0.8.2
|
|
59
|
-
|
|
60
|
-
## 0.8.1
|
|
61
|
-
|
|
62
|
-
### Patch Changes
|
|
63
|
-
|
|
64
|
-
- @module-federation/runtime@0.8.1
|
|
65
|
-
- @module-federation/sdk@0.8.1
|
|
66
|
-
|
|
67
|
-
## 0.8.0
|
|
68
|
-
|
|
69
|
-
### Patch Changes
|
|
70
|
-
|
|
71
|
-
- @module-federation/runtime@0.8.0
|
|
72
|
-
- @module-federation/sdk@0.8.0
|
|
73
|
-
|
|
74
|
-
## 0.7.7
|
|
75
|
-
|
|
76
|
-
### Patch Changes
|
|
77
|
-
|
|
78
|
-
- Updated dependencies [a960c88]
|
|
79
|
-
- Updated dependencies [e26d107]
|
|
80
|
-
- @module-federation/runtime@0.7.7
|
|
81
|
-
- @module-federation/sdk@0.7.7
|
|
82
|
-
|
|
83
|
-
## 0.7.6
|
|
84
|
-
|
|
85
|
-
### Patch Changes
|
|
86
|
-
|
|
87
|
-
- @module-federation/runtime@0.7.6
|
|
88
|
-
- @module-federation/sdk@0.7.6
|
|
89
|
-
|
|
90
|
-
## 0.7.5
|
|
91
|
-
|
|
92
|
-
### Patch Changes
|
|
93
|
-
|
|
94
|
-
- @module-federation/runtime@0.7.5
|
|
95
|
-
- @module-federation/sdk@0.7.5
|
|
96
|
-
|
|
97
|
-
## 0.7.4
|
|
98
|
-
|
|
99
|
-
### Patch Changes
|
|
100
|
-
|
|
101
|
-
- Updated dependencies [ff8ce29]
|
|
102
|
-
- @module-federation/runtime@0.7.4
|
|
103
|
-
- @module-federation/sdk@0.7.4
|
|
104
|
-
|
|
105
|
-
## 0.7.3
|
|
106
|
-
|
|
107
|
-
### Patch Changes
|
|
108
|
-
|
|
109
|
-
- 4ab9295: disable package json generation during build
|
|
110
|
-
- Updated dependencies [4ab9295]
|
|
111
|
-
- Updated dependencies [c2b1efa]
|
|
112
|
-
- @module-federation/runtime@0.7.3
|
|
113
|
-
- @module-federation/sdk@0.7.3
|
|
114
|
-
|
|
115
|
-
## 0.7.2
|
|
116
|
-
|
|
117
|
-
### Patch Changes
|
|
118
|
-
|
|
119
|
-
- @module-federation/runtime@0.7.2
|
|
120
|
-
- @module-federation/sdk@0.7.2
|
|
121
|
-
|
|
122
|
-
## 0.7.1
|
|
123
|
-
|
|
124
|
-
### Patch Changes
|
|
125
|
-
|
|
126
|
-
- Updated dependencies [66ba7b1]
|
|
127
|
-
- Updated dependencies [8712967]
|
|
128
|
-
- Updated dependencies [6db4c5f]
|
|
129
|
-
- @module-federation/runtime@0.7.1
|
|
130
|
-
- @module-federation/sdk@0.7.1
|
|
131
|
-
|
|
132
|
-
## 0.7.0
|
|
133
|
-
|
|
134
|
-
### Minor Changes
|
|
135
|
-
|
|
136
|
-
- 206b56d: disable hoistTransitiveImports for better tree shake
|
|
137
|
-
- Updated dependencies [752537b]
|
|
138
|
-
- Updated dependencies [54bfd1c]
|
|
139
|
-
- Updated dependencies [879ad87]
|
|
140
|
-
- Updated dependencies [4eb09e7]
|
|
141
|
-
- Updated dependencies [206b56d]
|
|
142
|
-
- @module-federation/runtime@0.7.0
|
|
143
|
-
- @module-federation/sdk@0.7.0
|
|
144
|
-
|
|
145
|
-
## 0.6.16
|
|
146
|
-
|
|
147
|
-
### Patch Changes
|
|
148
|
-
|
|
149
|
-
- 024df60: disable hoistTransitiveImports for better tree shake
|
|
150
|
-
- Updated dependencies [f779188]
|
|
151
|
-
- Updated dependencies [024df60]
|
|
152
|
-
- @module-federation/sdk@0.6.16
|
|
153
|
-
- @module-federation/runtime@0.6.16
|
|
154
|
-
|
|
155
|
-
## 0.6.15
|
|
156
|
-
|
|
157
|
-
### Patch Changes
|
|
158
|
-
|
|
159
|
-
- Updated dependencies [ec31539]
|
|
160
|
-
- @module-federation/runtime@0.6.15
|
|
161
|
-
- @module-federation/sdk@0.6.15
|
|
162
|
-
|
|
163
|
-
## 0.6.14
|
|
164
|
-
|
|
165
|
-
### Patch Changes
|
|
166
|
-
|
|
167
|
-
- ad605d2: chore: unified logger
|
|
168
|
-
- Updated dependencies [ad605d2]
|
|
169
|
-
- @module-federation/runtime@0.6.14
|
|
170
|
-
- @module-federation/sdk@0.6.14
|
|
171
|
-
|
|
172
|
-
## 0.6.13
|
|
173
|
-
|
|
174
|
-
### Patch Changes
|
|
175
|
-
|
|
176
|
-
- @module-federation/runtime@0.6.13
|
|
177
|
-
- @module-federation/sdk@0.6.13
|
|
178
|
-
|
|
179
|
-
## 0.6.12
|
|
180
|
-
|
|
181
|
-
### Patch Changes
|
|
182
|
-
|
|
183
|
-
- @module-federation/runtime@0.6.12
|
|
184
|
-
- @module-federation/sdk@0.6.12
|
|
185
|
-
|
|
186
|
-
## 0.6.11
|
|
187
|
-
|
|
188
|
-
### Patch Changes
|
|
189
|
-
|
|
190
|
-
- Updated dependencies [ea6d417]
|
|
191
|
-
- Updated dependencies [d5a3072]
|
|
192
|
-
- @module-federation/runtime@0.6.11
|
|
193
|
-
- @module-federation/sdk@0.6.11
|
|
194
|
-
|
|
195
|
-
## 0.6.10
|
|
196
|
-
|
|
197
|
-
### Patch Changes
|
|
198
|
-
|
|
199
|
-
- 22a3b83: fix(data-prefetch): apply DataPrefetchPlugin on demand
|
|
200
|
-
- 22a3b83: fix(data-prefetch): set sharedStrategy in build options instead of using runtime plugin
|
|
201
|
-
- Updated dependencies [b704f30]
|
|
202
|
-
- Updated dependencies [22a3b83]
|
|
203
|
-
- @module-federation/runtime@0.6.10
|
|
204
|
-
- @module-federation/sdk@0.6.10
|
|
205
|
-
|
|
206
|
-
## 0.6.9
|
|
207
|
-
|
|
208
|
-
### Patch Changes
|
|
209
|
-
|
|
210
|
-
- @module-federation/runtime@0.6.9
|
|
211
|
-
- @module-federation/sdk@0.6.9
|
|
212
|
-
|
|
213
|
-
## 0.6.8
|
|
214
|
-
|
|
215
|
-
### Patch Changes
|
|
216
|
-
|
|
217
|
-
- Updated dependencies [32db0ac]
|
|
218
|
-
- Updated dependencies [32db0ac]
|
|
219
|
-
- Updated dependencies [6c5f444]
|
|
220
|
-
- Updated dependencies [fac6ecf]
|
|
221
|
-
- @module-federation/sdk@0.6.8
|
|
222
|
-
- @module-federation/runtime@0.6.8
|
|
223
|
-
|
|
224
|
-
## 0.6.7
|
|
225
|
-
|
|
226
|
-
### Patch Changes
|
|
227
|
-
|
|
228
|
-
- 9e32644: Refactored the way prefetch entries are imported for improved dynamic loading handling.
|
|
229
|
-
|
|
230
|
-
- Changed the import of prefetch entries to use a function wrapper for more dynamic control.
|
|
231
|
-
- Updated data types to ensure consistency with the new function-based import approach.
|
|
232
|
-
- Modified the `injectPrefetch` function structure to incorporate the new import method.
|
|
233
|
-
- Modified the `MFDataPrefetch` class to handle the new import function when resolving exports.
|
|
234
|
-
|
|
235
|
-
- Updated dependencies [9e32644]
|
|
236
|
-
- Updated dependencies [9e32644]
|
|
237
|
-
- Updated dependencies [9e32644]
|
|
238
|
-
- @module-federation/runtime@0.6.7
|
|
239
|
-
- @module-federation/sdk@0.6.7
|
|
240
|
-
|
|
241
|
-
## 0.6.6
|
|
242
|
-
|
|
243
|
-
### Patch Changes
|
|
244
|
-
|
|
245
|
-
- @module-federation/runtime@0.6.6
|
|
246
|
-
- @module-federation/sdk@0.6.6
|
package/__tests__/babel.spec.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
|
-
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
import { BabelFileResult, transformFileSync } from '@babel/core';
|
|
6
|
-
// @ts-ignore no use
|
|
7
|
-
import type from '@types/jest';
|
|
8
|
-
|
|
9
|
-
import babelPlugin from '../src/cli/babel';
|
|
10
|
-
|
|
11
|
-
const emptyRegexp = /\s+/g;
|
|
12
|
-
describe('Babel Plugin Test', () => {
|
|
13
|
-
const options = {
|
|
14
|
-
name: '@mf/test',
|
|
15
|
-
exposes: { './expose': path.resolve(__dirname, 'test') },
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const testFilePath = path.join(__dirname, './test');
|
|
19
|
-
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
fs.mkdirSync(path.dirname(testFilePath), { recursive: true });
|
|
22
|
-
fs.closeSync(fs.openSync(testFilePath, 'w'));
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
fs.unlinkSync(testFilePath);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Check if the plugin will add the id parameter when it is not present
|
|
30
|
-
test('it adds id argument to usePrefetch call when it is absent', () => {
|
|
31
|
-
const input = `
|
|
32
|
-
import { usePrefetch } from "@module-federation/data-prefetch/react";
|
|
33
|
-
usePrefetch({});
|
|
34
|
-
`;
|
|
35
|
-
|
|
36
|
-
const expected = `
|
|
37
|
-
import { usePrefetch } from "@module-federation/data-prefetch/react";
|
|
38
|
-
usePrefetch({ id: "@mf/test/expose" });
|
|
39
|
-
`;
|
|
40
|
-
|
|
41
|
-
fs.writeFileSync(testFilePath, input);
|
|
42
|
-
const { code } = transformFileSync(testFilePath, {
|
|
43
|
-
plugins: [[babelPlugin, options]],
|
|
44
|
-
configFile: false,
|
|
45
|
-
}) as BabelFileResult;
|
|
46
|
-
|
|
47
|
-
expect(code?.replace(emptyRegexp, '')).toBe(
|
|
48
|
-
expected.replace(emptyRegexp, ''),
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// The plugin should retain the existing id parameter in the usePrefetch call
|
|
53
|
-
test('it does not overwrite existing id argument in usePrefetch call', () => {
|
|
54
|
-
const input = `
|
|
55
|
-
import { usePrefetch } from "@module-federation/data-prefetch/react";
|
|
56
|
-
usePrefetch({ id: "existingId" });
|
|
57
|
-
`;
|
|
58
|
-
|
|
59
|
-
const expected = `
|
|
60
|
-
import { usePrefetch } from "@module-federation/data-prefetch/react";
|
|
61
|
-
usePrefetch({ id: "existingId" });
|
|
62
|
-
`;
|
|
63
|
-
|
|
64
|
-
fs.writeFileSync(testFilePath, input);
|
|
65
|
-
const { code } = transformFileSync(testFilePath, {
|
|
66
|
-
plugins: [[babelPlugin, options]],
|
|
67
|
-
configFile: false,
|
|
68
|
-
babelrc: false,
|
|
69
|
-
}) as BabelFileResult;
|
|
70
|
-
|
|
71
|
-
expect(code?.replace(emptyRegexp, '')).toBe(
|
|
72
|
-
expected.replace(emptyRegexp, ''),
|
|
73
|
-
);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
// Import the necessary modules and functions
|
|
2
|
-
import { MFDataPrefetch } from '../src/prefetch';
|
|
3
|
-
import {
|
|
4
|
-
loadScript,
|
|
5
|
-
MFPrefetchCommon,
|
|
6
|
-
encodeName,
|
|
7
|
-
} from '@module-federation/sdk';
|
|
8
|
-
|
|
9
|
-
// Mock loadScript function from SDK
|
|
10
|
-
jest.mock('@module-federation/sdk', () => {
|
|
11
|
-
const originalModule = jest.requireActual('@module-federation/sdk');
|
|
12
|
-
return {
|
|
13
|
-
...originalModule,
|
|
14
|
-
loadScript: jest.fn(() => Promise.resolve()),
|
|
15
|
-
};
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('MF Data Prefetch', () => {
|
|
19
|
-
let prefetch: MFDataPrefetch;
|
|
20
|
-
|
|
21
|
-
const options = {
|
|
22
|
-
name: '@mf/test',
|
|
23
|
-
remoteSnapshot: {
|
|
24
|
-
buildVersion: '1.0.0',
|
|
25
|
-
globalName: 'TestGlobalName',
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
const exposeId = `${options.name}/button/${MFPrefetchCommon.identifier}`;
|
|
29
|
-
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
globalThis.__FEDERATION__.__PREFETCH__ = {
|
|
32
|
-
entryLoading: {},
|
|
33
|
-
instance: new Map(),
|
|
34
|
-
__PREFETCH_EXPORTS__: {},
|
|
35
|
-
};
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
prefetch = new MFDataPrefetch(options);
|
|
38
|
-
});
|
|
39
|
-
afterAll(() => {
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
delete globalThis.__FEDERATION__;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Instance gets added to global memory on creation
|
|
45
|
-
it('adds itself to global instances on creation', () => {
|
|
46
|
-
expect(prefetch.global.instance.get(options.name)).toBe(prefetch);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Loads entry script using loadScript from sdk
|
|
50
|
-
it('loads entry script using loadScript from sdk', async () => {
|
|
51
|
-
const url = 'testUrl'; // Url of the script to be loaded
|
|
52
|
-
|
|
53
|
-
await prefetch.loadEntry(url); // Call `loadEntry` function
|
|
54
|
-
|
|
55
|
-
// Expect that the loadScript function is called with the correct url
|
|
56
|
-
expect(loadScript).toHaveBeenCalledWith(url, expect.any(Object));
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Retrieves project exports
|
|
60
|
-
it('gets project exports', async () => {
|
|
61
|
-
const exposeExport = {
|
|
62
|
-
nyPrefetch: () => {},
|
|
63
|
-
};
|
|
64
|
-
const projectExport = {
|
|
65
|
-
[encodeName(exposeId)]: exposeExport,
|
|
66
|
-
};
|
|
67
|
-
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
|
|
68
|
-
() => Promise.resolve(projectExport);
|
|
69
|
-
|
|
70
|
-
await prefetch.getProjectExports();
|
|
71
|
-
expect(prefetch.getExposeExports(`${options.name}/button`)).toEqual(
|
|
72
|
-
exposeExport,
|
|
73
|
-
);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// Prefetching with memory and executing prefetch function
|
|
77
|
-
it('executes prefetch using prefetch function with and without memory', async () => {
|
|
78
|
-
const id = options.name;
|
|
79
|
-
const functionId = 'nyPrefetch';
|
|
80
|
-
const refetchParams = 'testParams';
|
|
81
|
-
const prefetchOptions = { id: `${id}/button`, functionId, refetchParams };
|
|
82
|
-
|
|
83
|
-
// Creating a mock prefetch function
|
|
84
|
-
const executePrefetch = jest.fn(() => 'Expected Result');
|
|
85
|
-
const prefetchExports = { [functionId]: executePrefetch };
|
|
86
|
-
|
|
87
|
-
// Mock Project Exports
|
|
88
|
-
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[id] = () =>
|
|
89
|
-
Promise.resolve({
|
|
90
|
-
[encodeName(exposeId)]: prefetchExports,
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
await prefetch.getProjectExports();
|
|
94
|
-
// Call the prefetch function first time
|
|
95
|
-
let result = await prefetch.prefetch(prefetchOptions);
|
|
96
|
-
|
|
97
|
-
// Verify that executePrefetch function is correctly executed
|
|
98
|
-
expect(executePrefetch).toHaveBeenCalled();
|
|
99
|
-
|
|
100
|
-
// Clear mock function calls data
|
|
101
|
-
executePrefetch.mockClear();
|
|
102
|
-
|
|
103
|
-
// Call the prefetch function again
|
|
104
|
-
result = await prefetch.prefetch(prefetchOptions);
|
|
105
|
-
|
|
106
|
-
// Verify that executePrefetch function is NOT called this time (since the result should come from memory)
|
|
107
|
-
expect(executePrefetch).not.toHaveBeenCalled();
|
|
108
|
-
|
|
109
|
-
// Clear mock function calls data
|
|
110
|
-
executePrefetch.mockClear();
|
|
111
|
-
|
|
112
|
-
prefetch.markOutdate(prefetchOptions, true);
|
|
113
|
-
|
|
114
|
-
// Call the prefetch function first time
|
|
115
|
-
result = await prefetch.prefetch(prefetchOptions);
|
|
116
|
-
|
|
117
|
-
// Verify that executePrefetch function is correctly executed
|
|
118
|
-
expect(executePrefetch).toHaveBeenCalled();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// Checking outdate marking
|
|
122
|
-
it('checks outdate marking', () => {
|
|
123
|
-
const markOptions = { id: 'testId', functionId: 'testFunction' };
|
|
124
|
-
|
|
125
|
-
// Mark the function as outdated
|
|
126
|
-
prefetch.markOutdate(markOptions, true);
|
|
127
|
-
|
|
128
|
-
// Verify that the function is marked as outdated
|
|
129
|
-
let isOutdated = prefetch.checkOutdate(markOptions);
|
|
130
|
-
expect(isOutdated).toBe(true);
|
|
131
|
-
|
|
132
|
-
// Mark the function as up-to-date
|
|
133
|
-
prefetch.markOutdate(markOptions, false);
|
|
134
|
-
|
|
135
|
-
// Verify that the function is marked as up-to-date
|
|
136
|
-
isOutdated = prefetch.checkOutdate(markOptions);
|
|
137
|
-
expect(isOutdated).toBe(false);
|
|
138
|
-
});
|
|
139
|
-
});
|
package/__tests__/react.spec.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { FederationHost, init } from '@module-federation/runtime';
|
|
2
|
-
import { renderHook, act } from '@testing-library/react-hooks';
|
|
3
|
-
import * as ModuleFederationSDK from '@module-federation/sdk';
|
|
4
|
-
import { usePrefetch } from '../src/react';
|
|
5
|
-
import { MFDataPrefetch } from '../src/prefetch';
|
|
6
|
-
|
|
7
|
-
const mockLoadScript = jest.spyOn(ModuleFederationSDK, 'loadScript');
|
|
8
|
-
mockLoadScript.mockImplementation(() => Promise.resolve());
|
|
9
|
-
|
|
10
|
-
describe('usePrefetch', () => {
|
|
11
|
-
// Mock prefetch function
|
|
12
|
-
global.fetch = jest.fn(() =>
|
|
13
|
-
Promise.resolve({
|
|
14
|
-
ok: true,
|
|
15
|
-
status: 200,
|
|
16
|
-
statusText: 'OK',
|
|
17
|
-
redirected: false,
|
|
18
|
-
type: 'basic',
|
|
19
|
-
url: '',
|
|
20
|
-
clone: jest.fn(),
|
|
21
|
-
headers: new Headers(),
|
|
22
|
-
body: null,
|
|
23
|
-
bodyUsed: false,
|
|
24
|
-
text: () => Promise.resolve(JSON.stringify({ data: 'testData' })),
|
|
25
|
-
json: () => Promise.resolve({ data: 'testData' }),
|
|
26
|
-
formData: () => Promise.resolve(new FormData()),
|
|
27
|
-
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
|
|
28
|
-
blob: () => Promise.resolve(new Blob()),
|
|
29
|
-
}),
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
let prefetch: MFDataPrefetch;
|
|
33
|
-
|
|
34
|
-
const options = {
|
|
35
|
-
// Options that will be passed to the `MFDataPrefetch` constructor
|
|
36
|
-
name: '@mf/test',
|
|
37
|
-
remoteSnapshot: {
|
|
38
|
-
buildVersion: '1.0.0',
|
|
39
|
-
globalName: 'TestGlobalName',
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
const testData = 'testData';
|
|
43
|
-
const newTestData = 'newTestData';
|
|
44
|
-
const functionId = 'testPrefetch';
|
|
45
|
-
const executePrefetch = jest.fn((params: string) =>
|
|
46
|
-
Promise.resolve(params ? params : testData),
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
new FederationHost({
|
|
50
|
-
name: options.name,
|
|
51
|
-
remotes: [],
|
|
52
|
-
});
|
|
53
|
-
init({
|
|
54
|
-
name: options.name,
|
|
55
|
-
remotes: [],
|
|
56
|
-
});
|
|
57
|
-
beforeEach(() => {
|
|
58
|
-
globalThis.__FEDERATION__.__PREFETCH__ = {
|
|
59
|
-
entryLoading: {},
|
|
60
|
-
instance: new Map(),
|
|
61
|
-
__PREFETCH_EXPORTS__: {},
|
|
62
|
-
};
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
prefetch = new MFDataPrefetch(options); // Create a new instance of `MFDataPrefetch` class before each test
|
|
65
|
-
|
|
66
|
-
const exposeExport = {
|
|
67
|
-
[functionId]: executePrefetch,
|
|
68
|
-
};
|
|
69
|
-
const exposeId = `${options.name}/button/${ModuleFederationSDK.MFPrefetchCommon.identifier}`;
|
|
70
|
-
const projectExport = {
|
|
71
|
-
[ModuleFederationSDK.encodeName(exposeId)]: exposeExport,
|
|
72
|
-
};
|
|
73
|
-
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
|
|
74
|
-
() => Promise.resolve(projectExport);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
afterEach(() => {
|
|
78
|
-
executePrefetch.mockClear();
|
|
79
|
-
mockLoadScript.mockRestore();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
afterAll(() => {
|
|
83
|
-
// @ts-ignore
|
|
84
|
-
delete globalThis.__FEDERATION__;
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should prefetch data on first mount', async () => {
|
|
88
|
-
const { result } = renderHook(() =>
|
|
89
|
-
usePrefetch({ id: `${options.name}/button`, functionId }),
|
|
90
|
-
);
|
|
91
|
-
await result.current[0];
|
|
92
|
-
expect(executePrefetch).toHaveBeenCalled();
|
|
93
|
-
// Verify the prefechState
|
|
94
|
-
expect(result.current[0]).resolves.toEqual(testData);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('should refetch data when refreshExecutor is called', async () => {
|
|
98
|
-
const { result } = renderHook(() =>
|
|
99
|
-
usePrefetch({ id: `${options.name}/button`, functionId }),
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
await result.current[0];
|
|
103
|
-
expect(executePrefetch).toHaveBeenCalled();
|
|
104
|
-
executePrefetch.mockClear();
|
|
105
|
-
const { result: newCallResult } = renderHook(() =>
|
|
106
|
-
usePrefetch({ id: `${options.name}/button`, functionId }),
|
|
107
|
-
);
|
|
108
|
-
await newCallResult.current[0];
|
|
109
|
-
expect(executePrefetch).not.toHaveBeenCalled();
|
|
110
|
-
// Call refreshExecutor
|
|
111
|
-
act(() => {
|
|
112
|
-
result.current[1](newTestData);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
expect(executePrefetch).toHaveBeenCalled();
|
|
116
|
-
// // Verify the prefetchState after refetch
|
|
117
|
-
expect(result.current[0]).resolves.toEqual(newTestData);
|
|
118
|
-
});
|
|
119
|
-
});
|