@module-federation/data-prefetch 0.0.0-next-20240808083234 → 0.0.0-next-20240909073915

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.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,9 @@
1
1
  @module-federation/data-prefetch
2
2
 
3
- ## 0.0.0-next-20240808083234
3
+ ## 0.0.0-next-20240909073915
4
4
 
5
5
  ### Patch Changes
6
6
 
7
7
  - 3ddab21: feat(@module-federation/data-prefetch): support data prefetch in Module Federation
8
- - @module-federation/runtime@0.0.0-next-20240808083234
9
- - @module-federation/sdk@0.0.0-next-20240808083234
8
+ - @module-federation/runtime@0.0.0-next-20240909073915
9
+ - @module-federation/sdk@0.0.0-next-20240909073915
@@ -45,17 +45,9 @@ var MFDataPrefetch = class {
45
45
  const remoteInfo = getRemoteInfo(remote);
46
46
  const module = origin.moduleCache.get(remoteInfo.name);
47
47
  return getRemoteEntry({
48
+ origin,
48
49
  remoteInfo,
49
- remoteEntryExports: module ? module.remoteEntryExports : void 0,
50
- createScriptHook: (url) => {
51
- const res = origin.loaderHook.lifecycle.createScript.emit({
52
- url
53
- });
54
- if (res instanceof HTMLScriptElement) {
55
- return res;
56
- }
57
- return;
58
- }
50
+ remoteEntryExports: module ? module.remoteEntryExports : void 0
59
51
  });
60
52
  }
61
53
  }
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MFDataPrefetch
3
- } from "./chunk-XYNYYK45.js";
3
+ } from "./chunk-57ZXBP7D.js";
4
4
  import {
5
5
  getScope
6
6
  } from "./chunk-VWDP6NMM.js";
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-TTJJJ2WZ.js";
4
4
  import {
5
5
  MFDataPrefetch
6
- } from "./chunk-XYNYYK45.js";
6
+ } from "./chunk-57ZXBP7D.js";
7
7
  import {
8
8
  getSignalFromManifest
9
9
  } from "./chunk-VWDP6NMM.js";
@@ -50,7 +50,71 @@ var prefetchPlugin = () => ({
50
50
  }
51
51
  return Promise.resolve().then(() => {
52
52
  const exports = instance.getExposeExports(id);
53
- logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
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
+ );
54
118
  const result = Object.keys(exports).map((k) => {
55
119
  const value = instance.prefetch({
56
120
  id,
package/dist/esm/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  prefetchPlugin
3
- } from "./chunk-TW3B6M25.js";
3
+ } from "./chunk-YXTPJG2M.js";
4
4
  import "./chunk-TTJJJ2WZ.js";
5
5
  import {
6
6
  MFDataPrefetch
7
- } from "./chunk-XYNYYK45.js";
7
+ } from "./chunk-57ZXBP7D.js";
8
8
  import "./chunk-VWDP6NMM.js";
9
9
  export {
10
10
  MFDataPrefetch,
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  plugin_default,
3
3
  prefetchPlugin
4
- } from "./chunk-TW3B6M25.js";
4
+ } from "./chunk-YXTPJG2M.js";
5
5
  import "./chunk-TTJJJ2WZ.js";
6
- import "./chunk-XYNYYK45.js";
6
+ import "./chunk-57ZXBP7D.js";
7
7
  import "./chunk-VWDP6NMM.js";
8
8
  export {
9
9
  plugin_default as default,
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  prefetch
3
- } from "../chunk-V2LJYA45.js";
3
+ } from "../chunk-OIIVUMED.js";
4
4
  import {
5
5
  logger_default
6
6
  } from "../chunk-TTJJJ2WZ.js";
7
7
  import {
8
8
  MFDataPrefetch
9
- } from "../chunk-XYNYYK45.js";
9
+ } from "../chunk-57ZXBP7D.js";
10
10
  import {
11
11
  getScope
12
12
  } from "../chunk-VWDP6NMM.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  prefetch
3
- } from "../chunk-V2LJYA45.js";
4
- import "../chunk-XYNYYK45.js";
3
+ } from "../chunk-OIIVUMED.js";
4
+ import "../chunk-57ZXBP7D.js";
5
5
  import "../chunk-VWDP6NMM.js";
6
6
  export {
7
7
  prefetch
package/dist/index.js CHANGED
@@ -81,17 +81,9 @@ var MFDataPrefetch = class {
81
81
  const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
82
82
  const module2 = origin.moduleCache.get(remoteInfo.name);
83
83
  return (0, import_runtime.getRemoteEntry)({
84
+ origin,
84
85
  remoteInfo,
85
- remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
86
- createScriptHook: (url) => {
87
- const res = origin.loaderHook.lifecycle.createScript.emit({
88
- url
89
- });
90
- if (res instanceof HTMLScriptElement) {
91
- return res;
92
- }
93
- return;
94
- }
86
+ remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
95
87
  });
96
88
  }
97
89
  }
@@ -242,7 +234,71 @@ var prefetchPlugin = () => ({
242
234
  }
243
235
  return Promise.resolve().then(() => {
244
236
  const exports = instance.getExposeExports(id);
245
- logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
237
+ logger_default.info(
238
+ `1. Start Prefetch initContainer: ${id} - ${performance.now()}`
239
+ );
240
+ const result = Object.keys(exports).map((k) => {
241
+ const value = instance.prefetch({
242
+ id,
243
+ functionId: k
244
+ });
245
+ const functionId = k;
246
+ return {
247
+ value,
248
+ functionId
249
+ };
250
+ });
251
+ return result;
252
+ });
253
+ });
254
+ loadingArray.push({
255
+ id,
256
+ promise
257
+ });
258
+ return options;
259
+ },
260
+ afterResolve(options) {
261
+ const { remoteSnapshot, remoteInfo, id, origin } = options;
262
+ const snapshot = remoteSnapshot;
263
+ const { name } = remoteInfo;
264
+ const prefetchOptions = {
265
+ name,
266
+ remote: remoteInfo,
267
+ origin,
268
+ remoteSnapshot: snapshot
269
+ };
270
+ const signal = getSignalFromManifest(snapshot);
271
+ if (!signal) {
272
+ return options;
273
+ }
274
+ const inited = loadingArray.some((info) => info.id === id);
275
+ if (!inited) {
276
+ return options;
277
+ }
278
+ if (sharedFlag !== strategy) {
279
+ throw new Error(
280
+ `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
281
+ );
282
+ }
283
+ const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
284
+ let prefetchUrl;
285
+ if (snapshot.prefetchEntry) {
286
+ prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
287
+ }
288
+ const index = loadingArray.findIndex((loading) => loading.id === id);
289
+ if (index !== -1) {
290
+ loadingArray.splice(index, 1);
291
+ }
292
+ const promise = instance.loadEntry(prefetchUrl).then(async () => {
293
+ const projectExports = instance.getProjectExports();
294
+ if (projectExports instanceof Promise) {
295
+ await projectExports;
296
+ }
297
+ return Promise.resolve().then(() => {
298
+ const exports = instance.getExposeExports(id);
299
+ logger_default.info(
300
+ `1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
301
+ );
246
302
  const result = Object.keys(exports).map((k) => {
247
303
  const value = instance.prefetch({
248
304
  id,
package/dist/plugin.js CHANGED
@@ -80,17 +80,9 @@ var MFDataPrefetch = class {
80
80
  const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
81
81
  const module2 = origin.moduleCache.get(remoteInfo.name);
82
82
  return (0, import_runtime.getRemoteEntry)({
83
+ origin,
83
84
  remoteInfo,
84
- remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
85
- createScriptHook: (url) => {
86
- const res = origin.loaderHook.lifecycle.createScript.emit({
87
- url
88
- });
89
- if (res instanceof HTMLScriptElement) {
90
- return res;
91
- }
92
- return;
93
- }
85
+ remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
94
86
  });
95
87
  }
96
88
  }
@@ -238,7 +230,71 @@ var prefetchPlugin = () => ({
238
230
  }
239
231
  return Promise.resolve().then(() => {
240
232
  const exports = instance.getExposeExports(id);
241
- logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
233
+ logger_default.info(
234
+ `1. Start Prefetch initContainer: ${id} - ${performance.now()}`
235
+ );
236
+ const result = Object.keys(exports).map((k) => {
237
+ const value = instance.prefetch({
238
+ id,
239
+ functionId: k
240
+ });
241
+ const functionId = k;
242
+ return {
243
+ value,
244
+ functionId
245
+ };
246
+ });
247
+ return result;
248
+ });
249
+ });
250
+ loadingArray.push({
251
+ id,
252
+ promise
253
+ });
254
+ return options;
255
+ },
256
+ afterResolve(options) {
257
+ const { remoteSnapshot, remoteInfo, id, origin } = options;
258
+ const snapshot = remoteSnapshot;
259
+ const { name } = remoteInfo;
260
+ const prefetchOptions = {
261
+ name,
262
+ remote: remoteInfo,
263
+ origin,
264
+ remoteSnapshot: snapshot
265
+ };
266
+ const signal = getSignalFromManifest(snapshot);
267
+ if (!signal) {
268
+ return options;
269
+ }
270
+ const inited = loadingArray.some((info) => info.id === id);
271
+ if (!inited) {
272
+ return options;
273
+ }
274
+ if (sharedFlag !== strategy) {
275
+ throw new Error(
276
+ `[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`
277
+ );
278
+ }
279
+ const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
280
+ let prefetchUrl;
281
+ if (snapshot.prefetchEntry) {
282
+ prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
283
+ }
284
+ const index = loadingArray.findIndex((loading) => loading.id === id);
285
+ if (index !== -1) {
286
+ loadingArray.splice(index, 1);
287
+ }
288
+ const promise = instance.loadEntry(prefetchUrl).then(async () => {
289
+ const projectExports = instance.getProjectExports();
290
+ if (projectExports instanceof Promise) {
291
+ await projectExports;
292
+ }
293
+ return Promise.resolve().then(() => {
294
+ const exports = instance.getExposeExports(id);
295
+ logger_default.info(
296
+ `1. Start Prefetch afterResolve: ${id} - ${performance.now()}`
297
+ );
242
298
  const result = Object.keys(exports).map((k) => {
243
299
  const value = instance.prefetch({
244
300
  id,
@@ -83,17 +83,9 @@ var MFDataPrefetch = class {
83
83
  const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
84
84
  const module2 = origin.moduleCache.get(remoteInfo.name);
85
85
  return (0, import_runtime.getRemoteEntry)({
86
+ origin,
86
87
  remoteInfo,
87
- remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
88
- createScriptHook: (url) => {
89
- const res = origin.loaderHook.lifecycle.createScript.emit({
90
- url
91
- });
92
- if (res instanceof HTMLScriptElement) {
93
- return res;
94
- }
95
- return;
96
- }
88
+ remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
97
89
  });
98
90
  }
99
91
  }
@@ -76,17 +76,9 @@ var MFDataPrefetch = class {
76
76
  const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
77
77
  const module2 = origin.moduleCache.get(remoteInfo.name);
78
78
  return (0, import_runtime.getRemoteEntry)({
79
+ origin,
79
80
  remoteInfo,
80
- remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
81
- createScriptHook: (url) => {
82
- const res = origin.loaderHook.lifecycle.createScript.emit({
83
- url
84
- });
85
- if (res instanceof HTMLScriptElement) {
86
- return res;
87
- }
88
- return;
89
- }
81
+ remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
90
82
  });
91
83
  }
92
84
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@module-federation/data-prefetch",
3
3
  "description": "Module Federation Data Prefetch",
4
- "version": "0.0.0-next-20240808083234",
4
+ "version": "0.0.0-next-20240909073915",
5
5
  "author": "nieyan <nyqykk@foxmail.com>",
6
6
  "homepage": "https://github.com/module-federation/core",
7
7
  "license": "MIT",
@@ -85,8 +85,8 @@
85
85
  },
86
86
  "dependencies": {
87
87
  "fs-extra": "9.1.0",
88
- "@module-federation/sdk": "0.0.0-next-20240808083234",
89
- "@module-federation/runtime": "0.0.0-next-20240808083234"
88
+ "@module-federation/sdk": "0.0.0-next-20240909073915",
89
+ "@module-federation/runtime": "0.0.0-next-20240909073915"
90
90
  },
91
91
  "scripts": {
92
92
  "dev": "cross-env WATCH=true tsup",
package/src/plugin.ts CHANGED
@@ -55,7 +55,6 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
55
55
  if (exist) {
56
56
  return options;
57
57
  }
58
- // @ts-ignore
59
58
  const promise = instance.loadEntry(prefetchUrl).then(async () => {
60
59
  const projectExports = instance!.getProjectExports();
61
60
  if (projectExports instanceof Promise) {
@@ -63,7 +62,9 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
63
62
  }
64
63
  return Promise.resolve().then(() => {
65
64
  const exports = instance!.getExposeExports(id);
66
- logger.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
65
+ logger.info(
66
+ `1. Start Prefetch initContainer: ${id} - ${performance.now()}`,
67
+ );
67
68
  const result = Object.keys(exports).map((k) => {
68
69
  const value = instance!.prefetch({
69
70
  id,
@@ -87,6 +88,80 @@ export const prefetchPlugin = (): FederationRuntimePlugin => ({
87
88
  return options;
88
89
  },
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
+ },
90
165
  async onLoad(options) {
91
166
  const { remote, id } = options;
92
167
  const { name } = remote;
package/src/prefetch.ts CHANGED
@@ -82,17 +82,9 @@ export class MFDataPrefetch {
82
82
  const remoteInfo = getRemoteInfo(remote as Remote);
83
83
  const module = origin!.moduleCache.get(remoteInfo.name);
84
84
  return getRemoteEntry({
85
+ origin: origin!,
85
86
  remoteInfo,
86
87
  remoteEntryExports: module ? module.remoteEntryExports : undefined,
87
- createScriptHook: (url: string) => {
88
- const res = origin!.loaderHook.lifecycle.createScript.emit({
89
- url,
90
- });
91
- if (res instanceof HTMLScriptElement) {
92
- return res;
93
- }
94
- return;
95
- },
96
88
  });
97
89
  }
98
90
  }