@module-federation/runtime 0.0.0-next-20241018073700 → 0.0.0-next-20241021083129

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,8 +1,17 @@
1
1
  'use strict';
2
2
 
3
3
  var share = require('./share.cjs.js');
4
- require('./polyfills.cjs.js');
5
4
  require('@module-federation/sdk');
5
+ require('./polyfills.cjs.js');
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
+ });
6
15
 
7
16
  const ShareUtils = {
8
17
  getRegisteredShare: share.getRegisteredShare,
@@ -26,7 +35,9 @@ const GlobalUtils = {
26
35
  registerGlobalPlugins: share.registerGlobalPlugins,
27
36
  getGlobalHostPlugins: share.getGlobalHostPlugins,
28
37
  getPreloaded: share.getPreloaded,
29
- setPreloaded: share.setPreloaded
38
+ setPreloaded: share.setPreloaded,
39
+ registerPlugins: share.registerPlugins,
40
+ pluginHelper
30
41
  };
31
42
  var helpers = {
32
43
  global: GlobalUtils,
@@ -1,6 +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.js';
2
- import './polyfills.esm.js';
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.js';
3
2
  import '@module-federation/sdk';
3
+ import './polyfills.esm.js';
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
+ });
4
13
 
5
14
  const ShareUtils = {
6
15
  getRegisteredShare,
@@ -24,7 +33,9 @@ const GlobalUtils = {
24
33
  registerGlobalPlugins,
25
34
  getGlobalHostPlugins,
26
35
  getPreloaded,
27
- setPreloaded
36
+ setPreloaded,
37
+ registerPlugins,
38
+ pluginHelper
28
39
  };
29
40
  var helpers = {
30
41
  global: GlobalUtils,
package/dist/index.cjs.js CHANGED
@@ -68,26 +68,6 @@ function matchRemote(remotes, nameOrAlias) {
68
68
  return;
69
69
  }
70
70
 
71
- function registerPlugins$1(plugins, hookInstances) {
72
- const globalPlugins = share.getGlobalHostPlugins();
73
- // Incorporate global plugins
74
- if (globalPlugins.length > 0) {
75
- globalPlugins.forEach((plugin)=>{
76
- if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
77
- plugins.push(plugin);
78
- }
79
- });
80
- }
81
- if (plugins && plugins.length > 0) {
82
- plugins.forEach((plugin)=>{
83
- hookInstances.forEach((hookInstance)=>{
84
- hookInstance.applyPlugin(plugin);
85
- });
86
- });
87
- }
88
- return plugins;
89
- }
90
-
91
71
  async function loadEsmEntry({ entry, remoteEntryExports }) {
92
72
  return new Promise((resolve, reject)=>{
93
73
  try {
@@ -319,7 +299,9 @@ let Module = class Module {
319
299
  // get exposeGetter
320
300
  const moduleFactory = await remoteEntryExports.get(expose);
321
301
  share.assert(moduleFactory, `${share.getFMId(this.remoteInfo)} remote don't export ${expose}.`);
322
- const wrapModuleFactory = this.wraperFactory(moduleFactory, id);
302
+ // keep symbol for module name always one format
303
+ const symbolName = share.processModuleAlias(this.remoteInfo.name, expose);
304
+ const wrapModuleFactory = this.wraperFactory(moduleFactory, symbolName);
323
305
  if (!loadFactory) {
324
306
  return wrapModuleFactory;
325
307
  }
@@ -359,191 +341,6 @@ let Module = class Module {
359
341
  }
360
342
  };
361
343
 
362
- class SyncHook {
363
- on(fn) {
364
- if (typeof fn === 'function') {
365
- this.listeners.add(fn);
366
- }
367
- }
368
- once(fn) {
369
- // eslint-disable-next-line @typescript-eslint/no-this-alias
370
- const self = this;
371
- this.on(function wrapper(...args) {
372
- self.remove(wrapper);
373
- // eslint-disable-next-line prefer-spread
374
- return fn.apply(null, args);
375
- });
376
- }
377
- emit(...data) {
378
- let result;
379
- if (this.listeners.size > 0) {
380
- // eslint-disable-next-line prefer-spread
381
- this.listeners.forEach((fn)=>{
382
- result = fn(...data);
383
- });
384
- }
385
- return result;
386
- }
387
- remove(fn) {
388
- this.listeners.delete(fn);
389
- }
390
- removeAll() {
391
- this.listeners.clear();
392
- }
393
- constructor(type){
394
- this.type = '';
395
- this.listeners = new Set();
396
- if (type) {
397
- this.type = type;
398
- }
399
- }
400
- }
401
-
402
- class AsyncHook extends SyncHook {
403
- emit(...data) {
404
- let result;
405
- const ls = Array.from(this.listeners);
406
- if (ls.length > 0) {
407
- let i = 0;
408
- const call = (prev)=>{
409
- if (prev === false) {
410
- return false; // Abort process
411
- } else if (i < ls.length) {
412
- return Promise.resolve(ls[i++].apply(null, data)).then(call);
413
- } else {
414
- return prev;
415
- }
416
- };
417
- result = call();
418
- }
419
- return Promise.resolve(result);
420
- }
421
- }
422
-
423
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
424
- function checkReturnData(originalData, returnedData) {
425
- if (!share.isObject(returnedData)) {
426
- return false;
427
- }
428
- if (originalData !== returnedData) {
429
- // eslint-disable-next-line no-restricted-syntax
430
- for(const key in originalData){
431
- if (!(key in returnedData)) {
432
- return false;
433
- }
434
- }
435
- }
436
- return true;
437
- }
438
- class SyncWaterfallHook extends SyncHook {
439
- emit(data) {
440
- if (!share.isObject(data)) {
441
- share.error(`The data for the "${this.type}" hook should be an object.`);
442
- }
443
- for (const fn of this.listeners){
444
- try {
445
- const tempData = fn(data);
446
- if (checkReturnData(data, tempData)) {
447
- data = tempData;
448
- } else {
449
- this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
450
- break;
451
- }
452
- } catch (e) {
453
- share.warn(e);
454
- this.onerror(e);
455
- }
456
- }
457
- return data;
458
- }
459
- constructor(type){
460
- super(), this.onerror = share.error;
461
- this.type = type;
462
- }
463
- }
464
-
465
- class AsyncWaterfallHook extends SyncHook {
466
- emit(data) {
467
- if (!share.isObject(data)) {
468
- share.error(`The response data for the "${this.type}" hook must be an object.`);
469
- }
470
- const ls = Array.from(this.listeners);
471
- if (ls.length > 0) {
472
- let i = 0;
473
- const processError = (e)=>{
474
- share.warn(e);
475
- this.onerror(e);
476
- return data;
477
- };
478
- const call = (prevData)=>{
479
- if (checkReturnData(data, prevData)) {
480
- data = prevData;
481
- if (i < ls.length) {
482
- try {
483
- return Promise.resolve(ls[i++](data)).then(call, processError);
484
- } catch (e) {
485
- return processError(e);
486
- }
487
- }
488
- } else {
489
- this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
490
- }
491
- return data;
492
- };
493
- return Promise.resolve(call(data));
494
- }
495
- return Promise.resolve(data);
496
- }
497
- constructor(type){
498
- super(), this.onerror = share.error;
499
- this.type = type;
500
- }
501
- }
502
-
503
- class PluginSystem {
504
- applyPlugin(plugin) {
505
- share.assert(share.isPlainObject(plugin), 'Plugin configuration is invalid.');
506
- // The plugin's name is mandatory and must be unique
507
- const pluginName = plugin.name;
508
- share.assert(pluginName, 'A name must be provided by the plugin.');
509
- if (!this.registerPlugins[pluginName]) {
510
- this.registerPlugins[pluginName] = plugin;
511
- Object.keys(this.lifecycle).forEach((key)=>{
512
- const pluginLife = plugin[key];
513
- if (pluginLife) {
514
- this.lifecycle[key].on(pluginLife);
515
- }
516
- });
517
- }
518
- }
519
- removePlugin(pluginName) {
520
- share.assert(pluginName, 'A name is required.');
521
- const plugin = this.registerPlugins[pluginName];
522
- share.assert(plugin, `The plugin "${pluginName}" is not registered.`);
523
- Object.keys(plugin).forEach((key)=>{
524
- if (key !== 'name') {
525
- this.lifecycle[key].remove(plugin[key]);
526
- }
527
- });
528
- }
529
- // eslint-disable-next-line @typescript-eslint/no-shadow
530
- inherit({ lifecycle, registerPlugins }) {
531
- Object.keys(lifecycle).forEach((hookName)=>{
532
- share.assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
533
- this.lifecycle[hookName] = lifecycle[hookName];
534
- });
535
- Object.keys(registerPlugins).forEach((pluginName)=>{
536
- share.assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
537
- this.applyPlugin(registerPlugins[pluginName]);
538
- });
539
- }
540
- constructor(lifecycle){
541
- this.registerPlugins = {};
542
- this.lifecycle = lifecycle;
543
- this.lifecycleKeys = Object.keys(lifecycle);
544
- }
545
- }
546
-
547
344
  function defaultPreloadArgs(preloadConfig) {
548
345
  return polyfills._extends({
549
346
  resourceCategory: 'sync',
@@ -1057,6 +854,8 @@ class SnapshotHandler {
1057
854
  remoteSnapshot,
1058
855
  globalSnapshot
1059
856
  });
857
+ let mSnapshot;
858
+ let gSnapshot;
1060
859
  // global snapshot includes manifest or module info includes manifest
1061
860
  if (globalRemoteSnapshot) {
1062
861
  if (sdk.isManifestProvider(globalRemoteSnapshot)) {
@@ -1068,10 +867,8 @@ class SnapshotHandler {
1068
867
  // Therefore, set the snapshot key to the global address of the actual request
1069
868
  entry: remoteEntry
1070
869
  }), moduleSnapshot);
1071
- return {
1072
- remoteSnapshot: moduleSnapshot,
1073
- globalSnapshot: globalSnapshotRes
1074
- };
870
+ mSnapshot = moduleSnapshot;
871
+ gSnapshot = globalSnapshotRes;
1075
872
  } else {
1076
873
  const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
1077
874
  options: this.HostInstance.options,
@@ -1079,10 +876,8 @@ class SnapshotHandler {
1079
876
  remoteSnapshot: globalRemoteSnapshot,
1080
877
  from: 'global'
1081
878
  });
1082
- return {
1083
- remoteSnapshot: remoteSnapshotRes,
1084
- globalSnapshot: globalSnapshotRes
1085
- };
879
+ mSnapshot = remoteSnapshotRes;
880
+ gSnapshot = globalSnapshotRes;
1086
881
  }
1087
882
  } else {
1088
883
  if (share.isRemoteInfoWithEntry(moduleInfo)) {
@@ -1096,10 +891,8 @@ class SnapshotHandler {
1096
891
  remoteSnapshot: moduleSnapshot,
1097
892
  from: 'global'
1098
893
  });
1099
- return {
1100
- remoteSnapshot: remoteSnapshotRes,
1101
- globalSnapshot: globalSnapshotRes
1102
- };
894
+ mSnapshot = remoteSnapshotRes;
895
+ gSnapshot = globalSnapshotRes;
1103
896
  } else {
1104
897
  share.error(`
1105
898
  Cannot get remoteSnapshot with the name: '${moduleInfo.name}', version: '${moduleInfo.version}' from __FEDERATION__.moduleInfo. The following reasons may be causing the problem:\n
@@ -1109,6 +902,15 @@ class SnapshotHandler {
1109
902
  `);
1110
903
  }
1111
904
  }
905
+ await this.hooks.lifecycle.afterLoadSnapshot.emit({
906
+ options,
907
+ moduleInfo,
908
+ remoteSnapshot: mSnapshot
909
+ });
910
+ return {
911
+ remoteSnapshot: mSnapshot,
912
+ globalSnapshot: gSnapshot
913
+ };
1112
914
  }
1113
915
  getGlobalRemoteInfo(moduleInfo) {
1114
916
  return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
@@ -1158,10 +960,11 @@ class SnapshotHandler {
1158
960
  constructor(HostInstance){
1159
961
  this.loadingHostSnapshot = null;
1160
962
  this.manifestCache = new Map();
1161
- this.hooks = new PluginSystem({
1162
- beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
1163
- loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
1164
- loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
963
+ this.hooks = new share.PluginSystem({
964
+ beforeLoadRemoteSnapshot: new share.AsyncHook('beforeLoadRemoteSnapshot'),
965
+ loadSnapshot: new share.AsyncWaterfallHook('loadGlobalSnapshot'),
966
+ loadRemoteSnapshot: new share.AsyncWaterfallHook('loadRemoteSnapshot'),
967
+ afterLoadSnapshot: new share.AsyncWaterfallHook('afterLoadSnapshot')
1165
968
  });
1166
969
  this.manifestLoading = share.Global.__FEDERATION__.__MANIFEST_LOADING__;
1167
970
  this.HostInstance = HostInstance;
@@ -1515,14 +1318,14 @@ class SharedHandler {
1515
1318
  }
1516
1319
  }
1517
1320
  constructor(host){
1518
- this.hooks = new PluginSystem({
1519
- afterResolve: new AsyncWaterfallHook('afterResolve'),
1520
- beforeLoadShare: new AsyncWaterfallHook('beforeLoadShare'),
1321
+ this.hooks = new share.PluginSystem({
1322
+ afterResolve: new share.AsyncWaterfallHook('afterResolve'),
1323
+ beforeLoadShare: new share.AsyncWaterfallHook('beforeLoadShare'),
1521
1324
  // not used yet
1522
- loadShare: new AsyncHook(),
1523
- resolveShare: new SyncWaterfallHook('resolveShare'),
1325
+ loadShare: new share.AsyncHook(),
1326
+ resolveShare: new share.SyncWaterfallHook('resolveShare'),
1524
1327
  // maybe will change, temporarily for internal use only
1525
- initContainerShareScopeMap: new SyncWaterfallHook('initContainerShareScopeMap')
1328
+ initContainerShareScopeMap: new share.SyncWaterfallHook('initContainerShareScopeMap')
1526
1329
  });
1527
1330
  this.host = host;
1528
1331
  this.shareScopeMap = {};
@@ -1869,18 +1672,18 @@ class RemoteHandler {
1869
1672
  }
1870
1673
  }
1871
1674
  constructor(host){
1872
- this.hooks = new PluginSystem({
1873
- beforeRegisterRemote: new SyncWaterfallHook('beforeRegisterRemote'),
1874
- registerRemote: new SyncWaterfallHook('registerRemote'),
1875
- beforeRequest: new AsyncWaterfallHook('beforeRequest'),
1876
- onLoad: new AsyncHook('onLoad'),
1877
- handlePreloadModule: new SyncHook('handlePreloadModule'),
1878
- errorLoadRemote: new AsyncHook('errorLoadRemote'),
1879
- beforePreloadRemote: new AsyncHook('beforePreloadRemote'),
1880
- generatePreloadAssets: new AsyncHook('generatePreloadAssets'),
1675
+ this.hooks = new share.PluginSystem({
1676
+ beforeRegisterRemote: new share.SyncWaterfallHook('beforeRegisterRemote'),
1677
+ registerRemote: new share.SyncWaterfallHook('registerRemote'),
1678
+ beforeRequest: new share.AsyncWaterfallHook('beforeRequest'),
1679
+ onLoad: new share.AsyncHook('onLoad'),
1680
+ handlePreloadModule: new share.SyncHook('handlePreloadModule'),
1681
+ errorLoadRemote: new share.AsyncHook('errorLoadRemote'),
1682
+ beforePreloadRemote: new share.AsyncHook('beforePreloadRemote'),
1683
+ generatePreloadAssets: new share.AsyncHook('generatePreloadAssets'),
1881
1684
  // not used yet
1882
- afterPreloadRemote: new AsyncHook(),
1883
- loadEntry: new AsyncHook()
1685
+ afterPreloadRemote: new share.AsyncHook(),
1686
+ loadEntry: new share.AsyncHook()
1884
1687
  });
1885
1688
  this.host = host;
1886
1689
  this.idToRemoteMap = {};
@@ -1964,7 +1767,7 @@ class FederationHost {
1964
1767
  return optionsRes;
1965
1768
  }
1966
1769
  registerPlugins(plugins) {
1967
- const pluginRes = registerPlugins$1(plugins, [
1770
+ const pluginRes = share.registerPlugins(plugins, [
1968
1771
  this.hooks,
1969
1772
  this.remoteHandler.hooks,
1970
1773
  this.sharedHandler.hooks,
@@ -1984,23 +1787,23 @@ class FederationHost {
1984
1787
  return this.remoteHandler.registerRemotes(remotes, options);
1985
1788
  }
1986
1789
  constructor(userOptions){
1987
- this.hooks = new PluginSystem({
1988
- beforeInit: new SyncWaterfallHook('beforeInit'),
1989
- init: new SyncHook(),
1790
+ this.hooks = new share.PluginSystem({
1791
+ beforeInit: new share.SyncWaterfallHook('beforeInit'),
1792
+ init: new share.SyncHook(),
1990
1793
  // maybe will change, temporarily for internal use only
1991
- beforeInitContainer: new AsyncWaterfallHook('beforeInitContainer'),
1794
+ beforeInitContainer: new share.AsyncWaterfallHook('beforeInitContainer'),
1992
1795
  // maybe will change, temporarily for internal use only
1993
- initContainer: new AsyncWaterfallHook('initContainer')
1796
+ initContainer: new share.AsyncWaterfallHook('initContainer')
1994
1797
  });
1995
1798
  this.version = "0.6.11";
1996
1799
  this.moduleCache = new Map();
1997
- this.loaderHook = new PluginSystem({
1800
+ this.loaderHook = new share.PluginSystem({
1998
1801
  // FIXME: may not be suitable , not open to the public yet
1999
- getModuleInfo: new SyncHook(),
2000
- createScript: new SyncHook(),
2001
- createLink: new SyncHook(),
1802
+ getModuleInfo: new share.SyncHook(),
1803
+ createScript: new share.SyncHook(),
1804
+ createLink: new share.SyncHook(),
2002
1805
  // only work for manifest , so not open to the public yet
2003
- fetch: new AsyncHook()
1806
+ fetch: new share.AsyncHook()
2004
1807
  });
2005
1808
  // TODO: Validate the details of the options
2006
1809
  // Initialize options with default values