@module-federation/data-prefetch 0.6.6 → 0.6.7

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