@module-federation/data-prefetch 1.0.16 → 2.0.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 (114) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -1
  3. package/dist/LICENSE +21 -0
  4. package/dist/babel.cjs.cjs +54 -0
  5. package/dist/babel.cjs.cjs.map +1 -0
  6. package/dist/babel.cjs.d.ts +2 -0
  7. package/dist/babel.d.ts +2 -0
  8. package/dist/babel.esm.js +52 -0
  9. package/dist/babel.esm.js.map +1 -0
  10. package/dist/cli.cjs.cjs +156 -0
  11. package/dist/cli.cjs.cjs.map +1 -0
  12. package/dist/cli.cjs.d.ts +1 -0
  13. package/dist/cli.d.ts +1 -0
  14. package/dist/cli.esm.js +153 -0
  15. package/dist/cli.esm.js.map +1 -0
  16. package/dist/constant.cjs.cjs +6 -0
  17. package/dist/constant.cjs.cjs.map +1 -0
  18. package/dist/constant.esm.js +4 -0
  19. package/dist/constant.esm.js.map +1 -0
  20. package/dist/index.cjs.cjs +10 -0
  21. package/dist/index.cjs.cjs.map +1 -0
  22. package/dist/index.cjs.d.ts +1 -0
  23. package/dist/index.cjs2.cjs +8 -0
  24. package/dist/index.cjs2.cjs.map +1 -0
  25. package/dist/index.d.ts +1 -5
  26. package/dist/index.esm.js +3 -0
  27. package/dist/index.esm.js.map +1 -0
  28. package/dist/index.esm2.js +6 -0
  29. package/dist/index.esm2.js.map +1 -0
  30. package/dist/plugin.cjs.cjs +158 -0
  31. package/dist/plugin.cjs.cjs.map +1 -0
  32. package/dist/plugin.cjs.d.ts +2 -0
  33. package/dist/plugin.d.ts +2 -5
  34. package/dist/plugin.esm.js +153 -0
  35. package/dist/plugin.esm.js.map +1 -0
  36. package/dist/prefetch.cjs.cjs +182 -0
  37. package/dist/prefetch.cjs.cjs.map +1 -0
  38. package/dist/prefetch.esm.js +179 -0
  39. package/dist/prefetch.esm.js.map +1 -0
  40. package/dist/react.cjs.cjs +76 -0
  41. package/dist/react.cjs.cjs.map +1 -0
  42. package/dist/react.cjs.d.ts +1 -0
  43. package/dist/react.d.ts +1 -0
  44. package/dist/react.esm.js +74 -0
  45. package/dist/react.esm.js.map +1 -0
  46. package/dist/runtime-utils.cjs.cjs +29 -0
  47. package/dist/runtime-utils.cjs.cjs.map +1 -0
  48. package/dist/runtime-utils.esm.js +24 -0
  49. package/dist/runtime-utils.esm.js.map +1 -0
  50. package/dist/{cli → src/cli}/babel.d.ts +2 -4
  51. package/dist/{cli → src/cli}/index.d.ts +5 -8
  52. package/dist/src/common/constant.d.ts +1 -0
  53. package/dist/src/common/node-utils.d.ts +2 -0
  54. package/dist/src/common/runtime-utils.d.ts +5 -0
  55. package/dist/src/constant.d.ts +1 -0
  56. package/dist/src/logger/index.d.ts +2 -0
  57. package/dist/src/plugin.d.ts +3 -0
  58. package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +14 -17
  59. package/dist/src/react/hooks.d.ts +11 -0
  60. package/dist/src/react/utils.d.ts +1 -0
  61. package/dist/src/universal/index.d.ts +2 -0
  62. package/dist/universal.cjs.cjs +30 -0
  63. package/dist/universal.cjs.cjs.map +1 -0
  64. package/dist/universal.cjs.d.ts +1 -0
  65. package/dist/universal.d.ts +1 -0
  66. package/dist/universal.esm.js +28 -0
  67. package/dist/universal.esm.js.map +1 -0
  68. package/package.json +49 -39
  69. package/CHANGELOG.md +0 -9
  70. package/__tests__/babel.spec.ts +0 -75
  71. package/__tests__/prefetch.spec.ts +0 -138
  72. package/__tests__/react.spec.ts +0 -119
  73. package/dist/cli/babel.js +0 -84
  74. package/dist/cli/index.js +0 -205
  75. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  76. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  77. package/dist/esm/chunk-ISLXMAIA.js +0 -155
  78. package/dist/esm/chunk-KI4QGPIS.js +0 -32
  79. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  80. package/dist/esm/chunk-VVZ6XIM6.js +0 -166
  81. package/dist/esm/cli/babel.js +0 -57
  82. package/dist/esm/cli/index.js +0 -178
  83. package/dist/esm/index.js +0 -13
  84. package/dist/esm/plugin.js +0 -12
  85. package/dist/esm/react/index.js +0 -94
  86. package/dist/esm/shared/index.js +0 -27
  87. package/dist/esm/universal/index.js +0 -9
  88. package/dist/index.js +0 -350
  89. package/dist/plugin.js +0 -346
  90. package/dist/react/index.d.ts +0 -16
  91. package/dist/react/index.js +0 -286
  92. package/dist/shared/index.d.ts +0 -5
  93. package/dist/shared/index.js +0 -48
  94. package/dist/universal/index.d.ts +0 -8
  95. package/dist/universal/index.js +0 -204
  96. package/jest.config.js +0 -29
  97. package/project.json +0 -21
  98. package/src/cli/babel.ts +0 -94
  99. package/src/cli/index.ts +0 -171
  100. package/src/common/constant.ts +0 -1
  101. package/src/common/node-utils.ts +0 -24
  102. package/src/common/runtime-utils.ts +0 -34
  103. package/src/logger/index.ts +0 -3
  104. package/src/plugin.ts +0 -191
  105. package/src/prefetch.ts +0 -207
  106. package/src/react/hooks.ts +0 -95
  107. package/src/react/utils.ts +0 -11
  108. package/src/shared/index.ts +0 -26
  109. package/src/universal/index.ts +0 -27
  110. package/tsconfig.json +0 -27
  111. package/tsup.config.ts +0 -35
  112. /package/{src/common/index.ts → dist/src/common/index.d.ts} +0 -0
  113. /package/{src/index.ts → dist/src/index.d.ts} +0 -0
  114. /package/{src/react/index.ts → dist/src/react/index.d.ts} +0 -0
package/dist/cli/index.js DELETED
@@ -1,205 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
-
26
- // src/cli/index.ts
27
- var cli_exports = {};
28
- __export(cli_exports, {
29
- PrefetchPlugin: () => PrefetchPlugin,
30
- getFederationGlobalScope: () => getFederationGlobalScope
31
- });
32
- module.exports = __toCommonJS(cli_exports);
33
- var import_path2 = __toESM(require("path"));
34
- var import_fs_extra2 = __toESM(require("fs-extra"));
35
- var import_sdk2 = require("@module-federation/sdk");
36
- var import_normalize_webpack_path = require("@module-federation/sdk/normalize-webpack-path");
37
-
38
- // src/common/constant.ts
39
- var TEMP_DIR = ".mf";
40
-
41
- // src/common/node-utils.ts
42
- var import_path = __toESM(require("path"));
43
- var import_fs_extra = __toESM(require("fs-extra"));
44
- var fileExistsWithCaseSync = (filepath) => {
45
- const dir = import_path.default.dirname(filepath);
46
- if (filepath === "/" || filepath === ".") {
47
- return true;
48
- }
49
- const filenames = import_fs_extra.default.readdirSync(dir);
50
- if (filenames.indexOf(import_path.default.basename(filepath)) === -1) {
51
- return false;
52
- }
53
- return fileExistsWithCaseSync(dir);
54
- };
55
- var fixPrefetchPath = (exposePath) => {
56
- const pathExt = [".js", ".ts"];
57
- const extReg = /\.(ts|js|tsx|jsx)$/;
58
- if (extReg.test(exposePath)) {
59
- return pathExt.map((ext) => exposePath.replace(extReg, `.prefetch${ext}`));
60
- } else {
61
- return pathExt.map((ext) => exposePath + `.prefetch${ext}`);
62
- }
63
- };
64
-
65
- // src/common/runtime-utils.ts
66
- var import_runtime = require("@module-federation/runtime");
67
- var import_sdk = require("@module-federation/sdk");
68
- var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
69
-
70
- // src/cli/index.ts
71
- var { RuntimeGlobals, Template } = require((0, import_normalize_webpack_path.normalizeWebpackPath)("webpack"));
72
- function getFederationGlobalScope(runtimeGlobals) {
73
- return `${runtimeGlobals.require || "__webpack_require__"}.federation`;
74
- }
75
- var PrefetchPlugin = class {
76
- constructor(options) {
77
- this.options = options;
78
- this._reWriteExports = "";
79
- }
80
- apply(compiler) {
81
- var _a, _b;
82
- const { name, exposes } = this.options;
83
- if (!exposes) {
84
- return;
85
- }
86
- if (!compiler.options.context) {
87
- throw new Error("compiler.options.context is not defined");
88
- }
89
- const { runtimePlugins } = this.options;
90
- if (!Array.isArray(runtimePlugins)) {
91
- this.options.runtimePlugins = [];
92
- }
93
- const runtimePath = import_path2.default.resolve(__dirname, "../esm/plugin.js");
94
- const sharedPath = import_path2.default.resolve(__dirname, "../esm/shared/index.js");
95
- if (!((_a = this.options.runtimePlugins) == null ? void 0 : _a.includes(runtimePath))) {
96
- this.options.runtimePlugins.push(runtimePath);
97
- }
98
- if (!((_b = this.options.runtimePlugins) == null ? void 0 : _b.includes(sharedPath))) {
99
- this.options.runtimePlugins.push(sharedPath);
100
- }
101
- const encodedName = (0, import_sdk2.encodeName)(name);
102
- const asyncEntryPath = import_path2.default.resolve(
103
- compiler.options.context,
104
- `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`
105
- );
106
- if (import_fs_extra2.default.existsSync(asyncEntryPath)) {
107
- import_fs_extra2.default.unlinkSync(asyncEntryPath);
108
- }
109
- if (!this.options.dataPrefetch) {
110
- return;
111
- }
112
- const prefetchs = [];
113
- const exposeAlias = Object.keys(exposes);
114
- exposeAlias.forEach((alias) => {
115
- let exposePath;
116
- const exposeValue = exposes[alias];
117
- if (typeof exposeValue === "string") {
118
- exposePath = exposeValue;
119
- } else {
120
- exposePath = exposeValue.import[0];
121
- }
122
- const targetPaths = fixPrefetchPath(exposePath);
123
- for (const pathItem of targetPaths) {
124
- const absolutePath = import_path2.default.resolve(compiler.options.context, pathItem);
125
- if (fileExistsWithCaseSync(absolutePath)) {
126
- prefetchs.push(pathItem);
127
- const absoluteAlias = alias.replace(".", "");
128
- this._reWriteExports += `export * as ${getPrefetchId(
129
- `${name}${absoluteAlias}`
130
- )} from '${absolutePath}';
131
- `;
132
- break;
133
- }
134
- }
135
- });
136
- if (!this._reWriteExports) {
137
- return;
138
- }
139
- const tempDirRealPath = import_path2.default.resolve(
140
- compiler.options.context,
141
- "node_modules",
142
- TEMP_DIR
143
- );
144
- if (!import_fs_extra2.default.existsSync(tempDirRealPath)) {
145
- import_fs_extra2.default.mkdirSync(tempDirRealPath);
146
- }
147
- if (!import_fs_extra2.default.existsSync(`${tempDirRealPath}/${encodedName}`)) {
148
- import_fs_extra2.default.mkdirSync(`${tempDirRealPath}/${encodedName}`);
149
- }
150
- import_fs_extra2.default.writeFileSync(asyncEntryPath, this._reWriteExports);
151
- new compiler.webpack.DefinePlugin({
152
- FederationDataPrefetch: JSON.stringify(asyncEntryPath)
153
- }).apply(compiler);
154
- }
155
- static addRuntime(compiler, options) {
156
- const encodedName = (0, import_sdk2.encodeName)(options.name);
157
- if (!compiler.options.context) {
158
- throw new Error("compiler.options.context is not defined");
159
- }
160
- const prefetchEntry = import_path2.default.resolve(
161
- compiler.options.context,
162
- `node_modules/.mf/${encodedName}/bootstrap.js`
163
- );
164
- const federationGlobal = getFederationGlobalScope(
165
- RuntimeGlobals || {}
166
- );
167
- return Template.asString([
168
- import_fs_extra2.default.existsSync(prefetchEntry) ? Template.indent([
169
- "function injectPrefetch() {",
170
- `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
171
- `globalThis.__FEDERATION__['${import_sdk2.MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${import_sdk2.MFPrefetchCommon.globalKey}'] || {`,
172
- `entryLoading: {},`,
173
- `instance: new Map(),`,
174
- `__PREFETCH_EXPORTS__: {},`,
175
- `};`,
176
- `globalThis.__FEDERATION__['${import_sdk2.MFPrefetchCommon.globalKey}']['${import_sdk2.MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${import_sdk2.MFPrefetchCommon.globalKey}']['${import_sdk2.MFPrefetchCommon.exportsKey}'] || {};`,
177
- `globalThis.__FEDERATION__['${import_sdk2.MFPrefetchCommon.globalKey}']['${import_sdk2.MFPrefetchCommon.exportsKey}']['${options.name}'] = import('${prefetchEntry}');`,
178
- "}",
179
- `${federationGlobal}.prefetch = injectPrefetch`
180
- ]) : "",
181
- Template.indent([
182
- `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
183
- `${federationGlobal}.prefetch()`,
184
- "}"
185
- ])
186
- ]);
187
- }
188
- static setRemoteIdentifier() {
189
- const federationGlobal = getFederationGlobalScope(
190
- RuntimeGlobals || {}
191
- );
192
- return Template.indent([`${federationGlobal}.isMFRemote = true;`]);
193
- }
194
- static removeRemoteIdentifier() {
195
- const federationGlobal = getFederationGlobalScope(
196
- RuntimeGlobals || {}
197
- );
198
- return Template.indent([`${federationGlobal}.isMFRemote = false;`]);
199
- }
200
- };
201
- // Annotate the CommonJS export names for ESM import in node:
202
- 0 && (module.exports = {
203
- PrefetchPlugin,
204
- getFederationGlobalScope
205
- });
@@ -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
- };
@@ -1,57 +0,0 @@
1
- import "../chunk-EZUCZHGV.js";
2
-
3
- // src/cli/babel.ts
4
- import path from "path";
5
- var attribute = "id";
6
- var hookId = "usePrefetch";
7
- var importPackage = "@module-federation/data-prefetch/react";
8
- var babel_default = (babel, options) => {
9
- const t = babel.types;
10
- let shouldHandle = false;
11
- let scope = "";
12
- const { name, exposes } = options;
13
- if (!exposes) {
14
- return {};
15
- }
16
- const exposesKey = Object.keys(exposes);
17
- const processedExposes = exposesKey.map((expose) => ({
18
- key: expose.replace(".", ""),
19
- value: path.resolve(
20
- typeof exposes[expose] === "string" ? exposes[expose] : exposes[expose].import
21
- )
22
- }));
23
- return {
24
- visitor: {
25
- ImportDeclaration(nodePath, state) {
26
- const source = nodePath.node.source.value;
27
- const { specifiers } = nodePath.node;
28
- const { filename } = state.file.opts;
29
- if (source === importPackage) {
30
- shouldHandle = specifiers.some(
31
- (specifier) => specifier.imported && specifier.imported.name === hookId && processedExposes.find(
32
- (expose) => expose.value === filename && (scope = expose.key)
33
- )
34
- );
35
- }
36
- },
37
- CallExpression(nodePath) {
38
- if (shouldHandle && t.isIdentifier(nodePath.node.callee, { name: hookId }) && nodePath.node.arguments.length > 0) {
39
- const objectExpression = nodePath.node.arguments[0];
40
- if (objectExpression && t.isObjectExpression(objectExpression) && !objectExpression.properties.find(
41
- (p) => p.key.name === attribute
42
- )) {
43
- objectExpression.properties.push(
44
- t.objectProperty(
45
- t.identifier(attribute),
46
- t.stringLiteral(name + scope)
47
- )
48
- );
49
- }
50
- }
51
- }
52
- }
53
- };
54
- };
55
- export {
56
- babel_default as default
57
- };