@module-federation/runtime-core 0.16.0 → 0.17.0

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.
@@ -196,7 +196,7 @@ function getGlobalFederationConstructor() {
196
196
  function setGlobalFederationConstructor(FederationConstructor, isDebug = sdk.isDebugMode()) {
197
197
  if (isDebug) {
198
198
  CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
199
- CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.16.0";
199
+ CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.17.0";
200
200
  }
201
201
  }
202
202
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -943,34 +943,6 @@ function getTargetSharedOptions(options) {
943
943
  return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
944
944
  }
945
945
 
946
- const ShareUtils = {
947
- getRegisteredShare,
948
- getGlobalShareScope
949
- };
950
- const GlobalUtils = {
951
- Global,
952
- nativeGlobal,
953
- resetFederationGlobalInfo,
954
- setGlobalFederationInstance,
955
- getGlobalFederationConstructor,
956
- setGlobalFederationConstructor,
957
- getInfoWithoutType,
958
- getGlobalSnapshot,
959
- getTargetSnapshotInfoByModuleInfo,
960
- getGlobalSnapshotInfoByModuleInfo,
961
- setGlobalSnapshotInfoByModuleInfo,
962
- addGlobalSnapshot,
963
- getRemoteEntryExports,
964
- registerGlobalPlugins,
965
- getGlobalHostPlugins,
966
- getPreloaded,
967
- setPreloaded
968
- };
969
- var helpers = {
970
- global: GlobalUtils,
971
- share: ShareUtils
972
- };
973
-
974
946
  function getBuilderId() {
975
947
  //@ts-ignore
976
948
  return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined' ? FEDERATION_BUILD_IDENTIFIER : '';
@@ -1040,8 +1012,16 @@ function matchRemote(remotes, nameOrAlias) {
1040
1012
  return;
1041
1013
  }
1042
1014
 
1043
- function registerPlugins(plugins, hookInstances) {
1015
+ function registerPlugins(plugins, instance) {
1044
1016
  const globalPlugins = getGlobalHostPlugins();
1017
+ const hookInstances = [
1018
+ instance.hooks,
1019
+ instance.remoteHandler.hooks,
1020
+ instance.sharedHandler.hooks,
1021
+ instance.snapshotHandler.hooks,
1022
+ instance.loaderHook,
1023
+ instance.bridgeHook
1024
+ ];
1045
1025
  // Incorporate global plugins
1046
1026
  if (globalPlugins.length > 0) {
1047
1027
  globalPlugins.forEach((plugin)=>{
@@ -1053,7 +1033,7 @@ function registerPlugins(plugins, hookInstances) {
1053
1033
  if (plugins && plugins.length > 0) {
1054
1034
  plugins.forEach((plugin)=>{
1055
1035
  hookInstances.forEach((hookInstance)=>{
1056
- hookInstance.applyPlugin(plugin);
1036
+ hookInstance.applyPlugin(plugin, instance);
1057
1037
  });
1058
1038
  });
1059
1039
  }
@@ -1240,6 +1220,202 @@ function getRemoteInfo(remote) {
1240
1220
  });
1241
1221
  }
1242
1222
 
1223
+ function defaultPreloadArgs(preloadConfig) {
1224
+ return polyfills._extends({
1225
+ resourceCategory: 'sync',
1226
+ share: true,
1227
+ depsRemote: true,
1228
+ prefetchInterface: false
1229
+ }, preloadConfig);
1230
+ }
1231
+ function formatPreloadArgs(remotes, preloadArgs) {
1232
+ return preloadArgs.map((args)=>{
1233
+ const remoteInfo = matchRemote(remotes, args.nameOrAlias);
1234
+ assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo && sdk.safeToString({
1235
+ remoteInfo,
1236
+ remotes
1237
+ })}`);
1238
+ return {
1239
+ remote: remoteInfo,
1240
+ preloadConfig: defaultPreloadArgs(args)
1241
+ };
1242
+ });
1243
+ }
1244
+ function normalizePreloadExposes(exposes) {
1245
+ if (!exposes) {
1246
+ return [];
1247
+ }
1248
+ return exposes.map((expose)=>{
1249
+ if (expose === '.') {
1250
+ return expose;
1251
+ }
1252
+ if (expose.startsWith('./')) {
1253
+ return expose.replace('./', '');
1254
+ }
1255
+ return expose;
1256
+ });
1257
+ }
1258
+ function preloadAssets(remoteInfo, host, assets, // It is used to distinguish preload from load remote parallel loading
1259
+ useLinkPreload = true) {
1260
+ const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
1261
+ if (host.options.inBrowser) {
1262
+ entryAssets.forEach((asset)=>{
1263
+ const { moduleInfo } = asset;
1264
+ const module = host.moduleCache.get(remoteInfo.name);
1265
+ if (module) {
1266
+ getRemoteEntry({
1267
+ origin: host,
1268
+ remoteInfo: moduleInfo,
1269
+ remoteEntryExports: module.remoteEntryExports
1270
+ });
1271
+ } else {
1272
+ getRemoteEntry({
1273
+ origin: host,
1274
+ remoteInfo: moduleInfo,
1275
+ remoteEntryExports: undefined
1276
+ });
1277
+ }
1278
+ });
1279
+ if (useLinkPreload) {
1280
+ const defaultAttrs = {
1281
+ rel: 'preload',
1282
+ as: 'style'
1283
+ };
1284
+ cssAssets.forEach((cssUrl)=>{
1285
+ const { link: cssEl, needAttach } = sdk.createLink({
1286
+ url: cssUrl,
1287
+ cb: ()=>{
1288
+ // noop
1289
+ },
1290
+ attrs: defaultAttrs,
1291
+ createLinkHook: (url, attrs)=>{
1292
+ const res = host.loaderHook.lifecycle.createLink.emit({
1293
+ url,
1294
+ attrs
1295
+ });
1296
+ if (res instanceof HTMLLinkElement) {
1297
+ return res;
1298
+ }
1299
+ return;
1300
+ }
1301
+ });
1302
+ needAttach && document.head.appendChild(cssEl);
1303
+ });
1304
+ } else {
1305
+ const defaultAttrs = {
1306
+ rel: 'stylesheet',
1307
+ type: 'text/css'
1308
+ };
1309
+ cssAssets.forEach((cssUrl)=>{
1310
+ const { link: cssEl, needAttach } = sdk.createLink({
1311
+ url: cssUrl,
1312
+ cb: ()=>{
1313
+ // noop
1314
+ },
1315
+ attrs: defaultAttrs,
1316
+ createLinkHook: (url, attrs)=>{
1317
+ const res = host.loaderHook.lifecycle.createLink.emit({
1318
+ url,
1319
+ attrs
1320
+ });
1321
+ if (res instanceof HTMLLinkElement) {
1322
+ return res;
1323
+ }
1324
+ return;
1325
+ },
1326
+ needDeleteLink: false
1327
+ });
1328
+ needAttach && document.head.appendChild(cssEl);
1329
+ });
1330
+ }
1331
+ if (useLinkPreload) {
1332
+ const defaultAttrs = {
1333
+ rel: 'preload',
1334
+ as: 'script'
1335
+ };
1336
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
1337
+ const { link: linkEl, needAttach } = sdk.createLink({
1338
+ url: jsUrl,
1339
+ cb: ()=>{
1340
+ // noop
1341
+ },
1342
+ attrs: defaultAttrs,
1343
+ createLinkHook: (url, attrs)=>{
1344
+ const res = host.loaderHook.lifecycle.createLink.emit({
1345
+ url,
1346
+ attrs
1347
+ });
1348
+ if (res instanceof HTMLLinkElement) {
1349
+ return res;
1350
+ }
1351
+ return;
1352
+ }
1353
+ });
1354
+ needAttach && document.head.appendChild(linkEl);
1355
+ });
1356
+ } else {
1357
+ const defaultAttrs = {
1358
+ fetchpriority: 'high',
1359
+ type: (remoteInfo == null ? void 0 : remoteInfo.type) === 'module' ? 'module' : 'text/javascript'
1360
+ };
1361
+ jsAssetsWithoutEntry.forEach((jsUrl)=>{
1362
+ const { script: scriptEl, needAttach } = sdk.createScript({
1363
+ url: jsUrl,
1364
+ cb: ()=>{
1365
+ // noop
1366
+ },
1367
+ attrs: defaultAttrs,
1368
+ createScriptHook: (url, attrs)=>{
1369
+ const res = host.loaderHook.lifecycle.createScript.emit({
1370
+ url,
1371
+ attrs
1372
+ });
1373
+ if (res instanceof HTMLScriptElement) {
1374
+ return res;
1375
+ }
1376
+ return;
1377
+ },
1378
+ needDeleteScript: true
1379
+ });
1380
+ needAttach && document.head.appendChild(scriptEl);
1381
+ });
1382
+ }
1383
+ }
1384
+ }
1385
+
1386
+ const ShareUtils = {
1387
+ getRegisteredShare,
1388
+ getGlobalShareScope
1389
+ };
1390
+ const GlobalUtils = {
1391
+ Global,
1392
+ nativeGlobal,
1393
+ resetFederationGlobalInfo,
1394
+ setGlobalFederationInstance,
1395
+ getGlobalFederationConstructor,
1396
+ setGlobalFederationConstructor,
1397
+ getInfoWithoutType,
1398
+ getGlobalSnapshot,
1399
+ getTargetSnapshotInfoByModuleInfo,
1400
+ getGlobalSnapshotInfoByModuleInfo,
1401
+ setGlobalSnapshotInfoByModuleInfo,
1402
+ addGlobalSnapshot,
1403
+ getRemoteEntryExports,
1404
+ registerGlobalPlugins,
1405
+ getGlobalHostPlugins,
1406
+ getPreloaded,
1407
+ setPreloaded
1408
+ };
1409
+ var helpers = {
1410
+ global: GlobalUtils,
1411
+ share: ShareUtils,
1412
+ utils: {
1413
+ matchRemoteWithNameAndExpose,
1414
+ preloadAssets,
1415
+ getRemoteInfo
1416
+ }
1417
+ };
1418
+
1243
1419
  let Module = class Module {
1244
1420
  async getEntry() {
1245
1421
  if (this.remoteEntryExports) {
@@ -1520,13 +1696,14 @@ class AsyncWaterfallHook extends SyncHook {
1520
1696
  }
1521
1697
 
1522
1698
  class PluginSystem {
1523
- applyPlugin(plugin) {
1699
+ applyPlugin(plugin, instance) {
1524
1700
  assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
1525
1701
  // The plugin's name is mandatory and must be unique
1526
1702
  const pluginName = plugin.name;
1527
1703
  assert(pluginName, 'A name must be provided by the plugin.');
1528
1704
  if (!this.registerPlugins[pluginName]) {
1529
1705
  this.registerPlugins[pluginName] = plugin;
1706
+ plugin.apply == null ? void 0 : plugin.apply.call(plugin, instance);
1530
1707
  Object.keys(this.lifecycle).forEach((key)=>{
1531
1708
  const pluginLife = plugin[key];
1532
1709
  if (pluginLife) {
@@ -1545,17 +1722,6 @@ class PluginSystem {
1545
1722
  }
1546
1723
  });
1547
1724
  }
1548
- // eslint-disable-next-line @typescript-eslint/no-shadow
1549
- inherit({ lifecycle, registerPlugins }) {
1550
- Object.keys(lifecycle).forEach((hookName)=>{
1551
- assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
1552
- this.lifecycle[hookName] = lifecycle[hookName];
1553
- });
1554
- Object.keys(registerPlugins).forEach((pluginName)=>{
1555
- assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
1556
- this.applyPlugin(registerPlugins[pluginName]);
1557
- });
1558
- }
1559
1725
  constructor(lifecycle){
1560
1726
  this.registerPlugins = {};
1561
1727
  this.lifecycle = lifecycle;
@@ -1563,169 +1729,6 @@ class PluginSystem {
1563
1729
  }
1564
1730
  }
1565
1731
 
1566
- function defaultPreloadArgs(preloadConfig) {
1567
- return polyfills._extends({
1568
- resourceCategory: 'sync',
1569
- share: true,
1570
- depsRemote: true,
1571
- prefetchInterface: false
1572
- }, preloadConfig);
1573
- }
1574
- function formatPreloadArgs(remotes, preloadArgs) {
1575
- return preloadArgs.map((args)=>{
1576
- const remoteInfo = matchRemote(remotes, args.nameOrAlias);
1577
- assert(remoteInfo, `Unable to preload ${args.nameOrAlias} as it is not included in ${!remoteInfo && sdk.safeToString({
1578
- remoteInfo,
1579
- remotes
1580
- })}`);
1581
- return {
1582
- remote: remoteInfo,
1583
- preloadConfig: defaultPreloadArgs(args)
1584
- };
1585
- });
1586
- }
1587
- function normalizePreloadExposes(exposes) {
1588
- if (!exposes) {
1589
- return [];
1590
- }
1591
- return exposes.map((expose)=>{
1592
- if (expose === '.') {
1593
- return expose;
1594
- }
1595
- if (expose.startsWith('./')) {
1596
- return expose.replace('./', '');
1597
- }
1598
- return expose;
1599
- });
1600
- }
1601
- function preloadAssets(remoteInfo, host, assets, // It is used to distinguish preload from load remote parallel loading
1602
- useLinkPreload = true) {
1603
- const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
1604
- if (host.options.inBrowser) {
1605
- entryAssets.forEach((asset)=>{
1606
- const { moduleInfo } = asset;
1607
- const module = host.moduleCache.get(remoteInfo.name);
1608
- if (module) {
1609
- getRemoteEntry({
1610
- origin: host,
1611
- remoteInfo: moduleInfo,
1612
- remoteEntryExports: module.remoteEntryExports
1613
- });
1614
- } else {
1615
- getRemoteEntry({
1616
- origin: host,
1617
- remoteInfo: moduleInfo,
1618
- remoteEntryExports: undefined
1619
- });
1620
- }
1621
- });
1622
- if (useLinkPreload) {
1623
- const defaultAttrs = {
1624
- rel: 'preload',
1625
- as: 'style'
1626
- };
1627
- cssAssets.forEach((cssUrl)=>{
1628
- const { link: cssEl, needAttach } = sdk.createLink({
1629
- url: cssUrl,
1630
- cb: ()=>{
1631
- // noop
1632
- },
1633
- attrs: defaultAttrs,
1634
- createLinkHook: (url, attrs)=>{
1635
- const res = host.loaderHook.lifecycle.createLink.emit({
1636
- url,
1637
- attrs
1638
- });
1639
- if (res instanceof HTMLLinkElement) {
1640
- return res;
1641
- }
1642
- return;
1643
- }
1644
- });
1645
- needAttach && document.head.appendChild(cssEl);
1646
- });
1647
- } else {
1648
- const defaultAttrs = {
1649
- rel: 'stylesheet',
1650
- type: 'text/css'
1651
- };
1652
- cssAssets.forEach((cssUrl)=>{
1653
- const { link: cssEl, needAttach } = sdk.createLink({
1654
- url: cssUrl,
1655
- cb: ()=>{
1656
- // noop
1657
- },
1658
- attrs: defaultAttrs,
1659
- createLinkHook: (url, attrs)=>{
1660
- const res = host.loaderHook.lifecycle.createLink.emit({
1661
- url,
1662
- attrs
1663
- });
1664
- if (res instanceof HTMLLinkElement) {
1665
- return res;
1666
- }
1667
- return;
1668
- },
1669
- needDeleteLink: false
1670
- });
1671
- needAttach && document.head.appendChild(cssEl);
1672
- });
1673
- }
1674
- if (useLinkPreload) {
1675
- const defaultAttrs = {
1676
- rel: 'preload',
1677
- as: 'script'
1678
- };
1679
- jsAssetsWithoutEntry.forEach((jsUrl)=>{
1680
- const { link: linkEl, needAttach } = sdk.createLink({
1681
- url: jsUrl,
1682
- cb: ()=>{
1683
- // noop
1684
- },
1685
- attrs: defaultAttrs,
1686
- createLinkHook: (url, attrs)=>{
1687
- const res = host.loaderHook.lifecycle.createLink.emit({
1688
- url,
1689
- attrs
1690
- });
1691
- if (res instanceof HTMLLinkElement) {
1692
- return res;
1693
- }
1694
- return;
1695
- }
1696
- });
1697
- needAttach && document.head.appendChild(linkEl);
1698
- });
1699
- } else {
1700
- const defaultAttrs = {
1701
- fetchpriority: 'high',
1702
- type: (remoteInfo == null ? void 0 : remoteInfo.type) === 'module' ? 'module' : 'text/javascript'
1703
- };
1704
- jsAssetsWithoutEntry.forEach((jsUrl)=>{
1705
- const { script: scriptEl, needAttach } = sdk.createScript({
1706
- url: jsUrl,
1707
- cb: ()=>{
1708
- // noop
1709
- },
1710
- attrs: defaultAttrs,
1711
- createScriptHook: (url, attrs)=>{
1712
- const res = host.loaderHook.lifecycle.createScript.emit({
1713
- url,
1714
- attrs
1715
- });
1716
- if (res instanceof HTMLScriptElement) {
1717
- return res;
1718
- }
1719
- return;
1720
- },
1721
- needDeleteScript: true
1722
- });
1723
- needAttach && document.head.appendChild(scriptEl);
1724
- });
1725
- }
1726
- }
1727
- }
1728
-
1729
1732
  function assignRemoteInfo(remoteInfo, remoteSnapshot) {
1730
1733
  const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
1731
1734
  if (!remoteEntryInfo.url) {
@@ -1931,7 +1934,7 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
1931
1934
  }
1932
1935
  }
1933
1936
  }, true, memo, remoteSnapshot);
1934
- if (remoteSnapshot.shared) {
1937
+ if (remoteSnapshot.shared && remoteSnapshot.shared.length > 0) {
1935
1938
  const collectSharedAssets = (shareInfo, snapshotShared)=>{
1936
1939
  const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.sharedHandler.hooks.lifecycle.resolveShare);
1937
1940
  // 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.
@@ -2918,7 +2921,7 @@ class RemoteHandler {
2918
2921
  }
2919
2922
 
2920
2923
  const USE_SNAPSHOT = typeof FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN === 'boolean' ? !FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN : true; // Default to true (use snapshot) when not explicitly defined
2921
- class FederationHost {
2924
+ class ModuleFederation {
2922
2925
  initOptions(userOptions) {
2923
2926
  this.registerPlugins(userOptions.plugins);
2924
2927
  const options = this.formatOptions(this.options, userOptions);
@@ -2995,14 +2998,7 @@ class FederationHost {
2995
2998
  return optionsRes;
2996
2999
  }
2997
3000
  registerPlugins(plugins) {
2998
- const pluginRes = registerPlugins(plugins, [
2999
- this.hooks,
3000
- this.remoteHandler.hooks,
3001
- this.sharedHandler.hooks,
3002
- this.snapshotHandler.hooks,
3003
- this.loaderHook,
3004
- this.bridgeHook
3005
- ]);
3001
+ const pluginRes = registerPlugins(plugins, this);
3006
3002
  // Merge plugin
3007
3003
  this.options.plugins = this.options.plugins.reduce((res, plugin)=>{
3008
3004
  if (!plugin) return res;
@@ -3015,6 +3011,11 @@ class FederationHost {
3015
3011
  registerRemotes(remotes, options) {
3016
3012
  return this.remoteHandler.registerRemotes(remotes, options);
3017
3013
  }
3014
+ registerShared(shared) {
3015
+ this.sharedHandler.registerShared(this.options, polyfills._extends({}, this.options, {
3016
+ shared
3017
+ }));
3018
+ }
3018
3019
  constructor(userOptions){
3019
3020
  this.hooks = new PluginSystem({
3020
3021
  beforeInit: new SyncWaterfallHook('beforeInit'),
@@ -3024,7 +3025,7 @@ class FederationHost {
3024
3025
  // maybe will change, temporarily for internal use only
3025
3026
  initContainer: new AsyncWaterfallHook('initContainer')
3026
3027
  });
3027
- this.version = "0.16.0";
3028
+ this.version = "0.17.0";
3028
3029
  this.moduleCache = new Map();
3029
3030
  this.loaderHook = new PluginSystem({
3030
3031
  // FIXME: may not be suitable , not open to the public yet
@@ -3076,9 +3077,9 @@ var index = /*#__PURE__*/Object.freeze({
3076
3077
  exports.loadScript = sdk.loadScript;
3077
3078
  exports.loadScriptNode = sdk.loadScriptNode;
3078
3079
  exports.CurrentGlobal = CurrentGlobal;
3079
- exports.FederationHost = FederationHost;
3080
3080
  exports.Global = Global;
3081
3081
  exports.Module = Module;
3082
+ exports.ModuleFederation = ModuleFederation;
3082
3083
  exports.addGlobalSnapshot = addGlobalSnapshot;
3083
3084
  exports.assert = assert;
3084
3085
  exports.getGlobalFederationConstructor = getGlobalFederationConstructor;