@module-federation/runtime 0.0.0-next-20240411081414 → 0.0.0-next-20240411125344
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/README.md +62 -2
- package/dist/helpers.esm.js +1 -1
- package/dist/index.cjs.js +64 -32
- package/dist/index.esm.js +66 -34
- package/dist/package.json +6 -6
- package/dist/share.cjs.js +36 -6
- package/dist/share.esm.js +35 -7
- package/dist/src/core.d.ts +8 -2
- package/dist/src/type/config.d.ts +6 -2
- package/dist/src/utils/share.d.ts +11 -5
- package/dist/src/utils/tool.d.ts +1 -0
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -50,9 +50,25 @@ type InitOptions {
|
|
|
50
50
|
// List of dependencies that need to be shared by the current host
|
|
51
51
|
// When using the build plugin, users can configure the dependencies that need to be shared in the build plugin, and the build plugin will inject the dependencies that need to be shared into the runtime shared configuration.
|
|
52
52
|
// Shared must be manually passed in the version instance reference when it is passed in at runtime, because it cannot be directly passed in at runtime.
|
|
53
|
-
shared?:
|
|
53
|
+
shared?: {
|
|
54
|
+
[pkgName: string]: ShareArgs | ShareArgs[];
|
|
55
|
+
};
|
|
54
56
|
};
|
|
55
57
|
|
|
58
|
+
type ShareArgs =
|
|
59
|
+
| (SharedBaseArgs & { get: SharedGetter })
|
|
60
|
+
| (SharedBaseArgs & { lib: () => Module });
|
|
61
|
+
|
|
62
|
+
type SharedBaseArgs = {
|
|
63
|
+
version: string;
|
|
64
|
+
shareConfig?: SharedConfig;
|
|
65
|
+
scope?: string | Array<string>;
|
|
66
|
+
deps?: Array<string>;
|
|
67
|
+
strategy?: 'version-first' | 'loaded-first';
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
type SharedGetter = (() => () => Module) | (() => Promise<() => Module>);
|
|
71
|
+
|
|
56
72
|
type RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {
|
|
57
73
|
alias?: string;
|
|
58
74
|
};
|
|
@@ -120,7 +136,7 @@ loadRemote('app2/util').then((m) => m.add(1, 2, 3));
|
|
|
120
136
|
|
|
121
137
|
### loadShare
|
|
122
138
|
|
|
123
|
-
- Type: `loadShare(pkgName: string)`
|
|
139
|
+
- Type: `loadShare(pkgName: string, extraOptions?: { customShareInfo?: Partial<Shared>;resolver?: (sharedOptions: ShareInfos[string]) => Shared;})`
|
|
124
140
|
- Gets the `share` dependency. When there are `share` dependencies that match the current `host` in the global environment, the existing and satisfying `share` dependencies will be reused first. Otherwise, load its own dependencies and store them in the global cache.
|
|
125
141
|
- This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.
|
|
126
142
|
|
|
@@ -161,6 +177,50 @@ loadShare('react').then((reactFactory) => {
|
|
|
161
177
|
});
|
|
162
178
|
```
|
|
163
179
|
|
|
180
|
+
If has set multiple version shared, `loadShare` will return the loaded and has max version shared. The behavior can be controlled by set `extraOptions.resolver`:
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
import { init, loadRemote, loadShare } from '@module-federation/runtime';
|
|
184
|
+
|
|
185
|
+
init({
|
|
186
|
+
name: '@demo/main-app',
|
|
187
|
+
remotes: [],
|
|
188
|
+
shared: {
|
|
189
|
+
react: [
|
|
190
|
+
{
|
|
191
|
+
version: '17.0.0',
|
|
192
|
+
scope: 'default',
|
|
193
|
+
get: async ()=>() => ({ version: '17.0.0)' }),
|
|
194
|
+
shareConfig: {
|
|
195
|
+
singleton: true,
|
|
196
|
+
requiredVersion: '^17.0.0',
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
version: '18.0.0',
|
|
201
|
+
scope: 'default',
|
|
202
|
+
// pass lib means the shared has loaded
|
|
203
|
+
lib: () => ({ version: '18.0.0)' }),
|
|
204
|
+
shareConfig: {
|
|
205
|
+
singleton: true,
|
|
206
|
+
requiredVersion: '^18.0.0',
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
loadShare('react', {
|
|
214
|
+
resolver: (sharedOptions) => {
|
|
215
|
+
return (
|
|
216
|
+
sharedOptions.find((i) => i.version === '17.0.0') ?? sharedOptions[0]
|
|
217
|
+
);
|
|
218
|
+
},
|
|
219
|
+
}).then((reactFactory) => {
|
|
220
|
+
console.log(reactFactory()); // { version: '17.0.0)' }
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
164
224
|
### preloadRemote
|
|
165
225
|
|
|
166
226
|
- Type: `preloadRemote(preloadOptions: Array<PreloadRemoteArgs>)`
|
package/dist/helpers.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as getRegisteredShare, v as getGlobalShareScope, G as Global, J as nativeGlobal, K as resetFederationGlobalInfo, E as getGlobalFederationInstance, H as setGlobalFederationInstance, F as getGlobalFederationConstructor, C as setGlobalFederationConstructor, l as getInfoWithoutType, u as getGlobalSnapshot, L as getTargetSnapshotInfoByModuleInfo, q as getGlobalSnapshotInfoByModuleInfo, t as setGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, c as getRemoteEntryExports, I as registerGlobalPlugins, g as getGlobalHostPlugins, m as getPreloaded, n as setPreloaded } from './share.esm.js';
|
|
2
2
|
|
|
3
3
|
const ShareUtils = {
|
|
4
4
|
getRegisteredShare,
|
package/dist/index.cjs.js
CHANGED
|
@@ -813,23 +813,33 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
813
813
|
}
|
|
814
814
|
}, true, memo, remoteSnapshot);
|
|
815
815
|
if (remoteSnapshot.shared) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
const shareInfo = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
819
|
-
// When data is downgraded, the shared configuration may be different.
|
|
820
|
-
if (!shareInfo) {
|
|
821
|
-
return;
|
|
822
|
-
}
|
|
823
|
-
const registeredShared = share.getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
816
|
+
const collectSharedAssets = (shareInfo, snapshotShared)=>{
|
|
817
|
+
const registeredShared = share.getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
824
818
|
// If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
|
|
825
819
|
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
826
|
-
|
|
820
|
+
snapshotShared.assets.js.sync.forEach((asset)=>{
|
|
827
821
|
loadedSharedJsAssets.add(asset);
|
|
828
822
|
});
|
|
829
|
-
|
|
823
|
+
snapshotShared.assets.css.sync.forEach((asset)=>{
|
|
830
824
|
loadedSharedCssAssets.add(asset);
|
|
831
825
|
});
|
|
832
826
|
}
|
|
827
|
+
};
|
|
828
|
+
remoteSnapshot.shared.forEach((shared)=>{
|
|
829
|
+
var _options_shared;
|
|
830
|
+
const shareInfos = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
831
|
+
if (!shareInfos) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
// if no version, preload all shared
|
|
835
|
+
const sharedOptions = shared.version ? shareInfos.find((s)=>s.version === shared.version) : shareInfos;
|
|
836
|
+
if (!sharedOptions) {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const arrayShareInfo = share.arrayOptions(sharedOptions);
|
|
840
|
+
arrayShareInfo.forEach((s)=>{
|
|
841
|
+
collectSharedAssets(s, shared);
|
|
842
|
+
});
|
|
833
843
|
});
|
|
834
844
|
}
|
|
835
845
|
const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
|
|
@@ -1114,13 +1124,16 @@ class FederationHost {
|
|
|
1114
1124
|
this.options = options;
|
|
1115
1125
|
return options;
|
|
1116
1126
|
}
|
|
1117
|
-
async loadShare(pkgName,
|
|
1118
|
-
var _this_options_shared;
|
|
1127
|
+
async loadShare(pkgName, extraOptions) {
|
|
1119
1128
|
// This function performs the following steps:
|
|
1120
1129
|
// 1. Checks if the currently loaded share already exists, if not, it throws an error
|
|
1121
1130
|
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
1122
1131
|
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
|
|
1123
|
-
const shareInfo =
|
|
1132
|
+
const shareInfo = share.getTargetSharedOptions({
|
|
1133
|
+
pkgName,
|
|
1134
|
+
extraOptions,
|
|
1135
|
+
shareInfos: this.options.shared
|
|
1136
|
+
});
|
|
1124
1137
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1125
1138
|
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
1126
1139
|
await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
|
|
@@ -1179,7 +1192,7 @@ class FederationHost {
|
|
|
1179
1192
|
});
|
|
1180
1193
|
return loading;
|
|
1181
1194
|
} else {
|
|
1182
|
-
if (customShareInfo) {
|
|
1195
|
+
if (extraOptions == null ? void 0 : extraOptions.customShareInfo) {
|
|
1183
1196
|
return false;
|
|
1184
1197
|
}
|
|
1185
1198
|
const asyncLoadProcess = async ()=>{
|
|
@@ -1210,9 +1223,12 @@ class FederationHost {
|
|
|
1210
1223
|
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1211
1224
|
// 2. If lib exists in local shared, it will be used directly
|
|
1212
1225
|
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1213
|
-
loadShareSync(pkgName,
|
|
1214
|
-
|
|
1215
|
-
|
|
1226
|
+
loadShareSync(pkgName, extraOptions) {
|
|
1227
|
+
const shareInfo = share.getTargetSharedOptions({
|
|
1228
|
+
pkgName,
|
|
1229
|
+
extraOptions,
|
|
1230
|
+
shareInfos: this.options.shared
|
|
1231
|
+
});
|
|
1216
1232
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1217
1233
|
shareInfo.scope.forEach((shareScope)=>{
|
|
1218
1234
|
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
@@ -1452,9 +1468,11 @@ class FederationHost {
|
|
|
1452
1468
|
};
|
|
1453
1469
|
Object.keys(this.options.shared).forEach((shareName)=>{
|
|
1454
1470
|
const shared = this.options.shared[shareName];
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1471
|
+
shared.forEach((s)=>{
|
|
1472
|
+
if (s.scope.includes(shareScopeName)) {
|
|
1473
|
+
register(shareName, s);
|
|
1474
|
+
}
|
|
1475
|
+
});
|
|
1458
1476
|
});
|
|
1459
1477
|
if (strategy === 'version-first') {
|
|
1460
1478
|
this.options.remotes.forEach((remote)=>{
|
|
@@ -1475,7 +1493,19 @@ class FederationHost {
|
|
|
1475
1493
|
}
|
|
1476
1494
|
formatOptions(globalOptions, userOptions) {
|
|
1477
1495
|
const formatShareOptions = share.formatShareConfigs(userOptions.shared || {}, userOptions.name);
|
|
1478
|
-
const shared = _extends({}, globalOptions.shared
|
|
1496
|
+
const shared = _extends({}, globalOptions.shared);
|
|
1497
|
+
Object.keys(formatShareOptions).forEach((shareKey)=>{
|
|
1498
|
+
if (!shared[shareKey]) {
|
|
1499
|
+
shared[shareKey] = formatShareOptions[shareKey];
|
|
1500
|
+
} else {
|
|
1501
|
+
formatShareOptions[shareKey].forEach((newUserSharedOptions)=>{
|
|
1502
|
+
const isSameVersion = shared[shareKey].find((s)=>s.version === newUserSharedOptions.version);
|
|
1503
|
+
if (!isSameVersion) {
|
|
1504
|
+
shared[shareKey].push(newUserSharedOptions);
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
});
|
|
1479
1509
|
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
1480
1510
|
origin: this,
|
|
1481
1511
|
userOptions,
|
|
@@ -1493,17 +1523,19 @@ class FederationHost {
|
|
|
1493
1523
|
const sharedKeys = Object.keys(formatShareOptions);
|
|
1494
1524
|
sharedKeys.forEach((sharedKey)=>{
|
|
1495
1525
|
const sharedVal = formatShareOptions[sharedKey];
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1526
|
+
sharedVal.forEach((s)=>{
|
|
1527
|
+
const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, s, this.hooks.lifecycle.resolveShare);
|
|
1528
|
+
if (!registeredShared && s && s.lib) {
|
|
1529
|
+
this.setShared({
|
|
1530
|
+
pkgName: sharedKey,
|
|
1531
|
+
lib: s.lib,
|
|
1532
|
+
get: s.get,
|
|
1533
|
+
loaded: true,
|
|
1534
|
+
shared: s,
|
|
1535
|
+
from: userOptions.name
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
});
|
|
1507
1539
|
});
|
|
1508
1540
|
const plugins = [
|
|
1509
1541
|
...globalOptionsRes.plugins
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as
|
|
2
|
-
export {
|
|
1
|
+
import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getInfoWithoutType, m as getPreloaded, n as setPreloaded, o as getRegisteredShare, p as arrayOptions, q as getGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, t as setGlobalSnapshotInfoByModuleInfo, u as getGlobalSnapshot, G as Global, v as getGlobalShareScope, x as getTargetSharedOptions, y as formatShareConfigs, z as getBuilderId, A as isBrowserEnv, B as addUniqueItem, C as setGlobalFederationConstructor, E as getGlobalFederationInstance, F as getGlobalFederationConstructor, H as setGlobalFederationInstance } from './share.esm.js';
|
|
2
|
+
export { I as registerGlobalPlugins } from './share.esm.js';
|
|
3
3
|
import { loadScriptNode, loadScript, composeKeyWithSeparator, createLink, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
|
|
4
4
|
export { loadScript, loadScriptNode } from '@module-federation/sdk';
|
|
5
5
|
|
|
@@ -811,23 +811,33 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
811
811
|
}
|
|
812
812
|
}, true, memo, remoteSnapshot);
|
|
813
813
|
if (remoteSnapshot.shared) {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
const shareInfo = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
817
|
-
// When data is downgraded, the shared configuration may be different.
|
|
818
|
-
if (!shareInfo) {
|
|
819
|
-
return;
|
|
820
|
-
}
|
|
821
|
-
const registeredShared = getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
814
|
+
const collectSharedAssets = (shareInfo, snapshotShared)=>{
|
|
815
|
+
const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
822
816
|
// If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
|
|
823
817
|
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
824
|
-
|
|
818
|
+
snapshotShared.assets.js.sync.forEach((asset)=>{
|
|
825
819
|
loadedSharedJsAssets.add(asset);
|
|
826
820
|
});
|
|
827
|
-
|
|
821
|
+
snapshotShared.assets.css.sync.forEach((asset)=>{
|
|
828
822
|
loadedSharedCssAssets.add(asset);
|
|
829
823
|
});
|
|
830
824
|
}
|
|
825
|
+
};
|
|
826
|
+
remoteSnapshot.shared.forEach((shared)=>{
|
|
827
|
+
var _options_shared;
|
|
828
|
+
const shareInfos = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
|
|
829
|
+
if (!shareInfos) {
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
// if no version, preload all shared
|
|
833
|
+
const sharedOptions = shared.version ? shareInfos.find((s)=>s.version === shared.version) : shareInfos;
|
|
834
|
+
if (!sharedOptions) {
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
const arrayShareInfo = arrayOptions(sharedOptions);
|
|
838
|
+
arrayShareInfo.forEach((s)=>{
|
|
839
|
+
collectSharedAssets(s, shared);
|
|
840
|
+
});
|
|
831
841
|
});
|
|
832
842
|
}
|
|
833
843
|
const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
|
|
@@ -1112,13 +1122,16 @@ class FederationHost {
|
|
|
1112
1122
|
this.options = options;
|
|
1113
1123
|
return options;
|
|
1114
1124
|
}
|
|
1115
|
-
async loadShare(pkgName,
|
|
1116
|
-
var _this_options_shared;
|
|
1125
|
+
async loadShare(pkgName, extraOptions) {
|
|
1117
1126
|
// This function performs the following steps:
|
|
1118
1127
|
// 1. Checks if the currently loaded share already exists, if not, it throws an error
|
|
1119
1128
|
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
1120
1129
|
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
|
|
1121
|
-
const shareInfo =
|
|
1130
|
+
const shareInfo = getTargetSharedOptions({
|
|
1131
|
+
pkgName,
|
|
1132
|
+
extraOptions,
|
|
1133
|
+
shareInfos: this.options.shared
|
|
1134
|
+
});
|
|
1122
1135
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1123
1136
|
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
1124
1137
|
await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
|
|
@@ -1177,7 +1190,7 @@ class FederationHost {
|
|
|
1177
1190
|
});
|
|
1178
1191
|
return loading;
|
|
1179
1192
|
} else {
|
|
1180
|
-
if (customShareInfo) {
|
|
1193
|
+
if (extraOptions == null ? void 0 : extraOptions.customShareInfo) {
|
|
1181
1194
|
return false;
|
|
1182
1195
|
}
|
|
1183
1196
|
const asyncLoadProcess = async ()=>{
|
|
@@ -1208,9 +1221,12 @@ class FederationHost {
|
|
|
1208
1221
|
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1209
1222
|
// 2. If lib exists in local shared, it will be used directly
|
|
1210
1223
|
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1211
|
-
loadShareSync(pkgName,
|
|
1212
|
-
|
|
1213
|
-
|
|
1224
|
+
loadShareSync(pkgName, extraOptions) {
|
|
1225
|
+
const shareInfo = getTargetSharedOptions({
|
|
1226
|
+
pkgName,
|
|
1227
|
+
extraOptions,
|
|
1228
|
+
shareInfos: this.options.shared
|
|
1229
|
+
});
|
|
1214
1230
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1215
1231
|
shareInfo.scope.forEach((shareScope)=>{
|
|
1216
1232
|
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
@@ -1450,9 +1466,11 @@ class FederationHost {
|
|
|
1450
1466
|
};
|
|
1451
1467
|
Object.keys(this.options.shared).forEach((shareName)=>{
|
|
1452
1468
|
const shared = this.options.shared[shareName];
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1469
|
+
shared.forEach((s)=>{
|
|
1470
|
+
if (s.scope.includes(shareScopeName)) {
|
|
1471
|
+
register(shareName, s);
|
|
1472
|
+
}
|
|
1473
|
+
});
|
|
1456
1474
|
});
|
|
1457
1475
|
if (strategy === 'version-first') {
|
|
1458
1476
|
this.options.remotes.forEach((remote)=>{
|
|
@@ -1473,7 +1491,19 @@ class FederationHost {
|
|
|
1473
1491
|
}
|
|
1474
1492
|
formatOptions(globalOptions, userOptions) {
|
|
1475
1493
|
const formatShareOptions = formatShareConfigs(userOptions.shared || {}, userOptions.name);
|
|
1476
|
-
const shared = _extends({}, globalOptions.shared
|
|
1494
|
+
const shared = _extends({}, globalOptions.shared);
|
|
1495
|
+
Object.keys(formatShareOptions).forEach((shareKey)=>{
|
|
1496
|
+
if (!shared[shareKey]) {
|
|
1497
|
+
shared[shareKey] = formatShareOptions[shareKey];
|
|
1498
|
+
} else {
|
|
1499
|
+
formatShareOptions[shareKey].forEach((newUserSharedOptions)=>{
|
|
1500
|
+
const isSameVersion = shared[shareKey].find((s)=>s.version === newUserSharedOptions.version);
|
|
1501
|
+
if (!isSameVersion) {
|
|
1502
|
+
shared[shareKey].push(newUserSharedOptions);
|
|
1503
|
+
}
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1477
1507
|
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
1478
1508
|
origin: this,
|
|
1479
1509
|
userOptions,
|
|
@@ -1491,17 +1521,19 @@ class FederationHost {
|
|
|
1491
1521
|
const sharedKeys = Object.keys(formatShareOptions);
|
|
1492
1522
|
sharedKeys.forEach((sharedKey)=>{
|
|
1493
1523
|
const sharedVal = formatShareOptions[sharedKey];
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1524
|
+
sharedVal.forEach((s)=>{
|
|
1525
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, s, this.hooks.lifecycle.resolveShare);
|
|
1526
|
+
if (!registeredShared && s && s.lib) {
|
|
1527
|
+
this.setShared({
|
|
1528
|
+
pkgName: sharedKey,
|
|
1529
|
+
lib: s.lib,
|
|
1530
|
+
get: s.get,
|
|
1531
|
+
loaded: true,
|
|
1532
|
+
shared: s,
|
|
1533
|
+
from: userOptions.name
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
});
|
|
1505
1537
|
});
|
|
1506
1538
|
const plugins = [
|
|
1507
1539
|
...globalOptionsRes.plugins
|
package/dist/package.json
CHANGED
|
@@ -15,19 +15,19 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/index.cjs.d.ts",
|
|
19
18
|
"import": "./dist/index.esm.js",
|
|
20
|
-
"require": "./dist/index.cjs.js"
|
|
19
|
+
"require": "./dist/index.cjs.js",
|
|
20
|
+
"types": "./dist/index.cjs.d.ts"
|
|
21
21
|
},
|
|
22
22
|
"./helpers": {
|
|
23
|
-
"types": "./dist/helpers.cjs.d.ts",
|
|
24
23
|
"import": "./dist/helpers.esm.js",
|
|
25
|
-
"require": "./dist/helpers.cjs.js"
|
|
24
|
+
"require": "./dist/helpers.cjs.js",
|
|
25
|
+
"types": "./dist/helpers.cjs.d.ts"
|
|
26
26
|
},
|
|
27
27
|
"./types": {
|
|
28
|
-
"types": "./dist/types.cjs.d.ts",
|
|
29
28
|
"import": "./dist/types.esm.js",
|
|
30
|
-
"require": "./dist/types.cjs.js"
|
|
29
|
+
"require": "./dist/types.cjs.js",
|
|
30
|
+
"types": "./dist/types.cjs.d.ts"
|
|
31
31
|
},
|
|
32
32
|
"./*": "./*"
|
|
33
33
|
},
|
package/dist/share.cjs.js
CHANGED
|
@@ -71,6 +71,11 @@ const objectToString = Object.prototype.toString;
|
|
|
71
71
|
function isPlainObject(val) {
|
|
72
72
|
return objectToString.call(val) === '[object Object]';
|
|
73
73
|
}
|
|
74
|
+
function arrayOptions(options) {
|
|
75
|
+
return Array.isArray(options) ? options : [
|
|
76
|
+
options
|
|
77
|
+
];
|
|
78
|
+
}
|
|
74
79
|
|
|
75
80
|
function _extends$1() {
|
|
76
81
|
_extends$1 = Object.assign || function(target) {
|
|
@@ -674,7 +679,11 @@ function formatShareConfigs(shareArgs, from) {
|
|
|
674
679
|
return {};
|
|
675
680
|
}
|
|
676
681
|
return Object.keys(shareArgs).reduce((res, pkgName)=>{
|
|
677
|
-
|
|
682
|
+
const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
|
|
683
|
+
res[pkgName] = res[pkgName] || [];
|
|
684
|
+
arrayShareArgs.forEach((shareConfig)=>{
|
|
685
|
+
res[pkgName].push(formatShare(shareConfig, from));
|
|
686
|
+
});
|
|
678
687
|
return res;
|
|
679
688
|
}, {});
|
|
680
689
|
}
|
|
@@ -697,12 +706,11 @@ function versionLt(a, b) {
|
|
|
697
706
|
return false;
|
|
698
707
|
}
|
|
699
708
|
}
|
|
700
|
-
const findVersion = (
|
|
701
|
-
const versions = shareScopeMap[scope][pkgName];
|
|
709
|
+
const findVersion = (shareVersionMap, cb)=>{
|
|
702
710
|
const callback = cb || function(prev, cur) {
|
|
703
711
|
return versionLt(prev, cur);
|
|
704
712
|
};
|
|
705
|
-
return Object.keys(
|
|
713
|
+
return Object.keys(shareVersionMap).reduce((prev, cur)=>{
|
|
706
714
|
if (!prev) {
|
|
707
715
|
return cur;
|
|
708
716
|
}
|
|
@@ -724,7 +732,7 @@ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
|
|
|
724
732
|
const callback = function(prev, cur) {
|
|
725
733
|
return !isLoaded(versions[prev]) && versionLt(prev, cur);
|
|
726
734
|
};
|
|
727
|
-
return findVersion(shareScopeMap
|
|
735
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
728
736
|
}
|
|
729
737
|
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
730
738
|
const versions = shareScopeMap[scope][pkgName];
|
|
@@ -741,7 +749,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
|
741
749
|
}
|
|
742
750
|
return versionLt(prev, cur);
|
|
743
751
|
};
|
|
744
|
-
return findVersion(shareScopeMap
|
|
752
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
745
753
|
}
|
|
746
754
|
function getFindShareFunction(strategy) {
|
|
747
755
|
if (strategy === 'loaded-first') {
|
|
@@ -804,12 +812,33 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
|
|
|
804
812
|
function getGlobalShareScope() {
|
|
805
813
|
return Global.__FEDERATION__.__SHARE__;
|
|
806
814
|
}
|
|
815
|
+
function getTargetSharedOptions(options) {
|
|
816
|
+
const { pkgName, extraOptions, shareInfos } = options;
|
|
817
|
+
const defaultResolver = (sharedOptions)=>{
|
|
818
|
+
if (!sharedOptions) {
|
|
819
|
+
return undefined;
|
|
820
|
+
}
|
|
821
|
+
const shareVersionMap = {};
|
|
822
|
+
sharedOptions.forEach((shared)=>{
|
|
823
|
+
shareVersionMap[shared.version] = shared;
|
|
824
|
+
});
|
|
825
|
+
const callback = function(prev, cur) {
|
|
826
|
+
return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
|
|
827
|
+
};
|
|
828
|
+
const maxVersion = findVersion(shareVersionMap, callback);
|
|
829
|
+
return shareVersionMap[maxVersion];
|
|
830
|
+
};
|
|
831
|
+
var _extraOptions_resolver;
|
|
832
|
+
const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
|
|
833
|
+
return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
|
|
834
|
+
}
|
|
807
835
|
|
|
808
836
|
exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE;
|
|
809
837
|
exports.DEFAULT_SCOPE = DEFAULT_SCOPE;
|
|
810
838
|
exports.Global = Global;
|
|
811
839
|
exports.addGlobalSnapshot = addGlobalSnapshot;
|
|
812
840
|
exports.addUniqueItem = addUniqueItem;
|
|
841
|
+
exports.arrayOptions = arrayOptions;
|
|
813
842
|
exports.assert = assert;
|
|
814
843
|
exports.error = error;
|
|
815
844
|
exports.formatShareConfigs = formatShareConfigs;
|
|
@@ -825,6 +854,7 @@ exports.getInfoWithoutType = getInfoWithoutType;
|
|
|
825
854
|
exports.getPreloaded = getPreloaded;
|
|
826
855
|
exports.getRegisteredShare = getRegisteredShare;
|
|
827
856
|
exports.getRemoteEntryExports = getRemoteEntryExports;
|
|
857
|
+
exports.getTargetSharedOptions = getTargetSharedOptions;
|
|
828
858
|
exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
|
|
829
859
|
exports.globalLoading = globalLoading;
|
|
830
860
|
exports.isBrowserEnv = isBrowserEnv;
|
package/dist/share.esm.js
CHANGED
|
@@ -69,6 +69,11 @@ const objectToString = Object.prototype.toString;
|
|
|
69
69
|
function isPlainObject(val) {
|
|
70
70
|
return objectToString.call(val) === '[object Object]';
|
|
71
71
|
}
|
|
72
|
+
function arrayOptions(options) {
|
|
73
|
+
return Array.isArray(options) ? options : [
|
|
74
|
+
options
|
|
75
|
+
];
|
|
76
|
+
}
|
|
72
77
|
|
|
73
78
|
function _extends$1() {
|
|
74
79
|
_extends$1 = Object.assign || function(target) {
|
|
@@ -672,7 +677,11 @@ function formatShareConfigs(shareArgs, from) {
|
|
|
672
677
|
return {};
|
|
673
678
|
}
|
|
674
679
|
return Object.keys(shareArgs).reduce((res, pkgName)=>{
|
|
675
|
-
|
|
680
|
+
const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
|
|
681
|
+
res[pkgName] = res[pkgName] || [];
|
|
682
|
+
arrayShareArgs.forEach((shareConfig)=>{
|
|
683
|
+
res[pkgName].push(formatShare(shareConfig, from));
|
|
684
|
+
});
|
|
676
685
|
return res;
|
|
677
686
|
}, {});
|
|
678
687
|
}
|
|
@@ -695,12 +704,11 @@ function versionLt(a, b) {
|
|
|
695
704
|
return false;
|
|
696
705
|
}
|
|
697
706
|
}
|
|
698
|
-
const findVersion = (
|
|
699
|
-
const versions = shareScopeMap[scope][pkgName];
|
|
707
|
+
const findVersion = (shareVersionMap, cb)=>{
|
|
700
708
|
const callback = cb || function(prev, cur) {
|
|
701
709
|
return versionLt(prev, cur);
|
|
702
710
|
};
|
|
703
|
-
return Object.keys(
|
|
711
|
+
return Object.keys(shareVersionMap).reduce((prev, cur)=>{
|
|
704
712
|
if (!prev) {
|
|
705
713
|
return cur;
|
|
706
714
|
}
|
|
@@ -722,7 +730,7 @@ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
|
|
|
722
730
|
const callback = function(prev, cur) {
|
|
723
731
|
return !isLoaded(versions[prev]) && versionLt(prev, cur);
|
|
724
732
|
};
|
|
725
|
-
return findVersion(shareScopeMap
|
|
733
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
726
734
|
}
|
|
727
735
|
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
728
736
|
const versions = shareScopeMap[scope][pkgName];
|
|
@@ -739,7 +747,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
|
739
747
|
}
|
|
740
748
|
return versionLt(prev, cur);
|
|
741
749
|
};
|
|
742
|
-
return findVersion(shareScopeMap
|
|
750
|
+
return findVersion(shareScopeMap[scope][pkgName], callback);
|
|
743
751
|
}
|
|
744
752
|
function getFindShareFunction(strategy) {
|
|
745
753
|
if (strategy === 'loaded-first') {
|
|
@@ -802,5 +810,25 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
|
|
|
802
810
|
function getGlobalShareScope() {
|
|
803
811
|
return Global.__FEDERATION__.__SHARE__;
|
|
804
812
|
}
|
|
813
|
+
function getTargetSharedOptions(options) {
|
|
814
|
+
const { pkgName, extraOptions, shareInfos } = options;
|
|
815
|
+
const defaultResolver = (sharedOptions)=>{
|
|
816
|
+
if (!sharedOptions) {
|
|
817
|
+
return undefined;
|
|
818
|
+
}
|
|
819
|
+
const shareVersionMap = {};
|
|
820
|
+
sharedOptions.forEach((shared)=>{
|
|
821
|
+
shareVersionMap[shared.version] = shared;
|
|
822
|
+
});
|
|
823
|
+
const callback = function(prev, cur) {
|
|
824
|
+
return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
|
|
825
|
+
};
|
|
826
|
+
const maxVersion = findVersion(shareVersionMap, callback);
|
|
827
|
+
return shareVersionMap[maxVersion];
|
|
828
|
+
};
|
|
829
|
+
var _extraOptions_resolver;
|
|
830
|
+
const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
|
|
831
|
+
return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
|
|
832
|
+
}
|
|
805
833
|
|
|
806
|
-
export {
|
|
834
|
+
export { isBrowserEnv as A, addUniqueItem as B, setGlobalFederationConstructor as C, DEFAULT_REMOTE_TYPE as D, getGlobalFederationInstance as E, getGlobalFederationConstructor as F, Global as G, setGlobalFederationInstance as H, registerGlobalPlugins as I, nativeGlobal as J, resetFederationGlobalInfo as K, getTargetSnapshotInfoByModuleInfo as L, globalLoading as a, DEFAULT_SCOPE as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getInfoWithoutType as l, getPreloaded as m, setPreloaded as n, getRegisteredShare as o, arrayOptions as p, getGlobalSnapshotInfoByModuleInfo as q, addGlobalSnapshot as r, safeToString as s, setGlobalSnapshotInfoByModuleInfo as t, getGlobalSnapshot as u, getGlobalShareScope as v, warn as w, getTargetSharedOptions as x, formatShareConfigs as y, getBuilderId as z };
|
package/dist/src/core.d.ts
CHANGED
|
@@ -136,8 +136,14 @@ export declare class FederationHost {
|
|
|
136
136
|
constructor(userOptions: UserOptions);
|
|
137
137
|
private _setGlobalShareScopeMap;
|
|
138
138
|
initOptions(userOptions: UserOptions): Options;
|
|
139
|
-
loadShare<T>(pkgName: string,
|
|
140
|
-
|
|
139
|
+
loadShare<T>(pkgName: string, extraOptions?: {
|
|
140
|
+
customShareInfo?: Partial<Shared>;
|
|
141
|
+
resolver?: (sharedOptions: ShareInfos[string]) => Shared;
|
|
142
|
+
}): Promise<false | (() => T | undefined)>;
|
|
143
|
+
loadShareSync<T>(pkgName: string, extraOptions?: {
|
|
144
|
+
customShareInfo?: Partial<Shared>;
|
|
145
|
+
resolver?: (sharedOptions: ShareInfos[string]) => Shared;
|
|
146
|
+
}): () => T | never;
|
|
141
147
|
initRawContainer(name: string, url: string, container: RemoteEntryExports): Module;
|
|
142
148
|
private _getRemoteModuleAndOptions;
|
|
143
149
|
loadRemote<T>(id: string, options?: {
|
|
@@ -15,6 +15,10 @@ export type RemoteInfoOptionalVersion = {
|
|
|
15
15
|
version?: string;
|
|
16
16
|
} & RemoteInfoCommon;
|
|
17
17
|
export type Remote = (RemoteWithEntry | RemoteWithVersion) & RemoteInfoCommon;
|
|
18
|
+
export type LoadShareExtraOptions = {
|
|
19
|
+
customShareInfo?: Partial<Shared>;
|
|
20
|
+
resolver?: (sharedOptions: ShareInfos[string]) => Shared;
|
|
21
|
+
};
|
|
18
22
|
export interface RemoteInfo {
|
|
19
23
|
name: string;
|
|
20
24
|
version?: string;
|
|
@@ -69,7 +73,7 @@ export type GlobalShareScopeMap = {
|
|
|
69
73
|
[instanceName: string]: ShareScopeMap;
|
|
70
74
|
};
|
|
71
75
|
export type ShareInfos = {
|
|
72
|
-
[pkgName: string]: Shared;
|
|
76
|
+
[pkgName: string]: Shared[];
|
|
73
77
|
};
|
|
74
78
|
export interface Options {
|
|
75
79
|
id?: string;
|
|
@@ -82,7 +86,7 @@ export interface Options {
|
|
|
82
86
|
}
|
|
83
87
|
export type UserOptions = Omit<Optional<Options, 'plugins'>, 'shared' | 'inBrowser'> & {
|
|
84
88
|
shared?: {
|
|
85
|
-
[pkgName: string]: ShareArgs;
|
|
89
|
+
[pkgName: string]: ShareArgs | ShareArgs[];
|
|
86
90
|
};
|
|
87
91
|
};
|
|
88
92
|
export type LoadModuleOptions = {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Federation } from '../global';
|
|
2
|
-
import { GlobalShareScopeMap, Shared, ShareArgs, ShareInfos, ShareScopeMap } from '../type';
|
|
2
|
+
import { GlobalShareScopeMap, Shared, ShareArgs, ShareInfos, ShareScopeMap, LoadShareExtraOptions, UserOptions } from '../type';
|
|
3
3
|
import { SyncWaterfallHook } from './hooks';
|
|
4
4
|
export declare function formatShare(shareArgs: ShareArgs, from: string): Shared;
|
|
5
|
-
export declare function formatShareConfigs(shareArgs:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare
|
|
5
|
+
export declare function formatShareConfigs(shareArgs: UserOptions['shared'], from: string): ShareInfos;
|
|
6
|
+
export declare function versionLt(a: string, b: string): boolean;
|
|
7
|
+
export declare const findVersion: (shareVersionMap: ShareScopeMap[string][string], cb?: ((prev: string, cur: string) => boolean) | undefined) => string;
|
|
8
|
+
export declare const isLoaded: (shared: Shared) => boolean;
|
|
9
|
+
export declare function getRegisteredShare(localShareScopeMap: ShareScopeMap, pkgName: string, shareInfo: Shared, resolveShare: SyncWaterfallHook<{
|
|
9
10
|
shareScopeMap: ShareScopeMap;
|
|
10
11
|
scope: string;
|
|
11
12
|
pkgName: string;
|
|
@@ -14,3 +15,8 @@ export declare function getRegisteredShare(localShareScopeMap: ShareScopeMap, pk
|
|
|
14
15
|
resolver: () => Shared | undefined;
|
|
15
16
|
}>): Shared | void;
|
|
16
17
|
export declare function getGlobalShareScope(): GlobalShareScopeMap;
|
|
18
|
+
export declare function getTargetSharedOptions(options: {
|
|
19
|
+
pkgName: string;
|
|
20
|
+
extraOptions?: LoadShareExtraOptions;
|
|
21
|
+
shareInfos: ShareInfos;
|
|
22
|
+
}): Shared & Partial<Shared>;
|
package/dist/src/utils/tool.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ export declare function isObject(val: any): boolean;
|
|
|
10
10
|
export declare const objectToString: () => string;
|
|
11
11
|
export declare function isPlainObject(val: any): val is object;
|
|
12
12
|
export declare function isStaticResourcesEqual(url1: string, url2: string): boolean;
|
|
13
|
+
export declare function arrayOptions<T>(options: T | Array<T>): Array<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/runtime",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240411125344",
|
|
4
4
|
"author": "zhouxiao <codingzx@gmail.com>",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
@@ -15,19 +15,19 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/index.cjs.d.ts",
|
|
19
18
|
"import": "./dist/index.esm.js",
|
|
20
|
-
"require": "./dist/index.cjs.js"
|
|
19
|
+
"require": "./dist/index.cjs.js",
|
|
20
|
+
"types": "./dist/index.cjs.d.ts"
|
|
21
21
|
},
|
|
22
22
|
"./helpers": {
|
|
23
|
-
"types": "./dist/helpers.cjs.d.ts",
|
|
24
23
|
"import": "./dist/helpers.esm.js",
|
|
25
|
-
"require": "./dist/helpers.cjs.js"
|
|
24
|
+
"require": "./dist/helpers.cjs.js",
|
|
25
|
+
"types": "./dist/helpers.cjs.d.ts"
|
|
26
26
|
},
|
|
27
27
|
"./types": {
|
|
28
|
-
"types": "./dist/types.cjs.d.ts",
|
|
29
28
|
"import": "./dist/types.esm.js",
|
|
30
|
-
"require": "./dist/types.cjs.js"
|
|
29
|
+
"require": "./dist/types.cjs.js",
|
|
30
|
+
"types": "./dist/types.cjs.d.ts"
|
|
31
31
|
},
|
|
32
32
|
"./*": "./*"
|
|
33
33
|
},
|
|
@@ -45,6 +45,6 @@
|
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@module-federation/sdk": "0.0.0-next-
|
|
48
|
+
"@module-federation/sdk": "0.0.0-next-20240411125344"
|
|
49
49
|
}
|
|
50
50
|
}
|