@module-federation/data-prefetch 0.0.0-next-20241015060033 → 0.0.0-next-20241016062654

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 (89) hide show
  1. package/.swcrc +29 -0
  2. package/CHANGELOG.md +41 -4
  3. package/LICENSE +1 -1
  4. package/dist/LICENSE +21 -0
  5. package/dist/babel.cjs.d.ts +2 -0
  6. package/dist/babel.cjs.js +50 -0
  7. package/dist/babel.esm.d.ts +2 -0
  8. package/dist/babel.esm.js +48 -0
  9. package/dist/cli.cjs.d.ts +1 -0
  10. package/dist/cli.cjs.js +156 -0
  11. package/dist/cli.esm.d.ts +1 -0
  12. package/dist/cli.esm.js +153 -0
  13. package/dist/constant.cjs.js +5 -0
  14. package/dist/constant.esm.js +3 -0
  15. package/dist/index.cjs.d.ts +1 -0
  16. package/dist/index.cjs.js +14 -0
  17. package/dist/index.cjs2.js +7 -0
  18. package/dist/index.esm.d.ts +1 -0
  19. package/dist/index.esm.js +7 -0
  20. package/dist/index.esm2.js +5 -0
  21. package/dist/package.json +88 -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/{cli → src/cli}/babel.d.ts +2 -4
  37. package/dist/{cli → src/cli}/index.d.ts +5 -8
  38. package/dist/src/common/constant.d.ts +1 -0
  39. package/dist/src/common/index.d.ts +1 -0
  40. package/dist/src/common/node-utils.d.ts +2 -0
  41. package/dist/src/common/runtime-utils.d.ts +5 -0
  42. package/dist/src/constant.d.ts +1 -0
  43. package/dist/src/index.d.ts +2 -0
  44. package/dist/src/logger/index.d.ts +3 -0
  45. package/dist/src/plugin.d.ts +3 -0
  46. package/dist/{prefetch-c08be471.d.ts → src/prefetch.d.ts} +4 -6
  47. package/dist/src/react/hooks.d.ts +11 -0
  48. package/dist/src/react/index.d.ts +1 -0
  49. package/dist/src/react/utils.d.ts +1 -0
  50. package/dist/src/universal/index.d.ts +2 -0
  51. package/dist/universal.cjs.d.ts +1 -0
  52. package/dist/universal.cjs.js +29 -0
  53. package/dist/universal.esm.d.ts +1 -0
  54. package/dist/universal.esm.js +27 -0
  55. package/package.json +28 -37
  56. package/project.json +48 -6
  57. package/rollup.config.js +45 -0
  58. package/src/cli/index.ts +7 -4
  59. package/src/constant.ts +1 -0
  60. package/src/plugin.ts +4 -4
  61. package/tsconfig.json +1 -1
  62. package/tsconfig.lib.json +10 -0
  63. package/dist/cli/babel.js +0 -84
  64. package/dist/cli/index.js +0 -207
  65. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  66. package/dist/esm/chunk-BGNOVRH5.js +0 -166
  67. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  68. package/dist/esm/chunk-ISF7VMBW.js +0 -156
  69. package/dist/esm/chunk-OCDMSRMZ.js +0 -32
  70. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  71. package/dist/esm/cli/babel.js +0 -57
  72. package/dist/esm/cli/index.js +0 -180
  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/index.d.ts +0 -5
  79. package/dist/index.js +0 -351
  80. package/dist/plugin.d.ts +0 -5
  81. package/dist/plugin.js +0 -347
  82. package/dist/react/index.d.ts +0 -16
  83. package/dist/react/index.js +0 -287
  84. package/dist/shared/index.d.ts +0 -5
  85. package/dist/shared/index.js +0 -48
  86. package/dist/universal/index.d.ts +0 -8
  87. package/dist/universal/index.js +0 -205
  88. package/src/shared/index.ts +0 -26
  89. package/tsup.config.ts +0 -35
@@ -1,8 +0,0 @@
1
- import { p as prefetchOptions } from '../prefetch-c08be471.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,205 +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 exportsPromiseFn = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
88
- const exportsPromise = typeof exportsPromiseFn === "function" ? exportsPromiseFn() : Promise.resolve({});
89
- const resolve = exportsPromise.then(
90
- (exports = {}) => {
91
- const memory = {};
92
- Object.keys(exports).forEach((key) => {
93
- memory[key] = {};
94
- const exportVal = exports[key];
95
- Object.keys(exportVal).reduce(
96
- (memo, current) => {
97
- if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
98
- memo[current] = exportVal[current];
99
- }
100
- return memo;
101
- },
102
- memory[key]
103
- );
104
- });
105
- this.memorizeExports(memory);
106
- }
107
- );
108
- return resolve;
109
- }
110
- memorizeExports(exports) {
111
- this._exports = exports;
112
- }
113
- getExposeExports(id) {
114
- const prefetchId = getPrefetchId(id);
115
- const compatId = compatGetPrefetchId(id);
116
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
117
- return prefetchExports || {};
118
- }
119
- prefetch(prefetchOptions) {
120
- const { id, functionId = "default", refetchParams } = prefetchOptions;
121
- let prefetchResult;
122
- const prefetchId = getPrefetchId(id);
123
- const compatId = compatGetPrefetchId(id);
124
- const memorizeId = id + functionId;
125
- const memory = this.prefetchMemory.get(memorizeId);
126
- if (!this.checkOutdate(prefetchOptions) && memory) {
127
- return memory;
128
- }
129
- const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
130
- if (!prefetchExports) {
131
- return;
132
- }
133
- const executePrefetch = prefetchExports[functionId];
134
- if (typeof executePrefetch === "function") {
135
- if (refetchParams) {
136
- prefetchResult = executePrefetch(refetchParams);
137
- } else {
138
- prefetchResult = executePrefetch();
139
- }
140
- } else {
141
- throw new Error(
142
- `[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
143
- );
144
- }
145
- this.memorize(memorizeId, prefetchResult);
146
- return prefetchResult;
147
- }
148
- memorize(id, value) {
149
- this.prefetchMemory.set(id, value);
150
- }
151
- markOutdate(markOptions, isOutdate) {
152
- const { id, functionId = "default" } = markOptions;
153
- if (!this.recordOutdate[id]) {
154
- this.recordOutdate[id] = {};
155
- }
156
- this.recordOutdate[id][functionId] = isOutdate;
157
- }
158
- checkOutdate(outdateOptions) {
159
- const { id, functionId = "default", cacheStrategy } = outdateOptions;
160
- if (typeof cacheStrategy === "function") {
161
- return cacheStrategy();
162
- }
163
- if (!this.recordOutdate[id]) {
164
- this.recordOutdate[id] = {};
165
- }
166
- if (this.recordOutdate[id][functionId]) {
167
- this.markOutdate(
168
- {
169
- id,
170
- functionId
171
- },
172
- false
173
- );
174
- return true;
175
- } else {
176
- return false;
177
- }
178
- }
179
- };
180
-
181
- // src/universal/index.ts
182
- function prefetch(options) {
183
- const { id, functionId = "default" } = options;
184
- const mfScope = getScope();
185
- const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
186
- name: mfScope
187
- });
188
- const res = prefetchInstance.getProjectExports();
189
- if (res instanceof Promise) {
190
- const promise = res.then(() => {
191
- const result = prefetchInstance.prefetch(options);
192
- prefetchInstance.memorize(id + functionId, result);
193
- return result;
194
- });
195
- return promise;
196
- } else {
197
- const result = prefetchInstance.prefetch(options);
198
- prefetchInstance.memorize(id + functionId, result);
199
- return result;
200
- }
201
- }
202
- // Annotate the CommonJS export names for ESM import in node:
203
- 0 && (module.exports = {
204
- prefetch
205
- });
@@ -1,26 +0,0 @@
1
- import type { FederationRuntimePlugin } from '@module-federation/runtime';
2
-
3
- const sharedStrategy: () => FederationRuntimePlugin = () => ({
4
- name: 'shared-strategy',
5
- beforeInit(args) {
6
- const { userOptions } = args;
7
- const shared = userOptions.shared;
8
- if (shared) {
9
- Object.keys(shared).forEach((sharedKey) => {
10
- const sharedConfigs = shared[sharedKey];
11
- const arraySharedConfigs = Array.isArray(sharedConfigs)
12
- ? sharedConfigs
13
- : [sharedConfigs];
14
- arraySharedConfigs.forEach((s) => {
15
- s.strategy = 'loaded-first';
16
- });
17
- });
18
- console.warn(
19
- `[Module Federation Data Prefetch]: Your shared strategy is set to 'loaded-first', this is a necessary condition for data prefetch`,
20
- );
21
- }
22
- return args;
23
- },
24
- });
25
-
26
- export default sharedStrategy;
package/tsup.config.ts DELETED
@@ -1,35 +0,0 @@
1
- import { replace } from 'esbuild-plugin-replace';
2
- import minimist from 'minimist';
3
- import type { Options } from 'tsup';
4
-
5
- import pkg from './package.json';
6
-
7
- const args = minimist(process.argv.slice(2));
8
- const watch = process.env.WATCH;
9
- const sourceMap = args.sourcemap || args.s;
10
-
11
- export const tsup: Options = {
12
- entry: [
13
- 'src/index.ts',
14
- 'src/react/index.ts',
15
- 'src/cli/index.ts',
16
- 'src/cli/babel.ts',
17
- 'src/universal/index.ts',
18
- 'src/plugin.ts',
19
- 'src/shared/index.ts',
20
- ],
21
- sourcemap: sourceMap,
22
- clean: true,
23
- dts: true,
24
- watch: watch ? 'src/' : false,
25
- format: ['esm', 'cjs'],
26
- legacyOutput: true,
27
- esbuildPlugins: [
28
- replace({
29
- __VERSION__: `'${pkg.version}'`,
30
- __DEV__:
31
- '(typeof process !== "undefined" && process.env && process.env.NODE_ENV ? (process.env.NODE_ENV !== "production") : false)',
32
- __TEST__: 'false',
33
- }),
34
- ],
35
- };