@module-federation/runtime 0.1.11 → 0.1.13
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/dist/helpers.esm.js +1 -1
- package/dist/index.cjs.js +482 -320
- package/dist/index.esm.js +485 -323
- package/dist/package.json +1 -1
- package/dist/share.cjs.js +22 -6
- package/dist/share.esm.js +23 -7
- package/dist/src/core.d.ts +7 -95
- package/dist/src/remote/index.d.ts +91 -0
- package/dist/src/shared/index.d.ts +56 -0
- package/dist/src/type/plugin.d.ts +11 -1
- package/dist/src/utils/plugin.d.ts +1 -1
- package/dist/src/utils/share.d.ts +7 -2
- package/package.json +2 -2
package/dist/index.cjs.js
CHANGED
|
@@ -89,8 +89,8 @@ function registerPlugins$1(plugins, hookInstances) {
|
|
|
89
89
|
return plugins;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
function _extends$
|
|
93
|
-
_extends$
|
|
92
|
+
function _extends$7() {
|
|
93
|
+
_extends$7 = Object.assign || function(target) {
|
|
94
94
|
for(var i = 1; i < arguments.length; i++){
|
|
95
95
|
var source = arguments[i];
|
|
96
96
|
for(var key in source){
|
|
@@ -101,7 +101,7 @@ function _extends$5() {
|
|
|
101
101
|
}
|
|
102
102
|
return target;
|
|
103
103
|
};
|
|
104
|
-
return _extends$
|
|
104
|
+
return _extends$7.apply(this, arguments);
|
|
105
105
|
}
|
|
106
106
|
async function loadEsmEntry({ entry, remoteEntryExports }) {
|
|
107
107
|
return new Promise((resolve, reject)=>{
|
|
@@ -189,7 +189,7 @@ async function getRemoteEntry({ remoteEntryExports, remoteInfo, createScriptHook
|
|
|
189
189
|
return share.globalLoading[uniqueKey];
|
|
190
190
|
}
|
|
191
191
|
function getRemoteInfo(remote) {
|
|
192
|
-
return _extends$
|
|
192
|
+
return _extends$7({}, remote, {
|
|
193
193
|
entry: 'entry' in remote ? remote.entry : '',
|
|
194
194
|
type: remote.type || share.DEFAULT_REMOTE_TYPE,
|
|
195
195
|
entryGlobalName: remote.entryGlobalName || remote.name,
|
|
@@ -197,8 +197,8 @@ function getRemoteInfo(remote) {
|
|
|
197
197
|
});
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
function _extends$
|
|
201
|
-
_extends$
|
|
200
|
+
function _extends$6() {
|
|
201
|
+
_extends$6 = Object.assign || function(target) {
|
|
202
202
|
for(var i = 1; i < arguments.length; i++){
|
|
203
203
|
var source = arguments[i];
|
|
204
204
|
for(var key in source){
|
|
@@ -209,7 +209,7 @@ function _extends$4() {
|
|
|
209
209
|
}
|
|
210
210
|
return target;
|
|
211
211
|
};
|
|
212
|
-
return _extends$
|
|
212
|
+
return _extends$6.apply(this, arguments);
|
|
213
213
|
}
|
|
214
214
|
let Module = class Module {
|
|
215
215
|
async getEntry() {
|
|
@@ -271,7 +271,7 @@ let Module = class Module {
|
|
|
271
271
|
origin: this.host
|
|
272
272
|
});
|
|
273
273
|
await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
|
|
274
|
-
await this.host.hooks.lifecycle.initContainer.emit(_extends$
|
|
274
|
+
await this.host.hooks.lifecycle.initContainer.emit(_extends$6({}, initContainerOptions, {
|
|
275
275
|
remoteEntryExports
|
|
276
276
|
}));
|
|
277
277
|
}
|
|
@@ -481,8 +481,8 @@ class PluginSystem {
|
|
|
481
481
|
}
|
|
482
482
|
}
|
|
483
483
|
|
|
484
|
-
function _extends$
|
|
485
|
-
_extends$
|
|
484
|
+
function _extends$5() {
|
|
485
|
+
_extends$5 = Object.assign || function(target) {
|
|
486
486
|
for(var i = 1; i < arguments.length; i++){
|
|
487
487
|
var source = arguments[i];
|
|
488
488
|
for(var key in source){
|
|
@@ -493,10 +493,10 @@ function _extends$3() {
|
|
|
493
493
|
}
|
|
494
494
|
return target;
|
|
495
495
|
};
|
|
496
|
-
return _extends$
|
|
496
|
+
return _extends$5.apply(this, arguments);
|
|
497
497
|
}
|
|
498
498
|
function defaultPreloadArgs(preloadConfig) {
|
|
499
|
-
return _extends$
|
|
499
|
+
return _extends$5({
|
|
500
500
|
resourceCategory: 'sync',
|
|
501
501
|
share: true,
|
|
502
502
|
depsRemote: true,
|
|
@@ -603,8 +603,8 @@ function preloadAssets(remoteInfo, host, assets) {
|
|
|
603
603
|
}
|
|
604
604
|
}
|
|
605
605
|
|
|
606
|
-
function _extends$
|
|
607
|
-
_extends$
|
|
606
|
+
function _extends$4() {
|
|
607
|
+
_extends$4 = Object.assign || function(target) {
|
|
608
608
|
for(var i = 1; i < arguments.length; i++){
|
|
609
609
|
var source = arguments[i];
|
|
610
610
|
for(var key in source){
|
|
@@ -615,7 +615,7 @@ function _extends$2() {
|
|
|
615
615
|
}
|
|
616
616
|
return target;
|
|
617
617
|
};
|
|
618
|
-
return _extends$
|
|
618
|
+
return _extends$4.apply(this, arguments);
|
|
619
619
|
}
|
|
620
620
|
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
621
621
|
if (!('remoteEntry' in remoteSnapshot) || !remoteSnapshot.remoteEntry) {
|
|
@@ -650,7 +650,7 @@ function snapshotPlugin() {
|
|
|
650
650
|
depsRemote: false
|
|
651
651
|
}
|
|
652
652
|
};
|
|
653
|
-
const assets = await origin.hooks.lifecycle.generatePreloadAssets.emit({
|
|
653
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
654
654
|
origin,
|
|
655
655
|
preloadOptions,
|
|
656
656
|
remoteInfo,
|
|
@@ -661,7 +661,7 @@ function snapshotPlugin() {
|
|
|
661
661
|
if (assets) {
|
|
662
662
|
preloadAssets(remoteInfo, origin, assets);
|
|
663
663
|
}
|
|
664
|
-
return _extends$
|
|
664
|
+
return _extends$4({}, args, {
|
|
665
665
|
remoteSnapshot
|
|
666
666
|
});
|
|
667
667
|
}
|
|
@@ -787,7 +787,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
787
787
|
for(let index = 0; index < assetsLength; index++){
|
|
788
788
|
const assetsInfo = moduleAssetsInfo[index];
|
|
789
789
|
const exposeFullPath = `${remoteInfo.name}/${assetsInfo.moduleName}`;
|
|
790
|
-
origin.hooks.lifecycle.handlePreloadModule.emit({
|
|
790
|
+
origin.remoteHandler.hooks.lifecycle.handlePreloadModule.emit({
|
|
791
791
|
id: assetsInfo.moduleName === '.' ? remoteInfo.name : exposeFullPath,
|
|
792
792
|
name: remoteInfo.name,
|
|
793
793
|
remoteSnapshot: moduleInfoSnapshot,
|
|
@@ -815,7 +815,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
815
815
|
}, true, memo, remoteSnapshot);
|
|
816
816
|
if (remoteSnapshot.shared) {
|
|
817
817
|
const collectSharedAssets = (shareInfo, snapshotShared)=>{
|
|
818
|
-
const registeredShared = share.getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
818
|
+
const registeredShared = share.getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
|
|
819
819
|
// 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.
|
|
820
820
|
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
821
821
|
snapshotShared.assets.js.sync.forEach((asset)=>{
|
|
@@ -882,8 +882,8 @@ const generatePreloadAssetsPlugin = function() {
|
|
|
882
882
|
};
|
|
883
883
|
};
|
|
884
884
|
|
|
885
|
-
function _extends$
|
|
886
|
-
_extends$
|
|
885
|
+
function _extends$3() {
|
|
886
|
+
_extends$3 = Object.assign || function(target) {
|
|
887
887
|
for(var i = 1; i < arguments.length; i++){
|
|
888
888
|
var source = arguments[i];
|
|
889
889
|
for(var key in source){
|
|
@@ -894,7 +894,7 @@ function _extends$1() {
|
|
|
894
894
|
}
|
|
895
895
|
return target;
|
|
896
896
|
};
|
|
897
|
-
return _extends$
|
|
897
|
+
return _extends$3.apply(this, arguments);
|
|
898
898
|
}
|
|
899
899
|
class SnapshotHandler {
|
|
900
900
|
async loadSnapshot(moduleInfo) {
|
|
@@ -937,7 +937,7 @@ class SnapshotHandler {
|
|
|
937
937
|
// This ensures the snapshot's integrity and helps the chrome plugin correctly identify all producer modules, ensuring that proxyable producer modules will not be missing.
|
|
938
938
|
if (hostSnapshot && 'remotesInfo' in hostSnapshot && !share.getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
|
|
939
939
|
if ('version' in moduleInfo || 'entry' in moduleInfo) {
|
|
940
|
-
hostSnapshot.remotesInfo = _extends$
|
|
940
|
+
hostSnapshot.remotesInfo = _extends$3({}, hostSnapshot == null ? void 0 : hostSnapshot.remotesInfo, {
|
|
941
941
|
[moduleInfo.name]: {
|
|
942
942
|
matchedVersion: 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry
|
|
943
943
|
}
|
|
@@ -957,7 +957,7 @@ class SnapshotHandler {
|
|
|
957
957
|
if (sdk.isManifestProvider(globalRemoteSnapshot)) {
|
|
958
958
|
const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
|
|
959
959
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
960
|
-
const globalSnapshotRes = share.setGlobalSnapshotInfoByModuleInfo(_extends$
|
|
960
|
+
const globalSnapshotRes = share.setGlobalSnapshotInfoByModuleInfo(_extends$3({}, moduleInfo, {
|
|
961
961
|
// The global remote may be overridden
|
|
962
962
|
// Therefore, set the snapshot key to the global address of the actual request
|
|
963
963
|
entry: globalRemoteSnapshot.remoteEntry
|
|
@@ -1085,8 +1085,8 @@ class SnapshotHandler {
|
|
|
1085
1085
|
}
|
|
1086
1086
|
}
|
|
1087
1087
|
|
|
1088
|
-
function _extends() {
|
|
1089
|
-
_extends = Object.assign || function(target) {
|
|
1088
|
+
function _extends$2() {
|
|
1089
|
+
_extends$2 = Object.assign || function(target) {
|
|
1090
1090
|
for(var i = 1; i < arguments.length; i++){
|
|
1091
1091
|
var source = arguments[i];
|
|
1092
1092
|
for(var key in source){
|
|
@@ -1097,7 +1097,7 @@ function _extends() {
|
|
|
1097
1097
|
}
|
|
1098
1098
|
return target;
|
|
1099
1099
|
};
|
|
1100
|
-
return _extends.apply(this, arguments);
|
|
1100
|
+
return _extends$2.apply(this, arguments);
|
|
1101
1101
|
}
|
|
1102
1102
|
function _object_without_properties_loose(source, excluded) {
|
|
1103
1103
|
if (source == null) return {};
|
|
@@ -1111,21 +1111,34 @@ function _object_without_properties_loose(source, excluded) {
|
|
|
1111
1111
|
}
|
|
1112
1112
|
return target;
|
|
1113
1113
|
}
|
|
1114
|
-
class
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
const
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1114
|
+
class SharedHandler {
|
|
1115
|
+
// register shared in shareScopeMap
|
|
1116
|
+
registerShared(globalOptions, userOptions) {
|
|
1117
|
+
const { shareInfos, shared } = share.formatShareConfigs(globalOptions, userOptions);
|
|
1118
|
+
const sharedKeys = Object.keys(shareInfos);
|
|
1119
|
+
sharedKeys.forEach((sharedKey)=>{
|
|
1120
|
+
const sharedVals = shareInfos[sharedKey];
|
|
1121
|
+
sharedVals.forEach((sharedVal)=>{
|
|
1122
|
+
const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
1123
|
+
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
1124
|
+
this.setShared({
|
|
1125
|
+
pkgName: sharedKey,
|
|
1126
|
+
lib: sharedVal.lib,
|
|
1127
|
+
get: sharedVal.get,
|
|
1128
|
+
loaded: true,
|
|
1129
|
+
shared: sharedVal,
|
|
1130
|
+
from: userOptions.name
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
});
|
|
1135
|
+
return {
|
|
1136
|
+
shareInfos,
|
|
1137
|
+
shared
|
|
1138
|
+
};
|
|
1127
1139
|
}
|
|
1128
1140
|
async loadShare(pkgName, extraOptions) {
|
|
1141
|
+
const { host } = this;
|
|
1129
1142
|
// This function performs the following steps:
|
|
1130
1143
|
// 1. Checks if the currently loaded share already exists, if not, it throws an error
|
|
1131
1144
|
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
@@ -1133,7 +1146,7 @@ class FederationHost {
|
|
|
1133
1146
|
const shareInfo = share.getTargetSharedOptions({
|
|
1134
1147
|
pkgName,
|
|
1135
1148
|
extraOptions,
|
|
1136
|
-
shareInfos:
|
|
1149
|
+
shareInfos: host.options.shared
|
|
1137
1150
|
});
|
|
1138
1151
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1139
1152
|
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
@@ -1144,19 +1157,19 @@ class FederationHost {
|
|
|
1144
1157
|
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
|
|
1145
1158
|
pkgName,
|
|
1146
1159
|
shareInfo,
|
|
1147
|
-
shared:
|
|
1148
|
-
origin:
|
|
1160
|
+
shared: host.options.shared,
|
|
1161
|
+
origin: host
|
|
1149
1162
|
});
|
|
1150
1163
|
const { shareInfo: shareInfoRes } = loadShareRes;
|
|
1151
1164
|
// Assert that shareInfoRes exists, if not, throw an error
|
|
1152
|
-
share.assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${
|
|
1165
|
+
share.assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
1153
1166
|
// Retrieve from cache
|
|
1154
1167
|
const registeredShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1155
1168
|
const addUseIn = (shared)=>{
|
|
1156
1169
|
if (!shared.useIn) {
|
|
1157
1170
|
shared.useIn = [];
|
|
1158
1171
|
}
|
|
1159
|
-
share.addUniqueItem(shared.useIn,
|
|
1172
|
+
share.addUniqueItem(shared.useIn, host.options.name);
|
|
1160
1173
|
};
|
|
1161
1174
|
if (registeredShared && registeredShared.lib) {
|
|
1162
1175
|
addUseIn(registeredShared);
|
|
@@ -1187,7 +1200,7 @@ class FederationHost {
|
|
|
1187
1200
|
pkgName,
|
|
1188
1201
|
loaded: false,
|
|
1189
1202
|
shared: registeredShared,
|
|
1190
|
-
from:
|
|
1203
|
+
from: host.options.name,
|
|
1191
1204
|
lib: null,
|
|
1192
1205
|
loading
|
|
1193
1206
|
});
|
|
@@ -1213,22 +1226,80 @@ class FederationHost {
|
|
|
1213
1226
|
pkgName,
|
|
1214
1227
|
loaded: false,
|
|
1215
1228
|
shared: shareInfoRes,
|
|
1216
|
-
from:
|
|
1229
|
+
from: host.options.name,
|
|
1217
1230
|
lib: null,
|
|
1218
1231
|
loading
|
|
1219
1232
|
});
|
|
1220
1233
|
return loading;
|
|
1221
1234
|
}
|
|
1222
1235
|
}
|
|
1236
|
+
/**
|
|
1237
|
+
* This function initializes the sharing sequence (executed only once per share scope).
|
|
1238
|
+
* It accepts one argument, the name of the share scope.
|
|
1239
|
+
* If the share scope does not exist, it creates one.
|
|
1240
|
+
*/ // eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1241
|
+
initializeSharing(shareScopeName = share.DEFAULT_SCOPE, strategy) {
|
|
1242
|
+
const { host } = this;
|
|
1243
|
+
const shareScope = this.shareScopeMap;
|
|
1244
|
+
const hostName = host.options.name;
|
|
1245
|
+
// Creates a new share scope if necessary
|
|
1246
|
+
if (!shareScope[shareScopeName]) {
|
|
1247
|
+
shareScope[shareScopeName] = {};
|
|
1248
|
+
}
|
|
1249
|
+
// Executes all initialization snippets from all accessible modules
|
|
1250
|
+
const scope = shareScope[shareScopeName];
|
|
1251
|
+
const register = (name, shared)=>{
|
|
1252
|
+
var _activeVersion_shareConfig;
|
|
1253
|
+
const { version, eager } = shared;
|
|
1254
|
+
scope[name] = scope[name] || {};
|
|
1255
|
+
const versions = scope[name];
|
|
1256
|
+
const activeVersion = versions[version];
|
|
1257
|
+
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
|
|
1258
|
+
if (!activeVersion || activeVersion.strategy !== 'loaded-first' && !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
|
|
1259
|
+
versions[version] = shared;
|
|
1260
|
+
}
|
|
1261
|
+
};
|
|
1262
|
+
const promises = [];
|
|
1263
|
+
const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
|
|
1264
|
+
const initRemoteModule = async (key)=>{
|
|
1265
|
+
const { module } = await host.remoteHandler.getRemoteModuleAndOptions({
|
|
1266
|
+
id: key
|
|
1267
|
+
});
|
|
1268
|
+
if (module.getEntry) {
|
|
1269
|
+
const entry = await module.getEntry();
|
|
1270
|
+
if (!module.inited) {
|
|
1271
|
+
initFn(entry);
|
|
1272
|
+
module.inited = true;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
};
|
|
1276
|
+
Object.keys(host.options.shared).forEach((shareName)=>{
|
|
1277
|
+
const sharedArr = host.options.shared[shareName];
|
|
1278
|
+
sharedArr.forEach((shared)=>{
|
|
1279
|
+
if (shared.scope.includes(shareScopeName)) {
|
|
1280
|
+
register(shareName, shared);
|
|
1281
|
+
}
|
|
1282
|
+
});
|
|
1283
|
+
});
|
|
1284
|
+
if (strategy === 'version-first') {
|
|
1285
|
+
host.options.remotes.forEach((remote)=>{
|
|
1286
|
+
if (remote.shareScope === shareScopeName) {
|
|
1287
|
+
promises.push(initRemoteModule(remote.name));
|
|
1288
|
+
}
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
return promises;
|
|
1292
|
+
}
|
|
1223
1293
|
// The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
|
|
1224
1294
|
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1225
1295
|
// 2. If lib exists in local shared, it will be used directly
|
|
1226
1296
|
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1227
1297
|
loadShareSync(pkgName, extraOptions) {
|
|
1298
|
+
const { host } = this;
|
|
1228
1299
|
const shareInfo = share.getTargetSharedOptions({
|
|
1229
1300
|
pkgName,
|
|
1230
1301
|
extraOptions,
|
|
1231
|
-
shareInfos:
|
|
1302
|
+
shareInfos: host.options.shared
|
|
1232
1303
|
});
|
|
1233
1304
|
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1234
1305
|
shareInfo.scope.forEach((shareScope)=>{
|
|
@@ -1240,14 +1311,14 @@ class FederationHost {
|
|
|
1240
1311
|
if (!shared.useIn) {
|
|
1241
1312
|
shared.useIn = [];
|
|
1242
1313
|
}
|
|
1243
|
-
share.addUniqueItem(shared.useIn,
|
|
1314
|
+
share.addUniqueItem(shared.useIn, host.options.name);
|
|
1244
1315
|
};
|
|
1245
1316
|
if (registeredShared) {
|
|
1246
1317
|
if (typeof registeredShared.lib === 'function') {
|
|
1247
1318
|
addUseIn(registeredShared);
|
|
1248
1319
|
if (!registeredShared.loaded) {
|
|
1249
1320
|
registeredShared.loaded = true;
|
|
1250
|
-
if (registeredShared.from ===
|
|
1321
|
+
if (registeredShared.from === host.options.name) {
|
|
1251
1322
|
shareInfo.loaded = true;
|
|
1252
1323
|
}
|
|
1253
1324
|
}
|
|
@@ -1260,7 +1331,7 @@ class FederationHost {
|
|
|
1260
1331
|
this.setShared({
|
|
1261
1332
|
pkgName,
|
|
1262
1333
|
loaded: true,
|
|
1263
|
-
from:
|
|
1334
|
+
from: host.options.name,
|
|
1264
1335
|
lib: module,
|
|
1265
1336
|
shared: registeredShared
|
|
1266
1337
|
});
|
|
@@ -1278,7 +1349,7 @@ class FederationHost {
|
|
|
1278
1349
|
const module = shareInfo.get();
|
|
1279
1350
|
if (module instanceof Promise) {
|
|
1280
1351
|
throw new Error(`
|
|
1281
|
-
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${
|
|
1352
|
+
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${host.options.name}.
|
|
1282
1353
|
Possible reasons for failure: \n
|
|
1283
1354
|
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
1284
1355
|
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
@@ -1288,77 +1359,112 @@ class FederationHost {
|
|
|
1288
1359
|
this.setShared({
|
|
1289
1360
|
pkgName,
|
|
1290
1361
|
loaded: true,
|
|
1291
|
-
from:
|
|
1362
|
+
from: host.options.name,
|
|
1292
1363
|
lib: shareInfo.lib,
|
|
1293
1364
|
shared: shareInfo
|
|
1294
1365
|
});
|
|
1295
1366
|
return shareInfo.lib;
|
|
1296
1367
|
}
|
|
1297
1368
|
throw new Error(`
|
|
1298
|
-
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${
|
|
1369
|
+
The loadShareSync function was unable to load ${pkgName}. The ${pkgName} could not be found in ${host.options.name}.
|
|
1299
1370
|
Possible reasons for failure: \n
|
|
1300
1371
|
1. The ${pkgName} share was registered with the 'get' attribute, but loadShare was not used beforehand.\n
|
|
1301
1372
|
2. The ${pkgName} share was not registered with the 'lib' attribute.\n
|
|
1302
1373
|
`);
|
|
1303
1374
|
}
|
|
1304
|
-
|
|
1305
|
-
const
|
|
1306
|
-
|
|
1307
|
-
|
|
1375
|
+
initShareScopeMap(scopeName, shareScope) {
|
|
1376
|
+
const { host } = this;
|
|
1377
|
+
this.shareScopeMap[scopeName] = shareScope;
|
|
1378
|
+
this.hooks.lifecycle.initContainerShareScopeMap.emit({
|
|
1379
|
+
shareScope,
|
|
1380
|
+
options: host.options,
|
|
1381
|
+
origin: host
|
|
1308
1382
|
});
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1383
|
+
}
|
|
1384
|
+
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
1385
|
+
const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
|
|
1386
|
+
"version",
|
|
1387
|
+
"scope"
|
|
1388
|
+
]);
|
|
1389
|
+
const scopes = Array.isArray(scope) ? scope : [
|
|
1390
|
+
scope
|
|
1391
|
+
];
|
|
1392
|
+
scopes.forEach((sc)=>{
|
|
1393
|
+
if (!this.shareScopeMap[sc]) {
|
|
1394
|
+
this.shareScopeMap[sc] = {};
|
|
1395
|
+
}
|
|
1396
|
+
if (!this.shareScopeMap[sc][pkgName]) {
|
|
1397
|
+
this.shareScopeMap[sc][pkgName] = {};
|
|
1398
|
+
}
|
|
1399
|
+
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
this.shareScopeMap[sc][pkgName][version] = _extends$2({
|
|
1403
|
+
version,
|
|
1404
|
+
scope: [
|
|
1405
|
+
'default'
|
|
1406
|
+
]
|
|
1407
|
+
}, shareInfo, {
|
|
1408
|
+
lib,
|
|
1409
|
+
loaded,
|
|
1410
|
+
loading
|
|
1411
|
+
});
|
|
1412
|
+
if (get) {
|
|
1413
|
+
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
1414
|
+
}
|
|
1312
1415
|
});
|
|
1313
|
-
module.remoteEntryExports = container;
|
|
1314
|
-
this.moduleCache.set(name, module);
|
|
1315
|
-
return module;
|
|
1316
1416
|
}
|
|
1317
|
-
|
|
1318
|
-
const
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1417
|
+
_setGlobalShareScopeMap(hostOptions) {
|
|
1418
|
+
const globalShareScopeMap = share.getGlobalShareScope();
|
|
1419
|
+
const identifier = hostOptions.id || hostOptions.name;
|
|
1420
|
+
if (identifier && !globalShareScopeMap[identifier]) {
|
|
1421
|
+
globalShareScopeMap[identifier] = this.shareScopeMap;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
constructor(host){
|
|
1425
|
+
this.hooks = new PluginSystem({
|
|
1426
|
+
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
1427
|
+
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
1428
|
+
// not used yet
|
|
1429
|
+
loadShare: new AsyncHook(),
|
|
1430
|
+
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
1431
|
+
// maybe will change, temporarily for internal use only
|
|
1432
|
+
initContainerShareScopeMap: new AsyncWaterfallHook('initContainer')
|
|
1322
1433
|
});
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
options: this.options,
|
|
1339
|
-
origin: this,
|
|
1340
|
-
remoteInfo
|
|
1341
|
-
}));
|
|
1342
|
-
const { remote, expose } = matchInfo;
|
|
1343
|
-
share.assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
1344
|
-
let module = this.moduleCache.get(remote.name);
|
|
1345
|
-
const moduleOptions = {
|
|
1346
|
-
host: this,
|
|
1347
|
-
remoteInfo
|
|
1348
|
-
};
|
|
1349
|
-
if (!module) {
|
|
1350
|
-
module = new Module(moduleOptions);
|
|
1351
|
-
this.moduleCache.set(remote.name, module);
|
|
1434
|
+
this.host = host;
|
|
1435
|
+
this.shareScopeMap = {};
|
|
1436
|
+
this._setGlobalShareScopeMap(host.options);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
function _extends$1() {
|
|
1441
|
+
_extends$1 = Object.assign || function(target) {
|
|
1442
|
+
for(var i = 1; i < arguments.length; i++){
|
|
1443
|
+
var source = arguments[i];
|
|
1444
|
+
for(var key in source){
|
|
1445
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
1446
|
+
target[key] = source[key];
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1352
1449
|
}
|
|
1353
|
-
return
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1450
|
+
return target;
|
|
1451
|
+
};
|
|
1452
|
+
return _extends$1.apply(this, arguments);
|
|
1453
|
+
}
|
|
1454
|
+
class RemoteHandler {
|
|
1455
|
+
formatAndRegisterRemote(globalOptions, userOptions) {
|
|
1456
|
+
const userRemotes = userOptions.remotes || [];
|
|
1457
|
+
return userRemotes.reduce((res, remote)=>{
|
|
1458
|
+
this.registerRemote(remote, res, {
|
|
1459
|
+
force: false
|
|
1460
|
+
});
|
|
1461
|
+
return res;
|
|
1462
|
+
}, globalOptions.remotes);
|
|
1358
1463
|
}
|
|
1359
1464
|
// eslint-disable-next-line max-lines-per-function
|
|
1360
1465
|
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1361
1466
|
async loadRemote(id, options) {
|
|
1467
|
+
const { host } = this;
|
|
1362
1468
|
try {
|
|
1363
1469
|
const { loadFactory = true } = options || {
|
|
1364
1470
|
loadFactory: true
|
|
@@ -1370,7 +1476,9 @@ class FederationHost {
|
|
|
1370
1476
|
// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
|
|
1371
1477
|
// id: alias(app1) + expose(button) = app1/button
|
|
1372
1478
|
// id: alias(app1/utils) + expose(loadash/sort) = app1/utils/loadash/sort
|
|
1373
|
-
const { module, moduleOptions, remoteMatchInfo } = await this.
|
|
1479
|
+
const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({
|
|
1480
|
+
id
|
|
1481
|
+
});
|
|
1374
1482
|
const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
|
|
1375
1483
|
const moduleOrFactory = await module.get(expose, options);
|
|
1376
1484
|
const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
|
|
@@ -1382,7 +1490,7 @@ class FederationHost {
|
|
|
1382
1490
|
remote,
|
|
1383
1491
|
options: moduleOptions,
|
|
1384
1492
|
moduleInstance: module,
|
|
1385
|
-
origin:
|
|
1493
|
+
origin: host
|
|
1386
1494
|
});
|
|
1387
1495
|
if (typeof moduleWrapper === 'function') {
|
|
1388
1496
|
return moduleWrapper;
|
|
@@ -1396,7 +1504,8 @@ class FederationHost {
|
|
|
1396
1504
|
id,
|
|
1397
1505
|
error,
|
|
1398
1506
|
from,
|
|
1399
|
-
|
|
1507
|
+
lifecycle: 'onLoad',
|
|
1508
|
+
origin: host
|
|
1400
1509
|
});
|
|
1401
1510
|
if (!failOver) {
|
|
1402
1511
|
throw error;
|
|
@@ -1406,18 +1515,19 @@ class FederationHost {
|
|
|
1406
1515
|
}
|
|
1407
1516
|
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1408
1517
|
async preloadRemote(preloadOptions) {
|
|
1518
|
+
const { host } = this;
|
|
1409
1519
|
await this.hooks.lifecycle.beforePreloadRemote.emit({
|
|
1410
1520
|
preloadOptions,
|
|
1411
|
-
options:
|
|
1412
|
-
origin:
|
|
1521
|
+
options: host.options,
|
|
1522
|
+
origin: host
|
|
1413
1523
|
});
|
|
1414
|
-
const preloadOps = formatPreloadArgs(
|
|
1524
|
+
const preloadOps = formatPreloadArgs(host.options.remotes, preloadOptions);
|
|
1415
1525
|
await Promise.all(preloadOps.map(async (ops)=>{
|
|
1416
1526
|
const { remote } = ops;
|
|
1417
1527
|
const remoteInfo = getRemoteInfo(remote);
|
|
1418
|
-
const { globalSnapshot, remoteSnapshot } = await
|
|
1528
|
+
const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo(remote);
|
|
1419
1529
|
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
|
|
1420
|
-
origin:
|
|
1530
|
+
origin: host,
|
|
1421
1531
|
preloadOptions: ops,
|
|
1422
1532
|
remote,
|
|
1423
1533
|
remoteInfo,
|
|
@@ -1427,117 +1537,279 @@ class FederationHost {
|
|
|
1427
1537
|
if (!assets) {
|
|
1428
1538
|
return;
|
|
1429
1539
|
}
|
|
1430
|
-
preloadAssets(remoteInfo,
|
|
1540
|
+
preloadAssets(remoteInfo, host, assets);
|
|
1431
1541
|
}));
|
|
1432
1542
|
}
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1543
|
+
registerRemotes(remotes, options) {
|
|
1544
|
+
const { host } = this;
|
|
1545
|
+
remotes.forEach((remote)=>{
|
|
1546
|
+
this.registerRemote(remote, host.options.remotes, {
|
|
1547
|
+
force: options == null ? void 0 : options.force
|
|
1548
|
+
});
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
async getRemoteModuleAndOptions(options) {
|
|
1552
|
+
const { host } = this;
|
|
1553
|
+
const { id } = options;
|
|
1554
|
+
let loadRemoteArgs;
|
|
1555
|
+
try {
|
|
1556
|
+
loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
|
|
1557
|
+
id,
|
|
1558
|
+
options: host.options,
|
|
1559
|
+
origin: host
|
|
1560
|
+
});
|
|
1561
|
+
} catch (error) {
|
|
1562
|
+
loadRemoteArgs = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
1563
|
+
id,
|
|
1564
|
+
options: host.options,
|
|
1565
|
+
origin: host,
|
|
1566
|
+
from: 'runtime',
|
|
1567
|
+
error,
|
|
1568
|
+
lifecycle: 'beforeRequest'
|
|
1569
|
+
});
|
|
1570
|
+
if (!loadRemoteArgs) {
|
|
1571
|
+
throw error;
|
|
1456
1572
|
}
|
|
1457
|
-
}
|
|
1458
|
-
const
|
|
1459
|
-
const
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1573
|
+
}
|
|
1574
|
+
const { id: idRes } = loadRemoteArgs;
|
|
1575
|
+
const remoteSplitInfo = matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
1576
|
+
share.assert(remoteSplitInfo, `
|
|
1577
|
+
Unable to locate ${idRes} in ${host.options.name}. Potential reasons for failure include:\n
|
|
1578
|
+
1. ${idRes} was not included in the 'remotes' parameter of ${host.options.name || 'the host'}.\n
|
|
1579
|
+
2. ${idRes} could not be found in the 'remotes' of ${host.options.name} with either 'name' or 'alias' attributes.
|
|
1580
|
+
3. ${idRes} is not online, injected, or loaded.
|
|
1581
|
+
4. ${idRes} cannot be accessed on the expected.
|
|
1582
|
+
5. The 'beforeRequest' hook was provided but did not return the correct 'remoteInfo' when attempting to load ${idRes}.
|
|
1583
|
+
`);
|
|
1584
|
+
const { remote: rawRemote } = remoteSplitInfo;
|
|
1585
|
+
const remoteInfo = getRemoteInfo(rawRemote);
|
|
1586
|
+
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit(_extends$1({
|
|
1587
|
+
id: idRes
|
|
1588
|
+
}, remoteSplitInfo, {
|
|
1589
|
+
options: host.options,
|
|
1590
|
+
origin: host,
|
|
1591
|
+
remoteInfo
|
|
1592
|
+
}));
|
|
1593
|
+
const { remote, expose } = matchInfo;
|
|
1594
|
+
share.assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
1595
|
+
let module = host.moduleCache.get(remote.name);
|
|
1596
|
+
const moduleOptions = {
|
|
1597
|
+
host: host,
|
|
1598
|
+
remoteInfo
|
|
1599
|
+
};
|
|
1600
|
+
if (!module) {
|
|
1601
|
+
module = new Module(moduleOptions);
|
|
1602
|
+
host.moduleCache.set(remote.name, module);
|
|
1603
|
+
}
|
|
1604
|
+
return {
|
|
1605
|
+
module,
|
|
1606
|
+
moduleOptions,
|
|
1607
|
+
remoteMatchInfo: matchInfo
|
|
1608
|
+
};
|
|
1609
|
+
}
|
|
1610
|
+
registerRemote(remote, targetRemotes, options) {
|
|
1611
|
+
const normalizeRemote = ()=>{
|
|
1612
|
+
if (remote.alias) {
|
|
1613
|
+
// Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error
|
|
1614
|
+
// As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported
|
|
1615
|
+
const findEqual = targetRemotes.find((item)=>{
|
|
1616
|
+
var _item_alias;
|
|
1617
|
+
return remote.alias && (item.name.startsWith(remote.alias) || ((_item_alias = item.alias) == null ? void 0 : _item_alias.startsWith(remote.alias)));
|
|
1618
|
+
});
|
|
1619
|
+
share.assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
1620
|
+
}
|
|
1621
|
+
// Set the remote entry to a complete path
|
|
1622
|
+
if ('entry' in remote) {
|
|
1623
|
+
if (sdk.isBrowserEnv() && !remote.entry.startsWith('http')) {
|
|
1624
|
+
remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
1467
1625
|
}
|
|
1468
1626
|
}
|
|
1627
|
+
if (!remote.shareScope) {
|
|
1628
|
+
remote.shareScope = share.DEFAULT_SCOPE;
|
|
1629
|
+
}
|
|
1630
|
+
if (!remote.type) {
|
|
1631
|
+
remote.type = share.DEFAULT_REMOTE_TYPE;
|
|
1632
|
+
}
|
|
1469
1633
|
};
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1634
|
+
const registeredRemote = targetRemotes.find((item)=>item.name === remote.name);
|
|
1635
|
+
if (!registeredRemote) {
|
|
1636
|
+
normalizeRemote();
|
|
1637
|
+
targetRemotes.push(remote);
|
|
1638
|
+
} else {
|
|
1639
|
+
const messages = [
|
|
1640
|
+
`The remote "${remote.name}" is already registered.`,
|
|
1641
|
+
(options == null ? void 0 : options.force) ? 'Hope you have known that OVERRIDE it may have some unexpected errors' : 'If you want to merge the remote, you can set "force: true".'
|
|
1642
|
+
];
|
|
1643
|
+
if (options == null ? void 0 : options.force) {
|
|
1644
|
+
// remove registered remote
|
|
1645
|
+
this.removeRemote(registeredRemote);
|
|
1646
|
+
normalizeRemote();
|
|
1647
|
+
targetRemotes.push(remote);
|
|
1648
|
+
}
|
|
1649
|
+
sdk.warn(messages.join(' '));
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
removeRemote(remote) {
|
|
1653
|
+
const { host } = this;
|
|
1654
|
+
const { name } = remote;
|
|
1655
|
+
const remoteIndex = host.options.remotes.findIndex((item)=>item.name === name);
|
|
1656
|
+
if (remoteIndex !== -1) {
|
|
1657
|
+
host.options.remotes.splice(remoteIndex, 1);
|
|
1658
|
+
}
|
|
1659
|
+
const loadedModule = host.moduleCache.get(remote.name);
|
|
1660
|
+
if (loadedModule) {
|
|
1661
|
+
const remoteInfo = loadedModule.remoteInfo;
|
|
1662
|
+
const key = remoteInfo.entryGlobalName;
|
|
1663
|
+
if (globalThis[key]) {
|
|
1664
|
+
delete globalThis[key];
|
|
1665
|
+
}
|
|
1666
|
+
const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
1667
|
+
if (share.globalLoading[remoteEntryUniqueKey]) {
|
|
1668
|
+
delete share.globalLoading[remoteEntryUniqueKey];
|
|
1669
|
+
}
|
|
1670
|
+
// delete un loaded shared and instance
|
|
1671
|
+
let remoteInsId = remoteInfo.buildVersion ? sdk.composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
|
|
1672
|
+
const remoteInsIndex = globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins)=>{
|
|
1673
|
+
if (remoteInfo.buildVersion) {
|
|
1674
|
+
return ins.options.id === remoteInsId;
|
|
1675
|
+
} else {
|
|
1676
|
+
return ins.name === remoteInsId;
|
|
1475
1677
|
}
|
|
1476
1678
|
});
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1679
|
+
if (remoteInsIndex !== -1) {
|
|
1680
|
+
const remoteIns = globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
|
|
1681
|
+
remoteInsId = remoteIns.options.id || remoteInsId;
|
|
1682
|
+
const globalShareScopeMap = share.getGlobalShareScope();
|
|
1683
|
+
let isAllSharedNotUsed = true;
|
|
1684
|
+
const needDeleteKeys = [];
|
|
1685
|
+
Object.keys(globalShareScopeMap).forEach((instId)=>{
|
|
1686
|
+
Object.keys(globalShareScopeMap[instId]).forEach((shareScope)=>{
|
|
1687
|
+
Object.keys(globalShareScopeMap[instId][shareScope]).forEach((shareName)=>{
|
|
1688
|
+
Object.keys(globalShareScopeMap[instId][shareScope][shareName]).forEach((shareVersion)=>{
|
|
1689
|
+
const shared = globalShareScopeMap[instId][shareScope][shareName][shareVersion];
|
|
1690
|
+
if (shared.from === remoteInfo.name) {
|
|
1691
|
+
if (shared.loaded || shared.loading) {
|
|
1692
|
+
shared.useIn = shared.useIn.filter((usedHostName)=>usedHostName !== remoteInfo.name);
|
|
1693
|
+
if (shared.useIn.length) {
|
|
1694
|
+
isAllSharedNotUsed = false;
|
|
1695
|
+
} else {
|
|
1696
|
+
needDeleteKeys.push([
|
|
1697
|
+
instId,
|
|
1698
|
+
shareScope,
|
|
1699
|
+
shareName,
|
|
1700
|
+
shareVersion
|
|
1701
|
+
]);
|
|
1702
|
+
}
|
|
1703
|
+
} else {
|
|
1704
|
+
needDeleteKeys.push([
|
|
1705
|
+
instId,
|
|
1706
|
+
shareScope,
|
|
1707
|
+
shareName,
|
|
1708
|
+
shareVersion
|
|
1709
|
+
]);
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
});
|
|
1713
|
+
});
|
|
1714
|
+
});
|
|
1715
|
+
});
|
|
1716
|
+
if (isAllSharedNotUsed) {
|
|
1717
|
+
remoteIns.shareScopeMap = {};
|
|
1718
|
+
delete globalShareScopeMap[remoteInsId];
|
|
1482
1719
|
}
|
|
1483
|
-
|
|
1720
|
+
needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion])=>{
|
|
1721
|
+
var _globalShareScopeMap_insId_shareScope_shareName, _globalShareScopeMap_insId_shareScope, _globalShareScopeMap_insId;
|
|
1722
|
+
(_globalShareScopeMap_insId = globalShareScopeMap[insId]) == null ? true : (_globalShareScopeMap_insId_shareScope = _globalShareScopeMap_insId[shareScope]) == null ? true : (_globalShareScopeMap_insId_shareScope_shareName = _globalShareScopeMap_insId_shareScope[shareName]) == null ? true : delete _globalShareScopeMap_insId_shareScope_shareName[shareVersion];
|
|
1723
|
+
});
|
|
1724
|
+
globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
|
|
1725
|
+
}
|
|
1726
|
+
host.moduleCache.delete(remote.name);
|
|
1484
1727
|
}
|
|
1485
|
-
return promises;
|
|
1486
1728
|
}
|
|
1487
|
-
|
|
1488
|
-
this.
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1729
|
+
constructor(host){
|
|
1730
|
+
this.hooks = new PluginSystem({
|
|
1731
|
+
beforeRequest: new AsyncWaterfallHook('beforeRequest'),
|
|
1732
|
+
onLoad: new AsyncHook('onLoad'),
|
|
1733
|
+
handlePreloadModule: new SyncHook('handlePreloadModule'),
|
|
1734
|
+
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
1735
|
+
beforePreloadRemote: new AsyncHook(),
|
|
1736
|
+
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
1737
|
+
// not used yet
|
|
1738
|
+
afterPreloadRemote: new AsyncHook()
|
|
1493
1739
|
});
|
|
1740
|
+
this.host = host;
|
|
1494
1741
|
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
shared[shareKey].push(newUserSharedOptions);
|
|
1506
|
-
}
|
|
1507
|
-
});
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
function _extends() {
|
|
1745
|
+
_extends = Object.assign || function(target) {
|
|
1746
|
+
for(var i = 1; i < arguments.length; i++){
|
|
1747
|
+
var source = arguments[i];
|
|
1748
|
+
for(var key in source){
|
|
1749
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
1750
|
+
target[key] = source[key];
|
|
1751
|
+
}
|
|
1508
1752
|
}
|
|
1753
|
+
}
|
|
1754
|
+
return target;
|
|
1755
|
+
};
|
|
1756
|
+
return _extends.apply(this, arguments);
|
|
1757
|
+
}
|
|
1758
|
+
class FederationHost {
|
|
1759
|
+
initOptions(userOptions) {
|
|
1760
|
+
this.registerPlugins(userOptions.plugins);
|
|
1761
|
+
const options = this.formatOptions(this.options, userOptions);
|
|
1762
|
+
this.options = options;
|
|
1763
|
+
return options;
|
|
1764
|
+
}
|
|
1765
|
+
async loadShare(pkgName, extraOptions) {
|
|
1766
|
+
return this.sharedHandler.loadShare(pkgName, extraOptions);
|
|
1767
|
+
}
|
|
1768
|
+
// The lib function will only be available if the shared set by eager or runtime init is set or the shared is successfully loaded.
|
|
1769
|
+
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1770
|
+
// 2. If lib exists in local shared, it will be used directly
|
|
1771
|
+
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1772
|
+
loadShareSync(pkgName, extraOptions) {
|
|
1773
|
+
return this.sharedHandler.loadShareSync(pkgName, extraOptions);
|
|
1774
|
+
}
|
|
1775
|
+
initializeSharing(shareScopeName = share.DEFAULT_SCOPE, strategy) {
|
|
1776
|
+
return this.sharedHandler.initializeSharing(shareScopeName, strategy);
|
|
1777
|
+
}
|
|
1778
|
+
initRawContainer(name, url, container) {
|
|
1779
|
+
const remoteInfo = getRemoteInfo({
|
|
1780
|
+
name,
|
|
1781
|
+
entry: url
|
|
1782
|
+
});
|
|
1783
|
+
const module = new Module({
|
|
1784
|
+
host: this,
|
|
1785
|
+
remoteInfo
|
|
1509
1786
|
});
|
|
1787
|
+
module.remoteEntryExports = container;
|
|
1788
|
+
this.moduleCache.set(name, module);
|
|
1789
|
+
return module;
|
|
1790
|
+
}
|
|
1791
|
+
// eslint-disable-next-line max-lines-per-function
|
|
1792
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1793
|
+
async loadRemote(id, options) {
|
|
1794
|
+
return this.remoteHandler.loadRemote(id, options);
|
|
1795
|
+
}
|
|
1796
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1797
|
+
async preloadRemote(preloadOptions) {
|
|
1798
|
+
return this.remoteHandler.preloadRemote(preloadOptions);
|
|
1799
|
+
}
|
|
1800
|
+
initShareScopeMap(scopeName, shareScope) {
|
|
1801
|
+
this.sharedHandler.initShareScopeMap(scopeName, shareScope);
|
|
1802
|
+
}
|
|
1803
|
+
formatOptions(globalOptions, userOptions) {
|
|
1804
|
+
const { shared } = share.formatShareConfigs(globalOptions, userOptions);
|
|
1510
1805
|
const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
|
|
1511
1806
|
origin: this,
|
|
1512
1807
|
userOptions,
|
|
1513
1808
|
options: globalOptions,
|
|
1514
1809
|
shareInfo: shared
|
|
1515
1810
|
});
|
|
1516
|
-
const
|
|
1517
|
-
const
|
|
1518
|
-
this.registerRemote(remote, res, {
|
|
1519
|
-
force: false
|
|
1520
|
-
});
|
|
1521
|
-
return res;
|
|
1522
|
-
}, globalOptionsRes.remotes);
|
|
1523
|
-
// register shared in shareScopeMap
|
|
1524
|
-
const sharedKeys = Object.keys(formatShareOptions);
|
|
1525
|
-
sharedKeys.forEach((sharedKey)=>{
|
|
1526
|
-
const sharedVals = formatShareOptions[sharedKey];
|
|
1527
|
-
sharedVals.forEach((sharedVal)=>{
|
|
1528
|
-
const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
1529
|
-
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
1530
|
-
this.setShared({
|
|
1531
|
-
pkgName: sharedKey,
|
|
1532
|
-
lib: sharedVal.lib,
|
|
1533
|
-
get: sharedVal.get,
|
|
1534
|
-
loaded: true,
|
|
1535
|
-
shared: sharedVal,
|
|
1536
|
-
from: userOptions.name
|
|
1537
|
-
});
|
|
1538
|
-
}
|
|
1539
|
-
});
|
|
1540
|
-
});
|
|
1811
|
+
const remotes = this.remoteHandler.formatAndRegisterRemote(globalOptionsRes, userOptionsRes);
|
|
1812
|
+
const { shared: handledShared } = this.sharedHandler.registerShared(globalOptionsRes, userOptionsRes);
|
|
1541
1813
|
const plugins = [
|
|
1542
1814
|
...globalOptionsRes.plugins
|
|
1543
1815
|
];
|
|
@@ -1551,7 +1823,7 @@ class FederationHost {
|
|
|
1551
1823
|
const optionsRes = _extends({}, globalOptions, userOptions, {
|
|
1552
1824
|
plugins,
|
|
1553
1825
|
remotes,
|
|
1554
|
-
shared
|
|
1826
|
+
shared: handledShared
|
|
1555
1827
|
});
|
|
1556
1828
|
this.hooks.lifecycle.init.emit({
|
|
1557
1829
|
origin: this,
|
|
@@ -1562,6 +1834,8 @@ class FederationHost {
|
|
|
1562
1834
|
registerPlugins(plugins) {
|
|
1563
1835
|
const pluginRes = registerPlugins$1(plugins, [
|
|
1564
1836
|
this.hooks,
|
|
1837
|
+
this.remoteHandler.hooks,
|
|
1838
|
+
this.sharedHandler.hooks,
|
|
1565
1839
|
this.snapshotHandler.hooks,
|
|
1566
1840
|
this.loaderHook
|
|
1567
1841
|
]);
|
|
@@ -1574,132 +1848,19 @@ class FederationHost {
|
|
|
1574
1848
|
return res;
|
|
1575
1849
|
}, pluginRes || []);
|
|
1576
1850
|
}
|
|
1577
|
-
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
1578
|
-
const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
|
|
1579
|
-
"version",
|
|
1580
|
-
"scope"
|
|
1581
|
-
]);
|
|
1582
|
-
const scopes = Array.isArray(scope) ? scope : [
|
|
1583
|
-
scope
|
|
1584
|
-
];
|
|
1585
|
-
scopes.forEach((sc)=>{
|
|
1586
|
-
if (!this.shareScopeMap[sc]) {
|
|
1587
|
-
this.shareScopeMap[sc] = {};
|
|
1588
|
-
}
|
|
1589
|
-
if (!this.shareScopeMap[sc][pkgName]) {
|
|
1590
|
-
this.shareScopeMap[sc][pkgName] = {};
|
|
1591
|
-
}
|
|
1592
|
-
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
1593
|
-
return;
|
|
1594
|
-
}
|
|
1595
|
-
this.shareScopeMap[sc][pkgName][version] = _extends({
|
|
1596
|
-
version,
|
|
1597
|
-
scope: [
|
|
1598
|
-
'default'
|
|
1599
|
-
]
|
|
1600
|
-
}, shareInfo, {
|
|
1601
|
-
lib,
|
|
1602
|
-
loaded,
|
|
1603
|
-
loading
|
|
1604
|
-
});
|
|
1605
|
-
if (get) {
|
|
1606
|
-
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
1607
|
-
}
|
|
1608
|
-
});
|
|
1609
|
-
}
|
|
1610
|
-
removeRemote(remote) {
|
|
1611
|
-
const { name } = remote;
|
|
1612
|
-
const remoteIndex = this.options.remotes.findIndex((item)=>item.name === name);
|
|
1613
|
-
if (remoteIndex !== -1) {
|
|
1614
|
-
this.options.remotes.splice(remoteIndex, 1);
|
|
1615
|
-
}
|
|
1616
|
-
const loadedModule = this.moduleCache.get(remote.name);
|
|
1617
|
-
if (loadedModule) {
|
|
1618
|
-
const key = loadedModule.remoteInfo.entryGlobalName;
|
|
1619
|
-
if (globalThis[key]) {
|
|
1620
|
-
delete globalThis[key];
|
|
1621
|
-
}
|
|
1622
|
-
const remoteEntryUniqueKey = getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
1623
|
-
if (share.globalLoading[remoteEntryUniqueKey]) {
|
|
1624
|
-
delete share.globalLoading[remoteEntryUniqueKey];
|
|
1625
|
-
}
|
|
1626
|
-
this.moduleCache.delete(remote.name);
|
|
1627
|
-
}
|
|
1628
|
-
}
|
|
1629
|
-
registerRemote(remote, targetRemotes, options) {
|
|
1630
|
-
const normalizeRemote = ()=>{
|
|
1631
|
-
if (remote.alias) {
|
|
1632
|
-
// Validate if alias equals the prefix of remote.name and remote.alias, if so, throw an error
|
|
1633
|
-
// As multi-level path references cannot guarantee unique names, alias being a prefix of remote.name is not supported
|
|
1634
|
-
const findEqual = targetRemotes.find((item)=>{
|
|
1635
|
-
var _item_alias;
|
|
1636
|
-
return remote.alias && (item.name.startsWith(remote.alias) || ((_item_alias = item.alias) == null ? void 0 : _item_alias.startsWith(remote.alias)));
|
|
1637
|
-
});
|
|
1638
|
-
share.assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
1639
|
-
}
|
|
1640
|
-
// Set the remote entry to a complete path
|
|
1641
|
-
if ('entry' in remote) {
|
|
1642
|
-
if (share.isBrowserEnv() && !remote.entry.startsWith('http')) {
|
|
1643
|
-
remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1646
|
-
if (!remote.shareScope) {
|
|
1647
|
-
remote.shareScope = share.DEFAULT_SCOPE;
|
|
1648
|
-
}
|
|
1649
|
-
if (!remote.type) {
|
|
1650
|
-
remote.type = share.DEFAULT_REMOTE_TYPE;
|
|
1651
|
-
}
|
|
1652
|
-
};
|
|
1653
|
-
const registeredRemote = targetRemotes.find((item)=>item.name === remote.name);
|
|
1654
|
-
if (!registeredRemote) {
|
|
1655
|
-
normalizeRemote();
|
|
1656
|
-
targetRemotes.push(remote);
|
|
1657
|
-
} else {
|
|
1658
|
-
const messages = [
|
|
1659
|
-
`The remote "${remote.name}" is already registered.`,
|
|
1660
|
-
(options == null ? void 0 : options.force) ? 'Hope you have known that OVERRIDE it may have some unexpected errors' : 'If you want to merge the remote, you can set "force: true".'
|
|
1661
|
-
];
|
|
1662
|
-
if (options == null ? void 0 : options.force) {
|
|
1663
|
-
// remove registered remote
|
|
1664
|
-
this.removeRemote(registeredRemote);
|
|
1665
|
-
normalizeRemote();
|
|
1666
|
-
targetRemotes.push(remote);
|
|
1667
|
-
}
|
|
1668
|
-
share.warn(messages.join(' '));
|
|
1669
|
-
}
|
|
1670
|
-
}
|
|
1671
1851
|
registerRemotes(remotes, options) {
|
|
1672
|
-
|
|
1673
|
-
this.registerRemote(remote, this.options.remotes, {
|
|
1674
|
-
force: options == null ? void 0 : options.force
|
|
1675
|
-
});
|
|
1676
|
-
});
|
|
1852
|
+
return this.remoteHandler.registerRemotes(remotes, options);
|
|
1677
1853
|
}
|
|
1678
1854
|
constructor(userOptions){
|
|
1679
1855
|
this.hooks = new PluginSystem({
|
|
1680
1856
|
beforeInit: new SyncWaterfallHook('beforeInit'),
|
|
1681
1857
|
init: new SyncHook(),
|
|
1682
|
-
beforeRequest: new AsyncWaterfallHook('beforeRequest'),
|
|
1683
|
-
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
1684
1858
|
// maybe will change, temporarily for internal use only
|
|
1685
1859
|
beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
|
|
1686
1860
|
// maybe will change, temporarily for internal use only
|
|
1687
|
-
|
|
1688
|
-
// maybe will change, temporarily for internal use only
|
|
1689
|
-
initContainer: new AsyncWaterfallHook('initContainer'),
|
|
1690
|
-
onLoad: new AsyncHook('onLoad'),
|
|
1691
|
-
handlePreloadModule: new SyncHook('handlePreloadModule'),
|
|
1692
|
-
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
1693
|
-
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
1694
|
-
// not used yet
|
|
1695
|
-
loadShare: new AsyncHook(),
|
|
1696
|
-
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
1697
|
-
beforePreloadRemote: new AsyncHook(),
|
|
1698
|
-
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
1699
|
-
// not used yet
|
|
1700
|
-
afterPreloadRemote: new AsyncHook()
|
|
1861
|
+
initContainer: new AsyncWaterfallHook('initContainer')
|
|
1701
1862
|
});
|
|
1702
|
-
this.version = "0.1.
|
|
1863
|
+
this.version = "0.1.13";
|
|
1703
1864
|
this.moduleCache = new Map();
|
|
1704
1865
|
this.loaderHook = new PluginSystem({
|
|
1705
1866
|
// FIXME: may not be suitable , not open to the public yet
|
|
@@ -1724,9 +1885,10 @@ class FederationHost {
|
|
|
1724
1885
|
};
|
|
1725
1886
|
this.name = userOptions.name;
|
|
1726
1887
|
this.options = defaultOptions;
|
|
1727
|
-
this.shareScopeMap = {};
|
|
1728
|
-
this._setGlobalShareScopeMap();
|
|
1729
1888
|
this.snapshotHandler = new SnapshotHandler(this);
|
|
1889
|
+
this.sharedHandler = new SharedHandler(this);
|
|
1890
|
+
this.remoteHandler = new RemoteHandler(this);
|
|
1891
|
+
this.shareScopeMap = this.sharedHandler.shareScopeMap;
|
|
1730
1892
|
this.registerPlugins([
|
|
1731
1893
|
...defaultOptions.plugins,
|
|
1732
1894
|
...userOptions.plugins || []
|