@module-federation/data-prefetch 0.6.6 → 0.6.8

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 (91) hide show
  1. package/.swcrc +29 -0
  2. package/CHANGELOG.md +28 -0
  3. package/LICENSE +1 -1
  4. package/__tests__/prefetch.spec.ts +3 -2
  5. package/__tests__/react.spec.ts +1 -1
  6. package/dist/LICENSE +21 -0
  7. package/dist/babel.cjs.d.ts +2 -0
  8. package/dist/babel.cjs.js +50 -0
  9. package/dist/babel.esm.d.ts +2 -0
  10. package/dist/babel.esm.js +48 -0
  11. package/dist/cli.cjs.d.ts +1 -0
  12. package/dist/cli.cjs.js +155 -0
  13. package/dist/cli.esm.d.ts +1 -0
  14. package/dist/cli.esm.js +152 -0
  15. package/dist/index.cjs.d.ts +1 -0
  16. package/dist/index.cjs.js +13 -0
  17. package/dist/index.cjs2.js +7 -0
  18. package/dist/index.esm.d.ts +1 -0
  19. package/dist/index.esm.js +6 -0
  20. package/dist/index.esm2.js +5 -0
  21. package/dist/package.json +100 -0
  22. package/dist/plugin.cjs.d.ts +2 -0
  23. package/dist/plugin.cjs.js +159 -0
  24. package/dist/plugin.esm.d.ts +2 -0
  25. package/dist/plugin.esm.js +154 -0
  26. package/dist/polyfills.cjs.js +14 -0
  27. package/dist/polyfills.esm.js +12 -0
  28. package/dist/prefetch.cjs.js +142 -0
  29. package/dist/prefetch.esm.js +140 -0
  30. package/dist/react.cjs.d.ts +1 -0
  31. package/dist/react.cjs.js +77 -0
  32. package/dist/react.esm.d.ts +1 -0
  33. package/dist/react.esm.js +75 -0
  34. package/dist/runtime-utils.cjs.js +27 -0
  35. package/dist/runtime-utils.esm.js +22 -0
  36. package/dist/shared.cjs.d.ts +2 -0
  37. package/dist/shared.cjs.js +24 -0
  38. package/dist/shared.esm.d.ts +2 -0
  39. package/dist/shared.esm.js +22 -0
  40. package/dist/{cli → src/cli}/babel.d.ts +2 -4
  41. package/dist/{cli → src/cli}/index.d.ts +5 -8
  42. package/dist/src/common/constant.d.ts +1 -0
  43. package/dist/src/common/index.d.ts +1 -0
  44. package/dist/src/common/node-utils.d.ts +2 -0
  45. package/dist/src/common/runtime-utils.d.ts +5 -0
  46. package/dist/src/index.d.ts +2 -0
  47. package/dist/src/logger/index.d.ts +3 -0
  48. package/dist/src/plugin.d.ts +3 -0
  49. package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +5 -7
  50. package/dist/src/react/hooks.d.ts +11 -0
  51. package/dist/src/react/index.d.ts +1 -0
  52. package/dist/src/react/utils.d.ts +1 -0
  53. package/dist/src/shared/index.d.ts +3 -0
  54. package/dist/src/universal/index.d.ts +2 -0
  55. package/dist/universal.cjs.d.ts +1 -0
  56. package/dist/universal.cjs.js +29 -0
  57. package/dist/universal.esm.d.ts +1 -0
  58. package/dist/universal.esm.js +27 -0
  59. package/jest.config.js +2 -2
  60. package/package.json +33 -30
  61. package/project.json +48 -6
  62. package/rollup.config.js +46 -0
  63. package/src/cli/index.ts +12 -10
  64. package/src/prefetch.ts +6 -2
  65. package/tsconfig.json +1 -1
  66. package/tsconfig.lib.json +10 -0
  67. package/dist/cli/babel.js +0 -84
  68. package/dist/cli/index.js +0 -205
  69. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  70. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  71. package/dist/esm/chunk-ISLXMAIA.js +0 -155
  72. package/dist/esm/chunk-KI4QGPIS.js +0 -32
  73. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  74. package/dist/esm/chunk-VVZ6XIM6.js +0 -166
  75. package/dist/esm/cli/babel.js +0 -57
  76. package/dist/esm/cli/index.js +0 -178
  77. package/dist/esm/index.js +0 -13
  78. package/dist/esm/plugin.js +0 -12
  79. package/dist/esm/react/index.js +0 -94
  80. package/dist/esm/shared/index.js +0 -27
  81. package/dist/esm/universal/index.js +0 -9
  82. package/dist/index.d.ts +0 -5
  83. package/dist/index.js +0 -350
  84. package/dist/plugin.d.ts +0 -5
  85. package/dist/plugin.js +0 -346
  86. package/dist/react/index.d.ts +0 -16
  87. package/dist/react/index.js +0 -286
  88. package/dist/shared/index.d.ts +0 -5
  89. package/dist/shared/index.js +0 -48
  90. package/dist/universal/index.d.ts +0 -8
  91. package/dist/universal/index.js +0 -204
@@ -1,8 +0,0 @@
1
- import { p as prefetchOptions } from '../prefetch-4e9646e4.js';
2
- import '@module-federation/runtime';
3
- import '@module-federation/sdk';
4
- import '@module-federation/runtime/types';
5
-
6
- declare function prefetch(options: prefetchOptions): Promise<any>;
7
-
8
- export { prefetch };
@@ -1,204 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/universal/index.ts
21
- var universal_exports = {};
22
- __export(universal_exports, {
23
- prefetch: () => prefetch
24
- });
25
- module.exports = __toCommonJS(universal_exports);
26
-
27
- // src/prefetch.ts
28
- var import_runtime2 = require("@module-federation/runtime");
29
- var import_sdk2 = require("@module-federation/sdk");
30
-
31
- // src/common/runtime-utils.ts
32
- var import_runtime = require("@module-federation/runtime");
33
- var import_sdk = require("@module-federation/sdk");
34
- var getScope = () => {
35
- return (0, import_runtime.getInstance)().options.name;
36
- };
37
- var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
38
- var compatGetPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/VmokPrefetch`);
39
-
40
- // src/prefetch.ts
41
- globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
42
- var _a;
43
- (_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
44
- entryLoading: {},
45
- instance: /* @__PURE__ */ new Map(),
46
- __PREFETCH_EXPORTS__: {}
47
- });
48
- var MFDataPrefetch = class {
49
- constructor(options) {
50
- this.prefetchMemory = /* @__PURE__ */ new Map();
51
- this.recordOutdate = {};
52
- this._exports = {};
53
- this._options = options;
54
- this.global.instance.set(options.name, this);
55
- }
56
- get global() {
57
- return globalThis.__FEDERATION__.__PREFETCH__;
58
- }
59
- static getInstance(id) {
60
- return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
61
- }
62
- async loadEntry(entry) {
63
- const { name, remoteSnapshot, remote, origin } = this._options;
64
- if (entry) {
65
- const { buildVersion, globalName } = remoteSnapshot;
66
- const uniqueKey = globalName || `${name}:${buildVersion}`;
67
- if (!this.global.entryLoading[uniqueKey]) {
68
- this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
69
- }
70
- return this.global.entryLoading[uniqueKey];
71
- } else {
72
- const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
73
- const module2 = origin.moduleCache.get(remoteInfo.name);
74
- return (0, import_runtime2.getRemoteEntry)({
75
- origin,
76
- remoteInfo,
77
- remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
78
- });
79
- }
80
- }
81
- getProjectExports() {
82
- var _a2;
83
- if (Object.keys(this._exports).length > 0) {
84
- return this._exports;
85
- }
86
- const { name } = this._options;
87
- const exportsPromise = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
88
- const resolve = exportsPromise.then(
89
- (exports = {}) => {
90
- const memory = {};
91
- Object.keys(exports).forEach((key) => {
92
- memory[key] = {};
93
- const exportVal = exports[key];
94
- Object.keys(exportVal).reduce(
95
- (memo, current) => {
96
- if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
97
- memo[current] = exportVal[current];
98
- }
99
- return memo;
100
- },
101
- memory[key]
102
- );
103
- });
104
- this.memorizeExports(memory);
105
- }
106
- );
107
- return resolve;
108
- }
109
- memorizeExports(exports) {
110
- this._exports = exports;
111
- }
112
- getExposeExports(id) {
113
- const prefetchId = getPrefetchId(id);
114
- const compatId = compatGetPrefetchId(id);
115
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
116
- return prefetchExports || {};
117
- }
118
- prefetch(prefetchOptions) {
119
- const { id, functionId = "default", refetchParams } = prefetchOptions;
120
- let prefetchResult;
121
- const prefetchId = getPrefetchId(id);
122
- const compatId = compatGetPrefetchId(id);
123
- const memorizeId = id + functionId;
124
- const memory = this.prefetchMemory.get(memorizeId);
125
- if (!this.checkOutdate(prefetchOptions) && memory) {
126
- return memory;
127
- }
128
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
129
- if (!prefetchExports) {
130
- return;
131
- }
132
- const executePrefetch = prefetchExports[functionId];
133
- if (typeof executePrefetch === "function") {
134
- if (refetchParams) {
135
- prefetchResult = executePrefetch(refetchParams);
136
- } else {
137
- prefetchResult = executePrefetch();
138
- }
139
- } else {
140
- throw new Error(
141
- `[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
142
- );
143
- }
144
- this.memorize(memorizeId, prefetchResult);
145
- return prefetchResult;
146
- }
147
- memorize(id, value) {
148
- this.prefetchMemory.set(id, value);
149
- }
150
- markOutdate(markOptions, isOutdate) {
151
- const { id, functionId = "default" } = markOptions;
152
- if (!this.recordOutdate[id]) {
153
- this.recordOutdate[id] = {};
154
- }
155
- this.recordOutdate[id][functionId] = isOutdate;
156
- }
157
- checkOutdate(outdateOptions) {
158
- const { id, functionId = "default", cacheStrategy } = outdateOptions;
159
- if (typeof cacheStrategy === "function") {
160
- return cacheStrategy();
161
- }
162
- if (!this.recordOutdate[id]) {
163
- this.recordOutdate[id] = {};
164
- }
165
- if (this.recordOutdate[id][functionId]) {
166
- this.markOutdate(
167
- {
168
- id,
169
- functionId
170
- },
171
- false
172
- );
173
- return true;
174
- } else {
175
- return false;
176
- }
177
- }
178
- };
179
-
180
- // src/universal/index.ts
181
- function prefetch(options) {
182
- const { id, functionId = "default" } = options;
183
- const mfScope = getScope();
184
- const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
185
- name: mfScope
186
- });
187
- const res = prefetchInstance.getProjectExports();
188
- if (res instanceof Promise) {
189
- const promise = res.then(() => {
190
- const result = prefetchInstance.prefetch(options);
191
- prefetchInstance.memorize(id + functionId, result);
192
- return result;
193
- });
194
- return promise;
195
- } else {
196
- const result = prefetchInstance.prefetch(options);
197
- prefetchInstance.memorize(id + functionId, result);
198
- return result;
199
- }
200
- }
201
- // Annotate the CommonJS export names for ESM import in node:
202
- 0 && (module.exports = {
203
- prefetch
204
- });