@module-federation/data-prefetch 2.0.0 → 2.1.0

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 (121) hide show
  1. package/dist/babel.cjs +95 -0
  2. package/dist/babel.d.cts +1 -0
  3. package/dist/babel.d.ts +1 -2
  4. package/dist/babel.js +5 -0
  5. package/dist/cli/babel.cjs +115 -0
  6. package/dist/cli/babel.d.ts +37 -0
  7. package/dist/cli/babel.js +52 -0
  8. package/dist/cli/index.cjs +230 -0
  9. package/dist/cli/index.d.ts +16 -0
  10. package/dist/cli/index.js +171 -0
  11. package/dist/cli.cjs +95 -0
  12. package/dist/cli.d.cts +1 -0
  13. package/dist/cli.d.ts +1 -1
  14. package/dist/cli.js +5 -0
  15. package/dist/common/constant.cjs +51 -0
  16. package/dist/common/constant.d.ts +1 -0
  17. package/dist/common/constant.js +5 -0
  18. package/dist/common/index.cjs +95 -0
  19. package/dist/common/index.d.ts +1 -0
  20. package/dist/common/index.js +5 -0
  21. package/dist/common/node-utils.cjs +100 -0
  22. package/dist/common/node-utils.d.ts +2 -0
  23. package/dist/common/node-utils.js +35 -0
  24. package/dist/common/runtime-utils.cjs +84 -0
  25. package/dist/common/runtime-utils.d.ts +5 -0
  26. package/dist/common/runtime-utils.js +29 -0
  27. package/dist/constant.cjs +51 -0
  28. package/dist/constant.d.ts +1 -0
  29. package/dist/constant.js +5 -0
  30. package/dist/index.cjs +106 -0
  31. package/dist/index.d.ts +2 -1
  32. package/dist/index.js +7 -0
  33. package/dist/logger/index.cjs +60 -0
  34. package/dist/logger/index.d.ts +2 -0
  35. package/dist/logger/index.js +10 -0
  36. package/dist/plugin.cjs +234 -0
  37. package/dist/{src/plugin.d.ts → plugin.d.cts} +1 -1
  38. package/dist/plugin.d.ts +3 -2
  39. package/dist/plugin.js +173 -0
  40. package/dist/prefetch.cjs +197 -0
  41. package/dist/prefetch.d.ts +42 -0
  42. package/dist/prefetch.js +149 -0
  43. package/dist/react/hooks.cjs +144 -0
  44. package/dist/react/hooks.d.ts +11 -0
  45. package/dist/{react.esm.js → react/hooks.js} +42 -30
  46. package/dist/react/index.cjs +95 -0
  47. package/dist/react/index.d.ts +1 -0
  48. package/dist/react/index.js +5 -0
  49. package/dist/react/utils.cjs +65 -0
  50. package/dist/react/utils.d.ts +1 -0
  51. package/dist/react/utils.js +15 -0
  52. package/dist/react.cjs +95 -0
  53. package/dist/react.d.cts +1 -0
  54. package/dist/react.d.ts +1 -1
  55. package/dist/react.js +5 -0
  56. package/dist/universal/index.cjs +81 -0
  57. package/dist/universal/index.d.ts +2 -0
  58. package/dist/{universal.esm.js → universal/index.js} +14 -10
  59. package/dist/universal.cjs +95 -0
  60. package/dist/universal.d.cts +1 -0
  61. package/dist/universal.d.ts +1 -1
  62. package/dist/universal.js +5 -0
  63. package/package.json +47 -21
  64. package/dist/babel.cjs.cjs +0 -54
  65. package/dist/babel.cjs.cjs.map +0 -1
  66. package/dist/babel.cjs.d.ts +0 -2
  67. package/dist/babel.esm.js +0 -52
  68. package/dist/babel.esm.js.map +0 -1
  69. package/dist/cli.cjs.cjs +0 -156
  70. package/dist/cli.cjs.cjs.map +0 -1
  71. package/dist/cli.cjs.d.ts +0 -1
  72. package/dist/cli.esm.js +0 -153
  73. package/dist/cli.esm.js.map +0 -1
  74. package/dist/constant.cjs.cjs +0 -6
  75. package/dist/constant.cjs.cjs.map +0 -1
  76. package/dist/constant.esm.js +0 -4
  77. package/dist/constant.esm.js.map +0 -1
  78. package/dist/index.cjs.cjs +0 -10
  79. package/dist/index.cjs.cjs.map +0 -1
  80. package/dist/index.cjs.d.ts +0 -1
  81. package/dist/index.cjs2.cjs +0 -8
  82. package/dist/index.cjs2.cjs.map +0 -1
  83. package/dist/index.esm.js +0 -3
  84. package/dist/index.esm.js.map +0 -1
  85. package/dist/index.esm2.js +0 -6
  86. package/dist/index.esm2.js.map +0 -1
  87. package/dist/plugin.cjs.cjs +0 -158
  88. package/dist/plugin.cjs.cjs.map +0 -1
  89. package/dist/plugin.cjs.d.ts +0 -2
  90. package/dist/plugin.esm.js +0 -153
  91. package/dist/plugin.esm.js.map +0 -1
  92. package/dist/prefetch.cjs.cjs +0 -182
  93. package/dist/prefetch.cjs.cjs.map +0 -1
  94. package/dist/prefetch.esm.js +0 -179
  95. package/dist/prefetch.esm.js.map +0 -1
  96. package/dist/react.cjs.cjs +0 -76
  97. package/dist/react.cjs.cjs.map +0 -1
  98. package/dist/react.cjs.d.ts +0 -1
  99. package/dist/react.esm.js.map +0 -1
  100. package/dist/runtime-utils.cjs.cjs +0 -29
  101. package/dist/runtime-utils.cjs.cjs.map +0 -1
  102. package/dist/runtime-utils.esm.js +0 -24
  103. package/dist/runtime-utils.esm.js.map +0 -1
  104. package/dist/universal.cjs.cjs +0 -30
  105. package/dist/universal.cjs.cjs.map +0 -1
  106. package/dist/universal.cjs.d.ts +0 -1
  107. package/dist/universal.esm.js.map +0 -1
  108. /package/dist/{src/cli/babel.d.ts → cli/babel.d.cts} +0 -0
  109. /package/dist/{src/cli/index.d.ts → cli/index.d.cts} +0 -0
  110. /package/dist/{src/common/constant.d.ts → common/constant.d.cts} +0 -0
  111. /package/dist/{src/common/index.d.ts → common/index.d.cts} +0 -0
  112. /package/dist/{src/common/node-utils.d.ts → common/node-utils.d.cts} +0 -0
  113. /package/dist/{src/common/runtime-utils.d.ts → common/runtime-utils.d.cts} +0 -0
  114. /package/dist/{src/constant.d.ts → constant.d.cts} +0 -0
  115. /package/dist/{src/index.d.ts → index.d.cts} +0 -0
  116. /package/dist/{src/logger/index.d.ts → logger/index.d.cts} +0 -0
  117. /package/dist/{src/prefetch.d.ts → prefetch.d.cts} +0 -0
  118. /package/dist/{src/react/hooks.d.ts → react/hooks.d.cts} +0 -0
  119. /package/dist/{src/react/index.d.ts → react/index.d.cts} +0 -0
  120. /package/dist/{src/react/utils.d.ts → react/utils.d.cts} +0 -0
  121. /package/dist/{src/universal/index.d.ts → universal/index.d.cts} +0 -0
package/dist/plugin.js ADDED
@@ -0,0 +1,173 @@
1
+ import { getResourceUrl } from "@module-federation/sdk";
2
+ import { getSignalFromManifest } from "./common/runtime-utils.js";
3
+ import { MFDataPrefetch } from "./prefetch.js";
4
+ import logger from "./logger/index.js";
5
+ import { SHARED_STRATEGY } from "./constant.js";
6
+
7
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
8
+
9
+ ;// CONCATENATED MODULE: external "./common/runtime-utils.js"
10
+
11
+ ;// CONCATENATED MODULE: external "./prefetch.js"
12
+
13
+ ;// CONCATENATED MODULE: external "./logger/index.js"
14
+
15
+ ;// CONCATENATED MODULE: external "./constant.js"
16
+
17
+ ;// CONCATENATED MODULE: ./src/plugin.ts
18
+
19
+
20
+
21
+
22
+
23
+ const loadingArray = [];
24
+ let sharedFlag = SHARED_STRATEGY;
25
+ // eslint-disable-next-line max-lines-per-function
26
+ const prefetchPlugin = ()=>({
27
+ name: 'data-prefetch-runtime-plugin',
28
+ initContainer (options) {
29
+ const { remoteSnapshot, remoteInfo, id, origin } = options;
30
+ if (!id) {
31
+ return options;
32
+ }
33
+ const snapshot = remoteSnapshot;
34
+ const { name } = remoteInfo;
35
+ const prefetchOptions = {
36
+ name,
37
+ remote: remoteInfo,
38
+ origin,
39
+ remoteSnapshot: snapshot
40
+ };
41
+ const signal = getSignalFromManifest(snapshot);
42
+ if (!signal) {
43
+ return options;
44
+ }
45
+ if (sharedFlag !== SHARED_STRATEGY) {
46
+ throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
47
+ }
48
+ const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
49
+ let prefetchUrl;
50
+ // @ts-expect-error
51
+ if (snapshot.prefetchEntry) {
52
+ // @ts-expect-error
53
+ prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
54
+ }
55
+ const exist = loadingArray.find((loading)=>loading.id === id);
56
+ if (exist) {
57
+ return options;
58
+ }
59
+ const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
60
+ const projectExports = instance.getProjectExports();
61
+ if (projectExports instanceof Promise) {
62
+ await projectExports;
63
+ }
64
+ return Promise.resolve().then(()=>{
65
+ const exports = instance.getExposeExports(id);
66
+ logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
67
+ const result = Object.keys(exports).map((k)=>{
68
+ const value = instance.prefetch({
69
+ id,
70
+ functionId: k
71
+ });
72
+ const functionId = k;
73
+ return {
74
+ value,
75
+ functionId
76
+ };
77
+ });
78
+ return result;
79
+ });
80
+ });
81
+ loadingArray.push({
82
+ id,
83
+ promise
84
+ });
85
+ return options;
86
+ },
87
+ afterResolve (options) {
88
+ const { remoteSnapshot, remoteInfo, id, origin } = options;
89
+ const snapshot = remoteSnapshot;
90
+ const { name } = remoteInfo;
91
+ const prefetchOptions = {
92
+ name,
93
+ remote: remoteInfo,
94
+ origin,
95
+ remoteSnapshot: snapshot
96
+ };
97
+ const signal = getSignalFromManifest(snapshot);
98
+ if (!signal) {
99
+ return options;
100
+ }
101
+ const inited = loadingArray.some((info)=>info.id === id);
102
+ if (!inited) {
103
+ return options;
104
+ }
105
+ if (sharedFlag !== SHARED_STRATEGY) {
106
+ throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
107
+ }
108
+ const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
109
+ let prefetchUrl;
110
+ // @ts-expect-error
111
+ if (snapshot.prefetchEntry) {
112
+ // @ts-expect-error
113
+ prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
114
+ }
115
+ const index = loadingArray.findIndex((loading)=>loading.id === id);
116
+ // clear cache
117
+ if (index !== -1) {
118
+ loadingArray.splice(index, 1);
119
+ }
120
+ const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
121
+ const projectExports = instance.getProjectExports();
122
+ if (projectExports instanceof Promise) {
123
+ await projectExports;
124
+ }
125
+ return Promise.resolve().then(()=>{
126
+ const exports = instance.getExposeExports(id);
127
+ logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
128
+ const result = Object.keys(exports).map((k)=>{
129
+ const value = instance.prefetch({
130
+ id,
131
+ functionId: k
132
+ });
133
+ const functionId = k;
134
+ return {
135
+ value,
136
+ functionId
137
+ };
138
+ });
139
+ return result;
140
+ });
141
+ });
142
+ loadingArray.push({
143
+ id,
144
+ promise
145
+ });
146
+ return options;
147
+ },
148
+ async onLoad (options) {
149
+ var _loadingArray_find;
150
+ const { remote, id } = options;
151
+ const { name } = remote;
152
+ const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) === null || _loadingArray_find === void 0 ? void 0 : _loadingArray_find.promise;
153
+ if (promise) {
154
+ const prefetch = await promise;
155
+ const prefetchValue = prefetch.map((result)=>result.value);
156
+ await Promise.all(prefetchValue);
157
+ const instance = MFDataPrefetch.getInstance(name);
158
+ prefetch.forEach((result)=>{
159
+ const { value, functionId } = result;
160
+ instance.memorize(id + functionId, value);
161
+ });
162
+ }
163
+ return options;
164
+ },
165
+ beforeLoadShare (options) {
166
+ const shareInfo = options.shareInfo;
167
+ sharedFlag = (shareInfo === null || shareInfo === void 0 ? void 0 : shareInfo.strategy) || sharedFlag;
168
+ return options;
169
+ }
170
+ });
171
+ /* ESM default export */ const src_plugin = (prefetchPlugin);
172
+
173
+ export { src_plugin as default, prefetchPlugin };
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3
+ return typeof document === 'undefined'
4
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5
+ : (document.currentScript && document.currentScript.src) ||
6
+ new URL('main.js', document.baseURI).href;
7
+ })();
8
+ ;
9
+ // The require scope
10
+ var __webpack_require__ = {};
11
+
12
+ /************************************************************************/
13
+ // webpack/runtime/define_property_getters
14
+ (() => {
15
+ __webpack_require__.d = (exports, definition) => {
16
+ for(var key in definition) {
17
+ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
18
+ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
19
+ }
20
+ }
21
+ };
22
+ })();
23
+ // webpack/runtime/has_own_property
24
+ (() => {
25
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
26
+ })();
27
+ // webpack/runtime/make_namespace_object
28
+ (() => {
29
+ // define __esModule on exports
30
+ __webpack_require__.r = (exports) => {
31
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
32
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
33
+ }
34
+ Object.defineProperty(exports, '__esModule', { value: true });
35
+ };
36
+ })();
37
+ /************************************************************************/
38
+ var __webpack_exports__ = {};
39
+ // ESM COMPAT FLAG
40
+ __webpack_require__.r(__webpack_exports__);
41
+
42
+ // EXPORTS
43
+ __webpack_require__.d(__webpack_exports__, {
44
+ MFDataPrefetch: () => (/* binding */ MFDataPrefetch)
45
+ });
46
+
47
+ ;// CONCATENATED MODULE: external "@module-federation/runtime"
48
+ const runtime_namespaceObject = require("@module-federation/runtime");
49
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
50
+ const sdk_namespaceObject = require("@module-federation/sdk");
51
+ ;// CONCATENATED MODULE: external "./common/runtime-utils.cjs"
52
+ const runtime_utils_cjs_namespaceObject = require("./common/runtime-utils.cjs");
53
+ ;// CONCATENATED MODULE: ./src/prefetch.ts
54
+
55
+
56
+
57
+ // @ts-ignore init global variable for test
58
+ const federationGlobal = globalThis;
59
+ federationGlobal.__FEDERATION__ ??= {};
60
+ federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
61
+ entryLoading: {},
62
+ instance: new Map(),
63
+ __PREFETCH_EXPORTS__: {}
64
+ };
65
+ const getFederation = ()=>globalThis.__FEDERATION__;
66
+ class MFDataPrefetch {
67
+ get global() {
68
+ return getFederation().__PREFETCH__;
69
+ }
70
+ static getInstance(id) {
71
+ return getFederation().__PREFETCH__.instance.get(id);
72
+ }
73
+ async loadEntry(entry) {
74
+ const { name, remoteSnapshot, remote, origin } = this._options;
75
+ if (entry) {
76
+ const { buildVersion, globalName } = remoteSnapshot;
77
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
78
+ if (!this.global.entryLoading[uniqueKey]) {
79
+ this.global.entryLoading[uniqueKey] = (0,sdk_namespaceObject.loadScript)(entry, {});
80
+ }
81
+ return this.global.entryLoading[uniqueKey];
82
+ } else {
83
+ const remoteInfo = (0,runtime_namespaceObject.getRemoteInfo)(remote);
84
+ const module = origin.moduleCache.get(remoteInfo.name);
85
+ return (0,runtime_namespaceObject.getRemoteEntry)({
86
+ origin: origin,
87
+ remoteInfo,
88
+ remoteEntryExports: module ? module.remoteEntryExports : undefined
89
+ });
90
+ }
91
+ }
92
+ getProjectExports() {
93
+ var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
94
+ if (Object.keys(this._exports).length > 0) {
95
+ return this._exports;
96
+ }
97
+ const { name } = this._options;
98
+ const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
99
+ const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
100
+ const resolve = exportsPromise.then((exports = {})=>{
101
+ // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
102
+ // Not all functions should be directly identified as prefetch functions
103
+ const memory = {};
104
+ Object.keys(exports).forEach((key)=>{
105
+ memory[key] = {};
106
+ const exportVal = exports[key];
107
+ Object.keys(exportVal).reduce((memo, current)=>{
108
+ if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
109
+ memo[current] = exportVal[current];
110
+ }
111
+ return memo;
112
+ }, memory[key]);
113
+ });
114
+ this.memorizeExports(memory);
115
+ });
116
+ return resolve;
117
+ }
118
+ memorizeExports(exports) {
119
+ this._exports = exports;
120
+ }
121
+ getExposeExports(id) {
122
+ const prefetchId = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
123
+ const compatId = (0,runtime_utils_cjs_namespaceObject.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 = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
131
+ const compatId = (0,runtime_utils_cjs_namespaceObject.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(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
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
+ id,
175
+ functionId
176
+ }, false);
177
+ return true;
178
+ } else {
179
+ return false;
180
+ }
181
+ }
182
+ constructor(options){
183
+ this.prefetchMemory = new Map();
184
+ this.recordOutdate = {};
185
+ this._exports = {};
186
+ this._options = options;
187
+ this.global.instance.set(options.name, this);
188
+ }
189
+ }
190
+
191
+ exports.MFDataPrefetch = __webpack_exports__.MFDataPrefetch;
192
+ for(var __webpack_i__ in __webpack_exports__) {
193
+ if(["MFDataPrefetch"].indexOf(__webpack_i__) === -1) {
194
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
195
+ }
196
+ }
197
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,42 @@
1
+ import { ModuleFederation } from '@module-federation/runtime';
2
+ import { ModuleInfo } from '@module-federation/sdk';
3
+ import { Remote } from '@module-federation/runtime/types';
4
+ import type { Federation } from '@module-federation/runtime';
5
+ interface FederationWithPrefetch extends Federation {
6
+ __PREFETCH__: {
7
+ entryLoading: Record<string, undefined | Promise<void>>;
8
+ instance: Map<string, MFDataPrefetch>;
9
+ __PREFETCH_EXPORTS__: Record<string, () => Promise<Record<string, any>>>;
10
+ };
11
+ }
12
+ type PrefetchExports = Record<string, any>;
13
+ export interface DataPrefetchOptions {
14
+ name: string;
15
+ remote?: Remote;
16
+ origin?: ModuleFederation;
17
+ remoteSnapshot?: ModuleInfo;
18
+ }
19
+ export interface prefetchOptions {
20
+ id: string;
21
+ functionId?: string;
22
+ cacheStrategy?: () => boolean;
23
+ refetchParams?: any;
24
+ }
25
+ export declare class MFDataPrefetch {
26
+ prefetchMemory: Map<string, Promise<any>>;
27
+ recordOutdate: Record<string, Record<string, boolean>>;
28
+ private _exports;
29
+ private _options;
30
+ constructor(options: DataPrefetchOptions);
31
+ get global(): FederationWithPrefetch['__PREFETCH__'];
32
+ static getInstance(id: string): MFDataPrefetch | undefined;
33
+ loadEntry(entry: string | undefined): Promise<any>;
34
+ getProjectExports(): Promise<void> | Record<string, any>;
35
+ memorizeExports(exports: Record<string, any>): void;
36
+ getExposeExports(id: string): PrefetchExports;
37
+ prefetch(prefetchOptions: prefetchOptions): any;
38
+ memorize(id: string, value: any): void;
39
+ markOutdate(markOptions: Omit<prefetchOptions, 'cacheStrategy'>, isOutdate: boolean): void;
40
+ checkOutdate(outdateOptions: prefetchOptions): boolean;
41
+ }
42
+ export {};
@@ -0,0 +1,149 @@
1
+ import { getRemoteEntry, getRemoteInfo } from "@module-federation/runtime";
2
+ import { loadScript } from "@module-federation/sdk";
3
+ import { compatGetPrefetchId, getPrefetchId } from "./common/runtime-utils.js";
4
+
5
+ ;// CONCATENATED MODULE: external "@module-federation/runtime"
6
+
7
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
8
+
9
+ ;// CONCATENATED MODULE: external "./common/runtime-utils.js"
10
+
11
+ ;// CONCATENATED MODULE: ./src/prefetch.ts
12
+
13
+
14
+
15
+ // @ts-ignore init global variable for test
16
+ const federationGlobal = globalThis;
17
+ federationGlobal.__FEDERATION__ ??= {};
18
+ federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
19
+ entryLoading: {},
20
+ instance: new Map(),
21
+ __PREFETCH_EXPORTS__: {}
22
+ };
23
+ const getFederation = ()=>globalThis.__FEDERATION__;
24
+ class MFDataPrefetch {
25
+ get global() {
26
+ return getFederation().__PREFETCH__;
27
+ }
28
+ static getInstance(id) {
29
+ return getFederation().__PREFETCH__.instance.get(id);
30
+ }
31
+ async loadEntry(entry) {
32
+ const { name, remoteSnapshot, remote, origin } = this._options;
33
+ if (entry) {
34
+ const { buildVersion, globalName } = remoteSnapshot;
35
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
36
+ if (!this.global.entryLoading[uniqueKey]) {
37
+ this.global.entryLoading[uniqueKey] = loadScript(entry, {});
38
+ }
39
+ return this.global.entryLoading[uniqueKey];
40
+ } else {
41
+ const remoteInfo = getRemoteInfo(remote);
42
+ const module = origin.moduleCache.get(remoteInfo.name);
43
+ return getRemoteEntry({
44
+ origin: origin,
45
+ remoteInfo,
46
+ remoteEntryExports: module ? module.remoteEntryExports : undefined
47
+ });
48
+ }
49
+ }
50
+ getProjectExports() {
51
+ var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
52
+ if (Object.keys(this._exports).length > 0) {
53
+ return this._exports;
54
+ }
55
+ const { name } = this._options;
56
+ const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
57
+ const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
58
+ const resolve = exportsPromise.then((exports = {})=>{
59
+ // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
60
+ // Not all functions should be directly identified as prefetch functions
61
+ const memory = {};
62
+ Object.keys(exports).forEach((key)=>{
63
+ memory[key] = {};
64
+ const exportVal = exports[key];
65
+ Object.keys(exportVal).reduce((memo, current)=>{
66
+ if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
67
+ memo[current] = exportVal[current];
68
+ }
69
+ return memo;
70
+ }, memory[key]);
71
+ });
72
+ this.memorizeExports(memory);
73
+ });
74
+ return resolve;
75
+ }
76
+ memorizeExports(exports) {
77
+ this._exports = exports;
78
+ }
79
+ getExposeExports(id) {
80
+ const prefetchId = getPrefetchId(id);
81
+ const compatId = compatGetPrefetchId(id);
82
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
83
+ return prefetchExports || {};
84
+ }
85
+ prefetch(prefetchOptions) {
86
+ const { id, functionId = 'default', refetchParams } = prefetchOptions;
87
+ let prefetchResult;
88
+ const prefetchId = getPrefetchId(id);
89
+ const compatId = compatGetPrefetchId(id);
90
+ const memorizeId = id + functionId;
91
+ const memory = this.prefetchMemory.get(memorizeId);
92
+ if (!this.checkOutdate(prefetchOptions) && memory) {
93
+ return memory;
94
+ }
95
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
96
+ if (!prefetchExports) {
97
+ return;
98
+ }
99
+ const executePrefetch = prefetchExports[functionId];
100
+ if (typeof executePrefetch === 'function') {
101
+ if (refetchParams) {
102
+ prefetchResult = executePrefetch(refetchParams);
103
+ } else {
104
+ prefetchResult = executePrefetch();
105
+ }
106
+ } else {
107
+ throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
108
+ }
109
+ this.memorize(memorizeId, prefetchResult);
110
+ return prefetchResult;
111
+ }
112
+ memorize(id, value) {
113
+ this.prefetchMemory.set(id, value);
114
+ }
115
+ markOutdate(markOptions, isOutdate) {
116
+ const { id, functionId = 'default' } = markOptions;
117
+ if (!this.recordOutdate[id]) {
118
+ this.recordOutdate[id] = {};
119
+ }
120
+ this.recordOutdate[id][functionId] = isOutdate;
121
+ }
122
+ checkOutdate(outdateOptions) {
123
+ const { id, functionId = 'default', cacheStrategy } = outdateOptions;
124
+ if (typeof cacheStrategy === 'function') {
125
+ return cacheStrategy();
126
+ }
127
+ if (!this.recordOutdate[id]) {
128
+ this.recordOutdate[id] = {};
129
+ }
130
+ if (this.recordOutdate[id][functionId]) {
131
+ this.markOutdate({
132
+ id,
133
+ functionId
134
+ }, false);
135
+ return true;
136
+ } else {
137
+ return false;
138
+ }
139
+ }
140
+ constructor(options){
141
+ this.prefetchMemory = new Map();
142
+ this.recordOutdate = {};
143
+ this._exports = {};
144
+ this._options = options;
145
+ this.global.instance.set(options.name, this);
146
+ }
147
+ }
148
+
149
+ export { MFDataPrefetch };