Skip to content

Commit ff0527f

Browse files
authored
fix: multiple version of a package loaded causes problem (#684)
1 parent 6316562 commit ff0527f

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

packages/lib/src/prod/expose-production.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ export function prodExposePlugin(
144144
export const init =(shareScope) => {
145145
globalThis.__federation_shared__= globalThis.__federation_shared__|| {};
146146
Object.entries(shareScope).forEach(([key, value]) => {
147-
const versionKey = Object.keys(value)[0];
148-
const versionValue = Object.values(value)[0];
149-
const scope = versionValue.scope || 'default'
150-
globalThis.__federation_shared__[scope] = globalThis.__federation_shared__[scope] || {};
151-
const shared= globalThis.__federation_shared__[scope];
152-
(shared[key] = shared[key]||{})[versionKey] = versionValue;
147+
for (const [versionKey, versionValue] of Object.entries(value)) {
148+
const scope = versionValue.scope || 'default'
149+
globalThis.__federation_shared__[scope] = globalThis.__federation_shared__[scope] || {};
150+
const shared= globalThis.__federation_shared__[scope];
151+
(shared[key] = shared[key]||{})[versionKey] = versionValue;
152+
}
153153
});
154154
}`
155155
},

packages/lib/src/prod/federation_fn_import.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ async function getSharedFromRuntime(name, shareScope) {
1919
let module = null
2020
if (globalThis?.__federation_shared__?.[shareScope]?.[name]) {
2121
const versionObj = globalThis.__federation_shared__[shareScope][name]
22-
const versionKey = Object.keys(versionObj)[0]
23-
const versionValue = Object.values(versionObj)[0]
24-
if (moduleMap[name]?.requiredVersion) {
25-
// judge version satisfy
26-
if (satisfy(versionKey, moduleMap[name].requiredVersion)) {
22+
const requiredVersion = moduleMap[name]?.requiredVersion
23+
const hasRequiredVersion = !!requiredVersion
24+
if (hasRequiredVersion) {
25+
const versionKey = Object.keys(versionObj).find((version) =>
26+
satisfy(version, requiredVersion)
27+
)
28+
if (versionKey) {
29+
const versionValue = versionObj[versionKey]
2730
module = await (await versionValue.get())()
2831
} else {
2932
console.log(
3033
`provider support ${name}(${versionKey}) is not satisfied requiredVersion(\${moduleMap[name].requiredVersion})`
3134
)
3235
}
3336
} else {
37+
const versionKey = Object.keys(versionObj)[0]
38+
const versionValue = versionObj[versionKey]
3439
module = await (await versionValue.get())()
3540
}
3641
}

0 commit comments

Comments
 (0)