@module-federation/data-prefetch 0.0.0-next-20240909062831 → 0.0.0-next-20240909073915
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 +3 -3
- package/dist/esm/{chunk-H3DTWHY4.js → chunk-YXTPJG2M.js} +65 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/plugin.js +1 -1
- package/dist/index.js +65 -1
- package/dist/plugin.js +65 -1
- package/package.json +3 -3
- package/src/plugin.ts +77 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
@module-federation/data-prefetch
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20240909073915
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
7
|
- 3ddab21: feat(@module-federation/data-prefetch): support data prefetch in Module Federation
|
|
8
|
-
- @module-federation/runtime@0.0.0-next-
|
|
9
|
-
- @module-federation/sdk@0.0.0-next-
|
|
8
|
+
- @module-federation/runtime@0.0.0-next-20240909073915
|
|
9
|
+
- @module-federation/sdk@0.0.0-next-20240909073915
|
|
@@ -50,7 +50,71 @@ var prefetchPlugin = () => ({
|
|
|
50
50
|
}
|
|
51
51
|
return Promise.resolve().then(() => {
|
|
52
52
|
const exports = instance.getExposeExports(id);
|
|
53
|
-
logger_default.info(
|
|
53
|
+
logger_default.info(
|
|
54
|
+
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`
|
|
55
|
+
);
|
|
56
|
+
const result = Object.keys(exports).map((k) => {
|
|
57
|
+
const value = instance.prefetch({
|
|
58
|
+
id,
|
|
59
|
+
functionId: k
|
|
60
|
+
});
|
|
61
|
+
const functionId = k;
|
|
62
|
+
return {
|
|
63
|
+
value,
|
|
64
|
+
functionId
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
return result;
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
loadingArray.push({
|
|
71
|
+
id,
|
|
72
|
+
promise
|
|
73
|
+
});
|
|
74
|
+
return options;
|
|
75
|
+
},
|
|
76
|
+
afterResolve(options) {
|
|
77
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
78
|
+
const snapshot = remoteSnapshot;
|
|
79
|
+
const { name } = remoteInfo;
|
|
80
|
+
const prefetchOptions = {
|
|
81
|
+
name,
|
|
82
|
+
remote: remoteInfo,
|
|
83
|
+
origin,
|
|
84
|
+
remoteSnapshot: snapshot
|
|
85
|
+
};
|
|
86
|
+
const signal = getSignalFromManifest(snapshot);
|
|
87
|
+
if (!signal) {
|
|
88
|
+
return options;
|
|
89
|
+
}
|
|
90
|
+
const inited = loadingArray.some((info) => info.id === id);
|
|
91
|
+
if (!inited) {
|
|
92
|
+
return options;
|
|
93
|
+
}
|
|
94
|
+
if (sharedFlag !== strategy) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
100
|
+
let prefetchUrl;
|
|
101
|
+
if (snapshot.prefetchEntry) {
|
|
102
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
103
|
+
}
|
|
104
|
+
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
105
|
+
if (index !== -1) {
|
|
106
|
+
loadingArray.splice(index, 1);
|
|
107
|
+
}
|
|
108
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
109
|
+
const projectExports = instance.getProjectExports();
|
|
110
|
+
if (projectExports instanceof Promise) {
|
|
111
|
+
await projectExports;
|
|
112
|
+
}
|
|
113
|
+
return Promise.resolve().then(() => {
|
|
114
|
+
const exports = instance.getExposeExports(id);
|
|
115
|
+
logger_default.info(
|
|
116
|
+
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
|
|
117
|
+
);
|
|
54
118
|
const result = Object.keys(exports).map((k) => {
|
|
55
119
|
const value = instance.prefetch({
|
|
56
120
|
id,
|
package/dist/esm/index.js
CHANGED
package/dist/esm/plugin.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -234,7 +234,71 @@ var prefetchPlugin = () => ({
|
|
|
234
234
|
}
|
|
235
235
|
return Promise.resolve().then(() => {
|
|
236
236
|
const exports = instance.getExposeExports(id);
|
|
237
|
-
logger_default.info(
|
|
237
|
+
logger_default.info(
|
|
238
|
+
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`
|
|
239
|
+
);
|
|
240
|
+
const result = Object.keys(exports).map((k) => {
|
|
241
|
+
const value = instance.prefetch({
|
|
242
|
+
id,
|
|
243
|
+
functionId: k
|
|
244
|
+
});
|
|
245
|
+
const functionId = k;
|
|
246
|
+
return {
|
|
247
|
+
value,
|
|
248
|
+
functionId
|
|
249
|
+
};
|
|
250
|
+
});
|
|
251
|
+
return result;
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
loadingArray.push({
|
|
255
|
+
id,
|
|
256
|
+
promise
|
|
257
|
+
});
|
|
258
|
+
return options;
|
|
259
|
+
},
|
|
260
|
+
afterResolve(options) {
|
|
261
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
262
|
+
const snapshot = remoteSnapshot;
|
|
263
|
+
const { name } = remoteInfo;
|
|
264
|
+
const prefetchOptions = {
|
|
265
|
+
name,
|
|
266
|
+
remote: remoteInfo,
|
|
267
|
+
origin,
|
|
268
|
+
remoteSnapshot: snapshot
|
|
269
|
+
};
|
|
270
|
+
const signal = getSignalFromManifest(snapshot);
|
|
271
|
+
if (!signal) {
|
|
272
|
+
return options;
|
|
273
|
+
}
|
|
274
|
+
const inited = loadingArray.some((info) => info.id === id);
|
|
275
|
+
if (!inited) {
|
|
276
|
+
return options;
|
|
277
|
+
}
|
|
278
|
+
if (sharedFlag !== strategy) {
|
|
279
|
+
throw new Error(
|
|
280
|
+
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
284
|
+
let prefetchUrl;
|
|
285
|
+
if (snapshot.prefetchEntry) {
|
|
286
|
+
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
287
|
+
}
|
|
288
|
+
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
289
|
+
if (index !== -1) {
|
|
290
|
+
loadingArray.splice(index, 1);
|
|
291
|
+
}
|
|
292
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
293
|
+
const projectExports = instance.getProjectExports();
|
|
294
|
+
if (projectExports instanceof Promise) {
|
|
295
|
+
await projectExports;
|
|
296
|
+
}
|
|
297
|
+
return Promise.resolve().then(() => {
|
|
298
|
+
const exports = instance.getExposeExports(id);
|
|
299
|
+
logger_default.info(
|
|
300
|
+
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
|
|
301
|
+
);
|
|
238
302
|
const result = Object.keys(exports).map((k) => {
|
|
239
303
|
const value = instance.prefetch({
|
|
240
304
|
id,
|
package/dist/plugin.js
CHANGED
|
@@ -230,7 +230,71 @@ var prefetchPlugin = () => ({
|
|
|
230
230
|
}
|
|
231
231
|
return Promise.resolve().then(() => {
|
|
232
232
|
const exports = instance.getExposeExports(id);
|
|
233
|
-
logger_default.info(
|
|
233
|
+
logger_default.info(
|
|
234
|
+
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`
|
|
235
|
+
);
|
|
236
|
+
const result = Object.keys(exports).map((k) => {
|
|
237
|
+
const value = instance.prefetch({
|
|
238
|
+
id,
|
|
239
|
+
functionId: k
|
|
240
|
+
});
|
|
241
|
+
const functionId = k;
|
|
242
|
+
return {
|
|
243
|
+
value,
|
|
244
|
+
functionId
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
return result;
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
loadingArray.push({
|
|
251
|
+
id,
|
|
252
|
+
promise
|
|
253
|
+
});
|
|
254
|
+
return options;
|
|
255
|
+
},
|
|
256
|
+
afterResolve(options) {
|
|
257
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
258
|
+
const snapshot = remoteSnapshot;
|
|
259
|
+
const { name } = remoteInfo;
|
|
260
|
+
const prefetchOptions = {
|
|
261
|
+
name,
|
|
262
|
+
remote: remoteInfo,
|
|
263
|
+
origin,
|
|
264
|
+
remoteSnapshot: snapshot
|
|
265
|
+
};
|
|
266
|
+
const signal = getSignalFromManifest(snapshot);
|
|
267
|
+
if (!signal) {
|
|
268
|
+
return options;
|
|
269
|
+
}
|
|
270
|
+
const inited = loadingArray.some((info) => info.id === id);
|
|
271
|
+
if (!inited) {
|
|
272
|
+
return options;
|
|
273
|
+
}
|
|
274
|
+
if (sharedFlag !== strategy) {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
280
|
+
let prefetchUrl;
|
|
281
|
+
if (snapshot.prefetchEntry) {
|
|
282
|
+
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
283
|
+
}
|
|
284
|
+
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
285
|
+
if (index !== -1) {
|
|
286
|
+
loadingArray.splice(index, 1);
|
|
287
|
+
}
|
|
288
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
289
|
+
const projectExports = instance.getProjectExports();
|
|
290
|
+
if (projectExports instanceof Promise) {
|
|
291
|
+
await projectExports;
|
|
292
|
+
}
|
|
293
|
+
return Promise.resolve().then(() => {
|
|
294
|
+
const exports = instance.getExposeExports(id);
|
|
295
|
+
logger_default.info(
|
|
296
|
+
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
|
|
297
|
+
);
|
|
234
298
|
const result = Object.keys(exports).map((k) => {
|
|
235
299
|
const value = instance.prefetch({
|
|
236
300
|
id,
|
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-20240909073915",
|
|
5
5
|
"author": "nieyan <nyqykk@foxmail.com>",
|
|
6
6
|
"homepage": "https://github.com/module-federation/core",
|
|
7
7
|
"license": "MIT",
|
|
@@ -85,8 +85,8 @@
|
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
87
|
"fs-extra": "9.1.0",
|
|
88
|
-
"@module-federation/sdk": "0.0.0-next-
|
|
89
|
-
"@module-federation/runtime": "0.0.0-next-
|
|
88
|
+
"@module-federation/sdk": "0.0.0-next-20240909073915",
|
|
89
|
+
"@module-federation/runtime": "0.0.0-next-20240909073915"
|
|
90
90
|
},
|
|
91
91
|
"scripts": {
|
|
92
92
|
"dev": "cross-env WATCH=true tsup",
|
package/src/plugin.ts
CHANGED
|
@@ -55,7 +55,6 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
|
|
|
55
55
|
if (exist) {
|
|
56
56
|
return options;
|
|
57
57
|
}
|
|
58
|
-
// @ts-ignore
|
|
59
58
|
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
60
59
|
const projectExports = instance!.getProjectExports();
|
|
61
60
|
if (projectExports instanceof Promise) {
|
|
@@ -63,7 +62,9 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
|
|
|
63
62
|
}
|
|
64
63
|
return Promise.resolve().then(() => {
|
|
65
64
|
const exports = instance!.getExposeExports(id);
|
|
66
|
-
logger.info(
|
|
65
|
+
logger.info(
|
|
66
|
+
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`,
|
|
67
|
+
);
|
|
67
68
|
const result = Object.keys(exports).map((k) => {
|
|
68
69
|
const value = instance!.prefetch({
|
|
69
70
|
id,
|
|
@@ -87,6 +88,80 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
|
|
|
87
88
|
return options;
|
|
88
89
|
},
|
|
89
90
|
|
|
91
|
+
afterResolve(options) {
|
|
92
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
93
|
+
const snapshot = remoteSnapshot as ModuleInfo;
|
|
94
|
+
const { name } = remoteInfo;
|
|
95
|
+
|
|
96
|
+
const prefetchOptions = {
|
|
97
|
+
name,
|
|
98
|
+
remote: remoteInfo,
|
|
99
|
+
origin,
|
|
100
|
+
remoteSnapshot: snapshot,
|
|
101
|
+
};
|
|
102
|
+
const signal = getSignalFromManifest(snapshot);
|
|
103
|
+
if (!signal) {
|
|
104
|
+
return options;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const inited = loadingArray.some((info) => info.id === id);
|
|
108
|
+
if (!inited) {
|
|
109
|
+
return options;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (sharedFlag !== strategy) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const instance =
|
|
119
|
+
MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
120
|
+
|
|
121
|
+
let prefetchUrl;
|
|
122
|
+
// @ts-expect-error
|
|
123
|
+
if (snapshot.prefetchEntry) {
|
|
124
|
+
// @ts-expect-error
|
|
125
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry as string);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
129
|
+
// clear cache
|
|
130
|
+
if (index !== -1) {
|
|
131
|
+
loadingArray.splice(index, 1);
|
|
132
|
+
}
|
|
133
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
134
|
+
const projectExports = instance!.getProjectExports();
|
|
135
|
+
if (projectExports instanceof Promise) {
|
|
136
|
+
await projectExports;
|
|
137
|
+
}
|
|
138
|
+
return Promise.resolve().then(() => {
|
|
139
|
+
const exports = instance!.getExposeExports(id);
|
|
140
|
+
logger.info(
|
|
141
|
+
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`,
|
|
142
|
+
);
|
|
143
|
+
const result = Object.keys(exports).map((k) => {
|
|
144
|
+
const value = instance!.prefetch({
|
|
145
|
+
id,
|
|
146
|
+
functionId: k,
|
|
147
|
+
});
|
|
148
|
+
const functionId = k;
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
value,
|
|
152
|
+
functionId,
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
loadingArray.push({
|
|
160
|
+
id,
|
|
161
|
+
promise,
|
|
162
|
+
});
|
|
163
|
+
return options;
|
|
164
|
+
},
|
|
90
165
|
async onLoad(options) {
|
|
91
166
|
const { remote, id } = options;
|
|
92
167
|
const { name } = remote;
|