@module-federation/runtime 0.0.9 → 0.0.11

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.
@@ -1,4 +1,4 @@
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';
1
+ import { l as getRegisteredShare, u as getGlobalShareScope, G as Global, H as nativeGlobal, I as resetFederationGlobalInfo, B as getGlobalFederationInstance, E as setGlobalFederationInstance, C as getGlobalFederationConstructor, A as setGlobalFederationConstructor, m as getInfoWithoutType, t as getGlobalSnapshot, J as getTargetSnapshotInfoByModuleInfo, p as getGlobalSnapshotInfoByModuleInfo, r as setGlobalSnapshotInfoByModuleInfo, q as addGlobalSnapshot, c as getRemoteEntryExports, F as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
2
2
 
3
3
  const ShareUtils = {
4
4
  getRegisteredShare,
package/dist/index.cjs.js CHANGED
@@ -263,7 +263,7 @@ let Module = class Module {
263
263
  remoteInfo: this.remoteInfo,
264
264
  origin: this.host
265
265
  });
266
- remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
266
+ await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
267
267
  await this.host.hooks.lifecycle.initContainer.emit(_extends$4({}, initContainerOptions, {
268
268
  remoteEntryExports
269
269
  }));
@@ -674,9 +674,9 @@ function splitId(id) {
674
674
  }
675
675
  }
676
676
  // Traverse all nodes in moduleInfo and traverse the entire snapshot
677
- function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot, getModuleInfoHook) {
677
+ function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
678
678
  const id = share.getFMId(remoteInfo);
679
- const { value: snapshotValue } = share.getInfoWithoutType(globalSnapshot, id, getModuleInfoHook);
679
+ const { value: snapshotValue } = share.getInfoWithoutType(globalSnapshot, id);
680
680
  const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
681
681
  if (effectiveRemoteSnapshot && !sdk.isManifestProvider(effectiveRemoteSnapshot)) {
682
682
  traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
@@ -692,7 +692,7 @@ function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo =
692
692
  traverseModuleInfo(globalSnapshot, {
693
693
  name: subRemoteInfo.name,
694
694
  version: remoteValue.matchedVersion
695
- }, traverse, false, memo, undefined, getModuleInfoHook);
695
+ }, traverse, false, memo, undefined);
696
696
  }
697
697
  }
698
698
  }
@@ -792,16 +792,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
792
792
  share.setPreloaded(exposeFullPath);
793
793
  }
794
794
  }
795
- }, true, memo, remoteSnapshot, (target, key)=>{
796
- const res = origin.loaderHook.lifecycle.getModuleInfo.emit({
797
- target,
798
- key
799
- });
800
- if (res && !(res instanceof Promise)) {
801
- return res;
802
- }
803
- return;
804
- });
795
+ }, true, memo, remoteSnapshot);
805
796
  if (remoteSnapshot.shared) {
806
797
  remoteSnapshot.shared.forEach((shared)=>{
807
798
  var _options_shared;
@@ -894,37 +885,27 @@ class SnapshotHandler {
894
885
  // eslint-disable-next-line max-lines-per-function
895
886
  async loadRemoteSnapshotInfo(moduleInfo) {
896
887
  const { options } = this.HostInstance;
897
- const hostSnapshot = share.getGlobalSnapshotInfoByModuleInfo({
898
- name: this.HostInstance.options.name,
899
- version: this.HostInstance.options.version
900
- }, {
901
- getModuleInfoHook: (target, key)=>{
902
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
903
- target,
904
- key
905
- });
906
- if (res && !(res instanceof Promise)) {
907
- return res;
908
- }
909
- return;
910
- }
911
- });
912
888
  await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
913
889
  options,
914
890
  moduleInfo
915
891
  });
892
+ let hostSnapshot = share.getGlobalSnapshotInfoByModuleInfo({
893
+ name: this.HostInstance.options.name,
894
+ version: this.HostInstance.options.version
895
+ });
896
+ if (!hostSnapshot) {
897
+ hostSnapshot = {
898
+ version: this.HostInstance.options.version || '',
899
+ remoteEntry: '',
900
+ remotesInfo: {}
901
+ };
902
+ share.addGlobalSnapshot({
903
+ [this.HostInstance.options.name]: hostSnapshot
904
+ });
905
+ }
916
906
  // In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.
917
907
  // 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.
918
- if (hostSnapshot && 'remotesInfo' in hostSnapshot && !share.getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name, (target, key)=>{
919
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
920
- target,
921
- key
922
- });
923
- if (res && !(res instanceof Promise)) {
924
- return res;
925
- }
926
- return;
927
- }).value) {
908
+ if (hostSnapshot && 'remotesInfo' in hostSnapshot && !share.getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
928
909
  if ('version' in moduleInfo || 'entry' in moduleInfo) {
929
910
  hostSnapshot.remotesInfo = _extends$1({}, hostSnapshot == null ? void 0 : hostSnapshot.remotesInfo, {
930
911
  [moduleInfo.name]: {
@@ -946,7 +927,11 @@ class SnapshotHandler {
946
927
  if (sdk.isManifestProvider(globalRemoteSnapshot)) {
947
928
  const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
948
929
  // eslint-disable-next-line @typescript-eslint/no-shadow
949
- const globalSnapshotRes = share.setGlobalSnapshotInfoByModuleInfo(_extends$1({}, moduleInfo), moduleSnapshot);
930
+ const globalSnapshotRes = share.setGlobalSnapshotInfoByModuleInfo(_extends$1({}, moduleInfo, {
931
+ // The global remote may be overridden
932
+ // Therefore, set the snapshot key to the global address of the actual request
933
+ entry: globalRemoteSnapshot.remoteEntry
934
+ }), moduleSnapshot);
950
935
  return {
951
936
  remoteSnapshot: moduleSnapshot,
952
937
  globalSnapshot: globalSnapshotRes
@@ -993,29 +978,9 @@ class SnapshotHandler {
993
978
  const hostGlobalSnapshot = share.getGlobalSnapshotInfoByModuleInfo({
994
979
  name: this.HostInstance.options.name,
995
980
  version: this.HostInstance.options.version
996
- }, {
997
- getModuleInfoHook: (target, key)=>{
998
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
999
- target,
1000
- key
1001
- });
1002
- if (res && !(res instanceof Promise)) {
1003
- return res;
1004
- }
1005
- return;
1006
- }
1007
981
  });
1008
982
  // get remote detail info from global
1009
- const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && share.getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name, (target, key)=>{
1010
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1011
- target,
1012
- key
1013
- });
1014
- if (res && !(res instanceof Promise)) {
1015
- return res;
1016
- }
1017
- return;
1018
- }).value;
983
+ const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && share.getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
1019
984
  if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
1020
985
  return {
1021
986
  hostGlobalSnapshot,
@@ -1023,17 +988,6 @@ class SnapshotHandler {
1023
988
  remoteSnapshot: share.getGlobalSnapshotInfoByModuleInfo({
1024
989
  name: moduleInfo.name,
1025
990
  version: globalRemoteInfo.matchedVersion
1026
- }, {
1027
- getModuleInfoHook: (target, key)=>{
1028
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1029
- target,
1030
- key
1031
- });
1032
- if (res && !(res instanceof Promise)) {
1033
- return res;
1034
- }
1035
- return;
1036
- }
1037
991
  })
1038
992
  };
1039
993
  }
@@ -1043,17 +997,6 @@ class SnapshotHandler {
1043
997
  remoteSnapshot: share.getGlobalSnapshotInfoByModuleInfo({
1044
998
  name: moduleInfo.name,
1045
999
  version: 'version' in moduleInfo ? moduleInfo.version : undefined
1046
- }, {
1047
- getModuleInfoHook: (target, key)=>{
1048
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1049
- target,
1050
- key
1051
- });
1052
- if (res && !(res instanceof Promise)) {
1053
- return res;
1054
- }
1055
- return;
1056
- }
1057
1000
  })
1058
1001
  };
1059
1002
  }
@@ -1381,7 +1324,7 @@ class FederationHost {
1381
1324
  const { module, moduleOptions, remoteMatchInfo } = await this._getRemoteModuleAndOptions(id);
1382
1325
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1383
1326
  const moduleOrFactory = await module.get(expose, options);
1384
- await this.hooks.lifecycle.onLoad.emit({
1327
+ const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1385
1328
  id: idRes,
1386
1329
  pkgNameOrAlias,
1387
1330
  expose,
@@ -1392,6 +1335,9 @@ class FederationHost {
1392
1335
  moduleInstance: module,
1393
1336
  origin: this
1394
1337
  });
1338
+ if (typeof moduleWrapper === 'function') {
1339
+ return moduleWrapper;
1340
+ }
1395
1341
  return moduleOrFactory;
1396
1342
  } catch (error) {
1397
1343
  const { from = 'runtime' } = options || {
@@ -1456,7 +1402,7 @@ class FederationHost {
1456
1402
  const versions = scope[name];
1457
1403
  const activeVersion = versions[version];
1458
1404
  const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
1459
- if (!activeVersion || !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
1405
+ if (!activeVersion || activeVersion.strategy !== 'loaded-first' && !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
1460
1406
  versions[version] = shared;
1461
1407
  }
1462
1408
  };
@@ -1633,7 +1579,7 @@ class FederationHost {
1633
1579
  // not used yet
1634
1580
  afterPreloadRemote: new AsyncHook()
1635
1581
  });
1636
- this.version = "0.0.9";
1582
+ this.version = "0.0.11";
1637
1583
  this.moduleCache = new Map();
1638
1584
  this.loaderHook = new PluginSystem({
1639
1585
  // FIXME: may not be suitable , not open to the public yet
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
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
- export { E as registerGlobalPlugins } 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 addGlobalSnapshot, r as setGlobalSnapshotInfoByModuleInfo, t as getGlobalSnapshot, G as Global, u as getGlobalShareScope, v as formatShareConfigs, x as isBrowserEnv, y as getBuilderId, z as addUniqueItem, A as setGlobalFederationConstructor, B as getGlobalFederationInstance, C as getGlobalFederationConstructor, E as setGlobalFederationInstance } from './share.esm.js';
2
+ export { F as registerGlobalPlugins } from './share.esm.js';
3
3
  import { composeKeyWithSeparator, loadScriptNode, loadScript, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
4
4
  export { loadScript } from '@module-federation/sdk';
5
5
 
@@ -261,7 +261,7 @@ let Module = class Module {
261
261
  remoteInfo: this.remoteInfo,
262
262
  origin: this.host
263
263
  });
264
- remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
264
+ await remoteEntryExports.init(initContainerOptions.shareScope, initContainerOptions.initScope, initContainerOptions.remoteEntryInitOptions);
265
265
  await this.host.hooks.lifecycle.initContainer.emit(_extends$4({}, initContainerOptions, {
266
266
  remoteEntryExports
267
267
  }));
@@ -672,9 +672,9 @@ function splitId(id) {
672
672
  }
673
673
  }
674
674
  // Traverse all nodes in moduleInfo and traverse the entire snapshot
675
- function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot, getModuleInfoHook) {
675
+ function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo = {}, remoteSnapshot) {
676
676
  const id = getFMId(remoteInfo);
677
- const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id, getModuleInfoHook);
677
+ const { value: snapshotValue } = getInfoWithoutType(globalSnapshot, id);
678
678
  const effectiveRemoteSnapshot = remoteSnapshot || snapshotValue;
679
679
  if (effectiveRemoteSnapshot && !isManifestProvider(effectiveRemoteSnapshot)) {
680
680
  traverse(effectiveRemoteSnapshot, remoteInfo, isRoot);
@@ -690,7 +690,7 @@ function traverseModuleInfo(globalSnapshot, remoteInfo, traverse, isRoot, memo =
690
690
  traverseModuleInfo(globalSnapshot, {
691
691
  name: subRemoteInfo.name,
692
692
  version: remoteValue.matchedVersion
693
- }, traverse, false, memo, undefined, getModuleInfoHook);
693
+ }, traverse, false, memo, undefined);
694
694
  }
695
695
  }
696
696
  }
@@ -790,16 +790,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
790
790
  setPreloaded(exposeFullPath);
791
791
  }
792
792
  }
793
- }, true, memo, remoteSnapshot, (target, key)=>{
794
- const res = origin.loaderHook.lifecycle.getModuleInfo.emit({
795
- target,
796
- key
797
- });
798
- if (res && !(res instanceof Promise)) {
799
- return res;
800
- }
801
- return;
802
- });
793
+ }, true, memo, remoteSnapshot);
803
794
  if (remoteSnapshot.shared) {
804
795
  remoteSnapshot.shared.forEach((shared)=>{
805
796
  var _options_shared;
@@ -892,37 +883,27 @@ class SnapshotHandler {
892
883
  // eslint-disable-next-line max-lines-per-function
893
884
  async loadRemoteSnapshotInfo(moduleInfo) {
894
885
  const { options } = this.HostInstance;
895
- const hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
896
- name: this.HostInstance.options.name,
897
- version: this.HostInstance.options.version
898
- }, {
899
- getModuleInfoHook: (target, key)=>{
900
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
901
- target,
902
- key
903
- });
904
- if (res && !(res instanceof Promise)) {
905
- return res;
906
- }
907
- return;
908
- }
909
- });
910
886
  await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
911
887
  options,
912
888
  moduleInfo
913
889
  });
890
+ let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
891
+ name: this.HostInstance.options.name,
892
+ version: this.HostInstance.options.version
893
+ });
894
+ if (!hostSnapshot) {
895
+ hostSnapshot = {
896
+ version: this.HostInstance.options.version || '',
897
+ remoteEntry: '',
898
+ remotesInfo: {}
899
+ };
900
+ addGlobalSnapshot({
901
+ [this.HostInstance.options.name]: hostSnapshot
902
+ });
903
+ }
914
904
  // In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.
915
905
  // 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.
916
- if (hostSnapshot && 'remotesInfo' in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name, (target, key)=>{
917
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
918
- target,
919
- key
920
- });
921
- if (res && !(res instanceof Promise)) {
922
- return res;
923
- }
924
- return;
925
- }).value) {
906
+ if (hostSnapshot && 'remotesInfo' in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
926
907
  if ('version' in moduleInfo || 'entry' in moduleInfo) {
927
908
  hostSnapshot.remotesInfo = _extends$1({}, hostSnapshot == null ? void 0 : hostSnapshot.remotesInfo, {
928
909
  [moduleInfo.name]: {
@@ -944,7 +925,11 @@ class SnapshotHandler {
944
925
  if (isManifestProvider(globalRemoteSnapshot)) {
945
926
  const moduleSnapshot = await this.getManifestJson(globalRemoteSnapshot.remoteEntry, moduleInfo, {});
946
927
  // eslint-disable-next-line @typescript-eslint/no-shadow
947
- const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(_extends$1({}, moduleInfo), moduleSnapshot);
928
+ const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(_extends$1({}, moduleInfo, {
929
+ // The global remote may be overridden
930
+ // Therefore, set the snapshot key to the global address of the actual request
931
+ entry: globalRemoteSnapshot.remoteEntry
932
+ }), moduleSnapshot);
948
933
  return {
949
934
  remoteSnapshot: moduleSnapshot,
950
935
  globalSnapshot: globalSnapshotRes
@@ -991,29 +976,9 @@ class SnapshotHandler {
991
976
  const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
992
977
  name: this.HostInstance.options.name,
993
978
  version: this.HostInstance.options.version
994
- }, {
995
- getModuleInfoHook: (target, key)=>{
996
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
997
- target,
998
- key
999
- });
1000
- if (res && !(res instanceof Promise)) {
1001
- return res;
1002
- }
1003
- return;
1004
- }
1005
979
  });
1006
980
  // get remote detail info from global
1007
- const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name, (target, key)=>{
1008
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1009
- target,
1010
- key
1011
- });
1012
- if (res && !(res instanceof Promise)) {
1013
- return res;
1014
- }
1015
- return;
1016
- }).value;
981
+ const globalRemoteInfo = hostGlobalSnapshot && 'remotesInfo' in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
1017
982
  if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {
1018
983
  return {
1019
984
  hostGlobalSnapshot,
@@ -1021,17 +986,6 @@ class SnapshotHandler {
1021
986
  remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
1022
987
  name: moduleInfo.name,
1023
988
  version: globalRemoteInfo.matchedVersion
1024
- }, {
1025
- getModuleInfoHook: (target, key)=>{
1026
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1027
- target,
1028
- key
1029
- });
1030
- if (res && !(res instanceof Promise)) {
1031
- return res;
1032
- }
1033
- return;
1034
- }
1035
989
  })
1036
990
  };
1037
991
  }
@@ -1041,17 +995,6 @@ class SnapshotHandler {
1041
995
  remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
1042
996
  name: moduleInfo.name,
1043
997
  version: 'version' in moduleInfo ? moduleInfo.version : undefined
1044
- }, {
1045
- getModuleInfoHook: (target, key)=>{
1046
- const res = this.HostInstance.loaderHook.lifecycle.getModuleInfo.emit({
1047
- target,
1048
- key
1049
- });
1050
- if (res && !(res instanceof Promise)) {
1051
- return res;
1052
- }
1053
- return;
1054
- }
1055
998
  })
1056
999
  };
1057
1000
  }
@@ -1379,7 +1322,7 @@ class FederationHost {
1379
1322
  const { module, moduleOptions, remoteMatchInfo } = await this._getRemoteModuleAndOptions(id);
1380
1323
  const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo;
1381
1324
  const moduleOrFactory = await module.get(expose, options);
1382
- await this.hooks.lifecycle.onLoad.emit({
1325
+ const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
1383
1326
  id: idRes,
1384
1327
  pkgNameOrAlias,
1385
1328
  expose,
@@ -1390,6 +1333,9 @@ class FederationHost {
1390
1333
  moduleInstance: module,
1391
1334
  origin: this
1392
1335
  });
1336
+ if (typeof moduleWrapper === 'function') {
1337
+ return moduleWrapper;
1338
+ }
1393
1339
  return moduleOrFactory;
1394
1340
  } catch (error) {
1395
1341
  const { from = 'runtime' } = options || {
@@ -1454,7 +1400,7 @@ class FederationHost {
1454
1400
  const versions = scope[name];
1455
1401
  const activeVersion = versions[version];
1456
1402
  const activeVersionEager = Boolean(activeVersion && (activeVersion.eager || ((_activeVersion_shareConfig = activeVersion.shareConfig) == null ? void 0 : _activeVersion_shareConfig.eager)));
1457
- if (!activeVersion || !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
1403
+ if (!activeVersion || activeVersion.strategy !== 'loaded-first' && !activeVersion.loaded && (Boolean(!eager) !== !activeVersionEager ? eager : hostName > activeVersion.from)) {
1458
1404
  versions[version] = shared;
1459
1405
  }
1460
1406
  };
@@ -1631,7 +1577,7 @@ class FederationHost {
1631
1577
  // not used yet
1632
1578
  afterPreloadRemote: new AsyncHook()
1633
1579
  });
1634
- this.version = "0.0.9";
1580
+ this.version = "0.0.11";
1635
1581
  this.moduleCache = new Map();
1636
1582
  this.loaderHook = new PluginSystem({
1637
1583
  // FIXME: may not be suitable , not open to the public yet
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./index.cjs.js",
6
6
  "module": "./index.esm.js",
package/dist/share.cjs.js CHANGED
@@ -89,8 +89,13 @@ function _object_without_properties_loose(source, excluded) {
89
89
  }
90
90
  return target;
91
91
  }
92
- // export const nativeGlobal: typeof global = new Function('return this')();
93
- const nativeGlobal = new Function('return this')();
92
+ const nativeGlobal = (()=>{
93
+ try {
94
+ return new Function('return this')();
95
+ } catch (e) {
96
+ return globalThis;
97
+ }
98
+ })();
94
99
  const Global = nativeGlobal;
95
100
  function definePropertyGlobalVal(target, key, val) {
96
101
  Object.defineProperty(target, key, {
@@ -168,29 +173,48 @@ function setGlobalFederationInstance(FederationInstance) {
168
173
  function getGlobalFederationConstructor() {
169
174
  return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
170
175
  }
171
- function setGlobalFederationConstructor(FederationConstructor) {
172
- if (isDebugMode()) {
176
+ function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
177
+ if (isDebug) {
173
178
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
174
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.9";
179
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.11";
175
180
  }
176
181
  }
177
182
  // eslint-disable-next-line @typescript-eslint/ban-types
178
- function getInfoWithoutType(target, key, getModuleInfoHook) {
179
- let res = {
180
- value: target[key],
181
- key: key
182
- };
183
- if (getModuleInfoHook) {
184
- const hookRes = getModuleInfoHook(target, key);
185
- res = hookRes || res;
183
+ function getInfoWithoutType(target, key) {
184
+ if (typeof key === 'string') {
185
+ const keyRes = target[key];
186
+ if (keyRes) {
187
+ return {
188
+ value: target[key],
189
+ key: key
190
+ };
191
+ } else {
192
+ const targetKeys = Object.keys(target);
193
+ for (const targetKey of targetKeys){
194
+ const [targetTypeOrName, _] = targetKey.split(':');
195
+ const nKey = `${targetTypeOrName}:${key}`;
196
+ const typeWithKeyRes = target[nKey];
197
+ if (typeWithKeyRes) {
198
+ return {
199
+ value: typeWithKeyRes,
200
+ key: nKey
201
+ };
202
+ }
203
+ }
204
+ return {
205
+ value: undefined,
206
+ key: key
207
+ };
208
+ }
209
+ } else {
210
+ throw new Error('key must be string');
186
211
  }
187
- return res;
188
212
  }
189
213
  const getGlobalSnapshot = ()=>nativeGlobal.__FEDERATION__.moduleInfo;
190
- const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot, getModuleInfoHook)=>{
214
+ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot)=>{
191
215
  // Check if the remote is included in the hostSnapshot
192
216
  const moduleKey = getFMId(moduleInfo);
193
- const getModuleInfo = getInfoWithoutType(snapshot, moduleKey, getModuleInfoHook).value;
217
+ const getModuleInfo = getInfoWithoutType(snapshot, moduleKey).value;
194
218
  // The remoteSnapshot might not include a version
195
219
  if (getModuleInfo && !getModuleInfo.version && 'version' in moduleInfo && moduleInfo['version']) {
196
220
  getModuleInfo.version = moduleInfo['version'];
@@ -204,14 +228,14 @@ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot, getModuleInfoHo
204
228
  "version"
205
229
  ]);
206
230
  const moduleKeyWithoutVersion = getFMId(resModuleInfo);
207
- const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion, getModuleInfoHook).value;
231
+ const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion).value;
208
232
  if ((getModuleInfoWithoutVersion == null ? void 0 : getModuleInfoWithoutVersion.version) === version) {
209
233
  return getModuleInfoWithoutVersion;
210
234
  }
211
235
  }
212
236
  return;
213
237
  };
214
- const getGlobalSnapshotInfoByModuleInfo = (moduleInfo, extraOptions)=>getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo, extraOptions == null ? void 0 : extraOptions.getModuleInfoHook);
238
+ const getGlobalSnapshotInfoByModuleInfo = (moduleInfo)=>getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo);
215
239
  const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo)=>{
216
240
  const moduleKey = getFMId(remoteInfo);
217
241
  nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
package/dist/share.esm.js CHANGED
@@ -87,8 +87,13 @@ function _object_without_properties_loose(source, excluded) {
87
87
  }
88
88
  return target;
89
89
  }
90
- // export const nativeGlobal: typeof global = new Function('return this')();
91
- const nativeGlobal = new Function('return this')();
90
+ const nativeGlobal = (()=>{
91
+ try {
92
+ return new Function('return this')();
93
+ } catch (e) {
94
+ return globalThis;
95
+ }
96
+ })();
92
97
  const Global = nativeGlobal;
93
98
  function definePropertyGlobalVal(target, key, val) {
94
99
  Object.defineProperty(target, key, {
@@ -166,29 +171,48 @@ function setGlobalFederationInstance(FederationInstance) {
166
171
  function getGlobalFederationConstructor() {
167
172
  return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
168
173
  }
169
- function setGlobalFederationConstructor(FederationConstructor) {
170
- if (isDebugMode()) {
174
+ function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
175
+ if (isDebug) {
171
176
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
172
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.9";
177
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.0.11";
173
178
  }
174
179
  }
175
180
  // eslint-disable-next-line @typescript-eslint/ban-types
176
- function getInfoWithoutType(target, key, getModuleInfoHook) {
177
- let res = {
178
- value: target[key],
179
- key: key
180
- };
181
- if (getModuleInfoHook) {
182
- const hookRes = getModuleInfoHook(target, key);
183
- res = hookRes || res;
181
+ function getInfoWithoutType(target, key) {
182
+ if (typeof key === 'string') {
183
+ const keyRes = target[key];
184
+ if (keyRes) {
185
+ return {
186
+ value: target[key],
187
+ key: key
188
+ };
189
+ } else {
190
+ const targetKeys = Object.keys(target);
191
+ for (const targetKey of targetKeys){
192
+ const [targetTypeOrName, _] = targetKey.split(':');
193
+ const nKey = `${targetTypeOrName}:${key}`;
194
+ const typeWithKeyRes = target[nKey];
195
+ if (typeWithKeyRes) {
196
+ return {
197
+ value: typeWithKeyRes,
198
+ key: nKey
199
+ };
200
+ }
201
+ }
202
+ return {
203
+ value: undefined,
204
+ key: key
205
+ };
206
+ }
207
+ } else {
208
+ throw new Error('key must be string');
184
209
  }
185
- return res;
186
210
  }
187
211
  const getGlobalSnapshot = ()=>nativeGlobal.__FEDERATION__.moduleInfo;
188
- const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot, getModuleInfoHook)=>{
212
+ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot)=>{
189
213
  // Check if the remote is included in the hostSnapshot
190
214
  const moduleKey = getFMId(moduleInfo);
191
- const getModuleInfo = getInfoWithoutType(snapshot, moduleKey, getModuleInfoHook).value;
215
+ const getModuleInfo = getInfoWithoutType(snapshot, moduleKey).value;
192
216
  // The remoteSnapshot might not include a version
193
217
  if (getModuleInfo && !getModuleInfo.version && 'version' in moduleInfo && moduleInfo['version']) {
194
218
  getModuleInfo.version = moduleInfo['version'];
@@ -202,14 +226,14 @@ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot, getModuleInfoHo
202
226
  "version"
203
227
  ]);
204
228
  const moduleKeyWithoutVersion = getFMId(resModuleInfo);
205
- const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion, getModuleInfoHook).value;
229
+ const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion).value;
206
230
  if ((getModuleInfoWithoutVersion == null ? void 0 : getModuleInfoWithoutVersion.version) === version) {
207
231
  return getModuleInfoWithoutVersion;
208
232
  }
209
233
  }
210
234
  return;
211
235
  };
212
- const getGlobalSnapshotInfoByModuleInfo = (moduleInfo, extraOptions)=>getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo, extraOptions == null ? void 0 : extraOptions.getModuleInfoHook);
236
+ const getGlobalSnapshotInfoByModuleInfo = (moduleInfo)=>getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo);
213
237
  const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo)=>{
214
238
  const moduleKey = getFMId(remoteInfo);
215
239
  nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
@@ -770,4 +794,4 @@ function getGlobalShareScope() {
770
794
  return Global.__FEDERATION__.__SHARE__;
771
795
  }
772
796
 
773
- export { getGlobalFederationInstance as A, getGlobalFederationConstructor as B, setGlobalFederationInstance as C, DEFAULT_REMOTE_TYPE as D, registerGlobalPlugins as E, nativeGlobal as F, Global as G, resetFederationGlobalInfo as H, getTargetSnapshotInfoByModuleInfo as I, addGlobalSnapshot as J, DEFAULT_SCOPE as a, globalLoading as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getRegisteredShare as l, getInfoWithoutType as m, getPreloaded as n, setPreloaded as o, getGlobalSnapshotInfoByModuleInfo as p, setGlobalSnapshotInfoByModuleInfo as q, getGlobalSnapshot as r, safeToString as s, getGlobalShareScope as t, formatShareConfigs as u, isBrowserEnv as v, warn as w, getBuilderId as x, addUniqueItem as y, setGlobalFederationConstructor as z };
797
+ export { setGlobalFederationConstructor as A, getGlobalFederationInstance as B, getGlobalFederationConstructor as C, DEFAULT_REMOTE_TYPE as D, setGlobalFederationInstance as E, registerGlobalPlugins as F, Global as G, nativeGlobal as H, resetFederationGlobalInfo as I, getTargetSnapshotInfoByModuleInfo as J, DEFAULT_SCOPE as a, globalLoading as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getRegisteredShare as l, getInfoWithoutType as m, getPreloaded as n, setPreloaded as o, getGlobalSnapshotInfoByModuleInfo as p, addGlobalSnapshot as q, setGlobalSnapshotInfoByModuleInfo as r, safeToString as s, getGlobalSnapshot as t, getGlobalShareScope as u, formatShareConfigs as v, warn as w, isBrowserEnv as x, getBuilderId as y, addUniqueItem as z };
@@ -23,25 +23,14 @@ export declare function resetFederationGlobalInfo(): void;
23
23
  export declare function getGlobalFederationInstance(name: string, version: string | undefined): FederationHost | undefined;
24
24
  export declare function setGlobalFederationInstance(FederationInstance: FederationHost): void;
25
25
  export declare function getGlobalFederationConstructor(): typeof FederationHost | undefined;
26
- export declare function setGlobalFederationConstructor(FederationConstructor: typeof FederationHost): void;
27
- export declare function getInfoWithoutType<T extends object>(target: T, key: keyof T, getModuleInfoHook?: (target: any, key: string | number | symbol) => {
28
- value: any | undefined;
29
- key: string;
30
- } | void): {
26
+ export declare function setGlobalFederationConstructor(FederationConstructor: typeof FederationHost | undefined, isDebug?: boolean): void;
27
+ export declare function getInfoWithoutType<T extends object>(target: T, key: keyof T): {
31
28
  value: T[keyof T] | undefined;
32
29
  key: string;
33
30
  };
34
31
  export declare const getGlobalSnapshot: () => GlobalModuleInfo;
35
- export declare const getTargetSnapshotInfoByModuleInfo: (moduleInfo: Optional<Remote, 'alias'>, snapshot: GlobalModuleInfo, getModuleInfoHook?: ((target: any, key: string | number | symbol) => void | {
36
- value: any | undefined;
37
- key: string;
38
- }) | undefined) => GlobalModuleInfo[string] | undefined;
39
- export declare const getGlobalSnapshotInfoByModuleInfo: (moduleInfo: Optional<Remote, 'alias'>, extraOptions?: {
40
- getModuleInfoHook?: ((target: any, key: string | number | symbol) => void | {
41
- value: any | undefined;
42
- key: string;
43
- }) | undefined;
44
- } | undefined) => GlobalModuleInfo[string] | undefined;
32
+ export declare const getTargetSnapshotInfoByModuleInfo: (moduleInfo: Optional<Remote, 'alias'>, snapshot: GlobalModuleInfo) => GlobalModuleInfo[string] | undefined;
33
+ export declare const getGlobalSnapshotInfoByModuleInfo: (moduleInfo: Optional<Remote, 'alias'>) => GlobalModuleInfo[string] | undefined;
45
34
  export declare const setGlobalSnapshotInfoByModuleInfo: (remoteInfo: Remote, moduleDetailInfo: GlobalModuleInfo[string]) => GlobalModuleInfo;
46
35
  export declare const addGlobalSnapshot: (moduleInfos: GlobalModuleInfo) => (() => void);
47
36
  export declare const getRemoteEntryExports: (name: string, globalName: string | undefined) => {
@@ -38,12 +38,12 @@ type SharedBaseArgs = {
38
38
  deps?: Array<string>;
39
39
  strategy?: 'version-first' | 'loaded-first';
40
40
  };
41
+ export type SharedGetter = (() => () => Module) | (() => Promise<() => Module>);
41
42
  export type ShareArgs = (SharedBaseArgs & {
42
- get: () => () => Module | Promise<() => Module>;
43
+ get: SharedGetter;
43
44
  }) | (SharedBaseArgs & {
44
45
  lib: () => Module;
45
46
  });
46
- export type SharedGetter = () => () => Promise<() => Module> | Module;
47
47
  export type Shared = {
48
48
  version: string;
49
49
  get: SharedGetter;
@@ -95,6 +95,6 @@ export type RemoteEntryInitOptions = {
95
95
  export type InitScope = Array<Record<string, never>>;
96
96
  export type RemoteEntryExports = {
97
97
  get: (id: string) => () => Promise<Module>;
98
- init: (shareScope: ShareScopeMap[string], initScope?: InitScope, remoteEntryInitOPtions?: RemoteEntryInitOptions) => void;
98
+ init: (shareScope: ShareScopeMap[string], initScope?: InitScope, remoteEntryInitOPtions?: RemoteEntryInitOptions) => void | Promise<void>;
99
99
  };
100
100
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.esm.js",
@@ -45,6 +45,6 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "@module-federation/sdk": "0.0.9"
48
+ "@module-federation/sdk": "0.0.11"
49
49
  }
50
50
  }