@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/react/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { p as prefetchOptions } from '../prefetch-c08be471.js';
|
|
2
|
-
import '@module-federation/runtime';
|
|
3
|
-
import '@module-federation/sdk';
|
|
4
|
-
import '@module-federation/runtime/types';
|
|
5
|
-
|
|
6
|
-
type refetchParams = any;
|
|
7
|
-
type prefetchReturnType<T> = [
|
|
8
|
-
Promise<T>,
|
|
9
|
-
(refetchParams?: refetchParams) => void
|
|
10
|
-
];
|
|
11
|
-
type UsePrefetchOptions = prefetchOptions & {
|
|
12
|
-
deferId?: string;
|
|
13
|
-
};
|
|
14
|
-
declare const usePrefetch: <T>(options: UsePrefetchOptions) => prefetchReturnType<T>;
|
|
15
|
-
|
|
16
|
-
export { usePrefetch };
|
package/dist/react/index.js
DELETED
|
@@ -1,287 +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/react/index.ts
|
|
21
|
-
var react_exports = {};
|
|
22
|
-
__export(react_exports, {
|
|
23
|
-
usePrefetch: () => usePrefetch
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(react_exports);
|
|
26
|
-
|
|
27
|
-
// src/react/hooks.ts
|
|
28
|
-
var import_react2 = require("react");
|
|
29
|
-
|
|
30
|
-
// src/logger/index.ts
|
|
31
|
-
var import_sdk = require("@module-federation/sdk");
|
|
32
|
-
var logger_default = new import_sdk.Logger("[Module Federation Data Prefetch]");
|
|
33
|
-
|
|
34
|
-
// src/prefetch.ts
|
|
35
|
-
var import_runtime2 = require("@module-federation/runtime");
|
|
36
|
-
var import_sdk3 = require("@module-federation/sdk");
|
|
37
|
-
|
|
38
|
-
// src/common/runtime-utils.ts
|
|
39
|
-
var import_runtime = require("@module-federation/runtime");
|
|
40
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
41
|
-
var getScope = () => {
|
|
42
|
-
return (0, import_runtime.getInstance)().options.name;
|
|
43
|
-
};
|
|
44
|
-
var getPrefetchId = (id) => (0, import_sdk2.encodeName)(`${id}/${import_sdk2.MFPrefetchCommon.identifier}`);
|
|
45
|
-
var compatGetPrefetchId = (id) => (0, import_sdk2.encodeName)(`${id}/VmokPrefetch`);
|
|
46
|
-
|
|
47
|
-
// src/prefetch.ts
|
|
48
|
-
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
49
|
-
var _a;
|
|
50
|
-
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
51
|
-
entryLoading: {},
|
|
52
|
-
instance: /* @__PURE__ */ new Map(),
|
|
53
|
-
__PREFETCH_EXPORTS__: {}
|
|
54
|
-
});
|
|
55
|
-
var MFDataPrefetch = class {
|
|
56
|
-
constructor(options) {
|
|
57
|
-
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
58
|
-
this.recordOutdate = {};
|
|
59
|
-
this._exports = {};
|
|
60
|
-
this._options = options;
|
|
61
|
-
this.global.instance.set(options.name, this);
|
|
62
|
-
}
|
|
63
|
-
get global() {
|
|
64
|
-
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
65
|
-
}
|
|
66
|
-
static getInstance(id) {
|
|
67
|
-
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
68
|
-
}
|
|
69
|
-
async loadEntry(entry) {
|
|
70
|
-
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
71
|
-
if (entry) {
|
|
72
|
-
const { buildVersion, globalName } = remoteSnapshot;
|
|
73
|
-
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
74
|
-
if (!this.global.entryLoading[uniqueKey]) {
|
|
75
|
-
this.global.entryLoading[uniqueKey] = (0, import_sdk3.loadScript)(entry, {});
|
|
76
|
-
}
|
|
77
|
-
return this.global.entryLoading[uniqueKey];
|
|
78
|
-
} else {
|
|
79
|
-
const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
|
|
80
|
-
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
81
|
-
return (0, import_runtime2.getRemoteEntry)({
|
|
82
|
-
origin,
|
|
83
|
-
remoteInfo,
|
|
84
|
-
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
getProjectExports() {
|
|
89
|
-
var _a2;
|
|
90
|
-
if (Object.keys(this._exports).length > 0) {
|
|
91
|
-
return this._exports;
|
|
92
|
-
}
|
|
93
|
-
const { name } = this._options;
|
|
94
|
-
const exportsPromiseFn = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
95
|
-
const exportsPromise = typeof exportsPromiseFn === "function" ? exportsPromiseFn() : Promise.resolve({});
|
|
96
|
-
const resolve = exportsPromise.then(
|
|
97
|
-
(exports = {}) => {
|
|
98
|
-
const memory = {};
|
|
99
|
-
Object.keys(exports).forEach((key) => {
|
|
100
|
-
memory[key] = {};
|
|
101
|
-
const exportVal = exports[key];
|
|
102
|
-
Object.keys(exportVal).reduce(
|
|
103
|
-
(memo, current) => {
|
|
104
|
-
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
105
|
-
memo[current] = exportVal[current];
|
|
106
|
-
}
|
|
107
|
-
return memo;
|
|
108
|
-
},
|
|
109
|
-
memory[key]
|
|
110
|
-
);
|
|
111
|
-
});
|
|
112
|
-
this.memorizeExports(memory);
|
|
113
|
-
}
|
|
114
|
-
);
|
|
115
|
-
return resolve;
|
|
116
|
-
}
|
|
117
|
-
memorizeExports(exports) {
|
|
118
|
-
this._exports = exports;
|
|
119
|
-
}
|
|
120
|
-
getExposeExports(id) {
|
|
121
|
-
const prefetchId = getPrefetchId(id);
|
|
122
|
-
const compatId = compatGetPrefetchId(id);
|
|
123
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
124
|
-
return prefetchExports || {};
|
|
125
|
-
}
|
|
126
|
-
prefetch(prefetchOptions) {
|
|
127
|
-
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
128
|
-
let prefetchResult;
|
|
129
|
-
const prefetchId = getPrefetchId(id);
|
|
130
|
-
const compatId = compatGetPrefetchId(id);
|
|
131
|
-
const memorizeId = id + functionId;
|
|
132
|
-
const memory = this.prefetchMemory.get(memorizeId);
|
|
133
|
-
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
134
|
-
return memory;
|
|
135
|
-
}
|
|
136
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
137
|
-
if (!prefetchExports) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
const executePrefetch = prefetchExports[functionId];
|
|
141
|
-
if (typeof executePrefetch === "function") {
|
|
142
|
-
if (refetchParams) {
|
|
143
|
-
prefetchResult = executePrefetch(refetchParams);
|
|
144
|
-
} else {
|
|
145
|
-
prefetchResult = executePrefetch();
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
throw new Error(
|
|
149
|
-
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
this.memorize(memorizeId, prefetchResult);
|
|
153
|
-
return prefetchResult;
|
|
154
|
-
}
|
|
155
|
-
memorize(id, value) {
|
|
156
|
-
this.prefetchMemory.set(id, value);
|
|
157
|
-
}
|
|
158
|
-
markOutdate(markOptions, isOutdate) {
|
|
159
|
-
const { id, functionId = "default" } = markOptions;
|
|
160
|
-
if (!this.recordOutdate[id]) {
|
|
161
|
-
this.recordOutdate[id] = {};
|
|
162
|
-
}
|
|
163
|
-
this.recordOutdate[id][functionId] = isOutdate;
|
|
164
|
-
}
|
|
165
|
-
checkOutdate(outdateOptions) {
|
|
166
|
-
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
167
|
-
if (typeof cacheStrategy === "function") {
|
|
168
|
-
return cacheStrategy();
|
|
169
|
-
}
|
|
170
|
-
if (!this.recordOutdate[id]) {
|
|
171
|
-
this.recordOutdate[id] = {};
|
|
172
|
-
}
|
|
173
|
-
if (this.recordOutdate[id][functionId]) {
|
|
174
|
-
this.markOutdate(
|
|
175
|
-
{
|
|
176
|
-
id,
|
|
177
|
-
functionId
|
|
178
|
-
},
|
|
179
|
-
false
|
|
180
|
-
);
|
|
181
|
-
return true;
|
|
182
|
-
} else {
|
|
183
|
-
return false;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// src/universal/index.ts
|
|
189
|
-
function prefetch(options) {
|
|
190
|
-
const { id, functionId = "default" } = options;
|
|
191
|
-
const mfScope = getScope();
|
|
192
|
-
const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
|
|
193
|
-
name: mfScope
|
|
194
|
-
});
|
|
195
|
-
const res = prefetchInstance.getProjectExports();
|
|
196
|
-
if (res instanceof Promise) {
|
|
197
|
-
const promise = res.then(() => {
|
|
198
|
-
const result = prefetchInstance.prefetch(options);
|
|
199
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
200
|
-
return result;
|
|
201
|
-
});
|
|
202
|
-
return promise;
|
|
203
|
-
} else {
|
|
204
|
-
const result = prefetchInstance.prefetch(options);
|
|
205
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// src/react/utils.ts
|
|
211
|
-
var import_react = require("react");
|
|
212
|
-
var useFirstMounted = () => {
|
|
213
|
-
const ref = (0, import_react.useRef)(true);
|
|
214
|
-
(0, import_react.useEffect)(() => {
|
|
215
|
-
ref.current = false;
|
|
216
|
-
}, []);
|
|
217
|
-
return ref.current;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
// src/react/hooks.ts
|
|
221
|
-
var usePrefetch = (options) => {
|
|
222
|
-
const isFirstMounted = useFirstMounted();
|
|
223
|
-
if (isFirstMounted) {
|
|
224
|
-
const startTiming = performance.now();
|
|
225
|
-
logger_default.info(
|
|
226
|
-
`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || "default"} - ${startTiming}`
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
const { id, functionId, deferId } = options;
|
|
230
|
-
const prefetchInfo = {
|
|
231
|
-
id,
|
|
232
|
-
functionId
|
|
233
|
-
};
|
|
234
|
-
const mfScope = getScope();
|
|
235
|
-
let state;
|
|
236
|
-
const prefetchResult = prefetch(options);
|
|
237
|
-
if (deferId) {
|
|
238
|
-
if (prefetchResult instanceof Promise) {
|
|
239
|
-
state = prefetchResult.then(
|
|
240
|
-
(deferredData) => deferredData.data[deferId]
|
|
241
|
-
);
|
|
242
|
-
} else {
|
|
243
|
-
state = prefetchResult.data[deferId];
|
|
244
|
-
}
|
|
245
|
-
} else {
|
|
246
|
-
state = prefetchResult;
|
|
247
|
-
}
|
|
248
|
-
const [prefetchState, setPrefetchState] = (0, import_react2.useState)(
|
|
249
|
-
state
|
|
250
|
-
);
|
|
251
|
-
const prefetchInstance = MFDataPrefetch.getInstance(mfScope);
|
|
252
|
-
(0, import_react2.useEffect)(() => {
|
|
253
|
-
const useEffectTiming = performance.now();
|
|
254
|
-
logger_default.info(
|
|
255
|
-
`3. Start Execute UseEffect: ${options.id} - ${options.functionId || "default"} - ${useEffectTiming}`
|
|
256
|
-
);
|
|
257
|
-
return () => {
|
|
258
|
-
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
259
|
-
};
|
|
260
|
-
}, []);
|
|
261
|
-
const refreshExecutor = (refetchParams) => {
|
|
262
|
-
const refetchOptions = {
|
|
263
|
-
...options
|
|
264
|
-
};
|
|
265
|
-
if (refetchParams) {
|
|
266
|
-
refetchOptions.refetchParams = refetchParams;
|
|
267
|
-
}
|
|
268
|
-
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
269
|
-
const newVal = prefetch(refetchOptions);
|
|
270
|
-
let newState;
|
|
271
|
-
if (deferId) {
|
|
272
|
-
if (newVal instanceof Promise) {
|
|
273
|
-
newState = newVal.then((deferredData) => deferredData.data[deferId]);
|
|
274
|
-
} else {
|
|
275
|
-
newState = newVal.data[deferId];
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
newState = newVal;
|
|
279
|
-
}
|
|
280
|
-
setPrefetchState(newState);
|
|
281
|
-
};
|
|
282
|
-
return [prefetchState, refreshExecutor];
|
|
283
|
-
};
|
|
284
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
285
|
-
0 && (module.exports = {
|
|
286
|
-
usePrefetch
|
|
287
|
-
});
|
package/dist/shared/index.d.ts
DELETED
package/dist/shared/index.js
DELETED
|
@@ -1,48 +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/shared/index.ts
|
|
21
|
-
var shared_exports = {};
|
|
22
|
-
__export(shared_exports, {
|
|
23
|
-
default: () => shared_default
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(shared_exports);
|
|
26
|
-
var sharedStrategy = () => ({
|
|
27
|
-
name: "shared-strategy",
|
|
28
|
-
beforeInit(args) {
|
|
29
|
-
const { userOptions } = args;
|
|
30
|
-
const shared = userOptions.shared;
|
|
31
|
-
if (shared) {
|
|
32
|
-
Object.keys(shared).forEach((sharedKey) => {
|
|
33
|
-
const sharedConfigs = shared[sharedKey];
|
|
34
|
-
const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [sharedConfigs];
|
|
35
|
-
arraySharedConfigs.forEach((s) => {
|
|
36
|
-
s.strategy = "loaded-first";
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
console.warn(
|
|
40
|
-
`[Module Federation Data Prefetch]: Your shared strategy is set to 'loaded-first', this is a necessary condition for data prefetch`
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
return args;
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
var shared_default = sharedStrategy;
|
|
47
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
-
0 && (module.exports = {});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { p as prefetchOptions } from '../prefetch-c08be471.js';
|
|
2
|
-
import '@module-federation/runtime';
|
|
3
|
-
import '@module-federation/sdk';
|
|
4
|
-
import '@module-federation/runtime/types';
|
|
5
|
-
|
|
6
|
-
declare function prefetch(options: prefetchOptions): Promise<any>;
|
|
7
|
-
|
|
8
|
-
export { prefetch };
|
package/dist/universal/index.js
DELETED
|
@@ -1,205 +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/universal/index.ts
|
|
21
|
-
var universal_exports = {};
|
|
22
|
-
__export(universal_exports, {
|
|
23
|
-
prefetch: () => prefetch
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(universal_exports);
|
|
26
|
-
|
|
27
|
-
// src/prefetch.ts
|
|
28
|
-
var import_runtime2 = require("@module-federation/runtime");
|
|
29
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
30
|
-
|
|
31
|
-
// src/common/runtime-utils.ts
|
|
32
|
-
var import_runtime = require("@module-federation/runtime");
|
|
33
|
-
var import_sdk = require("@module-federation/sdk");
|
|
34
|
-
var getScope = () => {
|
|
35
|
-
return (0, import_runtime.getInstance)().options.name;
|
|
36
|
-
};
|
|
37
|
-
var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
|
|
38
|
-
var compatGetPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/VmokPrefetch`);
|
|
39
|
-
|
|
40
|
-
// src/prefetch.ts
|
|
41
|
-
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
42
|
-
var _a;
|
|
43
|
-
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
44
|
-
entryLoading: {},
|
|
45
|
-
instance: /* @__PURE__ */ new Map(),
|
|
46
|
-
__PREFETCH_EXPORTS__: {}
|
|
47
|
-
});
|
|
48
|
-
var MFDataPrefetch = class {
|
|
49
|
-
constructor(options) {
|
|
50
|
-
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
51
|
-
this.recordOutdate = {};
|
|
52
|
-
this._exports = {};
|
|
53
|
-
this._options = options;
|
|
54
|
-
this.global.instance.set(options.name, this);
|
|
55
|
-
}
|
|
56
|
-
get global() {
|
|
57
|
-
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
58
|
-
}
|
|
59
|
-
static getInstance(id) {
|
|
60
|
-
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
61
|
-
}
|
|
62
|
-
async loadEntry(entry) {
|
|
63
|
-
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
64
|
-
if (entry) {
|
|
65
|
-
const { buildVersion, globalName } = remoteSnapshot;
|
|
66
|
-
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
67
|
-
if (!this.global.entryLoading[uniqueKey]) {
|
|
68
|
-
this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
|
|
69
|
-
}
|
|
70
|
-
return this.global.entryLoading[uniqueKey];
|
|
71
|
-
} else {
|
|
72
|
-
const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
|
|
73
|
-
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
74
|
-
return (0, import_runtime2.getRemoteEntry)({
|
|
75
|
-
origin,
|
|
76
|
-
remoteInfo,
|
|
77
|
-
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
getProjectExports() {
|
|
82
|
-
var _a2;
|
|
83
|
-
if (Object.keys(this._exports).length > 0) {
|
|
84
|
-
return this._exports;
|
|
85
|
-
}
|
|
86
|
-
const { name } = this._options;
|
|
87
|
-
const exportsPromiseFn = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
88
|
-
const exportsPromise = typeof exportsPromiseFn === "function" ? exportsPromiseFn() : Promise.resolve({});
|
|
89
|
-
const resolve = exportsPromise.then(
|
|
90
|
-
(exports = {}) => {
|
|
91
|
-
const memory = {};
|
|
92
|
-
Object.keys(exports).forEach((key) => {
|
|
93
|
-
memory[key] = {};
|
|
94
|
-
const exportVal = exports[key];
|
|
95
|
-
Object.keys(exportVal).reduce(
|
|
96
|
-
(memo, current) => {
|
|
97
|
-
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
98
|
-
memo[current] = exportVal[current];
|
|
99
|
-
}
|
|
100
|
-
return memo;
|
|
101
|
-
},
|
|
102
|
-
memory[key]
|
|
103
|
-
);
|
|
104
|
-
});
|
|
105
|
-
this.memorizeExports(memory);
|
|
106
|
-
}
|
|
107
|
-
);
|
|
108
|
-
return resolve;
|
|
109
|
-
}
|
|
110
|
-
memorizeExports(exports) {
|
|
111
|
-
this._exports = exports;
|
|
112
|
-
}
|
|
113
|
-
getExposeExports(id) {
|
|
114
|
-
const prefetchId = getPrefetchId(id);
|
|
115
|
-
const compatId = compatGetPrefetchId(id);
|
|
116
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
117
|
-
return prefetchExports || {};
|
|
118
|
-
}
|
|
119
|
-
prefetch(prefetchOptions) {
|
|
120
|
-
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
121
|
-
let prefetchResult;
|
|
122
|
-
const prefetchId = getPrefetchId(id);
|
|
123
|
-
const compatId = compatGetPrefetchId(id);
|
|
124
|
-
const memorizeId = id + functionId;
|
|
125
|
-
const memory = this.prefetchMemory.get(memorizeId);
|
|
126
|
-
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
127
|
-
return memory;
|
|
128
|
-
}
|
|
129
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
130
|
-
if (!prefetchExports) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const executePrefetch = prefetchExports[functionId];
|
|
134
|
-
if (typeof executePrefetch === "function") {
|
|
135
|
-
if (refetchParams) {
|
|
136
|
-
prefetchResult = executePrefetch(refetchParams);
|
|
137
|
-
} else {
|
|
138
|
-
prefetchResult = executePrefetch();
|
|
139
|
-
}
|
|
140
|
-
} else {
|
|
141
|
-
throw new Error(
|
|
142
|
-
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
this.memorize(memorizeId, prefetchResult);
|
|
146
|
-
return prefetchResult;
|
|
147
|
-
}
|
|
148
|
-
memorize(id, value) {
|
|
149
|
-
this.prefetchMemory.set(id, value);
|
|
150
|
-
}
|
|
151
|
-
markOutdate(markOptions, isOutdate) {
|
|
152
|
-
const { id, functionId = "default" } = markOptions;
|
|
153
|
-
if (!this.recordOutdate[id]) {
|
|
154
|
-
this.recordOutdate[id] = {};
|
|
155
|
-
}
|
|
156
|
-
this.recordOutdate[id][functionId] = isOutdate;
|
|
157
|
-
}
|
|
158
|
-
checkOutdate(outdateOptions) {
|
|
159
|
-
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
160
|
-
if (typeof cacheStrategy === "function") {
|
|
161
|
-
return cacheStrategy();
|
|
162
|
-
}
|
|
163
|
-
if (!this.recordOutdate[id]) {
|
|
164
|
-
this.recordOutdate[id] = {};
|
|
165
|
-
}
|
|
166
|
-
if (this.recordOutdate[id][functionId]) {
|
|
167
|
-
this.markOutdate(
|
|
168
|
-
{
|
|
169
|
-
id,
|
|
170
|
-
functionId
|
|
171
|
-
},
|
|
172
|
-
false
|
|
173
|
-
);
|
|
174
|
-
return true;
|
|
175
|
-
} else {
|
|
176
|
-
return false;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
// src/universal/index.ts
|
|
182
|
-
function prefetch(options) {
|
|
183
|
-
const { id, functionId = "default" } = options;
|
|
184
|
-
const mfScope = getScope();
|
|
185
|
-
const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
|
|
186
|
-
name: mfScope
|
|
187
|
-
});
|
|
188
|
-
const res = prefetchInstance.getProjectExports();
|
|
189
|
-
if (res instanceof Promise) {
|
|
190
|
-
const promise = res.then(() => {
|
|
191
|
-
const result = prefetchInstance.prefetch(options);
|
|
192
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
193
|
-
return result;
|
|
194
|
-
});
|
|
195
|
-
return promise;
|
|
196
|
-
} else {
|
|
197
|
-
const result = prefetchInstance.prefetch(options);
|
|
198
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
199
|
-
return result;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
203
|
-
0 && (module.exports = {
|
|
204
|
-
prefetch
|
|
205
|
-
});
|