@module-federation/data-prefetch 0.6.7 → 0.6.9

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.
@@ -4,348 +4,139 @@ var runtime = require('@module-federation/runtime');
4
4
  var sdk = require('@module-federation/sdk');
5
5
  var runtimeUtils = require('./runtime-utils.cjs.js');
6
6
 
7
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
8
- try {
9
- var info = gen[key](arg);
10
- var value = info.value;
11
- } catch (error) {
12
- reject(error);
13
- return;
7
+ var // @ts-ignore init global variable for test
8
+ _globalThis, _globalThis___FEDERATION__;
9
+ var ___FEDERATION__;
10
+ (___FEDERATION__ = (_globalThis = globalThis).__FEDERATION__) != null ? ___FEDERATION__ : _globalThis.__FEDERATION__ = {};
11
+ var ___PREFETCH__;
12
+ (___PREFETCH__ = (_globalThis___FEDERATION__ = globalThis.__FEDERATION__).__PREFETCH__) != null ? ___PREFETCH__ : _globalThis___FEDERATION__.__PREFETCH__ = {
13
+ entryLoading: {},
14
+ instance: new Map(),
15
+ __PREFETCH_EXPORTS__: {}
16
+ };
17
+ class MFDataPrefetch {
18
+ get global() {
19
+ return globalThis.__FEDERATION__.__PREFETCH__;
14
20
  }
15
- if (info.done) {
16
- resolve(value);
17
- } else {
18
- Promise.resolve(value).then(_next, _throw);
21
+ static getInstance(id) {
22
+ return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
19
23
  }
20
- }
21
- function _async_to_generator(fn) {
22
- return function() {
23
- var self = this, args = arguments;
24
- return new Promise(function(resolve, reject) {
25
- var gen = fn.apply(self, args);
26
- function _next(value) {
27
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
28
- }
29
- function _throw(err) {
30
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
24
+ async loadEntry(entry) {
25
+ const { name, remoteSnapshot, remote, origin } = this._options;
26
+ if (entry) {
27
+ const { buildVersion, globalName } = remoteSnapshot;
28
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
29
+ if (!this.global.entryLoading[uniqueKey]) {
30
+ this.global.entryLoading[uniqueKey] = sdk.loadScript(entry, {});
31
31
  }
32
- _next(undefined);
33
- });
34
- };
35
- }
36
- function _class_call_check(instance, Constructor) {
37
- if (!(instance instanceof Constructor)) {
38
- throw new TypeError("Cannot call a class as a function");
39
- }
40
- }
41
- function _defineProperties(target, props) {
42
- for(var i = 0; i < props.length; i++){
43
- var descriptor = props[i];
44
- descriptor.enumerable = descriptor.enumerable || false;
45
- descriptor.configurable = true;
46
- if ("value" in descriptor) descriptor.writable = true;
47
- Object.defineProperty(target, descriptor.key, descriptor);
32
+ return this.global.entryLoading[uniqueKey];
33
+ } else {
34
+ const remoteInfo = runtime.getRemoteInfo(remote);
35
+ const module = origin.moduleCache.get(remoteInfo.name);
36
+ return runtime.getRemoteEntry({
37
+ origin: origin,
38
+ remoteInfo,
39
+ remoteEntryExports: module ? module.remoteEntryExports : undefined
40
+ });
41
+ }
48
42
  }
49
- }
50
- function _create_class(Constructor, protoProps, staticProps) {
51
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
52
- if (staticProps) _defineProperties(Constructor, staticProps);
53
- return Constructor;
54
- }
55
- function _define_property(obj, key, value) {
56
- if (key in obj) {
57
- Object.defineProperty(obj, key, {
58
- value: value,
59
- enumerable: true,
60
- configurable: true,
61
- writable: true
43
+ getProjectExports() {
44
+ var _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__;
45
+ if (Object.keys(this._exports).length > 0) {
46
+ return this._exports;
47
+ }
48
+ const { name } = this._options;
49
+ const exportsPromiseFn = (_globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__ = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__[name];
50
+ const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : Promise.resolve({});
51
+ const resolve = exportsPromise.then((exports = {})=>{
52
+ // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
53
+ // Not all functions should be directly identified as prefetch functions
54
+ const memory = {};
55
+ Object.keys(exports).forEach((key)=>{
56
+ memory[key] = {};
57
+ const exportVal = exports[key];
58
+ Object.keys(exportVal).reduce((memo, current)=>{
59
+ if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
60
+ memo[current] = exportVal[current];
61
+ }
62
+ return memo;
63
+ }, memory[key]);
64
+ });
65
+ this.memorizeExports(memory);
62
66
  });
63
- } else {
64
- obj[key] = value;
67
+ return resolve;
65
68
  }
66
- return obj;
67
- }
68
- function _ts_generator(thisArg, body) {
69
- var f, y, t, g, _ = {
70
- label: 0,
71
- sent: function() {
72
- if (t[0] & 1) throw t[1];
73
- return t[1];
74
- },
75
- trys: [],
76
- ops: []
77
- };
78
- return g = {
79
- next: verb(0),
80
- "throw": verb(1),
81
- "return": verb(2)
82
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
83
- return this;
84
- }), g;
85
- function verb(n) {
86
- return function(v) {
87
- return step([
88
- n,
89
- v
90
- ]);
91
- };
69
+ memorizeExports(exports) {
70
+ this._exports = exports;
92
71
  }
93
- function step(op) {
94
- if (f) throw new TypeError("Generator is already executing.");
95
- while(_)try {
96
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
97
- if (y = 0, t) op = [
98
- op[0] & 2,
99
- t.value
100
- ];
101
- switch(op[0]){
102
- case 0:
103
- case 1:
104
- t = op;
105
- break;
106
- case 4:
107
- _.label++;
108
- return {
109
- value: op[1],
110
- done: false
111
- };
112
- case 5:
113
- _.label++;
114
- y = op[1];
115
- op = [
116
- 0
117
- ];
118
- continue;
119
- case 7:
120
- op = _.ops.pop();
121
- _.trys.pop();
122
- continue;
123
- default:
124
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
125
- _ = 0;
126
- continue;
127
- }
128
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
129
- _.label = op[1];
130
- break;
131
- }
132
- if (op[0] === 6 && _.label < t[1]) {
133
- _.label = t[1];
134
- t = op;
135
- break;
136
- }
137
- if (t && _.label < t[2]) {
138
- _.label = t[2];
139
- _.ops.push(op);
140
- break;
141
- }
142
- if (t[2]) _.ops.pop();
143
- _.trys.pop();
144
- continue;
72
+ getExposeExports(id) {
73
+ const prefetchId = runtimeUtils.getPrefetchId(id);
74
+ const compatId = runtimeUtils.compatGetPrefetchId(id);
75
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
76
+ return prefetchExports || {};
77
+ }
78
+ prefetch(prefetchOptions) {
79
+ const { id, functionId = 'default', refetchParams } = prefetchOptions;
80
+ let prefetchResult;
81
+ const prefetchId = runtimeUtils.getPrefetchId(id);
82
+ const compatId = runtimeUtils.compatGetPrefetchId(id);
83
+ const memorizeId = id + functionId;
84
+ const memory = this.prefetchMemory.get(memorizeId);
85
+ if (!this.checkOutdate(prefetchOptions) && memory) {
86
+ return memory;
87
+ }
88
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
89
+ if (!prefetchExports) {
90
+ return;
91
+ }
92
+ const executePrefetch = prefetchExports[functionId];
93
+ if (typeof executePrefetch === 'function') {
94
+ if (refetchParams) {
95
+ prefetchResult = executePrefetch(refetchParams);
96
+ } else {
97
+ prefetchResult = executePrefetch();
145
98
  }
146
- op = body.call(thisArg, _);
147
- } catch (e) {
148
- op = [
149
- 6,
150
- e
151
- ];
152
- y = 0;
153
- } finally{
154
- f = t = 0;
99
+ } else {
100
+ throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
155
101
  }
156
- if (op[0] & 5) throw op[1];
157
- return {
158
- value: op[0] ? op[1] : void 0,
159
- done: true
160
- };
102
+ this.memorize(memorizeId, prefetchResult);
103
+ return prefetchResult;
161
104
  }
162
- }
163
- var // @ts-ignore init global variable for test
164
- _globalThis, _globalThis___FEDERATION__;
165
- var ___FEDERATION__;
166
- (___FEDERATION__ = (_globalThis = globalThis).__FEDERATION__) !== null && ___FEDERATION__ !== void 0 ? ___FEDERATION__ : _globalThis.__FEDERATION__ = {};
167
- var ___PREFETCH__;
168
- (___PREFETCH__ = (_globalThis___FEDERATION__ = globalThis.__FEDERATION__).__PREFETCH__) !== null && ___PREFETCH__ !== void 0 ? ___PREFETCH__ : _globalThis___FEDERATION__.__PREFETCH__ = {
169
- entryLoading: {},
170
- instance: new Map(),
171
- __PREFETCH_EXPORTS__: {}
172
- };
173
- var MFDataPrefetch = /*#__PURE__*/ function() {
174
- function MFDataPrefetch(options) {
175
- _class_call_check(this, MFDataPrefetch);
176
- _define_property(this, "prefetchMemory", void 0);
177
- _define_property(this, "recordOutdate", void 0);
178
- _define_property(this, "_exports", void 0);
179
- _define_property(this, "_options", void 0);
105
+ memorize(id, value) {
106
+ this.prefetchMemory.set(id, value);
107
+ }
108
+ markOutdate(markOptions, isOutdate) {
109
+ const { id, functionId = 'default' } = markOptions;
110
+ if (!this.recordOutdate[id]) {
111
+ this.recordOutdate[id] = {};
112
+ }
113
+ this.recordOutdate[id][functionId] = isOutdate;
114
+ }
115
+ checkOutdate(outdateOptions) {
116
+ const { id, functionId = 'default', cacheStrategy } = outdateOptions;
117
+ if (typeof cacheStrategy === 'function') {
118
+ return cacheStrategy();
119
+ }
120
+ if (!this.recordOutdate[id]) {
121
+ this.recordOutdate[id] = {};
122
+ }
123
+ if (this.recordOutdate[id][functionId]) {
124
+ this.markOutdate({
125
+ id,
126
+ functionId
127
+ }, false);
128
+ return true;
129
+ } else {
130
+ return false;
131
+ }
132
+ }
133
+ constructor(options){
180
134
  this.prefetchMemory = new Map();
181
135
  this.recordOutdate = {};
182
136
  this._exports = {};
183
137
  this._options = options;
184
138
  this.global.instance.set(options.name, this);
185
139
  }
186
- _create_class(MFDataPrefetch, [
187
- {
188
- key: "global",
189
- get: function get() {
190
- return globalThis.__FEDERATION__.__PREFETCH__;
191
- }
192
- },
193
- {
194
- key: "loadEntry",
195
- value: function loadEntry(entry) {
196
- var _this = this;
197
- return _async_to_generator(function() {
198
- var _this__options, name, remoteSnapshot, remote, origin, buildVersion, globalName, uniqueKey, remoteInfo, module;
199
- return _ts_generator(this, function(_state) {
200
- _this__options = _this._options, name = _this__options.name, remoteSnapshot = _this__options.remoteSnapshot, remote = _this__options.remote, origin = _this__options.origin;
201
- if (entry) {
202
- buildVersion = remoteSnapshot.buildVersion, globalName = remoteSnapshot.globalName;
203
- uniqueKey = globalName || "".concat(name, ":").concat(buildVersion);
204
- if (!_this.global.entryLoading[uniqueKey]) {
205
- _this.global.entryLoading[uniqueKey] = sdk.loadScript(entry, {});
206
- }
207
- return [
208
- 2,
209
- _this.global.entryLoading[uniqueKey]
210
- ];
211
- } else {
212
- remoteInfo = runtime.getRemoteInfo(remote);
213
- module = origin.moduleCache.get(remoteInfo.name);
214
- return [
215
- 2,
216
- runtime.getRemoteEntry({
217
- origin: origin,
218
- remoteInfo: remoteInfo,
219
- remoteEntryExports: module ? module.remoteEntryExports : undefined
220
- })
221
- ];
222
- }
223
- });
224
- })();
225
- }
226
- },
227
- {
228
- key: "getProjectExports",
229
- value: function getProjectExports() {
230
- var _this = this;
231
- var _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__;
232
- if (Object.keys(this._exports).length > 0) {
233
- return this._exports;
234
- }
235
- var name = this._options.name;
236
- var exportsPromiseFn = (_globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__ = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) === null || _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__[name];
237
- var exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : Promise.resolve({});
238
- var resolve = exportsPromise.then(function() {
239
- var exports = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
240
- // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
241
- // Not all functions should be directly identified as prefetch functions
242
- var memory = {};
243
- Object.keys(exports).forEach(function(key) {
244
- memory[key] = {};
245
- var exportVal = exports[key];
246
- Object.keys(exportVal).reduce(function(memo, current) {
247
- if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
248
- memo[current] = exportVal[current];
249
- }
250
- return memo;
251
- }, memory[key]);
252
- });
253
- _this.memorizeExports(memory);
254
- });
255
- return resolve;
256
- }
257
- },
258
- {
259
- key: "memorizeExports",
260
- value: function memorizeExports(exports) {
261
- this._exports = exports;
262
- }
263
- },
264
- {
265
- key: "getExposeExports",
266
- value: function getExposeExports(id) {
267
- var prefetchId = runtimeUtils.getPrefetchId(id);
268
- var compatId = runtimeUtils.compatGetPrefetchId(id);
269
- var prefetchExports = this._exports[prefetchId] || this._exports[compatId];
270
- return prefetchExports || {};
271
- }
272
- },
273
- {
274
- key: "prefetch",
275
- value: function prefetch(prefetchOptions) {
276
- var id = prefetchOptions.id, _prefetchOptions_functionId = prefetchOptions.functionId, functionId = _prefetchOptions_functionId === void 0 ? 'default' : _prefetchOptions_functionId, refetchParams = prefetchOptions.refetchParams;
277
- var prefetchResult;
278
- var prefetchId = runtimeUtils.getPrefetchId(id);
279
- var compatId = runtimeUtils.compatGetPrefetchId(id);
280
- var memorizeId = id + functionId;
281
- var memory = this.prefetchMemory.get(memorizeId);
282
- if (!this.checkOutdate(prefetchOptions) && memory) {
283
- return memory;
284
- }
285
- var prefetchExports = this._exports[prefetchId] || this._exports[compatId];
286
- if (!prefetchExports) {
287
- return;
288
- }
289
- var executePrefetch = prefetchExports[functionId];
290
- if (typeof executePrefetch === 'function') {
291
- if (refetchParams) {
292
- prefetchResult = executePrefetch(refetchParams);
293
- } else {
294
- prefetchResult = executePrefetch();
295
- }
296
- } else {
297
- throw new Error("[Module Federation Data Prefetch]: No prefetch function called ".concat(functionId, " export in prefetch file"));
298
- }
299
- this.memorize(memorizeId, prefetchResult);
300
- return prefetchResult;
301
- }
302
- },
303
- {
304
- key: "memorize",
305
- value: function memorize(id, value) {
306
- this.prefetchMemory.set(id, value);
307
- }
308
- },
309
- {
310
- key: "markOutdate",
311
- value: function markOutdate(markOptions, isOutdate) {
312
- var id = markOptions.id, _markOptions_functionId = markOptions.functionId, functionId = _markOptions_functionId === void 0 ? 'default' : _markOptions_functionId;
313
- if (!this.recordOutdate[id]) {
314
- this.recordOutdate[id] = {};
315
- }
316
- this.recordOutdate[id][functionId] = isOutdate;
317
- }
318
- },
319
- {
320
- key: "checkOutdate",
321
- value: function checkOutdate(outdateOptions) {
322
- var id = outdateOptions.id, _outdateOptions_functionId = outdateOptions.functionId, functionId = _outdateOptions_functionId === void 0 ? 'default' : _outdateOptions_functionId, cacheStrategy = outdateOptions.cacheStrategy;
323
- if (typeof cacheStrategy === 'function') {
324
- return cacheStrategy();
325
- }
326
- if (!this.recordOutdate[id]) {
327
- this.recordOutdate[id] = {};
328
- }
329
- if (this.recordOutdate[id][functionId]) {
330
- this.markOutdate({
331
- id: id,
332
- functionId: functionId
333
- }, false);
334
- return true;
335
- } else {
336
- return false;
337
- }
338
- }
339
- }
340
- ], [
341
- {
342
- key: "getInstance",
343
- value: function getInstance(id) {
344
- return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
345
- }
346
- }
347
- ]);
348
- return MFDataPrefetch;
349
- }();
140
+ }
350
141
 
351
142
  exports.MFDataPrefetch = MFDataPrefetch;