@module-federation/runtime 0.0.0-next-20231225041300 → 0.0.0-next-20231225073141
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.cjs.js +1 -1
- package/dist/helpers.esm.js +2 -2
- package/dist/index.cjs.js +137 -68
- package/dist/index.esm.js +136 -70
- package/dist/package.json +2 -2
- package/dist/share.cjs.js +70 -47
- package/dist/share.esm.js +70 -47
- package/dist/src/core.d.ts +15 -5
- package/dist/src/global.d.ts +2 -3
- package/dist/src/helpers.d.ts +2 -2
- package/dist/src/index.d.ts +1 -0
- package/dist/src/module/index.d.ts +4 -2
- package/dist/src/type/config.d.ts +5 -2
- 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/index.esm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { g as getGlobalHostPlugins, D as DEFAULT_REMOTE_TYPE, a as DEFAULT_SCOPE, b as globalLoading, c as getRemoteEntryExports, d as assert, s as safeToString, G as Global, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as
|
|
1
|
+
import { g as getGlobalHostPlugins, D as DEFAULT_REMOTE_TYPE, a as DEFAULT_SCOPE, b as globalLoading, c as getRemoteEntryExports, d as assert, s as safeToString, G as Global, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getRegisteredShare, m as getInfoWithoutType, n as getPreloaded, o as setPreloaded, p as getGlobalSnapshotInfoByModuleInfo, q as setGlobalSnapshotInfoByModuleInfo, r as getGlobalSnapshot, t as getGlobalShareScope, u as addUniqueItem, v as formatShareConfigs, x as isBrowserEnv, y as getBuilderId, z as setGlobalFederationConstructor, A as getGlobalFederationInstance, B as getGlobalFederationConstructor, C as setGlobalFederationInstance } from './share.esm.js';
|
|
2
2
|
export { E as registerGlobalPlugins } from './share.esm.js';
|
|
3
|
-
import { composeKeyWithSeparator, loadScript, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
|
|
3
|
+
import { composeKeyWithSeparator, loadScriptNode, loadScript, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
|
|
4
|
+
export { loadScript } from '@module-federation/sdk';
|
|
4
5
|
|
|
5
6
|
// Function to match a remote with its name and expose
|
|
6
7
|
// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
|
|
@@ -118,6 +119,24 @@ async function loadEntryScript({ name, globalName, entry, createScriptHook }) {
|
|
|
118
119
|
if (remoteEntryExports) {
|
|
119
120
|
return remoteEntryExports;
|
|
120
121
|
}
|
|
122
|
+
if (typeof document === 'undefined') {
|
|
123
|
+
return loadScriptNode(entry, {
|
|
124
|
+
attrs: {
|
|
125
|
+
name,
|
|
126
|
+
globalName
|
|
127
|
+
},
|
|
128
|
+
createScriptHook
|
|
129
|
+
}).then(()=>{
|
|
130
|
+
const { remoteEntryKey, entryExports } = getRemoteEntryExports(name, globalName);
|
|
131
|
+
assert(entryExports, `
|
|
132
|
+
Unable to use the ${name}'s '${entry}' URL with ${remoteEntryKey}'s globalName to get remoteEntry exports.
|
|
133
|
+
Possible reasons could be:\n
|
|
134
|
+
1. '${entry}' is not the correct URL, or the remoteEntry resource or name is incorrect.\n
|
|
135
|
+
2. ${remoteEntryKey} cannot be used to get remoteEntry exports in the window object.
|
|
136
|
+
`);
|
|
137
|
+
return entryExports;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
121
140
|
return loadScript(entry, {
|
|
122
141
|
attrs: {},
|
|
123
142
|
createScriptHook
|
|
@@ -191,6 +210,10 @@ let Module = class Module {
|
|
|
191
210
|
const res = this.loaderHook.lifecycle.createScript.emit({
|
|
192
211
|
url
|
|
193
212
|
});
|
|
213
|
+
if (typeof document === 'undefined') {
|
|
214
|
+
//todo: needs real fix
|
|
215
|
+
return res;
|
|
216
|
+
}
|
|
194
217
|
if (res instanceof HTMLScriptElement) {
|
|
195
218
|
return res;
|
|
196
219
|
}
|
|
@@ -206,15 +229,16 @@ let Module = class Module {
|
|
|
206
229
|
const { loadFactory = true } = options || {
|
|
207
230
|
loadFactory: true
|
|
208
231
|
};
|
|
232
|
+
this.hostInfo.name;
|
|
209
233
|
// Get remoteEntry.js
|
|
210
234
|
const remoteEntryExports = await this.getEntry();
|
|
211
235
|
if (!this.inited) {
|
|
212
|
-
const
|
|
236
|
+
const localShareScopeMap = this.shareScopeMap;
|
|
213
237
|
const remoteShareScope = this.remoteInfo.shareScope || 'default';
|
|
214
|
-
if (!
|
|
215
|
-
|
|
238
|
+
if (!localShareScopeMap[remoteShareScope]) {
|
|
239
|
+
localShareScopeMap[remoteShareScope] = {};
|
|
216
240
|
}
|
|
217
|
-
const shareScope =
|
|
241
|
+
const shareScope = localShareScopeMap[remoteShareScope];
|
|
218
242
|
// TODO: compat logic , it could be moved after providing startup hooks
|
|
219
243
|
const remoteEntryInitOptions = {
|
|
220
244
|
version: this.remoteInfo.version || '',
|
|
@@ -224,10 +248,18 @@ let Module = class Module {
|
|
|
224
248
|
remoteEntryExports.init(shareScope, [], remoteEntryInitOptions);
|
|
225
249
|
const federationInstance = Global.__FEDERATION__.__INSTANCES__.find((i)=>i.options.id === composeKeyWithSeparator(this.remoteInfo.name, this.remoteInfo.buildVersion));
|
|
226
250
|
if (federationInstance) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
251
|
+
// means the instance is prev vmok instance
|
|
252
|
+
if (!federationInstance.releaseNumber || Number(federationInstance.releaseNumber) <= 100) {
|
|
253
|
+
// 兼容旧的生产者传参
|
|
254
|
+
federationInstance.initOptions(_extends$4({}, remoteEntryInitOptions, {
|
|
255
|
+
remotes: [],
|
|
256
|
+
name: this.remoteInfo.name
|
|
257
|
+
}));
|
|
258
|
+
if (!__FEDERATION__.__SHARE__['default'] && this.shareScopeMap && this.shareScopeMap['default']) {
|
|
259
|
+
// @ts-ignore compat prev logic , and it will be optimized by supporting startup hook
|
|
260
|
+
__FEDERATION__.__SHARE__['default'] = this.shareScopeMap['default'];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
231
263
|
}
|
|
232
264
|
}
|
|
233
265
|
this.lib = remoteEntryExports;
|
|
@@ -242,7 +274,7 @@ let Module = class Module {
|
|
|
242
274
|
return exposeContent;
|
|
243
275
|
}
|
|
244
276
|
// loading: Record<string, undefined | Promise<RemoteEntryExports | void>> = {};
|
|
245
|
-
constructor({ hostInfo, remoteInfo, shared, loaderHook }){
|
|
277
|
+
constructor({ hostInfo, remoteInfo, shared, loaderHook, shareScopeMap }){
|
|
246
278
|
this.inited = false;
|
|
247
279
|
this.shared = {};
|
|
248
280
|
this.lib = undefined;
|
|
@@ -250,6 +282,7 @@ let Module = class Module {
|
|
|
250
282
|
this.remoteInfo = remoteInfo;
|
|
251
283
|
this.shared = shared;
|
|
252
284
|
this.loaderHook = loaderHook;
|
|
285
|
+
this.shareScopeMap = shareScopeMap;
|
|
253
286
|
}
|
|
254
287
|
};
|
|
255
288
|
|
|
@@ -776,9 +809,9 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
|
|
|
776
809
|
if (!shareInfo) {
|
|
777
810
|
return;
|
|
778
811
|
}
|
|
779
|
-
const
|
|
812
|
+
const registeredShared = getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
|
|
780
813
|
// 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.
|
|
781
|
-
if (
|
|
814
|
+
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
782
815
|
shared.assets.js.sync.forEach((asset)=>{
|
|
783
816
|
loadedSharedJsAssets.add(asset);
|
|
784
817
|
});
|
|
@@ -1105,6 +1138,13 @@ function _object_without_properties_loose(source, excluded) {
|
|
|
1105
1138
|
return target;
|
|
1106
1139
|
}
|
|
1107
1140
|
class FederationHost {
|
|
1141
|
+
_setGlobalShareScopeMap() {
|
|
1142
|
+
const globalShareScopeMap = getGlobalShareScope();
|
|
1143
|
+
const identifier = this.options.id || this.options.name;
|
|
1144
|
+
if (identifier && !globalShareScopeMap[identifier]) {
|
|
1145
|
+
globalShareScopeMap[identifier] = this.shareScopeMap;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1108
1148
|
initOptions(userOptions) {
|
|
1109
1149
|
this.registerPlugins(userOptions.plugins);
|
|
1110
1150
|
const options = this.formatOptions(this.options, userOptions);
|
|
@@ -1119,6 +1159,11 @@ class FederationHost {
|
|
|
1119
1159
|
// 2. Searches globally for a matching share, if found, it uses it directly
|
|
1120
1160
|
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
|
|
1121
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
|
+
shareInfo.scope.forEach((shareScope)=>{
|
|
1164
|
+
this.initializeSharing(shareScope, shareInfo.strategy);
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
1122
1167
|
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
|
|
1123
1168
|
pkgName,
|
|
1124
1169
|
shareInfo,
|
|
@@ -1129,30 +1174,42 @@ class FederationHost {
|
|
|
1129
1174
|
// Assert that shareInfoRes exists, if not, throw an error
|
|
1130
1175
|
assert(shareInfoRes, `Cannot find ${pkgName} Share in the ${this.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`);
|
|
1131
1176
|
// Retrieve from cache
|
|
1132
|
-
const
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1177
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1178
|
+
const addUseIn = (shared)=>{
|
|
1179
|
+
if (!shared.useIn) {
|
|
1180
|
+
shared.useIn = [];
|
|
1181
|
+
}
|
|
1182
|
+
addUniqueItem(shared.useIn, this.options.name);
|
|
1183
|
+
};
|
|
1184
|
+
if (registeredShared && registeredShared.lib) {
|
|
1185
|
+
addUseIn(registeredShared);
|
|
1186
|
+
return registeredShared.lib;
|
|
1187
|
+
} else if (registeredShared && registeredShared.loading && !registeredShared.loaded) {
|
|
1188
|
+
const factory = await registeredShared.loading;
|
|
1189
|
+
registeredShared.loaded = true;
|
|
1190
|
+
if (!registeredShared.lib) {
|
|
1191
|
+
registeredShared.lib = factory;
|
|
1192
|
+
}
|
|
1193
|
+
addUseIn(registeredShared);
|
|
1139
1194
|
return factory;
|
|
1140
|
-
} else if (
|
|
1195
|
+
} else if (registeredShared) {
|
|
1141
1196
|
const asyncLoadProcess = async ()=>{
|
|
1142
|
-
const factory = await
|
|
1197
|
+
const factory = await registeredShared.get();
|
|
1143
1198
|
shareInfoRes.lib = factory;
|
|
1144
|
-
|
|
1145
|
-
|
|
1199
|
+
shareInfoRes.loaded = true;
|
|
1200
|
+
addUseIn(shareInfoRes);
|
|
1201
|
+
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1146
1202
|
if (gShared) {
|
|
1147
1203
|
gShared.lib = factory;
|
|
1204
|
+
gShared.loaded = true;
|
|
1148
1205
|
}
|
|
1149
1206
|
return factory;
|
|
1150
1207
|
};
|
|
1151
1208
|
const loading = asyncLoadProcess();
|
|
1152
1209
|
this.setShared({
|
|
1153
1210
|
pkgName,
|
|
1154
|
-
loaded:
|
|
1155
|
-
shared:
|
|
1211
|
+
loaded: false,
|
|
1212
|
+
shared: registeredShared,
|
|
1156
1213
|
from: this.options.name,
|
|
1157
1214
|
lib: null,
|
|
1158
1215
|
loading
|
|
@@ -1165,17 +1222,19 @@ class FederationHost {
|
|
|
1165
1222
|
const asyncLoadProcess = async ()=>{
|
|
1166
1223
|
const factory = await shareInfoRes.get();
|
|
1167
1224
|
shareInfoRes.lib = factory;
|
|
1168
|
-
|
|
1169
|
-
|
|
1225
|
+
shareInfoRes.loaded = true;
|
|
1226
|
+
addUseIn(shareInfoRes);
|
|
1227
|
+
const gShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfoRes, this.hooks.lifecycle.resolveShare);
|
|
1170
1228
|
if (gShared) {
|
|
1171
1229
|
gShared.lib = factory;
|
|
1230
|
+
gShared.loaded = true;
|
|
1172
1231
|
}
|
|
1173
1232
|
return factory;
|
|
1174
1233
|
};
|
|
1175
1234
|
const loading = asyncLoadProcess();
|
|
1176
1235
|
this.setShared({
|
|
1177
1236
|
pkgName,
|
|
1178
|
-
loaded:
|
|
1237
|
+
loaded: false,
|
|
1179
1238
|
shared: shareInfoRes,
|
|
1180
1239
|
from: this.options.name,
|
|
1181
1240
|
lib: null,
|
|
@@ -1191,16 +1250,16 @@ class FederationHost {
|
|
|
1191
1250
|
loadShareSync(pkgName) {
|
|
1192
1251
|
var _this_options_shared;
|
|
1193
1252
|
const shareInfo = (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName];
|
|
1194
|
-
const
|
|
1195
|
-
if (
|
|
1196
|
-
addUniqueItem(
|
|
1197
|
-
if (!
|
|
1198
|
-
|
|
1199
|
-
if (
|
|
1253
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, pkgName, shareInfo, this.hooks.lifecycle.resolveShare);
|
|
1254
|
+
if (registeredShared && typeof registeredShared.lib === 'function') {
|
|
1255
|
+
addUniqueItem(registeredShared.useIn, this.options.name);
|
|
1256
|
+
if (!registeredShared.loaded) {
|
|
1257
|
+
registeredShared.loaded = true;
|
|
1258
|
+
if (registeredShared.from === this.options.name) {
|
|
1200
1259
|
shareInfo.loaded = true;
|
|
1201
1260
|
}
|
|
1202
1261
|
}
|
|
1203
|
-
return
|
|
1262
|
+
return registeredShared.lib;
|
|
1204
1263
|
}
|
|
1205
1264
|
if (shareInfo.lib) {
|
|
1206
1265
|
if (!shareInfo.loaded) {
|
|
@@ -1271,7 +1330,8 @@ class FederationHost {
|
|
|
1271
1330
|
remoteInfo,
|
|
1272
1331
|
shared: this.options.shared || {},
|
|
1273
1332
|
plugins: this.options.plugins,
|
|
1274
|
-
loaderHook: this.loaderHook
|
|
1333
|
+
loaderHook: this.loaderHook,
|
|
1334
|
+
shareScopeMap: this.shareScopeMap
|
|
1275
1335
|
};
|
|
1276
1336
|
if (!module) {
|
|
1277
1337
|
module = new Module(moduleOptions);
|
|
@@ -1351,14 +1411,9 @@ class FederationHost {
|
|
|
1351
1411
|
* It accepts one argument, the name of the share scope.
|
|
1352
1412
|
* If the share scope does not exist, it creates one.
|
|
1353
1413
|
*/ // eslint-disable-next-line @typescript-eslint/member-ordering
|
|
1354
|
-
initializeSharing(shareScopeName = DEFAULT_SCOPE) {
|
|
1355
|
-
const
|
|
1356
|
-
const shareScope = Global.__FEDERATION__.__SHARE__;
|
|
1414
|
+
initializeSharing(shareScopeName = DEFAULT_SCOPE, strategy) {
|
|
1415
|
+
const shareScope = this.shareScopeMap;
|
|
1357
1416
|
const hostName = this.options.name;
|
|
1358
|
-
// Executes only once
|
|
1359
|
-
if (shareScopeLoading[shareScopeName]) {
|
|
1360
|
-
return shareScopeLoading[shareScopeName];
|
|
1361
|
-
}
|
|
1362
1417
|
// Creates a new share scope if necessary
|
|
1363
1418
|
if (!shareScope[shareScopeName]) {
|
|
1364
1419
|
shareScope[shareScopeName] = {};
|
|
@@ -1366,21 +1421,27 @@ class FederationHost {
|
|
|
1366
1421
|
// Executes all initialization snippets from all accessible modules
|
|
1367
1422
|
const scope = shareScope[shareScopeName];
|
|
1368
1423
|
const register = (name, shared)=>{
|
|
1424
|
+
var _activeVersion_shareConfig;
|
|
1369
1425
|
const { version, eager } = shared;
|
|
1370
1426
|
scope[name] = scope[name] || {};
|
|
1371
1427
|
const versions = scope[name];
|
|
1372
1428
|
const activeVersion = versions[version];
|
|
1373
|
-
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || activeVersion.shareConfig.eager));
|
|
1429
|
+
const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
|
|
1374
1430
|
if (!activeVersion || !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
|
|
1375
1431
|
versions[version] = shared;
|
|
1376
1432
|
}
|
|
1377
1433
|
};
|
|
1378
1434
|
const promises = [];
|
|
1435
|
+
const initFn = (mod)=>mod && mod.init && mod.init(shareScope[shareScopeName]);
|
|
1379
1436
|
const initRemoteModule = async (key)=>{
|
|
1380
1437
|
const { module } = await this._getRemoteModuleAndOptions(key);
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1438
|
+
if (module.getEntry) {
|
|
1439
|
+
const entry = await module.getEntry();
|
|
1440
|
+
if (!module.inited) {
|
|
1441
|
+
initFn(entry);
|
|
1442
|
+
module.inited = true;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1384
1445
|
};
|
|
1385
1446
|
Object.keys(this.options.shared).forEach((shareName)=>{
|
|
1386
1447
|
const shared = this.options.shared[shareName];
|
|
@@ -1388,15 +1449,17 @@ class FederationHost {
|
|
|
1388
1449
|
register(shareName, shared);
|
|
1389
1450
|
}
|
|
1390
1451
|
});
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
return shareScopeLoading[shareScopeName] = true;
|
|
1452
|
+
if (strategy === 'version-first') {
|
|
1453
|
+
this.options.remotes.forEach((remote)=>{
|
|
1454
|
+
if (remote.shareScope === shareScopeName) {
|
|
1455
|
+
promises.push(initRemoteModule(remote.name));
|
|
1456
|
+
}
|
|
1457
|
+
});
|
|
1398
1458
|
}
|
|
1399
|
-
return
|
|
1459
|
+
return promises;
|
|
1460
|
+
}
|
|
1461
|
+
initShareScopeMap(scopeName, shareScope) {
|
|
1462
|
+
this.shareScopeMap[scopeName] = shareScope;
|
|
1400
1463
|
}
|
|
1401
1464
|
formatOptions(globalOptions, userOptions) {
|
|
1402
1465
|
const formatShareOptions = formatShareConfigs(userOptions.shared || {}, userOptions.name);
|
|
@@ -1436,16 +1499,17 @@ class FederationHost {
|
|
|
1436
1499
|
}
|
|
1437
1500
|
return res;
|
|
1438
1501
|
}, globalOptionsRes.remotes);
|
|
1439
|
-
// register shared
|
|
1502
|
+
// register shared in shareScopeMap
|
|
1440
1503
|
const sharedKeys = Object.keys(formatShareOptions);
|
|
1441
1504
|
sharedKeys.forEach((sharedKey)=>{
|
|
1442
1505
|
const sharedVal = formatShareOptions[sharedKey];
|
|
1443
|
-
const
|
|
1444
|
-
if (!
|
|
1506
|
+
const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
|
|
1507
|
+
if (!registeredShared && sharedVal && sharedVal.lib) {
|
|
1445
1508
|
this.setShared({
|
|
1446
1509
|
pkgName: sharedKey,
|
|
1447
1510
|
lib: sharedVal.lib,
|
|
1448
1511
|
get: sharedVal.get,
|
|
1512
|
+
loaded: true,
|
|
1449
1513
|
shared: sharedVal,
|
|
1450
1514
|
from: userOptions.name
|
|
1451
1515
|
});
|
|
@@ -1480,7 +1544,6 @@ class FederationHost {
|
|
|
1480
1544
|
]);
|
|
1481
1545
|
}
|
|
1482
1546
|
setShared({ pkgName, shared, from, lib, loading, loaded, get }) {
|
|
1483
|
-
const target = getGlobalShareScope();
|
|
1484
1547
|
const { version, scope = 'default' } = shared, shareInfo = _object_without_properties_loose(shared, [
|
|
1485
1548
|
"version",
|
|
1486
1549
|
"scope"
|
|
@@ -1489,23 +1552,23 @@ class FederationHost {
|
|
|
1489
1552
|
scope
|
|
1490
1553
|
];
|
|
1491
1554
|
scopes.forEach((sc)=>{
|
|
1492
|
-
if (!
|
|
1493
|
-
|
|
1555
|
+
if (!this.shareScopeMap[sc]) {
|
|
1556
|
+
this.shareScopeMap[sc] = {};
|
|
1494
1557
|
}
|
|
1495
|
-
if (!
|
|
1496
|
-
|
|
1558
|
+
if (!this.shareScopeMap[sc][pkgName]) {
|
|
1559
|
+
this.shareScopeMap[sc][pkgName] = {};
|
|
1497
1560
|
}
|
|
1498
|
-
if (
|
|
1561
|
+
if (this.shareScopeMap[sc][pkgName][version]) {
|
|
1499
1562
|
warn(// eslint-disable-next-line max-len
|
|
1500
1563
|
`The share \n ${safeToString({
|
|
1501
1564
|
scope: sc,
|
|
1502
1565
|
pkgName,
|
|
1503
1566
|
version,
|
|
1504
|
-
from:
|
|
1567
|
+
from: this.shareScopeMap[sc][pkgName][version].from
|
|
1505
1568
|
})} has been registered`);
|
|
1506
1569
|
return;
|
|
1507
1570
|
}
|
|
1508
|
-
|
|
1571
|
+
this.shareScopeMap[sc][pkgName][version] = _extends({
|
|
1509
1572
|
version,
|
|
1510
1573
|
scope: [
|
|
1511
1574
|
'default'
|
|
@@ -1516,7 +1579,7 @@ class FederationHost {
|
|
|
1516
1579
|
loading
|
|
1517
1580
|
});
|
|
1518
1581
|
if (get) {
|
|
1519
|
-
|
|
1582
|
+
this.shareScopeMap[sc][pkgName][version].get = get;
|
|
1520
1583
|
}
|
|
1521
1584
|
});
|
|
1522
1585
|
}
|
|
@@ -1531,11 +1594,13 @@ class FederationHost {
|
|
|
1531
1594
|
errorLoadRemote: new AsyncHook('errorLoadRemote'),
|
|
1532
1595
|
beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
|
|
1533
1596
|
loadShare: new AsyncHook(),
|
|
1597
|
+
resolveShare: new SyncWaterfallHook('resolveShare'),
|
|
1534
1598
|
beforePreloadRemote: new AsyncHook(),
|
|
1535
1599
|
generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
|
|
1536
1600
|
afterPreloadRemote: new AsyncHook()
|
|
1537
1601
|
});
|
|
1538
|
-
this.
|
|
1602
|
+
this.releaseNumber = `4`;
|
|
1603
|
+
this.version = `0.0.4`;
|
|
1539
1604
|
this.moduleCache = new Map();
|
|
1540
1605
|
this.loaderHook = new PluginSystem({
|
|
1541
1606
|
// FIXME: may not be suitable
|
|
@@ -1543,7 +1608,6 @@ class FederationHost {
|
|
|
1543
1608
|
createScript: new SyncHook(),
|
|
1544
1609
|
fetch: new AsyncHook('fetch')
|
|
1545
1610
|
});
|
|
1546
|
-
this.loadingShare = {};
|
|
1547
1611
|
// TODO: Validate the details of the options
|
|
1548
1612
|
// Initialize options with default values
|
|
1549
1613
|
const defaultOptions = {
|
|
@@ -1559,6 +1623,8 @@ class FederationHost {
|
|
|
1559
1623
|
};
|
|
1560
1624
|
this.name = userOptions.name;
|
|
1561
1625
|
this.options = defaultOptions;
|
|
1626
|
+
this.shareScopeMap = {};
|
|
1627
|
+
this._setGlobalShareScopeMap();
|
|
1562
1628
|
this.snapshotHandler = new SnapshotHandler(this);
|
|
1563
1629
|
this.registerPlugins([
|
|
1564
1630
|
...defaultOptions.plugins,
|
package/dist/package.json
CHANGED
package/dist/share.cjs.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
function getBuilderId() {
|
|
4
|
+
//@ts-ignore
|
|
4
5
|
return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined' ? FEDERATION_BUILD_IDENTIFIER : '';
|
|
5
6
|
}
|
|
6
7
|
function isDebugMode() {
|
|
@@ -43,7 +44,7 @@ function isRemoteInfoWithEntry(remote) {
|
|
|
43
44
|
return typeof remote.entry !== 'undefined';
|
|
44
45
|
}
|
|
45
46
|
function isPureRemoteEntry(remote) {
|
|
46
|
-
return
|
|
47
|
+
return remote.entry.endsWith('.js');
|
|
47
48
|
}
|
|
48
49
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
49
50
|
function safeToString(info) {
|
|
@@ -88,7 +89,7 @@ function _object_without_properties_loose(source, excluded) {
|
|
|
88
89
|
}
|
|
89
90
|
return target;
|
|
90
91
|
}
|
|
91
|
-
var _nativeGlobal___FEDERATION__, _nativeGlobal___FEDERATION__1, _nativeGlobal___FEDERATION__2, _nativeGlobal___FEDERATION__3, _nativeGlobal___FEDERATION__4, _nativeGlobal___FEDERATION__5
|
|
92
|
+
var _nativeGlobal___FEDERATION__, _nativeGlobal___FEDERATION__1, _nativeGlobal___FEDERATION__2, _nativeGlobal___FEDERATION__3, _nativeGlobal___FEDERATION__4, _nativeGlobal___FEDERATION__5;
|
|
92
93
|
// export const nativeGlobal: typeof global = new Function('return this')();
|
|
93
94
|
const nativeGlobal = new Function('return this')();
|
|
94
95
|
// This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
|
|
@@ -111,7 +112,6 @@ if (nativeGlobal.__VMOK__) {
|
|
|
111
112
|
moduleInfo: {},
|
|
112
113
|
__SHARE__: {},
|
|
113
114
|
__MANIFEST_LOADING__: {},
|
|
114
|
-
__SHARE_SCOPE_LOADING__: {},
|
|
115
115
|
__PRELOADED_MAP__: new Map()
|
|
116
116
|
};
|
|
117
117
|
nativeGlobal.__VMOK__ = nativeGlobal.__FEDERATION__;
|
|
@@ -126,10 +126,8 @@ var ___SHARE__;
|
|
|
126
126
|
(___SHARE__ = (_nativeGlobal___FEDERATION__3 = nativeGlobal.__FEDERATION__).__SHARE__) != null ? ___SHARE__ : _nativeGlobal___FEDERATION__3.__SHARE__ = {};
|
|
127
127
|
var ___MANIFEST_LOADING__;
|
|
128
128
|
(___MANIFEST_LOADING__ = (_nativeGlobal___FEDERATION__4 = nativeGlobal.__FEDERATION__).__MANIFEST_LOADING__) != null ? ___MANIFEST_LOADING__ : _nativeGlobal___FEDERATION__4.__MANIFEST_LOADING__ = {};
|
|
129
|
-
var ___SHARE_SCOPE_LOADING__;
|
|
130
|
-
(___SHARE_SCOPE_LOADING__ = (_nativeGlobal___FEDERATION__5 = nativeGlobal.__FEDERATION__).__SHARE_SCOPE_LOADING__) != null ? ___SHARE_SCOPE_LOADING__ : _nativeGlobal___FEDERATION__5.__SHARE_SCOPE_LOADING__ = {};
|
|
131
129
|
var ___PRELOADED_MAP__;
|
|
132
|
-
(___PRELOADED_MAP__ = (
|
|
130
|
+
(___PRELOADED_MAP__ = (_nativeGlobal___FEDERATION__5 = nativeGlobal.__FEDERATION__).__PRELOADED_MAP__) != null ? ___PRELOADED_MAP__ : _nativeGlobal___FEDERATION__5.__PRELOADED_MAP__ = new Map();
|
|
133
131
|
const Global = {
|
|
134
132
|
get __FEDERATION__ () {
|
|
135
133
|
const globalThisVal = new Function('return globalThis')();
|
|
@@ -142,7 +140,6 @@ function resetFederationGlobalInfo() {
|
|
|
142
140
|
nativeGlobal.__FEDERATION__.moduleInfo = {};
|
|
143
141
|
nativeGlobal.__FEDERATION__.__SHARE__ = {};
|
|
144
142
|
nativeGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
|
|
145
|
-
nativeGlobal.__FEDERATION__.__SHARE_SCOPE_LOADING__ = {};
|
|
146
143
|
}
|
|
147
144
|
function getGlobalFederationInstance(name, version) {
|
|
148
145
|
const buildId = getBuilderId();
|
|
@@ -625,6 +622,7 @@ function formatShare(shareArgs, from) {
|
|
|
625
622
|
loading: null
|
|
626
623
|
}, shareArgs, {
|
|
627
624
|
get,
|
|
625
|
+
loaded: 'lib' in shareArgs ? true : undefined,
|
|
628
626
|
scope: Array.isArray(shareArgs.scope) ? shareArgs.scope : [
|
|
629
627
|
'default'
|
|
630
628
|
],
|
|
@@ -641,15 +639,26 @@ function formatShareConfigs(shareArgs, from) {
|
|
|
641
639
|
}, {});
|
|
642
640
|
}
|
|
643
641
|
function versionLt(a, b) {
|
|
644
|
-
|
|
642
|
+
const transformInvalidVersion = (version)=>{
|
|
643
|
+
const isNumberVersion = !Number.isNaN(Number(version));
|
|
644
|
+
if (isNumberVersion) {
|
|
645
|
+
const splitArr = version.split('.');
|
|
646
|
+
let validVersion = version;
|
|
647
|
+
for(let i = 0; i < 3 - splitArr.length; i++){
|
|
648
|
+
validVersion += '.0';
|
|
649
|
+
}
|
|
650
|
+
return validVersion;
|
|
651
|
+
}
|
|
652
|
+
return version;
|
|
653
|
+
};
|
|
654
|
+
if (satisfy(transformInvalidVersion(a), `<=${transformInvalidVersion(b)}`)) {
|
|
645
655
|
return true;
|
|
646
656
|
} else {
|
|
647
657
|
return false;
|
|
648
658
|
}
|
|
649
659
|
}
|
|
650
|
-
const findVersion = (scope, pkgName, cb)=>{
|
|
651
|
-
const
|
|
652
|
-
const versions = globalShares[scope][pkgName];
|
|
660
|
+
const findVersion = (shareScopeMap, scope, pkgName, cb)=>{
|
|
661
|
+
const versions = shareScopeMap[scope][pkgName];
|
|
653
662
|
const callback = cb || function(prev, cur) {
|
|
654
663
|
return versionLt(prev, cur);
|
|
655
664
|
};
|
|
@@ -660,20 +669,22 @@ const findVersion = (scope, pkgName, cb)=>{
|
|
|
660
669
|
if (callback(prev, cur)) {
|
|
661
670
|
return cur;
|
|
662
671
|
}
|
|
672
|
+
// default version is '0' https://github.com/webpack/webpack/blob/main/lib/sharing/ProvideSharedModule.js#L136
|
|
673
|
+
if (prev === '0') {
|
|
674
|
+
return cur;
|
|
675
|
+
}
|
|
663
676
|
return prev;
|
|
664
677
|
}, 0);
|
|
665
678
|
};
|
|
666
|
-
function findSingletonVersionOrderByVersion(scope, pkgName) {
|
|
667
|
-
const
|
|
668
|
-
const versions = globalShares[scope][pkgName];
|
|
679
|
+
function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
|
|
680
|
+
const versions = shareScopeMap[scope][pkgName];
|
|
669
681
|
const callback = function(prev, cur) {
|
|
670
682
|
return !versions[prev].loaded && versionLt(prev, cur);
|
|
671
683
|
};
|
|
672
|
-
return findVersion(scope, pkgName, callback);
|
|
684
|
+
return findVersion(shareScopeMap, scope, pkgName, callback);
|
|
673
685
|
}
|
|
674
|
-
function findSingletonVersionOrderByLoaded(scope, pkgName) {
|
|
675
|
-
const
|
|
676
|
-
const versions = globalShares[scope][pkgName];
|
|
686
|
+
function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
|
|
687
|
+
const versions = shareScopeMap[scope][pkgName];
|
|
677
688
|
const callback = function(prev, cur) {
|
|
678
689
|
if (versions[cur].loaded) {
|
|
679
690
|
if (versions[prev].loaded) {
|
|
@@ -687,45 +698,57 @@ function findSingletonVersionOrderByLoaded(scope, pkgName) {
|
|
|
687
698
|
}
|
|
688
699
|
return versionLt(prev, cur);
|
|
689
700
|
};
|
|
690
|
-
return findVersion(scope, pkgName, callback);
|
|
701
|
+
return findVersion(shareScopeMap, scope, pkgName, callback);
|
|
702
|
+
}
|
|
703
|
+
function getFindShareFunction(strategy) {
|
|
704
|
+
if (strategy === 'loaded-first') {
|
|
705
|
+
return findSingletonVersionOrderByLoaded;
|
|
706
|
+
}
|
|
707
|
+
return findSingletonVersionOrderByVersion;
|
|
691
708
|
}
|
|
692
709
|
// Details about shared resources
|
|
693
710
|
// TODO: Implement strictVersion for alignment with module federation.
|
|
694
|
-
function
|
|
695
|
-
|
|
711
|
+
function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare) {
|
|
712
|
+
if (!localShareScopeMap) {
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
696
715
|
const { shareConfig, scope = DEFAULT_SCOPE, strategy } = shareInfo;
|
|
697
716
|
const scopes = Array.isArray(scope) ? scope : [
|
|
698
717
|
scope
|
|
699
718
|
];
|
|
700
719
|
for (const sc of scopes){
|
|
701
|
-
if (shareConfig &&
|
|
720
|
+
if (shareConfig && localShareScopeMap[sc] && localShareScopeMap[sc][pkgName]) {
|
|
702
721
|
const { requiredVersion } = shareConfig;
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
if (
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
}
|
|
721
|
-
// eslint-disable-next-line max-depth
|
|
722
|
-
for (const [versionKey, versionValue] of Object.entries(globalShares[sc][pkgName])){
|
|
723
|
-
// eslint-disable-next-line max-depth
|
|
724
|
-
if (satisfy(versionKey, requiredVersion)) {
|
|
725
|
-
return versionValue;
|
|
722
|
+
const findShareFunction = getFindShareFunction(strategy);
|
|
723
|
+
const maxOrSingletonVersion = findShareFunction(localShareScopeMap, sc, pkgName);
|
|
724
|
+
//@ts-ignore
|
|
725
|
+
const defaultResolver = ()=>{
|
|
726
|
+
if (shareConfig.singleton) {
|
|
727
|
+
if (typeof requiredVersion === 'string' && !satisfy(maxOrSingletonVersion, requiredVersion)) {
|
|
728
|
+
warn(`Version ${maxOrSingletonVersion} from ${maxOrSingletonVersion && localShareScopeMap[sc][pkgName][maxOrSingletonVersion].from} of shared singleton module ${pkgName} does not satisfy the requirement of ${shareInfo.from} which needs ${requiredVersion})`);
|
|
729
|
+
}
|
|
730
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
731
|
+
} else {
|
|
732
|
+
if (requiredVersion === false || requiredVersion === '*') {
|
|
733
|
+
return localShareScopeMap[sc][pkgName][maxOrSingletonVersion];
|
|
734
|
+
}
|
|
735
|
+
for (const [versionKey, versionValue] of Object.entries(localShareScopeMap[sc][pkgName])){
|
|
736
|
+
if (satisfy(versionKey, requiredVersion)) {
|
|
737
|
+
return versionValue;
|
|
738
|
+
}
|
|
726
739
|
}
|
|
727
740
|
}
|
|
728
|
-
}
|
|
741
|
+
};
|
|
742
|
+
const params = {
|
|
743
|
+
shareScopeMap: localShareScopeMap,
|
|
744
|
+
scope: sc,
|
|
745
|
+
pkgName,
|
|
746
|
+
version: maxOrSingletonVersion,
|
|
747
|
+
GlobalFederation: Global.__FEDERATION__,
|
|
748
|
+
resolver: defaultResolver
|
|
749
|
+
};
|
|
750
|
+
const resolveShared = resolveShare.emit(params) || params;
|
|
751
|
+
return resolveShared.resolver();
|
|
729
752
|
}
|
|
730
753
|
}
|
|
731
754
|
}
|
|
@@ -746,12 +769,12 @@ exports.getFMId = getFMId;
|
|
|
746
769
|
exports.getGlobalFederationConstructor = getGlobalFederationConstructor;
|
|
747
770
|
exports.getGlobalFederationInstance = getGlobalFederationInstance;
|
|
748
771
|
exports.getGlobalHostPlugins = getGlobalHostPlugins;
|
|
749
|
-
exports.getGlobalShare = getGlobalShare;
|
|
750
772
|
exports.getGlobalShareScope = getGlobalShareScope;
|
|
751
773
|
exports.getGlobalSnapshot = getGlobalSnapshot;
|
|
752
774
|
exports.getGlobalSnapshotInfoByModuleInfo = getGlobalSnapshotInfoByModuleInfo;
|
|
753
775
|
exports.getInfoWithoutType = getInfoWithoutType;
|
|
754
776
|
exports.getPreloaded = getPreloaded;
|
|
777
|
+
exports.getRegisteredShare = getRegisteredShare;
|
|
755
778
|
exports.getRemoteEntryExports = getRemoteEntryExports;
|
|
756
779
|
exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
|
|
757
780
|
exports.globalLoading = globalLoading;
|