@module-federation/data-prefetch
Advanced tools
Comparing version 0.0.0-next-20240909062831 to 0.0.0-next-20240909073915
@module-federation/data-prefetch | ||
## 0.0.0-next-20240909062831 | ||
## 0.0.0-next-20240909073915 | ||
@@ -8,3 +8,3 @@ ### Patch Changes | ||
- 3ddab21: feat(@module-federation/data-prefetch): support data prefetch in Module Federation | ||
- @module-federation/runtime@0.0.0-next-20240909062831 | ||
- @module-federation/sdk@0.0.0-next-20240909062831 | ||
- @module-federation/runtime@0.0.0-next-20240909073915 | ||
- @module-federation/sdk@0.0.0-next-20240909073915 |
import { | ||
prefetchPlugin | ||
} from "./chunk-H3DTWHY4.js"; | ||
} from "./chunk-YXTPJG2M.js"; | ||
import "./chunk-TTJJJ2WZ.js"; | ||
@@ -5,0 +5,0 @@ import { |
import { | ||
plugin_default, | ||
prefetchPlugin | ||
} from "./chunk-H3DTWHY4.js"; | ||
} from "./chunk-YXTPJG2M.js"; | ||
import "./chunk-TTJJJ2WZ.js"; | ||
@@ -6,0 +6,0 @@ import "./chunk-57ZXBP7D.js"; |
@@ -237,3 +237,5 @@ "use strict"; | ||
const exports = instance.getExposeExports(id); | ||
logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`); | ||
logger_default.info( | ||
`1. Start Prefetch initContainer: ${id} - ${performance.now()}` | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
@@ -259,2 +261,64 @@ const value = instance.prefetch({ | ||
}, | ||
afterResolve(options) { | ||
const { remoteSnapshot, remoteInfo, id, origin } = options; | ||
const snapshot = remoteSnapshot; | ||
const { name } = remoteInfo; | ||
const prefetchOptions = { | ||
name, | ||
remote: remoteInfo, | ||
origin, | ||
remoteSnapshot: snapshot | ||
}; | ||
const signal = getSignalFromManifest(snapshot); | ||
if (!signal) { | ||
return options; | ||
} | ||
const inited = loadingArray.some((info) => info.id === id); | ||
if (!inited) { | ||
return options; | ||
} | ||
if (sharedFlag !== strategy) { | ||
throw new Error( | ||
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'` | ||
); | ||
} | ||
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions); | ||
let prefetchUrl; | ||
if (snapshot.prefetchEntry) { | ||
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry); | ||
} | ||
const index = loadingArray.findIndex((loading) => loading.id === id); | ||
if (index !== -1) { | ||
loadingArray.splice(index, 1); | ||
} | ||
const promise = instance.loadEntry(prefetchUrl).then(async () => { | ||
const projectExports = instance.getProjectExports(); | ||
if (projectExports instanceof Promise) { | ||
await projectExports; | ||
} | ||
return Promise.resolve().then(() => { | ||
const exports = instance.getExposeExports(id); | ||
logger_default.info( | ||
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}` | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
const value = instance.prefetch({ | ||
id, | ||
functionId: k | ||
}); | ||
const functionId = k; | ||
return { | ||
value, | ||
functionId | ||
}; | ||
}); | ||
return result; | ||
}); | ||
}); | ||
loadingArray.push({ | ||
id, | ||
promise | ||
}); | ||
return options; | ||
}, | ||
async onLoad(options) { | ||
@@ -261,0 +325,0 @@ var _a2; |
@@ -233,3 +233,5 @@ "use strict"; | ||
const exports = instance.getExposeExports(id); | ||
logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`); | ||
logger_default.info( | ||
`1. Start Prefetch initContainer: ${id} - ${performance.now()}` | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
@@ -255,2 +257,64 @@ const value = instance.prefetch({ | ||
}, | ||
afterResolve(options) { | ||
const { remoteSnapshot, remoteInfo, id, origin } = options; | ||
const snapshot = remoteSnapshot; | ||
const { name } = remoteInfo; | ||
const prefetchOptions = { | ||
name, | ||
remote: remoteInfo, | ||
origin, | ||
remoteSnapshot: snapshot | ||
}; | ||
const signal = getSignalFromManifest(snapshot); | ||
if (!signal) { | ||
return options; | ||
} | ||
const inited = loadingArray.some((info) => info.id === id); | ||
if (!inited) { | ||
return options; | ||
} | ||
if (sharedFlag !== strategy) { | ||
throw new Error( | ||
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'` | ||
); | ||
} | ||
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions); | ||
let prefetchUrl; | ||
if (snapshot.prefetchEntry) { | ||
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry); | ||
} | ||
const index = loadingArray.findIndex((loading) => loading.id === id); | ||
if (index !== -1) { | ||
loadingArray.splice(index, 1); | ||
} | ||
const promise = instance.loadEntry(prefetchUrl).then(async () => { | ||
const projectExports = instance.getProjectExports(); | ||
if (projectExports instanceof Promise) { | ||
await projectExports; | ||
} | ||
return Promise.resolve().then(() => { | ||
const exports = instance.getExposeExports(id); | ||
logger_default.info( | ||
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}` | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
const value = instance.prefetch({ | ||
id, | ||
functionId: k | ||
}); | ||
const functionId = k; | ||
return { | ||
value, | ||
functionId | ||
}; | ||
}); | ||
return result; | ||
}); | ||
}); | ||
loadingArray.push({ | ||
id, | ||
promise | ||
}); | ||
return options; | ||
}, | ||
async onLoad(options) { | ||
@@ -257,0 +321,0 @@ var _a2; |
{ | ||
"name": "@module-federation/data-prefetch", | ||
"description": "Module Federation Data Prefetch", | ||
"version": "0.0.0-next-20240909062831", | ||
"version": "0.0.0-next-20240909073915", | ||
"author": "nieyan <nyqykk@foxmail.com>", | ||
@@ -88,4 +88,4 @@ "homepage": "https://github.com/module-federation/core", | ||
"fs-extra": "9.1.0", | ||
"@module-federation/sdk": "0.0.0-next-20240909062831", | ||
"@module-federation/runtime": "0.0.0-next-20240909062831" | ||
"@module-federation/sdk": "0.0.0-next-20240909073915", | ||
"@module-federation/runtime": "0.0.0-next-20240909073915" | ||
}, | ||
@@ -92,0 +92,0 @@ "scripts": { |
@@ -58,3 +58,2 @@ import type { FederationRuntimePlugin } from '@module-federation/runtime/types'; | ||
} | ||
// @ts-ignore | ||
const promise = instance.loadEntry(prefetchUrl).then(async () => { | ||
@@ -67,3 +66,5 @@ const projectExports = instance!.getProjectExports(); | ||
const exports = instance!.getExposeExports(id); | ||
logger.info(`1. Start Prefetch: ${id} - ${performance.now()}`); | ||
logger.info( | ||
`1. Start Prefetch initContainer: ${id} - ${performance.now()}`, | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
@@ -92,2 +93,76 @@ const value = instance!.prefetch({ | ||
afterResolve(options) { | ||
const { remoteSnapshot, remoteInfo, id, origin } = options; | ||
const snapshot = remoteSnapshot as ModuleInfo; | ||
const { name } = remoteInfo; | ||
const prefetchOptions = { | ||
name, | ||
remote: remoteInfo, | ||
origin, | ||
remoteSnapshot: snapshot, | ||
}; | ||
const signal = getSignalFromManifest(snapshot); | ||
if (!signal) { | ||
return options; | ||
} | ||
const inited = loadingArray.some((info) => info.id === id); | ||
if (!inited) { | ||
return options; | ||
} | ||
if (sharedFlag !== strategy) { | ||
throw new Error( | ||
`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`, | ||
); | ||
} | ||
const instance = | ||
MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions); | ||
let prefetchUrl; | ||
// @ts-expect-error | ||
if (snapshot.prefetchEntry) { | ||
// @ts-expect-error | ||
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry as string); | ||
} | ||
const index = loadingArray.findIndex((loading) => loading.id === id); | ||
// clear cache | ||
if (index !== -1) { | ||
loadingArray.splice(index, 1); | ||
} | ||
const promise = instance.loadEntry(prefetchUrl).then(async () => { | ||
const projectExports = instance!.getProjectExports(); | ||
if (projectExports instanceof Promise) { | ||
await projectExports; | ||
} | ||
return Promise.resolve().then(() => { | ||
const exports = instance!.getExposeExports(id); | ||
logger.info( | ||
`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`, | ||
); | ||
const result = Object.keys(exports).map((k) => { | ||
const value = instance!.prefetch({ | ||
id, | ||
functionId: k, | ||
}); | ||
const functionId = k; | ||
return { | ||
value, | ||
functionId, | ||
}; | ||
}); | ||
return result; | ||
}); | ||
}); | ||
loadingArray.push({ | ||
id, | ||
promise, | ||
}); | ||
return options; | ||
}, | ||
async onLoad(options) { | ||
@@ -94,0 +169,0 @@ const { remote, id } = options; |
113547
3379
+ Added@module-federation/runtime@0.0.0-next-20240909073915(transitive)
+ Added@module-federation/sdk@0.0.0-next-20240909073915(transitive)
- Removed@module-federation/runtime@0.0.0-next-20240909062831(transitive)
- Removed@module-federation/sdk@0.0.0-next-20240909062831(transitive)