@module-federation/runtime 0.0.0-next-20241106033302 → 0.0.0-next-20241106104434

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.
@@ -88,6 +88,9 @@ class FederationHost {
88
88
  get loaderHook() {
89
89
  return this._getInstance().loaderHook;
90
90
  }
91
+ get bridgeHook() {
92
+ return this._getInstance().bridgeHook;
93
+ }
91
94
  initOptions(...args) {
92
95
  return this._getInstance().initOptions(...args);
93
96
  }
@@ -86,6 +86,9 @@ class FederationHost {
86
86
  get loaderHook() {
87
87
  return this._getInstance().loaderHook;
88
88
  }
89
+ get bridgeHook() {
90
+ return this._getInstance().bridgeHook;
91
+ }
89
92
  initOptions(...args) {
90
93
  return this._getInstance().initOptions(...args);
91
94
  }
@@ -1,6 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  var share = require('./share.cjs.js');
4
+ require('@module-federation/sdk');
5
+ require('@module-federation/error-codes');
6
+
7
+ var pluginHelper = /*#__PURE__*/Object.freeze({
8
+ __proto__: null,
9
+ AsyncHook: share.AsyncHook,
10
+ AsyncWaterfallHook: share.AsyncWaterfallHook,
11
+ PluginSystem: share.PluginSystem,
12
+ SyncHook: share.SyncHook,
13
+ SyncWaterfallHook: share.SyncWaterfallHook
14
+ });
4
15
 
5
16
  const ShareUtils = {
6
17
  getRegisteredShare: share.getRegisteredShare,
@@ -24,7 +35,9 @@ const GlobalUtils = {
24
35
  registerGlobalPlugins: share.registerGlobalPlugins,
25
36
  getGlobalHostPlugins: share.getGlobalHostPlugins,
26
37
  getPreloaded: share.getPreloaded,
27
- setPreloaded: share.setPreloaded
38
+ setPreloaded: share.setPreloaded,
39
+ registerPlugins: share.registerPlugins,
40
+ pluginHelper
28
41
  };
29
42
  var helpers = {
30
43
  global: GlobalUtils,
@@ -1,4 +1,15 @@
1
- import { g as getRegisteredShare, a as getGlobalShareScope, G as Global, n as nativeGlobal, r as resetFederationGlobalInfo, b as getGlobalFederationInstance, s as setGlobalFederationInstance, c as getGlobalFederationConstructor, d as setGlobalFederationConstructor, e as getInfoWithoutType, f as getGlobalSnapshot, h as getTargetSnapshotInfoByModuleInfo, i as getGlobalSnapshotInfoByModuleInfo, j as setGlobalSnapshotInfoByModuleInfo, k as addGlobalSnapshot, l as getRemoteEntryExports, m as registerGlobalPlugins, o as getGlobalHostPlugins, p as getPreloaded, q as setPreloaded } from './share.esm.mjs';
1
+ import { A as AsyncHook, a as AsyncWaterfallHook, P as PluginSystem, S as SyncHook, b as SyncWaterfallHook, g as getRegisteredShare, c as getGlobalShareScope, G as Global, n as nativeGlobal, r as resetFederationGlobalInfo, d as getGlobalFederationInstance, s as setGlobalFederationInstance, e as getGlobalFederationConstructor, f as setGlobalFederationConstructor, h as getInfoWithoutType, i as getGlobalSnapshot, j as getTargetSnapshotInfoByModuleInfo, k as getGlobalSnapshotInfoByModuleInfo, l as setGlobalSnapshotInfoByModuleInfo, m as addGlobalSnapshot, o as getRemoteEntryExports, p as registerGlobalPlugins, q as getGlobalHostPlugins, t as getPreloaded, u as setPreloaded, v as registerPlugins } from './share.esm.mjs';
2
+ import '@module-federation/sdk';
3
+ import '@module-federation/error-codes';
4
+
5
+ var pluginHelper = /*#__PURE__*/Object.freeze({
6
+ __proto__: null,
7
+ AsyncHook: AsyncHook,
8
+ AsyncWaterfallHook: AsyncWaterfallHook,
9
+ PluginSystem: PluginSystem,
10
+ SyncHook: SyncHook,
11
+ SyncWaterfallHook: SyncWaterfallHook
12
+ });
2
13
 
3
14
  const ShareUtils = {
4
15
  getRegisteredShare,
@@ -22,7 +33,9 @@ const GlobalUtils = {
22
33
  registerGlobalPlugins,
23
34
  getGlobalHostPlugins,
24
35
  getPreloaded,
25
- setPreloaded
36
+ setPreloaded,
37
+ registerPlugins,
38
+ pluginHelper
26
39
  };
27
40
  var helpers = {
28
41
  global: GlobalUtils,
package/dist/index.cjs.js CHANGED
@@ -69,26 +69,6 @@ function matchRemote(remotes, nameOrAlias) {
69
69
  return;
70
70
  }
71
71
 
72
- function registerPlugins$1(plugins, hookInstances) {
73
- const globalPlugins = share.getGlobalHostPlugins();
74
- // Incorporate global plugins
75
- if (globalPlugins.length > 0) {
76
- globalPlugins.forEach((plugin)=>{
77
- if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
78
- plugins.push(plugin);
79
- }
80
- });
81
- }
82
- if (plugins && plugins.length > 0) {
83
- plugins.forEach((plugin)=>{
84
- hookInstances.forEach((hookInstance)=>{
85
- hookInstance.applyPlugin(plugin);
86
- });
87
- });
88
- }
89
- return plugins;
90
- }
91
-
92
72
  async function loadEsmEntry({ entry, remoteEntryExports }) {
93
73
  return new Promise((resolve, reject)=>{
94
74
  try {
@@ -333,7 +313,9 @@ let Module = class Module {
333
313
  moduleFactory = await remoteEntryExports.get(expose);
334
314
  }
335
315
  share.assert(moduleFactory, `${share.getFMId(this.remoteInfo)} remote don't export ${expose}.`);
336
- const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
316
+ // keep symbol for module name always one format
317
+ const symbolName = share.processModuleAlias(this.remoteInfo.name, expose);
318
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);
337
319
  if (!loadFactory) {
338
320
  return wrapModuleFactory;
339
321
  }
@@ -373,191 +355,6 @@ let Module = class Module {
373
355
  }
374
356
  };
375
357
 
376
- class SyncHook {
377
- on(fn) {
378
- if (typeof fn === 'function') {
379
- this.listeners.add(fn);
380
- }
381
- }
382
- once(fn) {
383
- // eslint-disable-next-line @typescript-eslint/no-this-alias
384
- const self = this;
385
- this.on(function wrapper(...args) {
386
- self.remove(wrapper);
387
- // eslint-disable-next-line prefer-spread
388
- return fn.apply(null, args);
389
- });
390
- }
391
- emit(...data) {
392
- let result;
393
- if (this.listeners.size > 0) {
394
- // eslint-disable-next-line prefer-spread
395
- this.listeners.forEach((fn)=>{
396
- result = fn(...data);
397
- });
398
- }
399
- return result;
400
- }
401
- remove(fn) {
402
- this.listeners.delete(fn);
403
- }
404
- removeAll() {
405
- this.listeners.clear();
406
- }
407
- constructor(type){
408
- this.type = '';
409
- this.listeners = new Set();
410
- if (type) {
411
- this.type = type;
412
- }
413
- }
414
- }
415
-
416
- class AsyncHook extends SyncHook {
417
- emit(...data) {
418
- let result;
419
- const ls = Array.from(this.listeners);
420
- if (ls.length > 0) {
421
- let i = 0;
422
- const call = (prev)=>{
423
- if (prev === false) {
424
- return false; // Abort process
425
- } else if (i < ls.length) {
426
- return Promise.resolve(ls[i++].apply(null, data)).then(call);
427
- } else {
428
- return prev;
429
- }
430
- };
431
- result = call();
432
- }
433
- return Promise.resolve(result);
434
- }
435
- }
436
-
437
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
438
- function checkReturnData(originalData, returnedData) {
439
- if (!share.isObject(returnedData)) {
440
- return false;
441
- }
442
- if (originalData !== returnedData) {
443
- // eslint-disable-next-line no-restricted-syntax
444
- for(const key in originalData){
445
- if (!(key in returnedData)) {
446
- return false;
447
- }
448
- }
449
- }
450
- return true;
451
- }
452
- class SyncWaterfallHook extends SyncHook {
453
- emit(data) {
454
- if (!share.isObject(data)) {
455
- share.error(`The data for the "${this.type}" hook should be an object.`);
456
- }
457
- for (const fn of this.listeners){
458
- try {
459
- const tempData = fn(data);
460
- if (checkReturnData(data, tempData)) {
461
- data = tempData;
462
- } else {
463
- this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
464
- break;
465
- }
466
- } catch (e) {
467
- share.warn(e);
468
- this.onerror(e);
469
- }
470
- }
471
- return data;
472
- }
473
- constructor(type){
474
- super(), this.onerror = share.error;
475
- this.type = type;
476
- }
477
- }
478
-
479
- class AsyncWaterfallHook extends SyncHook {
480
- emit(data) {
481
- if (!share.isObject(data)) {
482
- share.error(`The response data for the "${this.type}" hook must be an object.`);
483
- }
484
- const ls = Array.from(this.listeners);
485
- if (ls.length > 0) {
486
- let i = 0;
487
- const processError = (e)=>{
488
- share.warn(e);
489
- this.onerror(e);
490
- return data;
491
- };
492
- const call = (prevData)=>{
493
- if (checkReturnData(data, prevData)) {
494
- data = prevData;
495
- if (i < ls.length) {
496
- try {
497
- return Promise.resolve(ls[i++](data)).then(call, processError);
498
- } catch (e) {
499
- return processError(e);
500
- }
501
- }
502
- } else {
503
- this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
504
- }
505
- return data;
506
- };
507
- return Promise.resolve(call(data));
508
- }
509
- return Promise.resolve(data);
510
- }
511
- constructor(type){
512
- super(), this.onerror = share.error;
513
- this.type = type;
514
- }
515
- }
516
-
517
- class PluginSystem {
518
- applyPlugin(plugin) {
519
- share.assert(share.isPlainObject(plugin), 'Plugin configuration is invalid.');
520
- // The plugin's name is mandatory and must be unique
521
- const pluginName = plugin.name;
522
- share.assert(pluginName, 'A name must be provided by the plugin.');
523
- if (!this.registerPlugins[pluginName]) {
524
- this.registerPlugins[pluginName] = plugin;
525
- Object.keys(this.lifecycle).forEach((key)=>{
526
- const pluginLife = plugin[key];
527
- if (pluginLife) {
528
- this.lifecycle[key].on(pluginLife);
529
- }
530
- });
531
- }
532
- }
533
- removePlugin(pluginName) {
534
- share.assert(pluginName, 'A name is required.');
535
- const plugin = this.registerPlugins[pluginName];
536
- share.assert(plugin, `The plugin "${pluginName}" is not registered.`);
537
- Object.keys(plugin).forEach((key)=>{
538
- if (key !== 'name') {
539
- this.lifecycle[key].remove(plugin[key]);
540
- }
541
- });
542
- }
543
- // eslint-disable-next-line @typescript-eslint/no-shadow
544
- inherit({ lifecycle, registerPlugins }) {
545
- Object.keys(lifecycle).forEach((hookName)=>{
546
- share.assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
547
- this.lifecycle[hookName] = lifecycle[hookName];
548
- });
549
- Object.keys(registerPlugins).forEach((pluginName)=>{
550
- share.assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
551
- this.applyPlugin(registerPlugins[pluginName]);
552
- });
553
- }
554
- constructor(lifecycle){
555
- this.registerPlugins = {};
556
- this.lifecycle = lifecycle;
557
- this.lifecycleKeys = Object.keys(lifecycle);
558
- }
559
- }
560
-
561
358
  function defaultPreloadArgs(preloadConfig) {
562
359
  return polyfills._extends({
563
360
  resourceCategory: 'sync',
@@ -1071,6 +868,8 @@ class SnapshotHandler {
1071
868
  remoteSnapshot,
1072
869
  globalSnapshot
1073
870
  });
871
+ let mSnapshot;
872
+ let gSnapshot;
1074
873
  // global snapshot includes manifest or module info includes manifest
1075
874
  if (globalRemoteSnapshot) {
1076
875
  if (sdk.isManifestProvider(globalRemoteSnapshot)) {
@@ -1082,10 +881,8 @@ class SnapshotHandler {
1082
881
  // Therefore, set the snapshot key to the global address of the actual request
1083
882
  entry: remoteEntry
1084
883
  }), moduleSnapshot);
1085
- return {
1086
- remoteSnapshot: moduleSnapshot,
1087
- globalSnapshot: globalSnapshotRes
1088
- };
884
+ mSnapshot = moduleSnapshot;
885
+ gSnapshot = globalSnapshotRes;
1089
886
  } else {
1090
887
  const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
1091
888
  options: this.HostInstance.options,
@@ -1093,10 +890,8 @@ class SnapshotHandler {
1093
890
  remoteSnapshot: globalRemoteSnapshot,
1094
891
  from: 'global'
1095
892
  });
1096
- return {
1097
- remoteSnapshot: remoteSnapshotRes,
1098
- globalSnapshot: globalSnapshotRes
1099
- };
893
+ mSnapshot = remoteSnapshotRes;
894
+ gSnapshot = globalSnapshotRes;
1100
895
  }
1101
896
  } else {
1102
897
  if (share.isRemoteInfoWithEntry(moduleInfo)) {
@@ -1110,10 +905,8 @@ class SnapshotHandler {
1110
905
  remoteSnapshot: moduleSnapshot,
1111
906
  from: 'global'
1112
907
  });
1113
- return {
1114
- remoteSnapshot: remoteSnapshotRes,
1115
- globalSnapshot: globalSnapshotRes
1116
- };
908
+ mSnapshot = remoteSnapshotRes;
909
+ gSnapshot = globalSnapshotRes;
1117
910
  } else {
1118
911
  share.error(errorCodes.getShortErrorMsg(errorCodes.RUNTIME_007, errorCodes.runtimeDescMap, {
1119
912
  hostName: moduleInfo.name,
@@ -1122,6 +915,15 @@ class SnapshotHandler {
1122
915
  }));
1123
916
  }
1124
917
  }
918
+ await this.hooks.lifecycle.afterLoadSnapshot.emit({
919
+ options,
920
+ moduleInfo,
921
+ remoteSnapshot: mSnapshot
922
+ });
923
+ return {
924
+ remoteSnapshot: mSnapshot,
925
+ globalSnapshot: gSnapshot
926
+ };
1125
927
  }
1126
928
  getGlobalRemoteInfo(moduleInfo) {
1127
929
  return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
@@ -1172,10 +974,11 @@ class SnapshotHandler {
1172
974
  constructor(HostInstance){
1173
975
  this.loadingHostSnapshot = null;
1174
976
  this.manifestCache = new Map();
1175
- this.hooks = new PluginSystem({
1176
- beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
1177
- loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
1178
- loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
977
+ this.hooks = new share.PluginSystem({
978
+ beforeLoadRemoteSnapshot: new share.AsyncHook('beforeLoadRemoteSnapshot'),
979
+ loadSnapshot: new share.AsyncWaterfallHook('loadGlobalSnapshot'),
980
+ loadRemoteSnapshot: new share.AsyncWaterfallHook('loadRemoteSnapshot'),
981
+ afterLoadSnapshot: new share.AsyncWaterfallHook('afterLoadSnapshot')
1179
982
  });
1180
983
  this.manifestLoading = share.Global.__FEDERATION__.__MANIFEST_LOADING__;
1181
984
  this.HostInstance = HostInstance;
@@ -1526,14 +1329,14 @@ class SharedHandler {
1526
1329
  }
1527
1330
  }
1528
1331
  constructor(host){
1529
- this.hooks = new PluginSystem({
1530
- afterResolve: new AsyncWaterfallHook('afterResolve'),
1531
- beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
1332
+ this.hooks = new share.PluginSystem({
1333
+ afterResolve: new share.AsyncWaterfallHook('afterResolve'),
1334
+ beforeLoadShare: new share.AsyncWaterfallHook('beforeLoadShare'),
1532
1335
  // not used yet
1533
- loadShare: new AsyncHook(),
1534
- resolveShare: new SyncWaterfallHook('resolveShare'),
1336
+ loadShare: new share.AsyncHook(),
1337
+ resolveShare: new share.SyncWaterfallHook('resolveShare'),
1535
1338
  // maybe will change, temporarily for internal use only
1536
- initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
1339
+ initContainerShareScopeMap: new share.SyncWaterfallHook('initContainerShareScopeMap')
1537
1340
  });
1538
1341
  this.host = host;
1539
1342
  this.shareScopeMap = {};
@@ -1876,18 +1679,18 @@ class RemoteHandler {
1876
1679
  }
1877
1680
  }
1878
1681
  constructor(host){
1879
- this.hooks = new PluginSystem({
1880
- beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
1881
- registerRemote: new SyncWaterfallHook('registerRemote'),
1882
- beforeRequest: new AsyncWaterfallHook('beforeRequest'),
1883
- onLoad: new AsyncHook('onLoad'),
1884
- handlePreloadModule: new SyncHook('handlePreloadModule'),
1885
- errorLoadRemote: new AsyncHook('errorLoadRemote'),
1886
- beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
1887
- generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
1682
+ this.hooks = new share.PluginSystem({
1683
+ beforeRegisterRemote: new share.SyncWaterfallHook('beforeRegisterRemote'),
1684
+ registerRemote: new share.SyncWaterfallHook('registerRemote'),
1685
+ beforeRequest: new share.AsyncWaterfallHook('beforeRequest'),
1686
+ onLoad: new share.AsyncHook('onLoad'),
1687
+ handlePreloadModule: new share.SyncHook('handlePreloadModule'),
1688
+ errorLoadRemote: new share.AsyncHook('errorLoadRemote'),
1689
+ beforePreloadRemote: new share.AsyncHook('beforePreloadRemote'),
1690
+ generatePreloadAssets: new share.AsyncHook('generatePreloadAssets'),
1888
1691
  // not used yet
1889
- afterPreloadRemote: new AsyncHook(),
1890
- loadEntry: new AsyncHook()
1692
+ afterPreloadRemote: new share.AsyncHook(),
1693
+ loadEntry: new share.AsyncHook()
1891
1694
  });
1892
1695
  this.host = host;
1893
1696
  this.idToRemoteMap = {};
@@ -1971,12 +1774,13 @@ class FederationHost {
1971
1774
  return optionsRes;
1972
1775
  }
1973
1776
  registerPlugins(plugins) {
1974
- const pluginRes = registerPlugins$1(plugins, [
1777
+ const pluginRes = share.registerPlugins(plugins, [
1975
1778
  this.hooks,
1976
1779
  this.remoteHandler.hooks,
1977
1780
  this.sharedHandler.hooks,
1978
1781
  this.snapshotHandler.hooks,
1979
- this.loaderHook
1782
+ this.loaderHook,
1783
+ this.bridgeHook
1980
1784
  ]);
1981
1785
  // Merge plugin
1982
1786
  this.options.plugins = this.options.plugins.reduce((res, plugin)=>{
@@ -1991,24 +1795,30 @@ class FederationHost {
1991
1795
  return this.remoteHandler.registerRemotes(remotes, options);
1992
1796
  }
1993
1797
  constructor(userOptions){
1994
- this.hooks = new PluginSystem({
1995
- beforeInit: new SyncWaterfallHook('beforeInit'),
1996
- init: new SyncHook(),
1798
+ this.hooks = new share.PluginSystem({
1799
+ beforeInit: new share.SyncWaterfallHook('beforeInit'),
1800
+ init: new share.SyncHook(),
1997
1801
  // maybe will change, temporarily for internal use only
1998
- beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
1802
+ beforeInitContainer: new share.AsyncWaterfallHook('beforeInitContainer'),
1999
1803
  // maybe will change, temporarily for internal use only
2000
- initContainer: new AsyncWaterfallHook('initContainer')
1804
+ initContainer: new share.AsyncWaterfallHook('initContainer')
2001
1805
  });
2002
1806
  this.version = "0.7.0";
2003
1807
  this.moduleCache = new Map();
2004
- this.loaderHook = new PluginSystem({
1808
+ this.loaderHook = new share.PluginSystem({
2005
1809
  // FIXME: may not be suitable , not open to the public yet
2006
- getModuleInfo: new SyncHook(),
2007
- createScript: new SyncHook(),
2008
- createLink: new SyncHook(),
1810
+ getModuleInfo: new share.SyncHook(),
1811
+ createScript: new share.SyncHook(),
1812
+ createLink: new share.SyncHook(),
2009
1813
  // only work for manifest , so not open to the public yet
2010
- fetch: new AsyncHook(),
2011
- getModuleFactory: new AsyncHook()
1814
+ fetch: new share.AsyncHook(),
1815
+ getModuleFactory: new share.AsyncHook()
1816
+ });
1817
+ this.bridgeHook = new share.PluginSystem({
1818
+ beforeBridgeRender: new share.SyncHook(),
1819
+ afterBridgeRender: new share.SyncHook(),
1820
+ beforeBridgeDestroy: new share.SyncHook(),
1821
+ afterBridgeDestroy: new share.SyncHook()
2012
1822
  });
2013
1823
  // TODO: Validate the details of the options
2014
1824
  // Initialize options with default values