@module-federation/runtime 0.0.6 → 0.0.8
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 +513 -124
- package/dist/helpers.cjs.js +2 -1
- package/dist/helpers.esm.js +3 -2
- package/dist/index.cjs.js +208 -104
- package/dist/index.esm.js +207 -106
- package/dist/package.json +3 -3
- package/dist/share.cjs.js +147 -111
- package/dist/share.esm.js +147 -111
- package/dist/src/core.d.ts +39 -7
- package/dist/src/global.d.ts +3 -6
- package/dist/src/helpers.d.ts +4 -3
- package/dist/src/index.d.ts +3 -1
- package/dist/src/module/index.d.ts +4 -10
- package/dist/src/type/config.d.ts +8 -2
- package/dist/src/type/plugin.d.ts +1 -1
- package/dist/src/utils/plugin.d.ts +1 -1
- package/dist/src/utils/share.d.ts +12 -3
- package/dist/types.cjs.d.ts +1 -0
- package/package.json +4 -4
- package/dist/type.cjs.d.ts +0 -1
- /package/dist/{type.cjs.js → types.cjs.js} +0 -0
- /package/dist/{type.esm.js → types.esm.js} +0 -0
package/dist/helpers.cjs.js
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
var share = require('./share.cjs.js');
|
|
4
4
|
|
|
5
5
|
const ShareUtils = {
|
|
6
|
-
|
|
6
|
+
getRegisteredShare: share.getRegisteredShare,
|
|
7
7
|
getGlobalShareScope: share.getGlobalShareScope
|
|
8
8
|
};
|
|
9
9
|
const GlobalUtils = {
|
|
10
|
+
Global: share.Global,
|
|
10
11
|
nativeGlobal: share.nativeGlobal,
|
|
11
12
|
resetFederationGlobalInfo: share.resetFederationGlobalInfo,
|
|
12
13
|
getGlobalFederationInstance: share.getGlobalFederationInstance,
|
package/dist/helpers.esm.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { l as
|
|
1
|
+
import { l as getRegisteredShare, t as getGlobalShareScope, G as Global, F as nativeGlobal, H as resetFederationGlobalInfo, A as getGlobalFederationInstance, C as setGlobalFederationInstance, B as getGlobalFederationConstructor, z as setGlobalFederationConstructor, m as getInfoWithoutType, r as getGlobalSnapshot, I as getTargetSnapshotInfoByModuleInfo, p as getGlobalSnapshotInfoByModuleInfo, q as setGlobalSnapshotInfoByModuleInfo, J as addGlobalSnapshot, c as getRemoteEntryExports, E as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
|
|
2
2
|
|
|
3
3
|
const ShareUtils = {
|
|
4
|
-
|
|
4
|
+
getRegisteredShare,
|
|
5
5
|
getGlobalShareScope
|
|
6
6
|
};
|
|
7
7
|
const GlobalUtils = {
|
|
8
|
+
Global,
|
|
8
9
|
nativeGlobal,
|
|
9
10
|
resetFederationGlobalInfo,
|
|
10
11
|
getGlobalFederationInstance,
|
package/dist/index.cjs.js
CHANGED
|
@@ -121,6 +121,27 @@ async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
|
|
|
121
121
|
if (remoteEntryExports) {
|
|
122
122
|
return remoteEntryExports;
|
|
123
123
|
}
|
|
124
|
+
if (typeof document === 'undefined') {
|
|
125
|
+
return sdk.loadScriptNode(entry, {
|
|
126
|
+
attrs: {
|
|
127
|
+
name,
|
|
128
|
+
globalName
|
|
129
|
+
},
|
|
130
|
+
createScriptHook
|
|
131
|
+
}).then(()=>{
|
|
132
|
+
const { remoteEntryKey, entryExports } = share.getRemoteEntryExports(name, globalName);
|
|
133
|
+
share.assert(entryExports, `
|
|
134
|
+
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
135
|
+
Possible reasons could be:\n
|
|
136
|
+
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
137
|
+
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
138
|
+
`);
|
|
139
|
+
console.log(entryExports);
|
|
140
|
+
return entryExports;
|
|
141
|
+
}).catch((e)=>{
|
|
142
|
+
return e;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
124
145
|
return sdk.loadScript(entry, {
|
|
125
146
|
attrs: {},
|
|
126
147
|
createScriptHook
|
|
@@ -133,6 +154,8 @@ async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
|
|
|
133
154
|
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
134
155
|
`);
|
|
135
156
|
return entryExports;
|
|
157
|
+
}).catch((e)=>{
|
|
158
|
+
return e;
|
|
136
159
|
});
|
|
137
160
|
}
|
|
138
161
|
async function getRemoteEntry({ remoteEntryExports, remoteInfo, createScriptHook }) {
|
|
@@ -191,9 +214,13 @@ let Module = class Module {
|
|
|
191
214
|
remoteInfo: this.remoteInfo,
|
|
192
215
|
remoteEntryExports: this.remoteEntryExports,
|
|
193
216
|
createScriptHook: (url)=>{
|
|
194
|
-
const res = this.loaderHook.lifecycle.createScript.emit({
|
|
217
|
+
const res = this.host.loaderHook.lifecycle.createScript.emit({
|
|
195
218
|
url
|
|
196
219
|
});
|
|
220
|
+
if (typeof document === 'undefined') {
|
|
221
|
+
//todo: needs real fix
|
|
222
|
+
return res;
|
|
223
|
+
}
|
|
197
224
|
if (res instanceof HTMLScriptElement) {
|
|
198
225
|
return res;
|
|
199
226
|
}
|
|
@@ -212,26 +239,34 @@ let Module = class Module {
|
|
|
212
239
|
// Get remoteEntry.js
|
|
213
240
|
const remoteEntryExports = await this.getEntry();
|
|
214
241
|
if (!this.inited) {
|
|
215
|
-
const
|
|
242
|
+
const localShareScopeMap = this.host.shareScopeMap;
|
|
216
243
|
const remoteShareScope = this.remoteInfo.shareScope || 'default';
|
|
217
|
-
if (!
|
|
218
|
-
|
|
244
|
+
if (!localShareScopeMap[remoteShareScope]) {
|
|
245
|
+
localShareScopeMap[remoteShareScope] = {};
|
|
219
246
|
}
|
|
220
|
-
const shareScope =
|
|
221
|
-
|
|
247
|
+
const shareScope = localShareScopeMap[remoteShareScope];
|
|
248
|
+
const initScope = [];
|
|
222
249
|
const remoteEntryInitOptions = {
|
|
223
|
-
version: this.remoteInfo.version || ''
|
|
224
|
-
// @ts-ignore it will be passed by startup hooks
|
|
225
|
-
region: this.hostInfo.region
|
|
250
|
+
version: this.remoteInfo.version || ''
|
|
226
251
|
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
252
|
+
// Help to find host instance
|
|
253
|
+
Object.defineProperty(remoteEntryInitOptions, 'hostId', {
|
|
254
|
+
value: this.host.options.id || this.host.name,
|
|
255
|
+
// remoteEntryInitOptions will be traversed and assigned during container init, ,so this attribute is not allowed to be traversed
|
|
256
|
+
enumerable: false
|
|
257
|
+
});
|
|
258
|
+
const initContainerOptions = await this.host.hooks.lifecycle.beforeInitContainer.emit({
|
|
259
|
+
shareScope,
|
|
260
|
+
// @ts-ignore hostId will be set by Object.defineProperty
|
|
261
|
+
remoteEntryInitOptions,
|
|
262
|
+
initScope,
|
|
263
|
+
remoteInfo: this.remoteInfo,
|
|
264
|
+
origin: this.host
|
|
265
|
+
});
|
|
266
|
+
remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
|
|
267
|
+
await this.host.hooks.lifecycle.initContainer.emit(_extends$4({}, initContainerOptions, {
|
|
268
|
+
remoteEntryExports
|
|
269
|
+
}));
|
|
235
270
|
}
|
|
236
271
|
this.lib = remoteEntryExports;
|
|
237
272
|
this.inited = true;
|
|
@@ -244,15 +279,11 @@ let Module = class Module {
|
|
|
244
279
|
const exposeContent = await moduleFactory();
|
|
245
280
|
return exposeContent;
|
|
246
281
|
}
|
|
247
|
-
|
|
248
|
-
constructor({ hostInfo, remoteInfo, shared, loaderHook }){
|
|
282
|
+
constructor({ remoteInfo, host }){
|
|
249
283
|
this.inited = false;
|
|
250
|
-
this.shared = {};
|
|
251
284
|
this.lib = undefined;
|
|
252
|
-
this.hostInfo = hostInfo;
|
|
253
285
|
this.remoteInfo = remoteInfo;
|
|
254
|
-
this.
|
|
255
|
-
this.loaderHook = loaderHook;
|
|
286
|
+
this.host = host;
|
|
256
287
|
}
|
|
257
288
|
};
|
|
258
289
|
|
|
@@ -779,9 +810,9 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
779
810
|
if (!shareInfo) {
|
|
780
811
|
return;
|
|
781
812
|
}
|
|
782
|
-
const
|
|
813
|
+
const registeredShared = share.getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
783
814
|
// 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.
|
|
784
|
-
if (
|
|
815
|
+
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
785
816
|
shared.assets.js.sync.forEach((asset)=>{
|
|
786
817
|
loadedSharedJsAssets.add(asset);
|
|
787
818
|
});
|
|
@@ -1108,13 +1139,19 @@ function _object_without_properties_loose(source, excluded) {
|
|
|
1108
1139
|
return target;
|
|
1109
1140
|
}
|
|
1110
1141
|
class FederationHost {
|
|
1142
|
+
_setGlobalShareScopeMap() {
|
|
1143
|
+
const globalShareScopeMap = share.getGlobalShareScope();
|
|
1144
|
+
const identifier = this.options.id || this.options.name;
|
|
1145
|
+
if (identifier && !globalShareScopeMap[identifier]) {
|
|
1146
|
+
globalShareScopeMap[identifier] = this.shareScopeMap;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1111
1149
|
initOptions(userOptions) {
|
|
1112
1150
|
this.registerPlugins(userOptions.plugins);
|
|
1113
1151
|
const options = this.formatOptions(this.options, userOptions);
|
|
1114
1152
|
this.options = options;
|
|
1115
1153
|
return options;
|
|
1116
1154
|
}
|
|
1117
|
-
// overrideSharedOptions(shareScope: GlobalShareScope[string]): void {}
|
|
1118
1155
|
async loadShare(pkgName, customShareInfo) {
|
|
1119
1156
|
var _this_options_shared;
|
|
1120
1157
|
// This function performs the following steps:
|
|
@@ -1122,6 +1159,12 @@ class FederationHost {
|
|
|
1122
1159
|
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
1123
1160
|
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
|
|
1124
1161
|
const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
|
|
1162
|
+
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1163
|
+
await Promise.all(shareInfo.scope.map(async (shareScope)=>{
|
|
1164
|
+
await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
|
|
1165
|
+
return;
|
|
1166
|
+
}));
|
|
1167
|
+
}
|
|
1125
1168
|
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
|
|
1126
1169
|
pkgName,
|
|
1127
1170
|
shareInfo,
|
|
@@ -1132,30 +1175,42 @@ class FederationHost {
|
|
|
1132
1175
|
// Assert that shareInfoRes exists, if not, throw an error
|
|
1133
1176
|
share.assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${this.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
1134
1177
|
// Retrieve from cache
|
|
1135
|
-
const
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1178
|
+
const registeredShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1179
|
+
const addUseIn = (shared)=>{
|
|
1180
|
+
if (!shared.useIn) {
|
|
1181
|
+
shared.useIn = [];
|
|
1182
|
+
}
|
|
1183
|
+
share.addUniqueItem(shared.useIn, this.options.name);
|
|
1184
|
+
};
|
|
1185
|
+
if (registeredShared && registeredShared.lib) {
|
|
1186
|
+
addUseIn(registeredShared);
|
|
1187
|
+
return registeredShared.lib;
|
|
1188
|
+
} else if (registeredShared && registeredShared.loading && !registeredShared.loaded) {
|
|
1189
|
+
const factory = await registeredShared.loading;
|
|
1190
|
+
registeredShared.loaded = true;
|
|
1191
|
+
if (!registeredShared.lib) {
|
|
1192
|
+
registeredShared.lib = factory;
|
|
1193
|
+
}
|
|
1194
|
+
addUseIn(registeredShared);
|
|
1142
1195
|
return factory;
|
|
1143
|
-
} else if (
|
|
1196
|
+
} else if (registeredShared) {
|
|
1144
1197
|
const asyncLoadProcess = async ()=>{
|
|
1145
|
-
const factory = await
|
|
1198
|
+
const factory = await registeredShared.get();
|
|
1146
1199
|
shareInfoRes.lib = factory;
|
|
1147
|
-
|
|
1148
|
-
|
|
1200
|
+
shareInfoRes.loaded = true;
|
|
1201
|
+
addUseIn(shareInfoRes);
|
|
1202
|
+
const gShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1149
1203
|
if (gShared) {
|
|
1150
1204
|
gShared.lib = factory;
|
|
1205
|
+
gShared.loaded = true;
|
|
1151
1206
|
}
|
|
1152
1207
|
return factory;
|
|
1153
1208
|
};
|
|
1154
1209
|
const loading = asyncLoadProcess();
|
|
1155
1210
|
this.setShared({
|
|
1156
1211
|
pkgName,
|
|
1157
|
-
loaded:
|
|
1158
|
-
shared:
|
|
1212
|
+
loaded: false,
|
|
1213
|
+
shared: registeredShared,
|
|
1159
1214
|
from: this.options.name,
|
|
1160
1215
|
lib: null,
|
|
1161
1216
|
loading
|
|
@@ -1168,17 +1223,19 @@ class FederationHost {
|
|
|
1168
1223
|
const asyncLoadProcess = async ()=>{
|
|
1169
1224
|
const factory = await shareInfoRes.get();
|
|
1170
1225
|
shareInfoRes.lib = factory;
|
|
1171
|
-
|
|
1172
|
-
|
|
1226
|
+
shareInfoRes.loaded = true;
|
|
1227
|
+
addUseIn(shareInfoRes);
|
|
1228
|
+
const gShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1173
1229
|
if (gShared) {
|
|
1174
1230
|
gShared.lib = factory;
|
|
1231
|
+
gShared.loaded = true;
|
|
1175
1232
|
}
|
|
1176
1233
|
return factory;
|
|
1177
1234
|
};
|
|
1178
1235
|
const loading = asyncLoadProcess();
|
|
1179
1236
|
this.setShared({
|
|
1180
1237
|
pkgName,
|
|
1181
|
-
loaded:
|
|
1238
|
+
loaded: false,
|
|
1182
1239
|
shared: shareInfoRes,
|
|
1183
1240
|
from: this.options.name,
|
|
1184
1241
|
lib: null,
|
|
@@ -1191,19 +1248,46 @@ class FederationHost {
|
|
|
1191
1248
|
// 1. If the loaded shared already exists globally, then it will be reused
|
|
1192
1249
|
// 2. If lib exists in local shared, it will be used directly
|
|
1193
1250
|
// 3. If the local get returns something other than Promise, then it will be used directly
|
|
1194
|
-
loadShareSync(pkgName) {
|
|
1251
|
+
loadShareSync(pkgName, customShareInfo) {
|
|
1195
1252
|
var _this_options_shared;
|
|
1196
|
-
const shareInfo = (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName];
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1253
|
+
const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
|
|
1254
|
+
if (shareInfo == null ? void 0 : shareInfo.scope) {
|
|
1255
|
+
shareInfo.scope.forEach((shareScope)=>{
|
|
1256
|
+
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
const registeredShared = share.getRegisteredShare(this.shareScopeMap, pkgName, shareInfo, this.hooks.lifecycle.resolveShare);
|
|
1260
|
+
const addUseIn = (shared)=>{
|
|
1261
|
+
if (!shared.useIn) {
|
|
1262
|
+
shared.useIn = [];
|
|
1263
|
+
}
|
|
1264
|
+
share.addUniqueItem(shared.useIn, this.options.name);
|
|
1265
|
+
};
|
|
1266
|
+
if (registeredShared) {
|
|
1267
|
+
if (typeof registeredShared.lib === 'function') {
|
|
1268
|
+
addUseIn(registeredShared);
|
|
1269
|
+
if (!registeredShared.loaded) {
|
|
1270
|
+
registeredShared.loaded = true;
|
|
1271
|
+
if (registeredShared.from === this.options.name) {
|
|
1272
|
+
shareInfo.loaded = true;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return registeredShared.lib;
|
|
1276
|
+
}
|
|
1277
|
+
if (typeof registeredShared.get === 'function') {
|
|
1278
|
+
const module = registeredShared.get();
|
|
1279
|
+
if (!(module instanceof Promise)) {
|
|
1280
|
+
addUseIn(registeredShared);
|
|
1281
|
+
this.setShared({
|
|
1282
|
+
pkgName,
|
|
1283
|
+
loaded: true,
|
|
1284
|
+
from: this.options.name,
|
|
1285
|
+
lib: module,
|
|
1286
|
+
shared: registeredShared
|
|
1287
|
+
});
|
|
1288
|
+
return module;
|
|
1204
1289
|
}
|
|
1205
1290
|
}
|
|
1206
|
-
return globalShare.lib;
|
|
1207
1291
|
}
|
|
1208
1292
|
if (shareInfo.lib) {
|
|
1209
1293
|
if (!shareInfo.loaded) {
|
|
@@ -1267,14 +1351,8 @@ class FederationHost {
|
|
|
1267
1351
|
share.assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
1268
1352
|
let module = this.moduleCache.get(remote.name);
|
|
1269
1353
|
const moduleOptions = {
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
version: this.options.version || 'custom'
|
|
1273
|
-
},
|
|
1274
|
-
remoteInfo,
|
|
1275
|
-
shared: this.options.shared || {},
|
|
1276
|
-
plugins: this.options.plugins,
|
|
1277
|
-
loaderHook: this.loaderHook
|
|
1354
|
+
host: this,
|
|
1355
|
+
remoteInfo
|
|
1278
1356
|
};
|
|
1279
1357
|
if (!module) {
|
|
1280
1358
|
module = new Module(moduleOptions);
|
|
@@ -1316,11 +1394,19 @@ class FederationHost {
|
|
|
1316
1394
|
});
|
|
1317
1395
|
return moduleOrFactory;
|
|
1318
1396
|
} catch (error) {
|
|
1319
|
-
|
|
1397
|
+
const { from = 'runtime' } = options || {
|
|
1398
|
+
from: 'runtime'
|
|
1399
|
+
};
|
|
1400
|
+
const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
1320
1401
|
id,
|
|
1321
|
-
error
|
|
1402
|
+
error,
|
|
1403
|
+
from,
|
|
1404
|
+
origin: this
|
|
1322
1405
|
});
|
|
1323
|
-
|
|
1406
|
+
if (!failOver) {
|
|
1407
|
+
throw error;
|
|
1408
|
+
}
|
|
1409
|
+
return failOver;
|
|
1324
1410
|
}
|
|
1325
1411
|
}
|
|
1326
1412
|
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
@@ -1354,14 +1440,9 @@ class FederationHost {
|
|
|
1354
1440
|
* It accepts one argument, the name of the share scope.
|
|
1355
1441
|
* If the share scope does not exist, it creates one.
|
|
1356
1442
|
*/ // eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1357
|
-
initializeSharing(shareScopeName = share.DEFAULT_SCOPE) {
|
|
1358
|
-
const
|
|
1359
|
-
const shareScope = share.Global.__FEDERATION__.__SHARE__;
|
|
1443
|
+
initializeSharing(shareScopeName = share.DEFAULT_SCOPE, strategy) {
|
|
1444
|
+
const shareScope = this.shareScopeMap;
|
|
1360
1445
|
const hostName = this.options.name;
|
|
1361
|
-
// Executes only once
|
|
1362
|
-
if (shareScopeLoading[shareScopeName]) {
|
|
1363
|
-
return shareScopeLoading[shareScopeName];
|
|
1364
|
-
}
|
|
1365
1446
|
// Creates a new share scope if necessary
|
|
1366
1447
|
if (!shareScope[shareScopeName]) {
|
|
1367
1448
|
shareScope[shareScopeName] = {};
|
|
@@ -1369,21 +1450,27 @@ class FederationHost {
|
|
|
1369
1450
|
// Executes all initialization snippets from all accessible modules
|
|
1370
1451
|
const scope = shareScope[shareScopeName];
|
|
1371
1452
|
const register = (name, shared)=>{
|
|
1453
|
+
var _activeVersion_shareConfig;
|
|
1372
1454
|
const { version, eager } = shared;
|
|
1373
1455
|
scope[name] = scope[name] || {};
|
|
1374
1456
|
const versions = scope[name];
|
|
1375
1457
|
const activeVersion = versions[version];
|
|
1376
|
-
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || activeVersion.shareConfig.eager));
|
|
1458
|
+
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
|
|
1377
1459
|
if (!activeVersion || !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
|
|
1378
1460
|
versions[version] = shared;
|
|
1379
1461
|
}
|
|
1380
1462
|
};
|
|
1381
1463
|
const promises = [];
|
|
1464
|
+
const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
|
|
1382
1465
|
const initRemoteModule = async (key)=>{
|
|
1383
1466
|
const { module } = await this._getRemoteModuleAndOptions(key);
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1467
|
+
if (module.getEntry) {
|
|
1468
|
+
const entry = await module.getEntry();
|
|
1469
|
+
if (!module.inited) {
|
|
1470
|
+
initFn(entry);
|
|
1471
|
+
module.inited = true;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1387
1474
|
};
|
|
1388
1475
|
Object.keys(this.options.shared).forEach((shareName)=>{
|
|
1389
1476
|
const shared = this.options.shared[shareName];
|
|
@@ -1391,15 +1478,22 @@ class FederationHost {
|
|
|
1391
1478
|
register(shareName, shared);
|
|
1392
1479
|
}
|
|
1393
1480
|
});
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
return shareScopeLoading[shareScopeName] = true;
|
|
1481
|
+
if (strategy === 'version-first') {
|
|
1482
|
+
this.options.remotes.forEach((remote)=>{
|
|
1483
|
+
if (remote.shareScope === shareScopeName) {
|
|
1484
|
+
promises.push(initRemoteModule(remote.name));
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1401
1487
|
}
|
|
1402
|
-
return
|
|
1488
|
+
return promises;
|
|
1489
|
+
}
|
|
1490
|
+
initShareScopeMap(scopeName, shareScope) {
|
|
1491
|
+
this.shareScopeMap[scopeName] = shareScope;
|
|
1492
|
+
this.hooks.lifecycle.initContainerShareScopeMap.emit({
|
|
1493
|
+
shareScope,
|
|
1494
|
+
options: this.options,
|
|
1495
|
+
origin: this
|
|
1496
|
+
});
|
|
1403
1497
|
}
|
|
1404
1498
|
formatOptions(globalOptions, userOptions) {
|
|
1405
1499
|
const formatShareOptions = share.formatShareConfigs(userOptions.shared || {}, userOptions.name);
|
|
@@ -1432,23 +1526,23 @@ class FederationHost {
|
|
|
1432
1526
|
remote.shareScope = share.DEFAULT_SCOPE;
|
|
1433
1527
|
}
|
|
1434
1528
|
if (!remote.type) {
|
|
1435
|
-
// FIXME: The build plugin needs to support this field
|
|
1436
1529
|
remote.type = share.DEFAULT_REMOTE_TYPE;
|
|
1437
1530
|
}
|
|
1438
1531
|
res.push(remote);
|
|
1439
1532
|
}
|
|
1440
1533
|
return res;
|
|
1441
1534
|
}, globalOptionsRes.remotes);
|
|
1442
|
-
// register shared
|
|
1535
|
+
// register shared in shareScopeMap
|
|
1443
1536
|
const sharedKeys = Object.keys(formatShareOptions);
|
|
1444
1537
|
sharedKeys.forEach((sharedKey)=>{
|
|
1445
1538
|
const sharedVal = formatShareOptions[sharedKey];
|
|
1446
|
-
const
|
|
1447
|
-
if (!
|
|
1539
|
+
const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
1540
|
+
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
1448
1541
|
this.setShared({
|
|
1449
1542
|
pkgName: sharedKey,
|
|
1450
1543
|
lib: sharedVal.lib,
|
|
1451
1544
|
get: sharedVal.get,
|
|
1545
|
+
loaded: true,
|
|
1452
1546
|
shared: sharedVal,
|
|
1453
1547
|
from: userOptions.name
|
|
1454
1548
|
});
|
|
@@ -1483,7 +1577,6 @@ class FederationHost {
|
|
|
1483
1577
|
]);
|
|
1484
1578
|
}
|
|
1485
1579
|
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
1486
|
-
const target = share.getGlobalShareScope();
|
|
1487
1580
|
const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
|
|
1488
1581
|
"version",
|
|
1489
1582
|
"scope"
|
|
@@ -1492,23 +1585,16 @@ class FederationHost {
|
|
|
1492
1585
|
scope
|
|
1493
1586
|
];
|
|
1494
1587
|
scopes.forEach((sc)=>{
|
|
1495
|
-
if (!
|
|
1496
|
-
|
|
1588
|
+
if (!this.shareScopeMap[sc]) {
|
|
1589
|
+
this.shareScopeMap[sc] = {};
|
|
1497
1590
|
}
|
|
1498
|
-
if (!
|
|
1499
|
-
|
|
1591
|
+
if (!this.shareScopeMap[sc][pkgName]) {
|
|
1592
|
+
this.shareScopeMap[sc][pkgName] = {};
|
|
1500
1593
|
}
|
|
1501
|
-
if (
|
|
1502
|
-
share.warn(// eslint-disable-next-line max-len
|
|
1503
|
-
`The share \n ${share.safeToString({
|
|
1504
|
-
scope: sc,
|
|
1505
|
-
pkgName,
|
|
1506
|
-
version,
|
|
1507
|
-
from: target[sc][pkgName][version].from
|
|
1508
|
-
})} has been registered`);
|
|
1594
|
+
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
1509
1595
|
return;
|
|
1510
1596
|
}
|
|
1511
|
-
|
|
1597
|
+
this.shareScopeMap[sc][pkgName][version] = _extends({
|
|
1512
1598
|
version,
|
|
1513
1599
|
scope: [
|
|
1514
1600
|
'default'
|
|
@@ -1519,7 +1605,7 @@ class FederationHost {
|
|
|
1519
1605
|
loading
|
|
1520
1606
|
});
|
|
1521
1607
|
if (get) {
|
|
1522
|
-
|
|
1608
|
+
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
1523
1609
|
}
|
|
1524
1610
|
});
|
|
1525
1611
|
}
|
|
@@ -1529,24 +1615,33 @@ class FederationHost {
|
|
|
1529
1615
|
init: new SyncHook(),
|
|
1530
1616
|
beforeRequest: new AsyncWaterfallHook('beforeRequest'),
|
|
1531
1617
|
afterResolve: new AsyncWaterfallHook('afterResolve'),
|
|
1618
|
+
// maybe will change, temporarily for internal use only
|
|
1619
|
+
beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
|
|
1620
|
+
// maybe will change, temporarily for internal use only
|
|
1621
|
+
initContainerShareScopeMap: new AsyncWaterfallHook('initContainer'),
|
|
1622
|
+
// maybe will change, temporarily for internal use only
|
|
1623
|
+
initContainer: new AsyncWaterfallHook('initContainer'),
|
|
1532
1624
|
onLoad: new AsyncHook('onLoad'),
|
|
1533
1625
|
handlePreloadModule: new SyncHook('handlePreloadModule'),
|
|
1534
1626
|
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
1535
1627
|
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
1628
|
+
// not used yet
|
|
1536
1629
|
loadShare: new AsyncHook(),
|
|
1630
|
+
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
1537
1631
|
beforePreloadRemote: new AsyncHook(),
|
|
1538
1632
|
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
1633
|
+
// not used yet
|
|
1539
1634
|
afterPreloadRemote: new AsyncHook()
|
|
1540
1635
|
});
|
|
1541
|
-
this.version =
|
|
1636
|
+
this.version = "0.0.8";
|
|
1542
1637
|
this.moduleCache = new Map();
|
|
1543
1638
|
this.loaderHook = new PluginSystem({
|
|
1544
|
-
// FIXME: may not be suitable
|
|
1639
|
+
// FIXME: may not be suitable , not open to the public yet
|
|
1545
1640
|
getModuleInfo: new SyncHook(),
|
|
1546
1641
|
createScript: new SyncHook(),
|
|
1642
|
+
// only work for manifest , so not open to the public yet
|
|
1547
1643
|
fetch: new AsyncHook('fetch')
|
|
1548
1644
|
});
|
|
1549
|
-
this.loadingShare = {};
|
|
1550
1645
|
// TODO: Validate the details of the options
|
|
1551
1646
|
// Initialize options with default values
|
|
1552
1647
|
const defaultOptions = {
|
|
@@ -1562,6 +1657,8 @@ class FederationHost {
|
|
|
1562
1657
|
};
|
|
1563
1658
|
this.name = userOptions.name;
|
|
1564
1659
|
this.options = defaultOptions;
|
|
1660
|
+
this.shareScopeMap = {};
|
|
1661
|
+
this._setGlobalShareScopeMap();
|
|
1565
1662
|
this.snapshotHandler = new SnapshotHandler(this);
|
|
1566
1663
|
this.registerPlugins([
|
|
1567
1664
|
...defaultOptions.plugins,
|
|
@@ -1584,6 +1681,9 @@ function init(options) {
|
|
|
1584
1681
|
} else {
|
|
1585
1682
|
// Merge options
|
|
1586
1683
|
instance.initOptions(options);
|
|
1684
|
+
if (!FederationInstance) {
|
|
1685
|
+
FederationInstance = instance;
|
|
1686
|
+
}
|
|
1587
1687
|
return instance;
|
|
1588
1688
|
}
|
|
1589
1689
|
}
|
|
@@ -1611,6 +1711,10 @@ function preloadRemote(...args) {
|
|
|
1611
1711
|
share.setGlobalFederationConstructor(FederationHost);
|
|
1612
1712
|
|
|
1613
1713
|
exports.registerGlobalPlugins = share.registerGlobalPlugins;
|
|
1714
|
+
Object.defineProperty(exports, 'loadScript', {
|
|
1715
|
+
enumerable: true,
|
|
1716
|
+
get: function () { return sdk.loadScript; }
|
|
1717
|
+
});
|
|
1614
1718
|
exports.FederationHost = FederationHost;
|
|
1615
1719
|
exports.init = init;
|
|
1616
1720
|
exports.loadRemote = loadRemote;
|