@module-federation/data-prefetch 0.0.0-next-20241010084324 → 0.0.0-next-20241011070539
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/.swcrc +29 -0
- package/CHANGELOG.md +12 -4
- package/LICENSE +1 -1
- package/dist/LICENSE +21 -0
- package/dist/babel.cjs.d.ts +2 -0
- package/dist/babel.cjs.js +50 -0
- package/dist/babel.esm.d.ts +2 -0
- package/dist/babel.esm.js +48 -0
- package/dist/cli.cjs.d.ts +1 -0
- package/dist/cli.cjs.js +155 -0
- package/dist/cli.esm.d.ts +1 -0
- package/dist/cli.esm.js +152 -0
- package/dist/index.cjs.d.ts +1 -0
- package/dist/index.cjs.js +13 -0
- package/dist/index.cjs2.js +7 -0
- package/dist/index.esm.d.ts +1 -0
- package/dist/index.esm.js +6 -0
- package/dist/index.esm2.js +5 -0
- package/dist/package.json +100 -0
- package/dist/plugin.cjs.d.ts +2 -0
- package/dist/plugin.cjs.js +159 -0
- package/dist/plugin.esm.d.ts +2 -0
- package/dist/plugin.esm.js +154 -0
- package/dist/polyfills.cjs.js +14 -0
- package/dist/polyfills.esm.js +12 -0
- package/dist/prefetch.cjs.js +142 -0
- package/dist/prefetch.esm.js +140 -0
- package/dist/react.cjs.d.ts +1 -0
- package/dist/react.cjs.js +77 -0
- package/dist/react.esm.d.ts +1 -0
- package/dist/react.esm.js +75 -0
- package/dist/runtime-utils.cjs.js +27 -0
- package/dist/runtime-utils.esm.js +22 -0
- package/dist/shared.cjs.d.ts +2 -0
- package/dist/shared.cjs.js +24 -0
- package/dist/shared.esm.d.ts +2 -0
- package/dist/shared.esm.js +22 -0
- package/dist/{cli → src/cli}/babel.d.ts +2 -4
- package/dist/{cli → src/cli}/index.d.ts +5 -8
- package/dist/src/common/constant.d.ts +1 -0
- package/dist/src/common/index.d.ts +1 -0
- package/dist/src/common/node-utils.d.ts +2 -0
- package/dist/src/common/runtime-utils.d.ts +5 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/logger/index.d.ts +3 -0
- package/dist/src/plugin.d.ts +3 -0
- package/dist/{prefetch-c08be471.d.ts → src/prefetch.d.ts} +4 -6
- package/dist/src/react/hooks.d.ts +11 -0
- package/dist/src/react/index.d.ts +1 -0
- package/dist/src/react/utils.d.ts +1 -0
- package/dist/src/shared/index.d.ts +3 -0
- package/dist/src/universal/index.d.ts +2 -0
- package/dist/universal.cjs.d.ts +1 -0
- package/dist/universal.cjs.js +29 -0
- package/dist/universal.esm.d.ts +1 -0
- package/dist/universal.esm.js +27 -0
- package/package.json +33 -30
- package/project.json +48 -6
- package/rollup.config.js +46 -0
- package/src/cli/index.ts +2 -2
- package/tsconfig.json +1 -1
- package/tsconfig.lib.json +10 -0
- package/dist/cli/babel.js +0 -84
- package/dist/cli/index.js +0 -207
- package/dist/esm/chunk-AJPO2B2T.js +0 -30
- package/dist/esm/chunk-BGNOVRH5.js +0 -166
- package/dist/esm/chunk-EZUCZHGV.js +0 -11
- package/dist/esm/chunk-ISF7VMBW.js +0 -156
- package/dist/esm/chunk-OCDMSRMZ.js +0 -32
- package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
- package/dist/esm/cli/babel.js +0 -57
- package/dist/esm/cli/index.js +0 -180
- package/dist/esm/index.js +0 -13
- package/dist/esm/plugin.js +0 -12
- package/dist/esm/react/index.js +0 -94
- package/dist/esm/shared/index.js +0 -27
- package/dist/esm/universal/index.js +0 -9
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -351
- package/dist/plugin.d.ts +0 -5
- package/dist/plugin.js +0 -347
- package/dist/react/index.d.ts +0 -16
- package/dist/react/index.js +0 -287
- package/dist/shared/index.d.ts +0 -5
- package/dist/shared/index.js +0 -48
- package/dist/universal/index.d.ts +0 -8
- package/dist/universal/index.js +0 -205
package/dist/index.js
DELETED
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
MFDataPrefetch: () => MFDataPrefetch,
|
|
24
|
-
prefetchPlugin: () => prefetchPlugin
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(src_exports);
|
|
27
|
-
|
|
28
|
-
// src/prefetch.ts
|
|
29
|
-
var import_runtime2 = require("@module-federation/runtime");
|
|
30
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
31
|
-
|
|
32
|
-
// src/common/runtime-utils.ts
|
|
33
|
-
var import_runtime = require("@module-federation/runtime");
|
|
34
|
-
var import_sdk = require("@module-federation/sdk");
|
|
35
|
-
var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
|
|
36
|
-
var compatGetPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/VmokPrefetch`);
|
|
37
|
-
var getSignalFromManifest = (remoteSnapshot) => {
|
|
38
|
-
if (!remoteSnapshot) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
if (!("prefetchEntry" in remoteSnapshot) && !("prefetchInterface" in remoteSnapshot)) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return true;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// src/prefetch.ts
|
|
51
|
-
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
52
|
-
var _a;
|
|
53
|
-
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
54
|
-
entryLoading: {},
|
|
55
|
-
instance: /* @__PURE__ */ new Map(),
|
|
56
|
-
__PREFETCH_EXPORTS__: {}
|
|
57
|
-
});
|
|
58
|
-
var MFDataPrefetch = class {
|
|
59
|
-
constructor(options) {
|
|
60
|
-
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
61
|
-
this.recordOutdate = {};
|
|
62
|
-
this._exports = {};
|
|
63
|
-
this._options = options;
|
|
64
|
-
this.global.instance.set(options.name, this);
|
|
65
|
-
}
|
|
66
|
-
get global() {
|
|
67
|
-
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
68
|
-
}
|
|
69
|
-
static getInstance(id) {
|
|
70
|
-
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
71
|
-
}
|
|
72
|
-
async loadEntry(entry) {
|
|
73
|
-
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
74
|
-
if (entry) {
|
|
75
|
-
const { buildVersion, globalName } = remoteSnapshot;
|
|
76
|
-
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
77
|
-
if (!this.global.entryLoading[uniqueKey]) {
|
|
78
|
-
this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
|
|
79
|
-
}
|
|
80
|
-
return this.global.entryLoading[uniqueKey];
|
|
81
|
-
} else {
|
|
82
|
-
const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
|
|
83
|
-
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
84
|
-
return (0, import_runtime2.getRemoteEntry)({
|
|
85
|
-
origin,
|
|
86
|
-
remoteInfo,
|
|
87
|
-
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
getProjectExports() {
|
|
92
|
-
var _a2;
|
|
93
|
-
if (Object.keys(this._exports).length > 0) {
|
|
94
|
-
return this._exports;
|
|
95
|
-
}
|
|
96
|
-
const { name } = this._options;
|
|
97
|
-
const exportsPromiseFn = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
98
|
-
const exportsPromise = typeof exportsPromiseFn === "function" ? exportsPromiseFn() : Promise.resolve({});
|
|
99
|
-
const resolve = exportsPromise.then(
|
|
100
|
-
(exports = {}) => {
|
|
101
|
-
const memory = {};
|
|
102
|
-
Object.keys(exports).forEach((key) => {
|
|
103
|
-
memory[key] = {};
|
|
104
|
-
const exportVal = exports[key];
|
|
105
|
-
Object.keys(exportVal).reduce(
|
|
106
|
-
(memo, current) => {
|
|
107
|
-
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
108
|
-
memo[current] = exportVal[current];
|
|
109
|
-
}
|
|
110
|
-
return memo;
|
|
111
|
-
},
|
|
112
|
-
memory[key]
|
|
113
|
-
);
|
|
114
|
-
});
|
|
115
|
-
this.memorizeExports(memory);
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
return resolve;
|
|
119
|
-
}
|
|
120
|
-
memorizeExports(exports) {
|
|
121
|
-
this._exports = exports;
|
|
122
|
-
}
|
|
123
|
-
getExposeExports(id) {
|
|
124
|
-
const prefetchId = getPrefetchId(id);
|
|
125
|
-
const compatId = compatGetPrefetchId(id);
|
|
126
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
127
|
-
return prefetchExports || {};
|
|
128
|
-
}
|
|
129
|
-
prefetch(prefetchOptions) {
|
|
130
|
-
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
131
|
-
let prefetchResult;
|
|
132
|
-
const prefetchId = getPrefetchId(id);
|
|
133
|
-
const compatId = compatGetPrefetchId(id);
|
|
134
|
-
const memorizeId = id + functionId;
|
|
135
|
-
const memory = this.prefetchMemory.get(memorizeId);
|
|
136
|
-
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
137
|
-
return memory;
|
|
138
|
-
}
|
|
139
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
140
|
-
if (!prefetchExports) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
const executePrefetch = prefetchExports[functionId];
|
|
144
|
-
if (typeof executePrefetch === "function") {
|
|
145
|
-
if (refetchParams) {
|
|
146
|
-
prefetchResult = executePrefetch(refetchParams);
|
|
147
|
-
} else {
|
|
148
|
-
prefetchResult = executePrefetch();
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
throw new Error(
|
|
152
|
-
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
this.memorize(memorizeId, prefetchResult);
|
|
156
|
-
return prefetchResult;
|
|
157
|
-
}
|
|
158
|
-
memorize(id, value) {
|
|
159
|
-
this.prefetchMemory.set(id, value);
|
|
160
|
-
}
|
|
161
|
-
markOutdate(markOptions, isOutdate) {
|
|
162
|
-
const { id, functionId = "default" } = markOptions;
|
|
163
|
-
if (!this.recordOutdate[id]) {
|
|
164
|
-
this.recordOutdate[id] = {};
|
|
165
|
-
}
|
|
166
|
-
this.recordOutdate[id][functionId] = isOutdate;
|
|
167
|
-
}
|
|
168
|
-
checkOutdate(outdateOptions) {
|
|
169
|
-
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
170
|
-
if (typeof cacheStrategy === "function") {
|
|
171
|
-
return cacheStrategy();
|
|
172
|
-
}
|
|
173
|
-
if (!this.recordOutdate[id]) {
|
|
174
|
-
this.recordOutdate[id] = {};
|
|
175
|
-
}
|
|
176
|
-
if (this.recordOutdate[id][functionId]) {
|
|
177
|
-
this.markOutdate(
|
|
178
|
-
{
|
|
179
|
-
id,
|
|
180
|
-
functionId
|
|
181
|
-
},
|
|
182
|
-
false
|
|
183
|
-
);
|
|
184
|
-
return true;
|
|
185
|
-
} else {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
// src/plugin.ts
|
|
192
|
-
var import_sdk4 = require("@module-federation/sdk");
|
|
193
|
-
|
|
194
|
-
// src/logger/index.ts
|
|
195
|
-
var import_sdk3 = require("@module-federation/sdk");
|
|
196
|
-
var logger_default = new import_sdk3.Logger("[Module Federation Data Prefetch]");
|
|
197
|
-
|
|
198
|
-
// src/plugin.ts
|
|
199
|
-
var loadingArray = [];
|
|
200
|
-
var strategy = "loaded-first";
|
|
201
|
-
var sharedFlag = strategy;
|
|
202
|
-
var prefetchPlugin = () => ({
|
|
203
|
-
name: "data-prefetch-runtime-plugin",
|
|
204
|
-
initContainer(options) {
|
|
205
|
-
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
206
|
-
const snapshot = remoteSnapshot;
|
|
207
|
-
const { name } = remoteInfo;
|
|
208
|
-
const prefetchOptions = {
|
|
209
|
-
name,
|
|
210
|
-
remote: remoteInfo,
|
|
211
|
-
origin,
|
|
212
|
-
remoteSnapshot: snapshot
|
|
213
|
-
};
|
|
214
|
-
const signal = getSignalFromManifest(snapshot);
|
|
215
|
-
if (!signal) {
|
|
216
|
-
return options;
|
|
217
|
-
}
|
|
218
|
-
if (sharedFlag !== strategy) {
|
|
219
|
-
throw new Error(
|
|
220
|
-
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
224
|
-
let prefetchUrl;
|
|
225
|
-
if (snapshot.prefetchEntry) {
|
|
226
|
-
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
227
|
-
}
|
|
228
|
-
const exist = loadingArray.find((loading) => loading.id === id);
|
|
229
|
-
if (exist) {
|
|
230
|
-
return options;
|
|
231
|
-
}
|
|
232
|
-
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
233
|
-
const projectExports = instance.getProjectExports();
|
|
234
|
-
if (projectExports instanceof Promise) {
|
|
235
|
-
await projectExports;
|
|
236
|
-
}
|
|
237
|
-
return Promise.resolve().then(() => {
|
|
238
|
-
const exports = instance.getExposeExports(id);
|
|
239
|
-
logger_default.info(
|
|
240
|
-
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`
|
|
241
|
-
);
|
|
242
|
-
const result = Object.keys(exports).map((k) => {
|
|
243
|
-
const value = instance.prefetch({
|
|
244
|
-
id,
|
|
245
|
-
functionId: k
|
|
246
|
-
});
|
|
247
|
-
const functionId = k;
|
|
248
|
-
return {
|
|
249
|
-
value,
|
|
250
|
-
functionId
|
|
251
|
-
};
|
|
252
|
-
});
|
|
253
|
-
return result;
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
loadingArray.push({
|
|
257
|
-
id,
|
|
258
|
-
promise
|
|
259
|
-
});
|
|
260
|
-
return options;
|
|
261
|
-
},
|
|
262
|
-
afterResolve(options) {
|
|
263
|
-
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
264
|
-
const snapshot = remoteSnapshot;
|
|
265
|
-
const { name } = remoteInfo;
|
|
266
|
-
const prefetchOptions = {
|
|
267
|
-
name,
|
|
268
|
-
remote: remoteInfo,
|
|
269
|
-
origin,
|
|
270
|
-
remoteSnapshot: snapshot
|
|
271
|
-
};
|
|
272
|
-
const signal = getSignalFromManifest(snapshot);
|
|
273
|
-
if (!signal) {
|
|
274
|
-
return options;
|
|
275
|
-
}
|
|
276
|
-
const inited = loadingArray.some((info) => info.id === id);
|
|
277
|
-
if (!inited) {
|
|
278
|
-
return options;
|
|
279
|
-
}
|
|
280
|
-
if (sharedFlag !== strategy) {
|
|
281
|
-
throw new Error(
|
|
282
|
-
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
283
|
-
);
|
|
284
|
-
}
|
|
285
|
-
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
286
|
-
let prefetchUrl;
|
|
287
|
-
if (snapshot.prefetchEntry) {
|
|
288
|
-
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
289
|
-
}
|
|
290
|
-
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
291
|
-
if (index !== -1) {
|
|
292
|
-
loadingArray.splice(index, 1);
|
|
293
|
-
}
|
|
294
|
-
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
295
|
-
const projectExports = instance.getProjectExports();
|
|
296
|
-
if (projectExports instanceof Promise) {
|
|
297
|
-
await projectExports;
|
|
298
|
-
}
|
|
299
|
-
return Promise.resolve().then(() => {
|
|
300
|
-
const exports = instance.getExposeExports(id);
|
|
301
|
-
logger_default.info(
|
|
302
|
-
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
|
|
303
|
-
);
|
|
304
|
-
const result = Object.keys(exports).map((k) => {
|
|
305
|
-
const value = instance.prefetch({
|
|
306
|
-
id,
|
|
307
|
-
functionId: k
|
|
308
|
-
});
|
|
309
|
-
const functionId = k;
|
|
310
|
-
return {
|
|
311
|
-
value,
|
|
312
|
-
functionId
|
|
313
|
-
};
|
|
314
|
-
});
|
|
315
|
-
return result;
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
loadingArray.push({
|
|
319
|
-
id,
|
|
320
|
-
promise
|
|
321
|
-
});
|
|
322
|
-
return options;
|
|
323
|
-
},
|
|
324
|
-
async onLoad(options) {
|
|
325
|
-
var _a2;
|
|
326
|
-
const { remote, id } = options;
|
|
327
|
-
const { name } = remote;
|
|
328
|
-
const promise = (_a2 = loadingArray.find((loading) => loading.id === id)) == null ? void 0 : _a2.promise;
|
|
329
|
-
if (promise) {
|
|
330
|
-
const prefetch = await promise;
|
|
331
|
-
const prefetchValue = prefetch.map((result) => result.value);
|
|
332
|
-
await Promise.all(prefetchValue);
|
|
333
|
-
const instance = MFDataPrefetch.getInstance(name);
|
|
334
|
-
prefetch.forEach((result) => {
|
|
335
|
-
const { value, functionId } = result;
|
|
336
|
-
instance.memorize(id + functionId, value);
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
return options;
|
|
340
|
-
},
|
|
341
|
-
beforeLoadShare(options) {
|
|
342
|
-
const shareInfo = options.shareInfo;
|
|
343
|
-
sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
|
|
344
|
-
return options;
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
348
|
-
0 && (module.exports = {
|
|
349
|
-
MFDataPrefetch,
|
|
350
|
-
prefetchPlugin
|
|
351
|
-
});
|
package/dist/plugin.d.ts
DELETED
package/dist/plugin.js
DELETED
|
@@ -1,347 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/plugin.ts
|
|
21
|
-
var plugin_exports = {};
|
|
22
|
-
__export(plugin_exports, {
|
|
23
|
-
default: () => plugin_default,
|
|
24
|
-
prefetchPlugin: () => prefetchPlugin
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(plugin_exports);
|
|
27
|
-
var import_sdk4 = require("@module-federation/sdk");
|
|
28
|
-
|
|
29
|
-
// src/common/runtime-utils.ts
|
|
30
|
-
var import_runtime = require("@module-federation/runtime");
|
|
31
|
-
var import_sdk = require("@module-federation/sdk");
|
|
32
|
-
var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
|
|
33
|
-
var compatGetPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/VmokPrefetch`);
|
|
34
|
-
var getSignalFromManifest = (remoteSnapshot) => {
|
|
35
|
-
if (!remoteSnapshot) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
if (!("prefetchEntry" in remoteSnapshot) && !("prefetchInterface" in remoteSnapshot)) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// src/prefetch.ts
|
|
48
|
-
var import_runtime2 = require("@module-federation/runtime");
|
|
49
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
50
|
-
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
51
|
-
var _a;
|
|
52
|
-
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
53
|
-
entryLoading: {},
|
|
54
|
-
instance: /* @__PURE__ */ new Map(),
|
|
55
|
-
__PREFETCH_EXPORTS__: {}
|
|
56
|
-
});
|
|
57
|
-
var MFDataPrefetch = class {
|
|
58
|
-
constructor(options) {
|
|
59
|
-
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
60
|
-
this.recordOutdate = {};
|
|
61
|
-
this._exports = {};
|
|
62
|
-
this._options = options;
|
|
63
|
-
this.global.instance.set(options.name, this);
|
|
64
|
-
}
|
|
65
|
-
get global() {
|
|
66
|
-
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
67
|
-
}
|
|
68
|
-
static getInstance(id) {
|
|
69
|
-
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
70
|
-
}
|
|
71
|
-
async loadEntry(entry) {
|
|
72
|
-
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
73
|
-
if (entry) {
|
|
74
|
-
const { buildVersion, globalName } = remoteSnapshot;
|
|
75
|
-
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
76
|
-
if (!this.global.entryLoading[uniqueKey]) {
|
|
77
|
-
this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
|
|
78
|
-
}
|
|
79
|
-
return this.global.entryLoading[uniqueKey];
|
|
80
|
-
} else {
|
|
81
|
-
const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
|
|
82
|
-
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
83
|
-
return (0, import_runtime2.getRemoteEntry)({
|
|
84
|
-
origin,
|
|
85
|
-
remoteInfo,
|
|
86
|
-
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
getProjectExports() {
|
|
91
|
-
var _a2;
|
|
92
|
-
if (Object.keys(this._exports).length > 0) {
|
|
93
|
-
return this._exports;
|
|
94
|
-
}
|
|
95
|
-
const { name } = this._options;
|
|
96
|
-
const exportsPromiseFn = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
97
|
-
const exportsPromise = typeof exportsPromiseFn === "function" ? exportsPromiseFn() : Promise.resolve({});
|
|
98
|
-
const resolve = exportsPromise.then(
|
|
99
|
-
(exports = {}) => {
|
|
100
|
-
const memory = {};
|
|
101
|
-
Object.keys(exports).forEach((key) => {
|
|
102
|
-
memory[key] = {};
|
|
103
|
-
const exportVal = exports[key];
|
|
104
|
-
Object.keys(exportVal).reduce(
|
|
105
|
-
(memo, current) => {
|
|
106
|
-
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
107
|
-
memo[current] = exportVal[current];
|
|
108
|
-
}
|
|
109
|
-
return memo;
|
|
110
|
-
},
|
|
111
|
-
memory[key]
|
|
112
|
-
);
|
|
113
|
-
});
|
|
114
|
-
this.memorizeExports(memory);
|
|
115
|
-
}
|
|
116
|
-
);
|
|
117
|
-
return resolve;
|
|
118
|
-
}
|
|
119
|
-
memorizeExports(exports) {
|
|
120
|
-
this._exports = exports;
|
|
121
|
-
}
|
|
122
|
-
getExposeExports(id) {
|
|
123
|
-
const prefetchId = getPrefetchId(id);
|
|
124
|
-
const compatId = compatGetPrefetchId(id);
|
|
125
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
126
|
-
return prefetchExports || {};
|
|
127
|
-
}
|
|
128
|
-
prefetch(prefetchOptions) {
|
|
129
|
-
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
130
|
-
let prefetchResult;
|
|
131
|
-
const prefetchId = getPrefetchId(id);
|
|
132
|
-
const compatId = compatGetPrefetchId(id);
|
|
133
|
-
const memorizeId = id + functionId;
|
|
134
|
-
const memory = this.prefetchMemory.get(memorizeId);
|
|
135
|
-
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
136
|
-
return memory;
|
|
137
|
-
}
|
|
138
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
139
|
-
if (!prefetchExports) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const executePrefetch = prefetchExports[functionId];
|
|
143
|
-
if (typeof executePrefetch === "function") {
|
|
144
|
-
if (refetchParams) {
|
|
145
|
-
prefetchResult = executePrefetch(refetchParams);
|
|
146
|
-
} else {
|
|
147
|
-
prefetchResult = executePrefetch();
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
throw new Error(
|
|
151
|
-
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
this.memorize(memorizeId, prefetchResult);
|
|
155
|
-
return prefetchResult;
|
|
156
|
-
}
|
|
157
|
-
memorize(id, value) {
|
|
158
|
-
this.prefetchMemory.set(id, value);
|
|
159
|
-
}
|
|
160
|
-
markOutdate(markOptions, isOutdate) {
|
|
161
|
-
const { id, functionId = "default" } = markOptions;
|
|
162
|
-
if (!this.recordOutdate[id]) {
|
|
163
|
-
this.recordOutdate[id] = {};
|
|
164
|
-
}
|
|
165
|
-
this.recordOutdate[id][functionId] = isOutdate;
|
|
166
|
-
}
|
|
167
|
-
checkOutdate(outdateOptions) {
|
|
168
|
-
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
169
|
-
if (typeof cacheStrategy === "function") {
|
|
170
|
-
return cacheStrategy();
|
|
171
|
-
}
|
|
172
|
-
if (!this.recordOutdate[id]) {
|
|
173
|
-
this.recordOutdate[id] = {};
|
|
174
|
-
}
|
|
175
|
-
if (this.recordOutdate[id][functionId]) {
|
|
176
|
-
this.markOutdate(
|
|
177
|
-
{
|
|
178
|
-
id,
|
|
179
|
-
functionId
|
|
180
|
-
},
|
|
181
|
-
false
|
|
182
|
-
);
|
|
183
|
-
return true;
|
|
184
|
-
} else {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
// src/logger/index.ts
|
|
191
|
-
var import_sdk3 = require("@module-federation/sdk");
|
|
192
|
-
var logger_default = new import_sdk3.Logger("[Module Federation Data Prefetch]");
|
|
193
|
-
|
|
194
|
-
// src/plugin.ts
|
|
195
|
-
var loadingArray = [];
|
|
196
|
-
var strategy = "loaded-first";
|
|
197
|
-
var sharedFlag = strategy;
|
|
198
|
-
var prefetchPlugin = () => ({
|
|
199
|
-
name: "data-prefetch-runtime-plugin",
|
|
200
|
-
initContainer(options) {
|
|
201
|
-
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
202
|
-
const snapshot = remoteSnapshot;
|
|
203
|
-
const { name } = remoteInfo;
|
|
204
|
-
const prefetchOptions = {
|
|
205
|
-
name,
|
|
206
|
-
remote: remoteInfo,
|
|
207
|
-
origin,
|
|
208
|
-
remoteSnapshot: snapshot
|
|
209
|
-
};
|
|
210
|
-
const signal = getSignalFromManifest(snapshot);
|
|
211
|
-
if (!signal) {
|
|
212
|
-
return options;
|
|
213
|
-
}
|
|
214
|
-
if (sharedFlag !== strategy) {
|
|
215
|
-
throw new Error(
|
|
216
|
-
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
220
|
-
let prefetchUrl;
|
|
221
|
-
if (snapshot.prefetchEntry) {
|
|
222
|
-
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
223
|
-
}
|
|
224
|
-
const exist = loadingArray.find((loading) => loading.id === id);
|
|
225
|
-
if (exist) {
|
|
226
|
-
return options;
|
|
227
|
-
}
|
|
228
|
-
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
229
|
-
const projectExports = instance.getProjectExports();
|
|
230
|
-
if (projectExports instanceof Promise) {
|
|
231
|
-
await projectExports;
|
|
232
|
-
}
|
|
233
|
-
return Promise.resolve().then(() => {
|
|
234
|
-
const exports = instance.getExposeExports(id);
|
|
235
|
-
logger_default.info(
|
|
236
|
-
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`
|
|
237
|
-
);
|
|
238
|
-
const result = Object.keys(exports).map((k) => {
|
|
239
|
-
const value = instance.prefetch({
|
|
240
|
-
id,
|
|
241
|
-
functionId: k
|
|
242
|
-
});
|
|
243
|
-
const functionId = k;
|
|
244
|
-
return {
|
|
245
|
-
value,
|
|
246
|
-
functionId
|
|
247
|
-
};
|
|
248
|
-
});
|
|
249
|
-
return result;
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
loadingArray.push({
|
|
253
|
-
id,
|
|
254
|
-
promise
|
|
255
|
-
});
|
|
256
|
-
return options;
|
|
257
|
-
},
|
|
258
|
-
afterResolve(options) {
|
|
259
|
-
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
260
|
-
const snapshot = remoteSnapshot;
|
|
261
|
-
const { name } = remoteInfo;
|
|
262
|
-
const prefetchOptions = {
|
|
263
|
-
name,
|
|
264
|
-
remote: remoteInfo,
|
|
265
|
-
origin,
|
|
266
|
-
remoteSnapshot: snapshot
|
|
267
|
-
};
|
|
268
|
-
const signal = getSignalFromManifest(snapshot);
|
|
269
|
-
if (!signal) {
|
|
270
|
-
return options;
|
|
271
|
-
}
|
|
272
|
-
const inited = loadingArray.some((info) => info.id === id);
|
|
273
|
-
if (!inited) {
|
|
274
|
-
return options;
|
|
275
|
-
}
|
|
276
|
-
if (sharedFlag !== strategy) {
|
|
277
|
-
throw new Error(
|
|
278
|
-
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
282
|
-
let prefetchUrl;
|
|
283
|
-
if (snapshot.prefetchEntry) {
|
|
284
|
-
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
285
|
-
}
|
|
286
|
-
const index = loadingArray.findIndex((loading) => loading.id === id);
|
|
287
|
-
if (index !== -1) {
|
|
288
|
-
loadingArray.splice(index, 1);
|
|
289
|
-
}
|
|
290
|
-
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
291
|
-
const projectExports = instance.getProjectExports();
|
|
292
|
-
if (projectExports instanceof Promise) {
|
|
293
|
-
await projectExports;
|
|
294
|
-
}
|
|
295
|
-
return Promise.resolve().then(() => {
|
|
296
|
-
const exports = instance.getExposeExports(id);
|
|
297
|
-
logger_default.info(
|
|
298
|
-
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
|
|
299
|
-
);
|
|
300
|
-
const result = Object.keys(exports).map((k) => {
|
|
301
|
-
const value = instance.prefetch({
|
|
302
|
-
id,
|
|
303
|
-
functionId: k
|
|
304
|
-
});
|
|
305
|
-
const functionId = k;
|
|
306
|
-
return {
|
|
307
|
-
value,
|
|
308
|
-
functionId
|
|
309
|
-
};
|
|
310
|
-
});
|
|
311
|
-
return result;
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
loadingArray.push({
|
|
315
|
-
id,
|
|
316
|
-
promise
|
|
317
|
-
});
|
|
318
|
-
return options;
|
|
319
|
-
},
|
|
320
|
-
async onLoad(options) {
|
|
321
|
-
var _a2;
|
|
322
|
-
const { remote, id } = options;
|
|
323
|
-
const { name } = remote;
|
|
324
|
-
const promise = (_a2 = loadingArray.find((loading) => loading.id === id)) == null ? void 0 : _a2.promise;
|
|
325
|
-
if (promise) {
|
|
326
|
-
const prefetch = await promise;
|
|
327
|
-
const prefetchValue = prefetch.map((result) => result.value);
|
|
328
|
-
await Promise.all(prefetchValue);
|
|
329
|
-
const instance = MFDataPrefetch.getInstance(name);
|
|
330
|
-
prefetch.forEach((result) => {
|
|
331
|
-
const { value, functionId } = result;
|
|
332
|
-
instance.memorize(id + functionId, value);
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
return options;
|
|
336
|
-
},
|
|
337
|
-
beforeLoadShare(options) {
|
|
338
|
-
const shareInfo = options.shareInfo;
|
|
339
|
-
sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
|
|
340
|
-
return options;
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
var plugin_default = prefetchPlugin;
|
|
344
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
345
|
-
0 && (module.exports = {
|
|
346
|
-
prefetchPlugin
|
|
347
|
-
});
|