@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
@@ -1,138 +0,0 @@
1
- // Import the necessary modules and functions
2
- import { MFDataPrefetch } from '../src/prefetch';
3
- import {
4
- loadScript,
5
- MFPrefetchCommon,
6
- encodeName,
7
- } from '@module-federation/sdk';
8
-
9
- // Mock loadScript function from SDK
10
- jest.mock('@module-federation/sdk', () => {
11
- const originalModule = jest.requireActual('@module-federation/sdk');
12
- return {
13
- ...originalModule,
14
- loadScript: jest.fn(() => Promise.resolve()),
15
- };
16
- });
17
-
18
- describe('MF Data Prefetch', () => {
19
- let prefetch: MFDataPrefetch;
20
-
21
- const options = {
22
- name: '@mf/test',
23
- remoteSnapshot: {
24
- buildVersion: '1.0.0',
25
- globalName: 'TestGlobalName',
26
- },
27
- };
28
- const exposeId = `${options.name}/button/${MFPrefetchCommon.identifier}`;
29
-
30
- beforeEach(() => {
31
- globalThis.__FEDERATION__.__PREFETCH__ = {
32
- entryLoading: {},
33
- instance: new Map(),
34
- __PREFETCH_EXPORTS__: {},
35
- };
36
- // @ts-ignore
37
- prefetch = new MFDataPrefetch(options);
38
- });
39
- afterAll(() => {
40
- // @ts-ignore
41
- delete globalThis.__FEDERATION__;
42
- });
43
-
44
- // Instance gets added to global memory on creation
45
- it('adds itself to global instances on creation', () => {
46
- expect(prefetch.global.instance.get(options.name)).toBe(prefetch);
47
- });
48
-
49
- // Loads entry script using loadScript from sdk
50
- it('loads entry script using loadScript from sdk', async () => {
51
- const url = 'testUrl'; // Url of the script to be loaded
52
-
53
- await prefetch.loadEntry(url); // Call `loadEntry` function
54
-
55
- // Expect that the loadScript function is called with the correct url
56
- expect(loadScript).toHaveBeenCalledWith(url, expect.any(Object));
57
- });
58
-
59
- // Retrieves project exports
60
- it('gets project exports', async () => {
61
- const exposeExport = {
62
- nyPrefetch: () => {},
63
- };
64
- const projectExport = {
65
- [encodeName(exposeId)]: exposeExport,
66
- };
67
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
68
- Promise.resolve(projectExport);
69
-
70
- await prefetch.getProjectExports();
71
- expect(prefetch.getExposeExports(`${options.name}/button`)).toEqual(
72
- exposeExport,
73
- );
74
- });
75
- // Prefetching with memory and executing prefetch function
76
- it('executes prefetch using prefetch function with and without memory', async () => {
77
- const id = options.name;
78
- const functionId = 'nyPrefetch';
79
- const refetchParams = 'testParams';
80
- const prefetchOptions = { id: `${id}/button`, functionId, refetchParams };
81
-
82
- // Creating a mock prefetch function
83
- const executePrefetch = jest.fn(() => 'Expected Result');
84
- const prefetchExports = { [functionId]: executePrefetch };
85
-
86
- // Mock Project Exports
87
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[id] =
88
- Promise.resolve({
89
- [encodeName(exposeId)]: prefetchExports,
90
- });
91
-
92
- await prefetch.getProjectExports();
93
- // Call the prefetch function first time
94
- let result = await prefetch.prefetch(prefetchOptions);
95
-
96
- // Verify that executePrefetch function is correctly executed
97
- expect(executePrefetch).toHaveBeenCalled();
98
-
99
- // Clear mock function calls data
100
- executePrefetch.mockClear();
101
-
102
- // Call the prefetch function again
103
- result = await prefetch.prefetch(prefetchOptions);
104
-
105
- // Verify that executePrefetch function is NOT called this time (since the result should come from memory)
106
- expect(executePrefetch).not.toHaveBeenCalled();
107
-
108
- // Clear mock function calls data
109
- executePrefetch.mockClear();
110
-
111
- prefetch.markOutdate(prefetchOptions, true);
112
-
113
- // Call the prefetch function first time
114
- result = await prefetch.prefetch(prefetchOptions);
115
-
116
- // Verify that executePrefetch function is correctly executed
117
- expect(executePrefetch).toHaveBeenCalled();
118
- });
119
-
120
- // Checking outdate marking
121
- it('checks outdate marking', () => {
122
- const markOptions = { id: 'testId', functionId: 'testFunction' };
123
-
124
- // Mark the function as outdated
125
- prefetch.markOutdate(markOptions, true);
126
-
127
- // Verify that the function is marked as outdated
128
- let isOutdated = prefetch.checkOutdate(markOptions);
129
- expect(isOutdated).toBe(true);
130
-
131
- // Mark the function as up-to-date
132
- prefetch.markOutdate(markOptions, false);
133
-
134
- // Verify that the function is marked as up-to-date
135
- isOutdated = prefetch.checkOutdate(markOptions);
136
- expect(isOutdated).toBe(false);
137
- });
138
- });
@@ -1,119 +0,0 @@
1
- import { FederationHost, init } from '@module-federation/runtime';
2
- import { renderHook, act } from '@testing-library/react-hooks';
3
- import * as ModuleFederationSDK from '@module-federation/sdk';
4
- import { usePrefetch } from '../src/react';
5
- import { MFDataPrefetch } from '../src/prefetch';
6
-
7
- const mockLoadScript = jest.spyOn(ModuleFederationSDK, 'loadScript');
8
- mockLoadScript.mockImplementation(() => Promise.resolve());
9
-
10
- describe('usePrefetch', () => {
11
- // Mock prefetch function
12
- global.fetch = jest.fn(() =>
13
- Promise.resolve({
14
- ok: true,
15
- status: 200,
16
- statusText: 'OK',
17
- redirected: false,
18
- type: 'basic',
19
- url: '',
20
- clone: jest.fn(),
21
- headers: new Headers(),
22
- body: null,
23
- bodyUsed: false,
24
- text: () => Promise.resolve(JSON.stringify({ data: 'testData' })),
25
- json: () => Promise.resolve({ data: 'testData' }),
26
- formData: () => Promise.resolve(new FormData()),
27
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
28
- blob: () => Promise.resolve(new Blob()),
29
- }),
30
- );
31
-
32
- let prefetch: MFDataPrefetch;
33
-
34
- const options = {
35
- // Options that will be passed to the `MFDataPrefetch` constructor
36
- name: '@mf/test',
37
- remoteSnapshot: {
38
- buildVersion: '1.0.0',
39
- globalName: 'TestGlobalName',
40
- },
41
- };
42
- const testData = 'testData';
43
- const newTestData = 'newTestData';
44
- const functionId = 'testPrefetch';
45
- const executePrefetch = jest.fn((params: string) =>
46
- Promise.resolve(params ? params : testData),
47
- );
48
-
49
- new FederationHost({
50
- name: options.name,
51
- remotes: [],
52
- });
53
- init({
54
- name: options.name,
55
- remotes: [],
56
- });
57
- beforeEach(() => {
58
- globalThis.__FEDERATION__.__PREFETCH__ = {
59
- entryLoading: {},
60
- instance: new Map(),
61
- __PREFETCH_EXPORTS__: {},
62
- };
63
- // @ts-ignore
64
- prefetch = new MFDataPrefetch(options); // Create a new instance of `MFDataPrefetch` class before each test
65
-
66
- const exposeExport = {
67
- [functionId]: executePrefetch,
68
- };
69
- const exposeId = `${options.name}/button/${ModuleFederationSDK.MFPrefetchCommon.identifier}`;
70
- const projectExport = {
71
- [ModuleFederationSDK.encodeName(exposeId)]: exposeExport,
72
- };
73
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
74
- Promise.resolve(projectExport);
75
- });
76
-
77
- afterEach(() => {
78
- executePrefetch.mockClear();
79
- mockLoadScript.mockRestore();
80
- });
81
-
82
- afterAll(() => {
83
- // @ts-ignore
84
- delete globalThis.__FEDERATION__;
85
- });
86
-
87
- it('should prefetch data on first mount', async () => {
88
- const { result } = renderHook(() =>
89
- usePrefetch({ id: `${options.name}/button`, functionId }),
90
- );
91
- await result.current[0];
92
- expect(executePrefetch).toHaveBeenCalled();
93
- // Verify the prefechState
94
- expect(result.current[0]).resolves.toEqual(testData);
95
- });
96
-
97
- it('should refetch data when refreshExecutor is called', async () => {
98
- const { result } = renderHook(() =>
99
- usePrefetch({ id: `${options.name}/button`, functionId }),
100
- );
101
-
102
- await result.current[0];
103
- expect(executePrefetch).toHaveBeenCalled();
104
- executePrefetch.mockClear();
105
- const { result: newCallResult } = renderHook(() =>
106
- usePrefetch({ id: `${options.name}/button`, functionId }),
107
- );
108
- await newCallResult.current[0];
109
- expect(executePrefetch).not.toHaveBeenCalled();
110
- // Call refreshExecutor
111
- act(() => {
112
- result.current[1](newTestData);
113
- });
114
-
115
- expect(executePrefetch).toHaveBeenCalled();
116
- // // Verify the prefetchState after refetch
117
- expect(result.current[0]).resolves.toEqual(newTestData);
118
- });
119
- });
@@ -1,30 +0,0 @@
1
- // src/common/runtime-utils.ts
2
- import { getInstance } from "@module-federation/runtime";
3
- import {
4
- encodeName,
5
- MFPrefetchCommon
6
- } from "@module-federation/sdk";
7
- var getScope = () => {
8
- return getInstance().options.name;
9
- };
10
- var getPrefetchId = (id) => encodeName(`${id}/${MFPrefetchCommon.identifier}`);
11
- var compatGetPrefetchId = (id) => encodeName(`${id}/VmokPrefetch`);
12
- var getSignalFromManifest = (remoteSnapshot) => {
13
- if (!remoteSnapshot) {
14
- return false;
15
- }
16
- if (!("prefetchEntry" in remoteSnapshot) && !("prefetchInterface" in remoteSnapshot)) {
17
- return false;
18
- }
19
- if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
20
- return false;
21
- }
22
- return true;
23
- };
24
-
25
- export {
26
- getScope,
27
- getPrefetchId,
28
- compatGetPrefetchId,
29
- getSignalFromManifest
30
- };
@@ -1,11 +0,0 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined")
5
- return require.apply(this, arguments);
6
- throw new Error('Dynamic require of "' + x + '" is not supported');
7
- });
8
-
9
- export {
10
- __require
11
- };
@@ -1,155 +0,0 @@
1
- import {
2
- compatGetPrefetchId,
3
- getPrefetchId
4
- } from "./chunk-AJPO2B2T.js";
5
-
6
- // src/prefetch.ts
7
- import {
8
- getRemoteEntry,
9
- getRemoteInfo
10
- } from "@module-federation/runtime";
11
- import {
12
- loadScript
13
- } from "@module-federation/sdk";
14
- globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
15
- var _a;
16
- (_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
17
- entryLoading: {},
18
- instance: /* @__PURE__ */ new Map(),
19
- __PREFETCH_EXPORTS__: {}
20
- });
21
- var MFDataPrefetch = class {
22
- constructor(options) {
23
- this.prefetchMemory = /* @__PURE__ */ new Map();
24
- this.recordOutdate = {};
25
- this._exports = {};
26
- this._options = options;
27
- this.global.instance.set(options.name, this);
28
- }
29
- get global() {
30
- return globalThis.__FEDERATION__.__PREFETCH__;
31
- }
32
- static getInstance(id) {
33
- return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
34
- }
35
- async loadEntry(entry) {
36
- const { name, remoteSnapshot, remote, origin } = this._options;
37
- if (entry) {
38
- const { buildVersion, globalName } = remoteSnapshot;
39
- const uniqueKey = globalName || `${name}:${buildVersion}`;
40
- if (!this.global.entryLoading[uniqueKey]) {
41
- this.global.entryLoading[uniqueKey] = loadScript(entry, {});
42
- }
43
- return this.global.entryLoading[uniqueKey];
44
- } else {
45
- const remoteInfo = getRemoteInfo(remote);
46
- const module = origin.moduleCache.get(remoteInfo.name);
47
- return getRemoteEntry({
48
- origin,
49
- remoteInfo,
50
- remoteEntryExports: module ? module.remoteEntryExports : void 0
51
- });
52
- }
53
- }
54
- getProjectExports() {
55
- var _a2;
56
- if (Object.keys(this._exports).length > 0) {
57
- return this._exports;
58
- }
59
- const { name } = this._options;
60
- const exportsPromise = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
61
- const resolve = exportsPromise.then(
62
- (exports = {}) => {
63
- const memory = {};
64
- Object.keys(exports).forEach((key) => {
65
- memory[key] = {};
66
- const exportVal = exports[key];
67
- Object.keys(exportVal).reduce(
68
- (memo, current) => {
69
- if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
70
- memo[current] = exportVal[current];
71
- }
72
- return memo;
73
- },
74
- memory[key]
75
- );
76
- });
77
- this.memorizeExports(memory);
78
- }
79
- );
80
- return resolve;
81
- }
82
- memorizeExports(exports) {
83
- this._exports = exports;
84
- }
85
- getExposeExports(id) {
86
- const prefetchId = getPrefetchId(id);
87
- const compatId = compatGetPrefetchId(id);
88
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
89
- return prefetchExports || {};
90
- }
91
- prefetch(prefetchOptions) {
92
- const { id, functionId = "default", refetchParams } = prefetchOptions;
93
- let prefetchResult;
94
- const prefetchId = getPrefetchId(id);
95
- const compatId = compatGetPrefetchId(id);
96
- const memorizeId = id + functionId;
97
- const memory = this.prefetchMemory.get(memorizeId);
98
- if (!this.checkOutdate(prefetchOptions) && memory) {
99
- return memory;
100
- }
101
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
102
- if (!prefetchExports) {
103
- return;
104
- }
105
- const executePrefetch = prefetchExports[functionId];
106
- if (typeof executePrefetch === "function") {
107
- if (refetchParams) {
108
- prefetchResult = executePrefetch(refetchParams);
109
- } else {
110
- prefetchResult = executePrefetch();
111
- }
112
- } else {
113
- throw new Error(
114
- `[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
115
- );
116
- }
117
- this.memorize(memorizeId, prefetchResult);
118
- return prefetchResult;
119
- }
120
- memorize(id, value) {
121
- this.prefetchMemory.set(id, value);
122
- }
123
- markOutdate(markOptions, isOutdate) {
124
- const { id, functionId = "default" } = markOptions;
125
- if (!this.recordOutdate[id]) {
126
- this.recordOutdate[id] = {};
127
- }
128
- this.recordOutdate[id][functionId] = isOutdate;
129
- }
130
- checkOutdate(outdateOptions) {
131
- const { id, functionId = "default", cacheStrategy } = outdateOptions;
132
- if (typeof cacheStrategy === "function") {
133
- return cacheStrategy();
134
- }
135
- if (!this.recordOutdate[id]) {
136
- this.recordOutdate[id] = {};
137
- }
138
- if (this.recordOutdate[id][functionId]) {
139
- this.markOutdate(
140
- {
141
- id,
142
- functionId
143
- },
144
- false
145
- );
146
- return true;
147
- } else {
148
- return false;
149
- }
150
- }
151
- };
152
-
153
- export {
154
- MFDataPrefetch
155
- };
@@ -1,32 +0,0 @@
1
- import {
2
- MFDataPrefetch
3
- } from "./chunk-ISLXMAIA.js";
4
- import {
5
- getScope
6
- } from "./chunk-AJPO2B2T.js";
7
-
8
- // src/universal/index.ts
9
- function prefetch(options) {
10
- const { id, functionId = "default" } = options;
11
- const mfScope = getScope();
12
- const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
13
- name: mfScope
14
- });
15
- const res = prefetchInstance.getProjectExports();
16
- if (res instanceof Promise) {
17
- const promise = res.then(() => {
18
- const result = prefetchInstance.prefetch(options);
19
- prefetchInstance.memorize(id + functionId, result);
20
- return result;
21
- });
22
- return promise;
23
- } else {
24
- const result = prefetchInstance.prefetch(options);
25
- prefetchInstance.memorize(id + functionId, result);
26
- return result;
27
- }
28
- }
29
-
30
- export {
31
- prefetch
32
- };
@@ -1,7 +0,0 @@
1
- // src/logger/index.ts
2
- import { Logger } from "@module-federation/sdk";
3
- var logger_default = new Logger("[Module Federation Data Prefetch]");
4
-
5
- export {
6
- logger_default
7
- };
@@ -1,166 +0,0 @@
1
- import {
2
- logger_default
3
- } from "./chunk-TTJJJ2WZ.js";
4
- import {
5
- MFDataPrefetch
6
- } from "./chunk-ISLXMAIA.js";
7
- import {
8
- getSignalFromManifest
9
- } from "./chunk-AJPO2B2T.js";
10
-
11
- // src/plugin.ts
12
- import { getResourceUrl } from "@module-federation/sdk";
13
- var loadingArray = [];
14
- var strategy = "loaded-first";
15
- var sharedFlag = strategy;
16
- var prefetchPlugin = () => ({
17
- name: "data-prefetch-runtime-plugin",
18
- initContainer(options) {
19
- const { remoteSnapshot, remoteInfo, id, origin } = options;
20
- const snapshot = remoteSnapshot;
21
- const { name } = remoteInfo;
22
- const prefetchOptions = {
23
- name,
24
- remote: remoteInfo,
25
- origin,
26
- remoteSnapshot: snapshot
27
- };
28
- const signal = getSignalFromManifest(snapshot);
29
- if (!signal) {
30
- return options;
31
- }
32
- if (sharedFlag !== strategy) {
33
- throw new Error(
34
- `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
35
- );
36
- }
37
- const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
38
- let prefetchUrl;
39
- if (snapshot.prefetchEntry) {
40
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
41
- }
42
- const exist = loadingArray.find((loading) => loading.id === id);
43
- if (exist) {
44
- return options;
45
- }
46
- const promise = instance.loadEntry(prefetchUrl).then(async () => {
47
- const projectExports = instance.getProjectExports();
48
- if (projectExports instanceof Promise) {
49
- await projectExports;
50
- }
51
- return Promise.resolve().then(() => {
52
- const exports = instance.getExposeExports(id);
53
- logger_default.info(
54
- `1. Start Prefetch initContainer: ${id} - ${performance.now()}`
55
- );
56
- const result = Object.keys(exports).map((k) => {
57
- const value = instance.prefetch({
58
- id,
59
- functionId: k
60
- });
61
- const functionId = k;
62
- return {
63
- value,
64
- functionId
65
- };
66
- });
67
- return result;
68
- });
69
- });
70
- loadingArray.push({
71
- id,
72
- promise
73
- });
74
- return options;
75
- },
76
- afterResolve(options) {
77
- const { remoteSnapshot, remoteInfo, id, origin } = options;
78
- const snapshot = remoteSnapshot;
79
- const { name } = remoteInfo;
80
- const prefetchOptions = {
81
- name,
82
- remote: remoteInfo,
83
- origin,
84
- remoteSnapshot: snapshot
85
- };
86
- const signal = getSignalFromManifest(snapshot);
87
- if (!signal) {
88
- return options;
89
- }
90
- const inited = loadingArray.some((info) => info.id === id);
91
- if (!inited) {
92
- return options;
93
- }
94
- if (sharedFlag !== strategy) {
95
- throw new Error(
96
- `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
97
- );
98
- }
99
- const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
100
- let prefetchUrl;
101
- if (snapshot.prefetchEntry) {
102
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
103
- }
104
- const index = loadingArray.findIndex((loading) => loading.id === id);
105
- if (index !== -1) {
106
- loadingArray.splice(index, 1);
107
- }
108
- const promise = instance.loadEntry(prefetchUrl).then(async () => {
109
- const projectExports = instance.getProjectExports();
110
- if (projectExports instanceof Promise) {
111
- await projectExports;
112
- }
113
- return Promise.resolve().then(() => {
114
- const exports = instance.getExposeExports(id);
115
- logger_default.info(
116
- `1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
117
- );
118
- const result = Object.keys(exports).map((k) => {
119
- const value = instance.prefetch({
120
- id,
121
- functionId: k
122
- });
123
- const functionId = k;
124
- return {
125
- value,
126
- functionId
127
- };
128
- });
129
- return result;
130
- });
131
- });
132
- loadingArray.push({
133
- id,
134
- promise
135
- });
136
- return options;
137
- },
138
- async onLoad(options) {
139
- var _a;
140
- const { remote, id } = options;
141
- const { name } = remote;
142
- const promise = (_a = loadingArray.find((loading) => loading.id === id)) == null ? void 0 : _a.promise;
143
- if (promise) {
144
- const prefetch = await promise;
145
- const prefetchValue = prefetch.map((result) => result.value);
146
- await Promise.all(prefetchValue);
147
- const instance = MFDataPrefetch.getInstance(name);
148
- prefetch.forEach((result) => {
149
- const { value, functionId } = result;
150
- instance.memorize(id + functionId, value);
151
- });
152
- }
153
- return options;
154
- },
155
- beforeLoadShare(options) {
156
- const shareInfo = options.shareInfo;
157
- sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
158
- return options;
159
- }
160
- });
161
- var plugin_default = prefetchPlugin;
162
-
163
- export {
164
- prefetchPlugin,
165
- plugin_default
166
- };