@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.
- package/dist/embedded.cjs.js +3 -0
- package/dist/embedded.esm.mjs +3 -0
- package/dist/helpers.cjs.js +14 -1
- package/dist/helpers.esm.mjs +15 -2
- package/dist/index.cjs.js +62 -252
- package/dist/index.esm.mjs +35 -225
- package/dist/polyfills.esm.mjs +1 -1
- package/dist/share.cjs.js +228 -4
- package/dist/share.esm.mjs +222 -2
- package/dist/src/core.d.ts +6 -0
- package/dist/src/embedded.d.ts +6 -0
- package/dist/src/helpers.d.ts +4 -0
- package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +5 -0
- package/dist/src/type/config.d.ts +1 -1
- package/dist/src/type/plugin.d.ts +2 -1
- package/dist/src/utils/plugin.d.ts +2 -1
- package/dist/src/utils/tool.d.ts +1 -0
- package/package.json +3 -3
package/dist/index.esm.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isBrowserEnv, loadScriptNode, composeKeyWithSeparator, loadScript, safeToString, createLink, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest, warn
|
|
1
|
+
import { _ as _extends, a as _object_without_properties_loose } from './polyfills.esm.mjs';
|
|
2
|
+
import { isBrowserEnv, loadScriptNode, composeKeyWithSeparator, loadScript, safeToString, createLink, createScript, getResourceUrl, isManifestProvider, generateSnapshotFromManifest, warn } from '@module-federation/sdk';
|
|
3
3
|
export { loadScript, loadScriptNode } from '@module-federation/sdk';
|
|
4
|
-
import {
|
|
5
|
-
export {
|
|
4
|
+
import { w as globalLoading, D as DEFAULT_REMOTE_TYPE, x as DEFAULT_SCOPE, o as getRemoteEntryExports, y as assert, z as error, B as getFMId, C as processModuleAlias, E as isRemoteInfoWithEntry, F as isPureRemoteEntry, H as getRemoteEntryInfoFromSnapshot, h as getInfoWithoutType, t as getPreloaded, u as setPreloaded, g as getRegisteredShare, I as arrayOptions, k as getGlobalSnapshotInfoByModuleInfo, m as addGlobalSnapshot, l as setGlobalSnapshotInfoByModuleInfo, P as PluginSystem, A as AsyncHook, a as AsyncWaterfallHook, G as Global, i as getGlobalSnapshot, J as formatShareConfigs, K as getTargetSharedOptions, c as getGlobalShareScope, b as SyncWaterfallHook, L as addUniqueItem, M as CurrentGlobal, N as logger, S as SyncHook, v as registerPlugins$1, O as getBuilderId, f as setGlobalFederationConstructor, d as getGlobalFederationInstance, e as getGlobalFederationConstructor, s as setGlobalFederationInstance } from './share.esm.mjs';
|
|
5
|
+
export { p as registerGlobalPlugins } from './share.esm.mjs';
|
|
6
6
|
import { getShortErrorMsg, RUNTIME_001, runtimeDescMap, RUNTIME_002, RUNTIME_007, RUNTIME_003, RUNTIME_005, RUNTIME_006, RUNTIME_004 } from '@module-federation/error-codes';
|
|
7
7
|
|
|
8
8
|
// Function to match a remote with its name and expose
|
|
@@ -69,26 +69,6 @@ function matchRemote(remotes, nameOrAlias) {
|
|
|
69
69
|
return;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
function registerPlugins$1(plugins, hookInstances) {
|
|
73
|
-
const globalPlugins = 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
|
assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
|
|
336
|
-
|
|
316
|
+
// keep symbol for module name always one format
|
|
317
|
+
const symbolName = 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 (!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 (!isObject(data)) {
|
|
455
|
-
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
|
-
warn(e);
|
|
468
|
-
this.onerror(e);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
return data;
|
|
472
|
-
}
|
|
473
|
-
constructor(type){
|
|
474
|
-
super(), this.onerror = error;
|
|
475
|
-
this.type = type;
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
class AsyncWaterfallHook extends SyncHook {
|
|
480
|
-
emit(data) {
|
|
481
|
-
if (!isObject(data)) {
|
|
482
|
-
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
|
-
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 = error;
|
|
513
|
-
this.type = type;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
class PluginSystem {
|
|
518
|
-
applyPlugin(plugin) {
|
|
519
|
-
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
520
|
-
// The plugin's name is mandatory and must be unique
|
|
521
|
-
const pluginName = plugin.name;
|
|
522
|
-
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
|
-
assert(pluginName, 'A name is required.');
|
|
535
|
-
const plugin = this.registerPlugins[pluginName];
|
|
536
|
-
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
|
-
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
|
-
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 _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 (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
|
-
|
|
1086
|
-
|
|
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
|
-
|
|
1097
|
-
|
|
1098
|
-
globalSnapshot: globalSnapshotRes
|
|
1099
|
-
};
|
|
893
|
+
mSnapshot = remoteSnapshotRes;
|
|
894
|
+
gSnapshot = globalSnapshotRes;
|
|
1100
895
|
}
|
|
1101
896
|
} else {
|
|
1102
897
|
if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
@@ -1110,10 +905,8 @@ class SnapshotHandler {
|
|
|
1110
905
|
remoteSnapshot: moduleSnapshot,
|
|
1111
906
|
from: 'global'
|
|
1112
907
|
});
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
globalSnapshot: globalSnapshotRes
|
|
1116
|
-
};
|
|
908
|
+
mSnapshot = remoteSnapshotRes;
|
|
909
|
+
gSnapshot = globalSnapshotRes;
|
|
1117
910
|
} else {
|
|
1118
911
|
error(getShortErrorMsg(RUNTIME_007, 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);
|
|
@@ -1175,7 +977,8 @@ class SnapshotHandler {
|
|
|
1175
977
|
this.hooks = new PluginSystem({
|
|
1176
978
|
beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
|
|
1177
979
|
loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
|
|
1178
|
-
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
|
|
980
|
+
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot'),
|
|
981
|
+
afterLoadSnapshot: new AsyncWaterfallHook('afterLoadSnapshot')
|
|
1179
982
|
});
|
|
1180
983
|
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
1181
984
|
this.HostInstance = HostInstance;
|
|
@@ -1770,7 +1573,7 @@ class RemoteHandler {
|
|
|
1770
1573
|
origin: host
|
|
1771
1574
|
});
|
|
1772
1575
|
}
|
|
1773
|
-
warn
|
|
1576
|
+
warn(messages.join(' '));
|
|
1774
1577
|
}
|
|
1775
1578
|
}
|
|
1776
1579
|
removeRemote(remote) {
|
|
@@ -1976,7 +1779,8 @@ class FederationHost {
|
|
|
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)=>{
|
|
@@ -2010,6 +1814,12 @@ class FederationHost {
|
|
|
2010
1814
|
fetch: new AsyncHook(),
|
|
2011
1815
|
getModuleFactory: new AsyncHook()
|
|
2012
1816
|
});
|
|
1817
|
+
this.bridgeHook = new PluginSystem({
|
|
1818
|
+
beforeBridgeRender: new SyncHook(),
|
|
1819
|
+
afterBridgeRender: new SyncHook(),
|
|
1820
|
+
beforeBridgeDestroy: new SyncHook(),
|
|
1821
|
+
afterBridgeDestroy: new SyncHook()
|
|
1822
|
+
});
|
|
2013
1823
|
// TODO: Validate the details of the options
|
|
2014
1824
|
// Initialize options with default values
|
|
2015
1825
|
const defaultOptions = {
|
package/dist/polyfills.esm.mjs
CHANGED
package/dist/share.cjs.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var polyfills = require('./polyfills.cjs.js');
|
|
4
3
|
var sdk = require('@module-federation/sdk');
|
|
4
|
+
require('@module-federation/error-codes');
|
|
5
|
+
var polyfills = require('./polyfills.cjs.js');
|
|
5
6
|
|
|
6
7
|
function getBuilderId() {
|
|
7
8
|
//@ts-ignore
|
|
@@ -89,6 +90,20 @@ function getRemoteEntryInfoFromSnapshot(snapshot) {
|
|
|
89
90
|
}
|
|
90
91
|
return defaultRemoteEntryInfo;
|
|
91
92
|
}
|
|
93
|
+
const processModuleAlias = (name, subPath)=>{
|
|
94
|
+
// @host/ ./button -> @host/button
|
|
95
|
+
let moduleName;
|
|
96
|
+
if (name.endsWith('/')) {
|
|
97
|
+
moduleName = name.slice(0, -1);
|
|
98
|
+
} else {
|
|
99
|
+
moduleName = name;
|
|
100
|
+
}
|
|
101
|
+
if (subPath.startsWith('.')) {
|
|
102
|
+
subPath = subPath.slice(1);
|
|
103
|
+
}
|
|
104
|
+
moduleName = moduleName + subPath;
|
|
105
|
+
return moduleName;
|
|
106
|
+
};
|
|
92
107
|
|
|
93
108
|
const CurrentGlobal = typeof globalThis === 'object' ? globalThis : window;
|
|
94
109
|
const nativeGlobal = (()=>{
|
|
@@ -280,9 +295,214 @@ const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
|
|
|
280
295
|
const getPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);
|
|
281
296
|
const setPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
|
|
282
297
|
|
|
298
|
+
function registerPlugins(plugins, hookInstances) {
|
|
299
|
+
const globalPlugins = getGlobalHostPlugins();
|
|
300
|
+
// Incorporate global plugins
|
|
301
|
+
if (globalPlugins.length > 0) {
|
|
302
|
+
globalPlugins.forEach((plugin)=>{
|
|
303
|
+
if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
|
|
304
|
+
plugins.push(plugin);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
if (plugins && plugins.length > 0) {
|
|
309
|
+
plugins.forEach((plugin)=>{
|
|
310
|
+
hookInstances.forEach((hookInstance)=>{
|
|
311
|
+
hookInstance.applyPlugin(plugin);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
return plugins;
|
|
316
|
+
}
|
|
317
|
+
|
|
283
318
|
const DEFAULT_SCOPE = 'default';
|
|
284
319
|
const DEFAULT_REMOTE_TYPE = 'global';
|
|
285
320
|
|
|
321
|
+
class SyncHook {
|
|
322
|
+
on(fn) {
|
|
323
|
+
if (typeof fn === 'function') {
|
|
324
|
+
this.listeners.add(fn);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
once(fn) {
|
|
328
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
329
|
+
const self = this;
|
|
330
|
+
this.on(function wrapper(...args) {
|
|
331
|
+
self.remove(wrapper);
|
|
332
|
+
// eslint-disable-next-line prefer-spread
|
|
333
|
+
return fn.apply(null, args);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
emit(...data) {
|
|
337
|
+
let result;
|
|
338
|
+
if (this.listeners.size > 0) {
|
|
339
|
+
// eslint-disable-next-line prefer-spread
|
|
340
|
+
this.listeners.forEach((fn)=>{
|
|
341
|
+
result = fn(...data);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
remove(fn) {
|
|
347
|
+
this.listeners.delete(fn);
|
|
348
|
+
}
|
|
349
|
+
removeAll() {
|
|
350
|
+
this.listeners.clear();
|
|
351
|
+
}
|
|
352
|
+
constructor(type){
|
|
353
|
+
this.type = '';
|
|
354
|
+
this.listeners = new Set();
|
|
355
|
+
if (type) {
|
|
356
|
+
this.type = type;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
class AsyncHook extends SyncHook {
|
|
362
|
+
emit(...data) {
|
|
363
|
+
let result;
|
|
364
|
+
const ls = Array.from(this.listeners);
|
|
365
|
+
if (ls.length > 0) {
|
|
366
|
+
let i = 0;
|
|
367
|
+
const call = (prev)=>{
|
|
368
|
+
if (prev === false) {
|
|
369
|
+
return false; // Abort process
|
|
370
|
+
} else if (i < ls.length) {
|
|
371
|
+
return Promise.resolve(ls[i++].apply(null, data)).then(call);
|
|
372
|
+
} else {
|
|
373
|
+
return prev;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
result = call();
|
|
377
|
+
}
|
|
378
|
+
return Promise.resolve(result);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
383
|
+
function checkReturnData(originalData, returnedData) {
|
|
384
|
+
if (!isObject(returnedData)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
if (originalData !== returnedData) {
|
|
388
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
389
|
+
for(const key in originalData){
|
|
390
|
+
if (!(key in returnedData)) {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
class SyncWaterfallHook extends SyncHook {
|
|
398
|
+
emit(data) {
|
|
399
|
+
if (!isObject(data)) {
|
|
400
|
+
error(`The data for the "${this.type}" hook should be an object.`);
|
|
401
|
+
}
|
|
402
|
+
for (const fn of this.listeners){
|
|
403
|
+
try {
|
|
404
|
+
const tempData = fn(data);
|
|
405
|
+
if (checkReturnData(data, tempData)) {
|
|
406
|
+
data = tempData;
|
|
407
|
+
} else {
|
|
408
|
+
this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
} catch (e) {
|
|
412
|
+
warn(e);
|
|
413
|
+
this.onerror(e);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return data;
|
|
417
|
+
}
|
|
418
|
+
constructor(type){
|
|
419
|
+
super(), this.onerror = error;
|
|
420
|
+
this.type = type;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
class AsyncWaterfallHook extends SyncHook {
|
|
425
|
+
emit(data) {
|
|
426
|
+
if (!isObject(data)) {
|
|
427
|
+
error(`The response data for the "${this.type}" hook must be an object.`);
|
|
428
|
+
}
|
|
429
|
+
const ls = Array.from(this.listeners);
|
|
430
|
+
if (ls.length > 0) {
|
|
431
|
+
let i = 0;
|
|
432
|
+
const processError = (e)=>{
|
|
433
|
+
warn(e);
|
|
434
|
+
this.onerror(e);
|
|
435
|
+
return data;
|
|
436
|
+
};
|
|
437
|
+
const call = (prevData)=>{
|
|
438
|
+
if (checkReturnData(data, prevData)) {
|
|
439
|
+
data = prevData;
|
|
440
|
+
if (i < ls.length) {
|
|
441
|
+
try {
|
|
442
|
+
return Promise.resolve(ls[i++](data)).then(call, processError);
|
|
443
|
+
} catch (e) {
|
|
444
|
+
return processError(e);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
|
|
449
|
+
}
|
|
450
|
+
return data;
|
|
451
|
+
};
|
|
452
|
+
return Promise.resolve(call(data));
|
|
453
|
+
}
|
|
454
|
+
return Promise.resolve(data);
|
|
455
|
+
}
|
|
456
|
+
constructor(type){
|
|
457
|
+
super(), this.onerror = error;
|
|
458
|
+
this.type = type;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
class PluginSystem {
|
|
463
|
+
applyPlugin(plugin) {
|
|
464
|
+
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
465
|
+
// The plugin's name is mandatory and must be unique
|
|
466
|
+
const pluginName = plugin.name;
|
|
467
|
+
assert(pluginName, 'A name must be provided by the plugin.');
|
|
468
|
+
if (!this.registerPlugins[pluginName]) {
|
|
469
|
+
this.registerPlugins[pluginName] = plugin;
|
|
470
|
+
Object.keys(this.lifecycle).forEach((key)=>{
|
|
471
|
+
const pluginLife = plugin[key];
|
|
472
|
+
if (pluginLife) {
|
|
473
|
+
this.lifecycle[key].on(pluginLife);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
removePlugin(pluginName) {
|
|
479
|
+
assert(pluginName, 'A name is required.');
|
|
480
|
+
const plugin = this.registerPlugins[pluginName];
|
|
481
|
+
assert(plugin, `The plugin "${pluginName}" is not registered.`);
|
|
482
|
+
Object.keys(plugin).forEach((key)=>{
|
|
483
|
+
if (key !== 'name') {
|
|
484
|
+
this.lifecycle[key].remove(plugin[key]);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
489
|
+
inherit({ lifecycle, registerPlugins }) {
|
|
490
|
+
Object.keys(lifecycle).forEach((hookName)=>{
|
|
491
|
+
assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
|
|
492
|
+
this.lifecycle[hookName] = lifecycle[hookName];
|
|
493
|
+
});
|
|
494
|
+
Object.keys(registerPlugins).forEach((pluginName)=>{
|
|
495
|
+
assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
|
|
496
|
+
this.applyPlugin(registerPlugins[pluginName]);
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
constructor(lifecycle){
|
|
500
|
+
this.registerPlugins = {};
|
|
501
|
+
this.lifecycle = lifecycle;
|
|
502
|
+
this.lifecycleKeys = Object.keys(lifecycle);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
286
506
|
// fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
|
|
287
507
|
// those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant
|
|
288
508
|
// Copyright (c)
|
|
@@ -878,10 +1098,15 @@ function getTargetSharedOptions(options) {
|
|
|
878
1098
|
return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
|
|
879
1099
|
}
|
|
880
1100
|
|
|
1101
|
+
exports.AsyncHook = AsyncHook;
|
|
1102
|
+
exports.AsyncWaterfallHook = AsyncWaterfallHook;
|
|
881
1103
|
exports.CurrentGlobal = CurrentGlobal;
|
|
882
1104
|
exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE;
|
|
883
1105
|
exports.DEFAULT_SCOPE = DEFAULT_SCOPE;
|
|
884
1106
|
exports.Global = Global;
|
|
1107
|
+
exports.PluginSystem = PluginSystem;
|
|
1108
|
+
exports.SyncHook = SyncHook;
|
|
1109
|
+
exports.SyncWaterfallHook = SyncWaterfallHook;
|
|
885
1110
|
exports.addGlobalSnapshot = addGlobalSnapshot;
|
|
886
1111
|
exports.addUniqueItem = addUniqueItem;
|
|
887
1112
|
exports.arrayOptions = arrayOptions;
|
|
@@ -904,16 +1129,15 @@ exports.getRemoteEntryInfoFromSnapshot = getRemoteEntryInfoFromSnapshot;
|
|
|
904
1129
|
exports.getTargetSharedOptions = getTargetSharedOptions;
|
|
905
1130
|
exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
|
|
906
1131
|
exports.globalLoading = globalLoading;
|
|
907
|
-
exports.isObject = isObject;
|
|
908
|
-
exports.isPlainObject = isPlainObject;
|
|
909
1132
|
exports.isPureRemoteEntry = isPureRemoteEntry;
|
|
910
1133
|
exports.isRemoteInfoWithEntry = isRemoteInfoWithEntry;
|
|
911
1134
|
exports.logger = logger;
|
|
912
1135
|
exports.nativeGlobal = nativeGlobal;
|
|
1136
|
+
exports.processModuleAlias = processModuleAlias;
|
|
913
1137
|
exports.registerGlobalPlugins = registerGlobalPlugins;
|
|
1138
|
+
exports.registerPlugins = registerPlugins;
|
|
914
1139
|
exports.resetFederationGlobalInfo = resetFederationGlobalInfo;
|
|
915
1140
|
exports.setGlobalFederationConstructor = setGlobalFederationConstructor;
|
|
916
1141
|
exports.setGlobalFederationInstance = setGlobalFederationInstance;
|
|
917
1142
|
exports.setGlobalSnapshotInfoByModuleInfo = setGlobalSnapshotInfoByModuleInfo;
|
|
918
1143
|
exports.setPreloaded = setPreloaded;
|
|
919
|
-
exports.warn = warn;
|