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