forked from module-federation/module-federation-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control-share.js
70 lines (63 loc) · 2.39 KB
/
control-share.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const runtimeStore = {};
const ControlScopePlugin = () => {
return {
name: 'control-scope-plugin',
beforeInit: args => {
runtimeStore.name = args.options.name;
return args;
},
init: args => args,
beforeRequest: args => {
console.log('beforeRequest: ', args);
return args;
},
afterResolve: args => args,
onLoad: args => args,
resolveShare: args => {
if (!localStorage.getItem('formDataVMSC')) return args;
const overrides = JSON.parse(localStorage.getItem('formDataVMSC'));
const originalResolver = args.resolver;
const { shareScopeMap, scope, pkgName, version, GlobalFederation } = args;
args.resolver = function () {
if (!overrides[runtimeStore.name]) {
return originalResolver();
}
const overrideVersion = overrides[runtimeStore.name][pkgName];
const matchingInstance = GlobalFederation.__INSTANCES__.find(instance => {
return instance.options.shared[pkgName].version === overrideVersion;
});
if (matchingInstance) {
const current = shareScopeMap[scope][pkgName][version],
override = matchingInstance.options.shared[pkgName];
if (current.from === override.from) return current;
const originInstance = GlobalFederation.__INSTANCES__.find(instance => {
return instance.options.name === current.from;
});
originInstance.options.shared[pkgName].useIn = originInstance.options.shared[
pkgName
].useIn.filter(i => i !== store.name);
shareScopeMap[scope][pkgName][version] = matchingInstance.options.shared[pkgName];
if (!shareScopeMap[scope][pkgName][version].useIn.includes(store.name)) {
shareScopeMap[scope][pkgName][version].useIn.push(store.name);
}
return matchingInstance.options.shared[pkgName];
} else {
console.log('No matching instance found for overrideVersion', overrideVersion);
}
return originalResolver();
};
return args;
},
loadShare: async args => {
console.log('loadShare:', args);
},
beforeLoadShare: async args => {
console.log('beforeLoadShare:', args);
while (__FEDERATION__.__INSTANCES__.length <= 1) {
await new Promise(r => setTimeout(r, 50));
}
return args;
},
};
};
export default ControlScopePlugin;