@module-federation/runtime 0.0.0-next-20240920040817 → 0.0.0-next-20240920074009

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,17 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var share = require('./share.cjs.js');
4
- require('@module-federation/sdk');
5
4
  require('./polyfills.cjs.js');
6
-
7
- var pluginHelper = /*#__PURE__*/Object.freeze({
8
- __proto__: null,
9
- SyncHook: share.SyncHook,
10
- AsyncHook: share.AsyncHook,
11
- SyncWaterfallHook: share.SyncWaterfallHook,
12
- AsyncWaterfallHook: share.AsyncWaterfallHook,
13
- PluginSystem: share.PluginSystem
14
- });
5
+ require('@module-federation/sdk');
15
6
 
16
7
  const ShareUtils = {
17
8
  getRegisteredShare: share.getRegisteredShare,
@@ -35,9 +26,7 @@ const GlobalUtils = {
35
26
  registerGlobalPlugins: share.registerGlobalPlugins,
36
27
  getGlobalHostPlugins: share.getGlobalHostPlugins,
37
28
  getPreloaded: share.getPreloaded,
38
- setPreloaded: share.setPreloaded,
39
- registerPlugins: share.registerPlugins,
40
- pluginHelper
29
+ setPreloaded: share.setPreloaded
41
30
  };
42
31
  var helpers = {
43
32
  global: GlobalUtils,
@@ -1,15 +1,6 @@
1
- import { y as SyncHook, A as AsyncHook, S as SyncWaterfallHook, r as AsyncWaterfallHook, P as PluginSystem, l as getRegisteredShare, w as getGlobalShareScope, G as Global, J as nativeGlobal, K as resetFederationGlobalInfo, E as getGlobalFederationInstance, H as setGlobalFederationInstance, F as getGlobalFederationConstructor, C as setGlobalFederationConstructor, j as getInfoWithoutType, t as getGlobalSnapshot, L as getTargetSnapshotInfoByModuleInfo, n as getGlobalSnapshotInfoByModuleInfo, q as setGlobalSnapshotInfoByModuleInfo, o as addGlobalSnapshot, b as getRemoteEntryExports, I as registerGlobalPlugins, M as getGlobalHostPlugins, k as getPreloaded, s as setPreloaded, z as registerPlugins } from './share.esm.js';
2
- import '@module-federation/sdk';
1
+ import { o as getRegisteredShare, y as getGlobalShareScope, G as Global, I as nativeGlobal, J as resetFederationGlobalInfo, C as getGlobalFederationInstance, F as setGlobalFederationInstance, E as getGlobalFederationConstructor, B as setGlobalFederationConstructor, m as getInfoWithoutType, u as getGlobalSnapshot, K as getTargetSnapshotInfoByModuleInfo, q as getGlobalSnapshotInfoByModuleInfo, t as setGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, c as getRemoteEntryExports, H as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, s as setPreloaded } from './share.esm.js';
3
2
  import './polyfills.esm.js';
4
-
5
- var pluginHelper = /*#__PURE__*/Object.freeze({
6
- __proto__: null,
7
- SyncHook: SyncHook,
8
- AsyncHook: AsyncHook,
9
- SyncWaterfallHook: SyncWaterfallHook,
10
- AsyncWaterfallHook: AsyncWaterfallHook,
11
- PluginSystem: PluginSystem
12
- });
3
+ import '@module-federation/sdk';
13
4
 
14
5
  const ShareUtils = {
15
6
  getRegisteredShare,
@@ -33,9 +24,7 @@ const GlobalUtils = {
33
24
  registerGlobalPlugins,
34
25
  getGlobalHostPlugins,
35
26
  getPreloaded,
36
- setPreloaded,
37
- registerPlugins,
38
- pluginHelper
27
+ setPreloaded
39
28
  };
40
29
  var helpers = {
41
30
  global: GlobalUtils,
package/dist/index.cjs.js CHANGED
@@ -70,6 +70,26 @@ function matchRemote(remotes, nameOrAlias) {
70
70
  return;
71
71
  }
72
72
 
73
+ function registerPlugins$1(plugins, hookInstances) {
74
+ const globalPlugins = share.getGlobalHostPlugins();
75
+ // Incorporate global plugins
76
+ if (globalPlugins.length > 0) {
77
+ globalPlugins.forEach((plugin)=>{
78
+ if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
79
+ plugins.push(plugin);
80
+ }
81
+ });
82
+ }
83
+ if (plugins && plugins.length > 0) {
84
+ plugins.forEach((plugin)=>{
85
+ hookInstances.forEach((hookInstance)=>{
86
+ hookInstance.applyPlugin(plugin);
87
+ });
88
+ });
89
+ }
90
+ return plugins;
91
+ }
92
+
73
93
  async function loadEsmEntry({ entry, remoteEntryExports }) {
74
94
  return new Promise((resolve, reject)=>{
75
95
  try {
@@ -302,9 +322,7 @@ let Module = class Module {
302
322
  // get exposeGetter
303
323
  const moduleFactory = await remoteEntryExports.get(expose);
304
324
  share.assert(moduleFactory, `${share.getFMId(this.remoteInfo)} remote don't export ${expose}.`);
305
- // keep symbol for module name always one format
306
- const symbolName = share.processModuleAlias(this.remoteInfo.name, expose);
307
- const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);
325
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
308
326
  if (!loadFactory) {
309
327
  return wrapModuleFactory;
310
328
  }
@@ -344,6 +362,193 @@ let Module = class Module {
344
362
  }
345
363
  };
346
364
 
365
+ class SyncHook {
366
+ on(fn) {
367
+ if (typeof fn === 'function') {
368
+ this.listeners.add(fn);
369
+ }
370
+ }
371
+ once(fn) {
372
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
373
+ const self = this;
374
+ this.on(function wrapper(...args) {
375
+ self.remove(wrapper);
376
+ // eslint-disable-next-line prefer-spread
377
+ return fn.apply(null, args);
378
+ });
379
+ }
380
+ emit(...data) {
381
+ let result;
382
+ if (this.listeners.size > 0) {
383
+ // eslint-disable-next-line prefer-spread
384
+ this.listeners.forEach((fn)=>{
385
+ result = fn(...data);
386
+ });
387
+ }
388
+ return result;
389
+ }
390
+ remove(fn) {
391
+ this.listeners.delete(fn);
392
+ }
393
+ removeAll() {
394
+ this.listeners.clear();
395
+ }
396
+ constructor(type){
397
+ this.type = '';
398
+ this.listeners = new Set();
399
+ if (type) {
400
+ this.type = type;
401
+ }
402
+ }
403
+ }
404
+
405
+ class AsyncHook extends SyncHook {
406
+ emit(...data) {
407
+ let result;
408
+ const ls = Array.from(this.listeners);
409
+ if (ls.length > 0) {
410
+ let i = 0;
411
+ const call = (prev)=>{
412
+ if (prev === false) {
413
+ return false; // Abort process
414
+ } else if (i < ls.length) {
415
+ return Promise.resolve(ls[i++].apply(null, data)).then(call);
416
+ } else {
417
+ return prev;
418
+ }
419
+ };
420
+ result = call();
421
+ }
422
+ return Promise.resolve(result);
423
+ }
424
+ }
425
+
426
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
427
+ function checkReturnData(originalData, returnedData) {
428
+ if (!share.isObject(returnedData)) {
429
+ return false;
430
+ }
431
+ if (originalData !== returnedData) {
432
+ // eslint-disable-next-line no-restricted-syntax
433
+ for(const key in originalData){
434
+ if (!(key in returnedData)) {
435
+ return false;
436
+ }
437
+ }
438
+ }
439
+ return true;
440
+ }
441
+ class SyncWaterfallHook extends SyncHook {
442
+ emit(data) {
443
+ if (!share.isObject(data)) {
444
+ share.error(`The data for the "${this.type}" hook should be an object.`);
445
+ }
446
+ for (const fn of this.listeners){
447
+ try {
448
+ const tempData = fn(data);
449
+ if (checkReturnData(data, tempData)) {
450
+ data = tempData;
451
+ } else {
452
+ this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
453
+ break;
454
+ }
455
+ } catch (e) {
456
+ share.warn(e);
457
+ this.onerror(e);
458
+ }
459
+ }
460
+ return data;
461
+ }
462
+ constructor(type){
463
+ super();
464
+ this.onerror = share.error;
465
+ this.type = type;
466
+ }
467
+ }
468
+
469
+ class AsyncWaterfallHook extends SyncHook {
470
+ emit(data) {
471
+ if (!share.isObject(data)) {
472
+ share.error(`The response data for the "${this.type}" hook must be an object.`);
473
+ }
474
+ const ls = Array.from(this.listeners);
475
+ if (ls.length > 0) {
476
+ let i = 0;
477
+ const processError = (e)=>{
478
+ share.warn(e);
479
+ this.onerror(e);
480
+ return data;
481
+ };
482
+ const call = (prevData)=>{
483
+ if (checkReturnData(data, prevData)) {
484
+ data = prevData;
485
+ if (i < ls.length) {
486
+ try {
487
+ return Promise.resolve(ls[i++](data)).then(call, processError);
488
+ } catch (e) {
489
+ return processError(e);
490
+ }
491
+ }
492
+ } else {
493
+ this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
494
+ }
495
+ return data;
496
+ };
497
+ return Promise.resolve(call(data));
498
+ }
499
+ return Promise.resolve(data);
500
+ }
501
+ constructor(type){
502
+ super();
503
+ this.onerror = share.error;
504
+ this.type = type;
505
+ }
506
+ }
507
+
508
+ class PluginSystem {
509
+ applyPlugin(plugin) {
510
+ share.assert(share.isPlainObject(plugin), 'Plugin configuration is invalid.');
511
+ // The plugin's name is mandatory and must be unique
512
+ const pluginName = plugin.name;
513
+ share.assert(pluginName, 'A name must be provided by the plugin.');
514
+ if (!this.registerPlugins[pluginName]) {
515
+ this.registerPlugins[pluginName] = plugin;
516
+ Object.keys(this.lifecycle).forEach((key)=>{
517
+ const pluginLife = plugin[key];
518
+ if (pluginLife) {
519
+ this.lifecycle[key].on(pluginLife);
520
+ }
521
+ });
522
+ }
523
+ }
524
+ removePlugin(pluginName) {
525
+ share.assert(pluginName, 'A name is required.');
526
+ const plugin = this.registerPlugins[pluginName];
527
+ share.assert(plugin, `The plugin "${pluginName}" is not registered.`);
528
+ Object.keys(plugin).forEach((key)=>{
529
+ if (key !== 'name') {
530
+ this.lifecycle[key].remove(plugin[key]);
531
+ }
532
+ });
533
+ }
534
+ // eslint-disable-next-line @typescript-eslint/no-shadow
535
+ inherit({ lifecycle, registerPlugins }) {
536
+ Object.keys(lifecycle).forEach((hookName)=>{
537
+ share.assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
538
+ this.lifecycle[hookName] = lifecycle[hookName];
539
+ });
540
+ Object.keys(registerPlugins).forEach((pluginName)=>{
541
+ share.assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
542
+ this.applyPlugin(registerPlugins[pluginName]);
543
+ });
544
+ }
545
+ constructor(lifecycle){
546
+ this.registerPlugins = {};
547
+ this.lifecycle = lifecycle;
548
+ this.lifecycleKeys = Object.keys(lifecycle);
549
+ }
550
+ }
551
+
347
552
  function defaultPreloadArgs(preloadConfig) {
348
553
  return polyfills._extends({
349
554
  resourceCategory: 'sync',
@@ -851,8 +1056,6 @@ class SnapshotHandler {
851
1056
  remoteSnapshot,
852
1057
  globalSnapshot
853
1058
  });
854
- let mSnapshot;
855
- let gSnapshot;
856
1059
  // global snapshot includes manifest or module info includes manifest
857
1060
  if (globalRemoteSnapshot) {
858
1061
  if (sdk.isManifestProvider(globalRemoteSnapshot)) {
@@ -864,8 +1067,10 @@ class SnapshotHandler {
864
1067
  // Therefore, set the snapshot key to the global address of the actual request
865
1068
  entry: remoteEntry
866
1069
  }), moduleSnapshot);
867
- mSnapshot = moduleSnapshot;
868
- gSnapshot = globalSnapshotRes;
1070
+ return {
1071
+ remoteSnapshot: moduleSnapshot,
1072
+ globalSnapshot: globalSnapshotRes
1073
+ };
869
1074
  } else {
870
1075
  const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
871
1076
  options: this.HostInstance.options,
@@ -873,8 +1078,10 @@ class SnapshotHandler {
873
1078
  remoteSnapshot: globalRemoteSnapshot,
874
1079
  from: 'global'
875
1080
  });
876
- mSnapshot = remoteSnapshotRes;
877
- gSnapshot = globalSnapshotRes;
1081
+ return {
1082
+ remoteSnapshot: remoteSnapshotRes,
1083
+ globalSnapshot: globalSnapshotRes
1084
+ };
878
1085
  }
879
1086
  } else {
880
1087
  if (share.isRemoteInfoWithEntry(moduleInfo)) {
@@ -888,8 +1095,10 @@ class SnapshotHandler {
888
1095
  remoteSnapshot: moduleSnapshot,
889
1096
  from: 'global'
890
1097
  });
891
- mSnapshot = remoteSnapshotRes;
892
- gSnapshot = globalSnapshotRes;
1098
+ return {
1099
+ remoteSnapshot: remoteSnapshotRes,
1100
+ globalSnapshot: globalSnapshotRes
1101
+ };
893
1102
  } else {
894
1103
  share.error(`
895
1104
  Cannot get remoteSnapshot with the name: '${moduleInfo.name}', version: '${moduleInfo.version}' from __FEDERATION__.moduleInfo. The following reasons may be causing the problem:\n
@@ -899,15 +1108,6 @@ class SnapshotHandler {
899
1108
  `);
900
1109
  }
901
1110
  }
902
- await this.hooks.lifecycle.afterLoadSnapshot.emit({
903
- options,
904
- moduleInfo,
905
- remoteSnapshot: mSnapshot
906
- });
907
- return {
908
- remoteSnapshot: mSnapshot,
909
- globalSnapshot: gSnapshot
910
- };
911
1111
  }
912
1112
  getGlobalRemoteInfo(moduleInfo) {
913
1113
  return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
@@ -957,11 +1157,10 @@ class SnapshotHandler {
957
1157
  constructor(HostInstance){
958
1158
  this.loadingHostSnapshot = null;
959
1159
  this.manifestCache = new Map();
960
- this.hooks = new share.PluginSystem({
961
- beforeLoadRemoteSnapshot: new share.AsyncHook('beforeLoadRemoteSnapshot'),
962
- loadSnapshot: new share.AsyncWaterfallHook('loadGlobalSnapshot'),
963
- loadRemoteSnapshot: new share.AsyncWaterfallHook('loadRemoteSnapshot'),
964
- afterLoadSnapshot: new share.AsyncWaterfallHook('afterLoadSnapshot')
1160
+ this.hooks = new PluginSystem({
1161
+ beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
1162
+ loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
1163
+ loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
965
1164
  });
966
1165
  this.manifestLoading = share.Global.__FEDERATION__.__MANIFEST_LOADING__;
967
1166
  this.HostInstance = HostInstance;
@@ -1304,14 +1503,14 @@ class SharedHandler {
1304
1503
  }
1305
1504
  }
1306
1505
  constructor(host){
1307
- this.hooks = new share.PluginSystem({
1308
- afterResolve: new share.AsyncWaterfallHook('afterResolve'),
1309
- beforeLoadShare: new share.AsyncWaterfallHook('beforeLoadShare'),
1506
+ this.hooks = new PluginSystem({
1507
+ afterResolve: new AsyncWaterfallHook('afterResolve'),
1508
+ beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
1310
1509
  // not used yet
1311
- loadShare: new share.AsyncHook(),
1312
- resolveShare: new share.SyncWaterfallHook('resolveShare'),
1510
+ loadShare: new AsyncHook(),
1511
+ resolveShare: new SyncWaterfallHook('resolveShare'),
1313
1512
  // maybe will change, temporarily for internal use only
1314
- initContainerShareScopeMap: new share.SyncWaterfallHook('initContainerShareScopeMap')
1513
+ initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
1315
1514
  });
1316
1515
  this.host = host;
1317
1516
  this.shareScopeMap = {};
@@ -1658,18 +1857,18 @@ class RemoteHandler {
1658
1857
  }
1659
1858
  }
1660
1859
  constructor(host){
1661
- this.hooks = new share.PluginSystem({
1662
- beforeRegisterRemote: new share.SyncWaterfallHook('beforeRegisterRemote'),
1663
- registerRemote: new share.SyncWaterfallHook('registerRemote'),
1664
- beforeRequest: new share.AsyncWaterfallHook('beforeRequest'),
1665
- onLoad: new share.AsyncHook('onLoad'),
1666
- handlePreloadModule: new share.SyncHook('handlePreloadModule'),
1667
- errorLoadRemote: new share.AsyncHook('errorLoadRemote'),
1668
- beforePreloadRemote: new share.AsyncHook('beforePreloadRemote'),
1669
- generatePreloadAssets: new share.AsyncHook('generatePreloadAssets'),
1860
+ this.hooks = new PluginSystem({
1861
+ beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
1862
+ registerRemote: new SyncWaterfallHook('registerRemote'),
1863
+ beforeRequest: new AsyncWaterfallHook('beforeRequest'),
1864
+ onLoad: new AsyncHook('onLoad'),
1865
+ handlePreloadModule: new SyncHook('handlePreloadModule'),
1866
+ errorLoadRemote: new AsyncHook('errorLoadRemote'),
1867
+ beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
1868
+ generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
1670
1869
  // not used yet
1671
- afterPreloadRemote: new share.AsyncHook(),
1672
- loadEntry: new share.AsyncHook()
1870
+ afterPreloadRemote: new AsyncHook(),
1871
+ loadEntry: new AsyncHook()
1673
1872
  });
1674
1873
  this.host = host;
1675
1874
  this.idToRemoteMap = {};
@@ -1753,7 +1952,7 @@ class FederationHost {
1753
1952
  return optionsRes;
1754
1953
  }
1755
1954
  registerPlugins(plugins) {
1756
- const pluginRes = share.registerPlugins(plugins, [
1955
+ const pluginRes = registerPlugins$1(plugins, [
1757
1956
  this.hooks,
1758
1957
  this.remoteHandler.hooks,
1759
1958
  this.sharedHandler.hooks,
@@ -1773,23 +1972,23 @@ class FederationHost {
1773
1972
  return this.remoteHandler.registerRemotes(remotes, options);
1774
1973
  }
1775
1974
  constructor(userOptions){
1776
- this.hooks = new share.PluginSystem({
1777
- beforeInit: new share.SyncWaterfallHook('beforeInit'),
1778
- init: new share.SyncHook(),
1975
+ this.hooks = new PluginSystem({
1976
+ beforeInit: new SyncWaterfallHook('beforeInit'),
1977
+ init: new SyncHook(),
1779
1978
  // maybe will change, temporarily for internal use only
1780
- beforeInitContainer: new share.AsyncWaterfallHook('beforeInitContainer'),
1979
+ beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
1781
1980
  // maybe will change, temporarily for internal use only
1782
- initContainer: new share.AsyncWaterfallHook('initContainer')
1981
+ initContainer: new AsyncWaterfallHook('initContainer')
1783
1982
  });
1784
- this.version = "0.6.6";
1983
+ this.version = "0.6.5";
1785
1984
  this.moduleCache = new Map();
1786
- this.loaderHook = new share.PluginSystem({
1985
+ this.loaderHook = new PluginSystem({
1787
1986
  // FIXME: may not be suitable , not open to the public yet
1788
- getModuleInfo: new share.SyncHook(),
1789
- createScript: new share.SyncHook(),
1790
- createLink: new share.SyncHook(),
1987
+ getModuleInfo: new SyncHook(),
1988
+ createScript: new SyncHook(),
1989
+ createLink: new SyncHook(),
1791
1990
  // only work for manifest , so not open to the public yet
1792
- fetch: new share.AsyncHook()
1991
+ fetch: new AsyncHook()
1793
1992
  });
1794
1993
  // TODO: Validate the details of the options
1795
1994
  // Initialize options with default values