@module-federation/data-prefetch 1.0.16 → 2.0.1

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 (98) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -1
  3. package/dist/LICENSE +21 -0
  4. package/dist/babel.cjs +95 -0
  5. package/dist/babel.d.ts +1 -0
  6. package/dist/babel.js +5 -0
  7. package/dist/cli/babel.cjs +115 -0
  8. package/dist/cli/babel.d.ts +2 -4
  9. package/dist/cli/babel.js +50 -82
  10. package/dist/cli/index.cjs +217 -0
  11. package/dist/cli/index.d.ts +5 -8
  12. package/dist/cli/index.js +150 -197
  13. package/dist/cli.cjs +95 -0
  14. package/dist/cli.d.ts +1 -0
  15. package/dist/cli.js +5 -0
  16. package/dist/common/constant.cjs +51 -0
  17. package/dist/common/constant.d.ts +1 -0
  18. package/dist/common/constant.js +5 -0
  19. package/dist/common/index.cjs +95 -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 -5
  32. package/dist/index.js +3 -346
  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/plugin.d.ts +3 -5
  38. package/dist/plugin.js +167 -340
  39. package/dist/prefetch.cjs +197 -0
  40. package/dist/{prefetch-4e9646e4.d.ts → prefetch.d.ts} +14 -17
  41. package/dist/prefetch.js +149 -0
  42. package/dist/react/hooks.cjs +144 -0
  43. package/dist/react/hooks.d.ts +11 -0
  44. package/dist/react/hooks.js +86 -0
  45. package/dist/react/index.cjs +95 -0
  46. package/dist/react/index.d.ts +1 -16
  47. package/dist/react/index.js +2 -283
  48. package/dist/react/utils.cjs +65 -0
  49. package/dist/react/utils.d.ts +1 -0
  50. package/dist/react/utils.js +15 -0
  51. package/dist/react.cjs +95 -0
  52. package/dist/react.d.ts +1 -0
  53. package/dist/react.js +5 -0
  54. package/dist/universal/index.cjs +81 -0
  55. package/dist/universal/index.d.ts +2 -8
  56. package/dist/universal/index.js +24 -196
  57. package/dist/universal.cjs +95 -0
  58. package/dist/universal.d.ts +1 -0
  59. package/dist/universal.js +5 -0
  60. package/package.json +76 -40
  61. package/CHANGELOG.md +0 -9
  62. package/__tests__/babel.spec.ts +0 -75
  63. package/__tests__/prefetch.spec.ts +0 -138
  64. package/__tests__/react.spec.ts +0 -119
  65. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  66. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  67. package/dist/esm/chunk-ISLXMAIA.js +0 -155
  68. package/dist/esm/chunk-KI4QGPIS.js +0 -32
  69. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  70. package/dist/esm/chunk-VVZ6XIM6.js +0 -166
  71. package/dist/esm/cli/babel.js +0 -57
  72. package/dist/esm/cli/index.js +0 -178
  73. package/dist/esm/index.js +0 -13
  74. package/dist/esm/plugin.js +0 -12
  75. package/dist/esm/react/index.js +0 -94
  76. package/dist/esm/shared/index.js +0 -27
  77. package/dist/esm/universal/index.js +0 -9
  78. package/dist/shared/index.d.ts +0 -5
  79. package/dist/shared/index.js +0 -48
  80. package/jest.config.js +0 -29
  81. package/project.json +0 -21
  82. package/src/cli/babel.ts +0 -94
  83. package/src/cli/index.ts +0 -171
  84. package/src/common/constant.ts +0 -1
  85. package/src/common/node-utils.ts +0 -24
  86. package/src/common/runtime-utils.ts +0 -34
  87. package/src/index.ts +0 -2
  88. package/src/logger/index.ts +0 -3
  89. package/src/plugin.ts +0 -191
  90. package/src/prefetch.ts +0 -207
  91. package/src/react/hooks.ts +0 -95
  92. package/src/react/index.ts +0 -1
  93. package/src/react/utils.ts +0 -11
  94. package/src/shared/index.ts +0 -26
  95. package/src/universal/index.ts +0 -27
  96. package/tsconfig.json +0 -27
  97. package/tsup.config.ts +0 -35
  98. /package/{src/common/index.ts → dist/common/index.d.ts} +0 -0
package/src/cli/index.ts DELETED
@@ -1,171 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs-extra';
3
-
4
- import {
5
- encodeName,
6
- moduleFederationPlugin,
7
- MFPrefetchCommon,
8
- } from '@module-federation/sdk';
9
- import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
10
- import type { Compiler, WebpackPluginInstance } from 'webpack';
11
-
12
- import { TEMP_DIR } from '../common/constant';
13
- import { fileExistsWithCaseSync, fixPrefetchPath } from '../common/node-utils';
14
- import { getPrefetchId } from '../common/runtime-utils';
15
-
16
- const { RuntimeGlobals, Template } = require(
17
- normalizeWebpackPath('webpack'),
18
- ) as typeof import('webpack');
19
-
20
- export function getFederationGlobalScope(
21
- runtimeGlobals: typeof RuntimeGlobals,
22
- ): string {
23
- return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
24
- }
25
-
26
- export class PrefetchPlugin implements WebpackPluginInstance {
27
- public options: moduleFederationPlugin.ModuleFederationPluginOptions;
28
- private _reWriteExports: string;
29
-
30
- constructor(options: moduleFederationPlugin.ModuleFederationPluginOptions) {
31
- this.options = options;
32
- this._reWriteExports = '';
33
- }
34
-
35
- // eslint-disable-next-line max-lines-per-function
36
- apply(compiler: Compiler) {
37
- const { name, exposes } = this.options;
38
- if (!exposes) {
39
- return;
40
- }
41
- if (!compiler.options.context) {
42
- throw new Error('compiler.options.context is not defined');
43
- }
44
-
45
- const { runtimePlugins } = this.options;
46
- if (!Array.isArray(runtimePlugins)) {
47
- this.options.runtimePlugins = [];
48
- }
49
-
50
- const runtimePath = path.resolve(__dirname, '../esm/plugin.js');
51
- const sharedPath = path.resolve(__dirname, '../esm/shared/index.js');
52
- if (!this.options.runtimePlugins?.includes(runtimePath)) {
53
- this.options.runtimePlugins!.push(runtimePath);
54
- }
55
- if (!this.options.runtimePlugins?.includes(sharedPath)) {
56
- this.options.runtimePlugins!.push(sharedPath);
57
- }
58
-
59
- const encodedName = encodeName(name as string);
60
- const asyncEntryPath = path.resolve(
61
- compiler.options.context,
62
- `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`,
63
- );
64
- if (fs.existsSync(asyncEntryPath)) {
65
- fs.unlinkSync(asyncEntryPath);
66
- }
67
- if (!this.options.dataPrefetch) {
68
- return;
69
- }
70
-
71
- const prefetchs: Array<string> = [];
72
- const exposeAlias = Object.keys(exposes);
73
- exposeAlias.forEach((alias) => {
74
- let exposePath;
75
- // @ts-ignore
76
- const exposeValue = exposes[alias];
77
- if (typeof exposeValue === 'string') {
78
- exposePath = exposeValue;
79
- } else {
80
- exposePath = exposeValue.import[0];
81
- }
82
- const targetPaths = fixPrefetchPath(exposePath);
83
- for (const pathItem of targetPaths) {
84
- const absolutePath = path.resolve(compiler.options.context!, pathItem);
85
- if (fileExistsWithCaseSync(absolutePath)) {
86
- prefetchs.push(pathItem);
87
- const absoluteAlias = alias.replace('.', '');
88
- this._reWriteExports += `export * as ${getPrefetchId(
89
- `${name}${absoluteAlias}`,
90
- )} from '${absolutePath}';\n`;
91
- break;
92
- }
93
- }
94
- });
95
-
96
- if (!this._reWriteExports) {
97
- return;
98
- }
99
- const tempDirRealPath = path.resolve(
100
- compiler.options.context,
101
- 'node_modules',
102
- TEMP_DIR,
103
- );
104
- if (!fs.existsSync(tempDirRealPath)) {
105
- fs.mkdirSync(tempDirRealPath);
106
- }
107
- if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
108
- fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
109
- }
110
- fs.writeFileSync(asyncEntryPath, this._reWriteExports);
111
- new compiler.webpack.DefinePlugin({
112
- FederationDataPrefetch: JSON.stringify(asyncEntryPath),
113
- }).apply(compiler);
114
- }
115
-
116
- static addRuntime(
117
- compiler: Compiler,
118
- options: {
119
- name: string;
120
- },
121
- ) {
122
- const encodedName = encodeName(options.name as string);
123
- if (!compiler.options.context) {
124
- throw new Error('compiler.options.context is not defined');
125
- }
126
- const prefetchEntry = path.resolve(
127
- compiler.options.context,
128
- `node_modules/.mf/${encodedName}/bootstrap.js`,
129
- );
130
- const federationGlobal = getFederationGlobalScope(
131
- RuntimeGlobals || ({} as typeof RuntimeGlobals),
132
- );
133
-
134
- return Template.asString([
135
- fs.existsSync(prefetchEntry)
136
- ? Template.indent([
137
- 'function injectPrefetch() {',
138
- `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
139
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] || {`,
140
- `entryLoading: {},`,
141
- `instance: new Map(),`,
142
- `__PREFETCH_EXPORTS__: {},`,
143
- `};`,
144
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] || {};`,
145
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}']['${options.name}'] = import('${prefetchEntry}');`,
146
- '}',
147
- `${federationGlobal}.prefetch = injectPrefetch`,
148
- ])
149
- : '',
150
- Template.indent([
151
- `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
152
- `${federationGlobal}.prefetch()`,
153
- '}',
154
- ]),
155
- ]);
156
- }
157
-
158
- static setRemoteIdentifier() {
159
- const federationGlobal = getFederationGlobalScope(
160
- RuntimeGlobals || ({} as typeof RuntimeGlobals),
161
- );
162
- return Template.indent([`${federationGlobal}.isMFRemote = true;`]);
163
- }
164
-
165
- static removeRemoteIdentifier() {
166
- const federationGlobal = getFederationGlobalScope(
167
- RuntimeGlobals || ({} as typeof RuntimeGlobals),
168
- );
169
- return Template.indent([`${federationGlobal}.isMFRemote = false;`]);
170
- }
171
- }
@@ -1 +0,0 @@
1
- export const TEMP_DIR = '.mf';
@@ -1,24 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs-extra';
3
-
4
- export const fileExistsWithCaseSync = (filepath: string): boolean => {
5
- const dir = path.dirname(filepath);
6
- if (filepath === '/' || filepath === '.') {
7
- return true;
8
- }
9
- const filenames = fs.readdirSync(dir);
10
- if (filenames.indexOf(path.basename(filepath)) === -1) {
11
- return false;
12
- }
13
- return fileExistsWithCaseSync(dir);
14
- };
15
-
16
- export const fixPrefetchPath = (exposePath: string): Array<string> => {
17
- const pathExt = ['.js', '.ts'];
18
- const extReg = /\.(ts|js|tsx|jsx)$/;
19
- if (extReg.test(exposePath)) {
20
- return pathExt.map((ext) => exposePath.replace(extReg, `.prefetch${ext}`));
21
- } else {
22
- return pathExt.map((ext) => exposePath + `.prefetch${ext}`);
23
- }
24
- };
@@ -1,34 +0,0 @@
1
- import { getInstance } from '@module-federation/runtime';
2
- import {
3
- encodeName,
4
- ModuleInfo,
5
- MFPrefetchCommon,
6
- } from '@module-federation/sdk';
7
-
8
- export const getScope = (): string => {
9
- return getInstance()!.options.name;
10
- };
11
-
12
- export const getPrefetchId = (id: string): string =>
13
- encodeName(`${id}/${MFPrefetchCommon.identifier}`);
14
-
15
- export const compatGetPrefetchId = (id: string): string =>
16
- encodeName(`${id}/VmokPrefetch`);
17
-
18
- export const getSignalFromManifest = (remoteSnapshot: ModuleInfo): boolean => {
19
- if (!remoteSnapshot) {
20
- return false;
21
- }
22
-
23
- if (
24
- !('prefetchEntry' in remoteSnapshot) &&
25
- !('prefetchInterface' in remoteSnapshot)
26
- ) {
27
- return false;
28
- }
29
-
30
- if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
31
- return false;
32
- }
33
- return true;
34
- };
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './prefetch';
2
- export * from './plugin';
@@ -1,3 +0,0 @@
1
- import { Logger } from '@module-federation/sdk';
2
-
3
- export default new Logger('[Module Federation Data Prefetch]');
package/src/plugin.ts DELETED
@@ -1,191 +0,0 @@
1
- import type { FederationRuntimePlugin } from '@module-federation/runtime/types';
2
- import { ModuleInfo, getResourceUrl } from '@module-federation/sdk';
3
-
4
- import { getSignalFromManifest } from './common/runtime-utils';
5
- import { MFDataPrefetch } from './prefetch';
6
- import logger from './logger';
7
-
8
- interface Loading {
9
- id: string;
10
- promise: Promise<
11
- Array<{
12
- value: any;
13
- functionId: string;
14
- }>
15
- >;
16
- }
17
- const loadingArray: Array<Loading> = [];
18
- const strategy = 'loaded-first';
19
- let sharedFlag = strategy;
20
- // eslint-disable-next-line max-lines-per-function
21
- export const prefetchPlugin = (): FederationRuntimePlugin => ({
22
- name: 'data-prefetch-runtime-plugin',
23
- initContainer(options) {
24
- const { remoteSnapshot, remoteInfo, id, origin } = options;
25
- const snapshot = remoteSnapshot as ModuleInfo;
26
- const { name } = remoteInfo;
27
-
28
- const prefetchOptions = {
29
- name,
30
- remote: remoteInfo,
31
- origin,
32
- remoteSnapshot: snapshot,
33
- };
34
- const signal = getSignalFromManifest(snapshot);
35
- if (!signal) {
36
- return options;
37
- }
38
- if (sharedFlag !== strategy) {
39
- throw new Error(
40
- `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`,
41
- );
42
- }
43
-
44
- const instance =
45
- MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
46
-
47
- let prefetchUrl;
48
- // @ts-expect-error
49
- if (snapshot.prefetchEntry) {
50
- // @ts-expect-error
51
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry as string);
52
- }
53
-
54
- const exist = loadingArray.find((loading) => loading.id === id);
55
- if (exist) {
56
- return options;
57
- }
58
- const promise = instance.loadEntry(prefetchUrl).then(async () => {
59
- const projectExports = instance!.getProjectExports();
60
- if (projectExports instanceof Promise) {
61
- await projectExports;
62
- }
63
- return Promise.resolve().then(() => {
64
- const exports = instance!.getExposeExports(id);
65
- logger.info(
66
- `1. Start Prefetch initContainer: ${id} - ${performance.now()}`,
67
- );
68
- const result = Object.keys(exports).map((k) => {
69
- const value = instance!.prefetch({
70
- id,
71
- functionId: k,
72
- });
73
- const functionId = k;
74
-
75
- return {
76
- value,
77
- functionId,
78
- };
79
- });
80
- return result;
81
- });
82
- });
83
-
84
- loadingArray.push({
85
- id,
86
- promise,
87
- });
88
- return options;
89
- },
90
-
91
- afterResolve(options) {
92
- const { remoteSnapshot, remoteInfo, id, origin } = options;
93
- const snapshot = remoteSnapshot as ModuleInfo;
94
- const { name } = remoteInfo;
95
-
96
- const prefetchOptions = {
97
- name,
98
- remote: remoteInfo,
99
- origin,
100
- remoteSnapshot: snapshot,
101
- };
102
- const signal = getSignalFromManifest(snapshot);
103
- if (!signal) {
104
- return options;
105
- }
106
-
107
- const inited = loadingArray.some((info) => info.id === id);
108
- if (!inited) {
109
- return options;
110
- }
111
-
112
- if (sharedFlag !== strategy) {
113
- throw new Error(
114
- `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`,
115
- );
116
- }
117
-
118
- const instance =
119
- MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
120
-
121
- let prefetchUrl;
122
- // @ts-expect-error
123
- if (snapshot.prefetchEntry) {
124
- // @ts-expect-error
125
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry as string);
126
- }
127
-
128
- const index = loadingArray.findIndex((loading) => loading.id === id);
129
- // clear cache
130
- if (index !== -1) {
131
- loadingArray.splice(index, 1);
132
- }
133
- const promise = instance.loadEntry(prefetchUrl).then(async () => {
134
- const projectExports = instance!.getProjectExports();
135
- if (projectExports instanceof Promise) {
136
- await projectExports;
137
- }
138
- return Promise.resolve().then(() => {
139
- const exports = instance!.getExposeExports(id);
140
- logger.info(
141
- `1. Start Prefetch afterResolve: ${id} - ${performance.now()}`,
142
- );
143
- const result = Object.keys(exports).map((k) => {
144
- const value = instance!.prefetch({
145
- id,
146
- functionId: k,
147
- });
148
- const functionId = k;
149
-
150
- return {
151
- value,
152
- functionId,
153
- };
154
- });
155
- return result;
156
- });
157
- });
158
-
159
- loadingArray.push({
160
- id,
161
- promise,
162
- });
163
- return options;
164
- },
165
- async onLoad(options) {
166
- const { remote, id } = options;
167
- const { name } = remote;
168
- const promise = loadingArray.find((loading) => loading.id === id)?.promise;
169
-
170
- if (promise) {
171
- const prefetch = await promise;
172
- const prefetchValue = prefetch.map((result) => result.value);
173
- await Promise.all(prefetchValue);
174
- const instance = MFDataPrefetch.getInstance(name);
175
-
176
- prefetch.forEach((result: { value: any; functionId: string }) => {
177
- const { value, functionId } = result;
178
- instance!.memorize(id + functionId, value);
179
- });
180
- }
181
- return options;
182
- },
183
-
184
- beforeLoadShare(options) {
185
- const shareInfo = options.shareInfo;
186
- sharedFlag = shareInfo?.strategy || sharedFlag;
187
- return options;
188
- },
189
- });
190
-
191
- export default prefetchPlugin;
package/src/prefetch.ts DELETED
@@ -1,207 +0,0 @@
1
- import {
2
- FederationHost,
3
- getRemoteEntry,
4
- getRemoteInfo,
5
- } from '@module-federation/runtime';
6
- import {
7
- loadScript,
8
- ModuleInfo,
9
- ProviderModuleInfo,
10
- } from '@module-federation/sdk';
11
- import { Remote } from '@module-federation/runtime/types';
12
-
13
- import { getPrefetchId, compatGetPrefetchId } from './common/runtime-utils';
14
-
15
- declare module '@module-federation/runtime' {
16
- export interface Federation {
17
- __PREFETCH__: {
18
- entryLoading: Record<string, undefined | Promise<void>>;
19
- instance: Map<string, MFDataPrefetch>;
20
- __PREFETCH_EXPORTS__: Record<string, Promise<Record<string, any>>>;
21
- };
22
- }
23
- }
24
-
25
- type PrefetchExports = Record<string, any>;
26
-
27
- export interface DataPrefetchOptions {
28
- name: string;
29
- remote?: Remote;
30
- origin?: FederationHost;
31
- remoteSnapshot?: ModuleInfo;
32
- }
33
-
34
- export interface prefetchOptions {
35
- id: string;
36
- functionId?: string;
37
- cacheStrategy?: () => boolean;
38
- refetchParams?: any;
39
- }
40
-
41
- // @ts-ignore init global variable for test
42
- globalThis.__FEDERATION__ ??= {};
43
- globalThis.__FEDERATION__.__PREFETCH__ ??= {
44
- entryLoading: {},
45
- instance: new Map(),
46
- __PREFETCH_EXPORTS__: {},
47
- };
48
- export class MFDataPrefetch {
49
- public prefetchMemory: Map<string, Promise<any>>;
50
- public recordOutdate: Record<string, Record<string, boolean>>;
51
- private _exports: Record<string, any>;
52
- private _options: DataPrefetchOptions;
53
-
54
- constructor(options: DataPrefetchOptions) {
55
- this.prefetchMemory = new Map();
56
- this.recordOutdate = {};
57
- this._exports = {};
58
- this._options = options;
59
- this.global.instance.set(options.name, this);
60
- }
61
-
62
- get global(): Record<string, any> {
63
- return globalThis.__FEDERATION__.__PREFETCH__;
64
- }
65
-
66
- static getInstance(id: string): MFDataPrefetch | undefined {
67
- return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
68
- }
69
-
70
- async loadEntry(entry: string | undefined): Promise<any> {
71
- const { name, remoteSnapshot, remote, origin } = this._options;
72
-
73
- if (entry) {
74
- const { buildVersion, globalName } = remoteSnapshot as ProviderModuleInfo;
75
- const uniqueKey = globalName || `${name}:${buildVersion}`;
76
-
77
- if (!this.global.entryLoading[uniqueKey]) {
78
- this.global.entryLoading[uniqueKey] = loadScript(entry, {});
79
- }
80
- return this.global.entryLoading[uniqueKey];
81
- } else {
82
- const remoteInfo = getRemoteInfo(remote as Remote);
83
- const module = origin!.moduleCache.get(remoteInfo.name);
84
- return getRemoteEntry({
85
- origin: origin!,
86
- remoteInfo,
87
- remoteEntryExports: module ? module.remoteEntryExports : undefined,
88
- });
89
- }
90
- }
91
-
92
- getProjectExports() {
93
- if (Object.keys(this._exports).length > 0) {
94
- return this._exports;
95
- }
96
- const { name } = this._options;
97
- const exportsPromise =
98
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__?.[name];
99
- const resolve = exportsPromise.then(
100
- (exports: Record<string, Record<string, any>> = {}) => {
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: Record<string, Record<string, any>> = {};
104
- Object.keys(exports).forEach((key) => {
105
- memory[key] = {};
106
- const exportVal = exports[key];
107
- Object.keys(exportVal).reduce(
108
- (memo: Record<string, any>, current: string) => {
109
- if (
110
- current.toLocaleLowerCase().endsWith('prefetch') ||
111
- current.toLocaleLowerCase() === 'default'
112
- ) {
113
- memo[current] = exportVal[current];
114
- }
115
- return memo;
116
- },
117
- memory[key],
118
- );
119
- });
120
- this.memorizeExports(memory);
121
- },
122
- );
123
- return resolve;
124
- }
125
-
126
- memorizeExports(exports: Record<string, any>): void {
127
- this._exports = exports;
128
- }
129
-
130
- getExposeExports(id: string): PrefetchExports {
131
- const prefetchId = getPrefetchId(id);
132
- const compatId = compatGetPrefetchId(id);
133
- const prefetchExports =
134
- this._exports[prefetchId] || (this._exports[compatId] as PrefetchExports);
135
- return prefetchExports || {};
136
- }
137
-
138
- prefetch(prefetchOptions: prefetchOptions): any {
139
- const { id, functionId = 'default', refetchParams } = prefetchOptions;
140
- let prefetchResult;
141
- const prefetchId = getPrefetchId(id);
142
- const compatId = compatGetPrefetchId(id);
143
- const memorizeId = id + functionId;
144
- const memory = this.prefetchMemory.get(memorizeId);
145
- if (!this.checkOutdate(prefetchOptions) && memory) {
146
- return memory;
147
- }
148
-
149
- const prefetchExports =
150
- this._exports[prefetchId] || (this._exports[compatId] as PrefetchExports);
151
- if (!prefetchExports) {
152
- return;
153
- }
154
- const executePrefetch = prefetchExports[functionId];
155
- if (typeof executePrefetch === 'function') {
156
- if (refetchParams) {
157
- prefetchResult = executePrefetch(refetchParams);
158
- } else {
159
- prefetchResult = executePrefetch();
160
- }
161
- } else {
162
- throw new Error(
163
- `[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`,
164
- );
165
- }
166
- this.memorize(memorizeId, prefetchResult);
167
- return prefetchResult;
168
- }
169
-
170
- memorize(id: string, value: any): void {
171
- this.prefetchMemory.set(id, value);
172
- }
173
-
174
- markOutdate(
175
- markOptions: Omit<prefetchOptions, 'cacheStrategy'>,
176
- isOutdate: boolean,
177
- ): void {
178
- const { id, functionId = 'default' } = markOptions;
179
- if (!this.recordOutdate[id]) {
180
- this.recordOutdate[id] = {};
181
- }
182
- this.recordOutdate[id][functionId] = isOutdate;
183
- }
184
-
185
- checkOutdate(outdateOptions: prefetchOptions): boolean {
186
- const { id, functionId = 'default', cacheStrategy } = outdateOptions;
187
- if (typeof cacheStrategy === 'function') {
188
- return cacheStrategy();
189
- }
190
-
191
- if (!this.recordOutdate[id]) {
192
- this.recordOutdate[id] = {};
193
- }
194
- if (this.recordOutdate[id][functionId]) {
195
- this.markOutdate(
196
- {
197
- id,
198
- functionId,
199
- },
200
- false,
201
- );
202
- return true;
203
- } else {
204
- return false;
205
- }
206
- }
207
- }