@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.
- package/dist/helpers.cjs.js +13 -2
- package/dist/helpers.esm.js +14 -3
- package/dist/index.cjs.js +53 -250
- package/dist/index.esm.js +27 -224
- package/dist/polyfills.esm.js +1 -1
- package/dist/share.cjs.js +227 -4
- package/dist/share.esm.js +221 -2
- 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 +2 -2
package/dist/index.esm.js
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.js';
|
|
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 getFMId, B as processModuleAlias, C as isRemoteInfoWithEntry, E as isPureRemoteEntry, F as getRemoteEntryInfoFromSnapshot, H as error, 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, S as SyncHook, v as registerPlugins$1, M as getBuilderId, f as setGlobalFederationConstructor, d as getGlobalFederationInstance, e as getGlobalFederationConstructor, s as setGlobalFederationInstance } from './share.esm.js';
|
|
5
|
+
export { p as registerGlobalPlugins } from './share.esm.js';
|
|
6
6
|
|
|
7
7
|
// Function to match a remote with its name and expose
|
|
8
8
|
// id: pkgName(@federation/app1) + expose(button) = @federation/app1/button
|
|
@@ -68,26 +68,6 @@ function matchRemote(remotes, nameOrAlias) {
|
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
function registerPlugins$1(plugins, hookInstances) {
|
|
72
|
-
const globalPlugins = 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
|
assert(moduleFactory, `${getFMId(this.remoteInfo)} remote don't export ${expose}.`);
|
|
322
|
-
|
|
302
|
+
// keep symbol for module name always one format
|
|
303
|
+
const symbolName = 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 (!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 (!isObject(data)) {
|
|
441
|
-
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
|
-
warn(e);
|
|
454
|
-
this.onerror(e);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
return data;
|
|
458
|
-
}
|
|
459
|
-
constructor(type){
|
|
460
|
-
super(), this.onerror = error;
|
|
461
|
-
this.type = type;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
class AsyncWaterfallHook extends SyncHook {
|
|
466
|
-
emit(data) {
|
|
467
|
-
if (!isObject(data)) {
|
|
468
|
-
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
|
-
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 = error;
|
|
499
|
-
this.type = type;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
class PluginSystem {
|
|
504
|
-
applyPlugin(plugin) {
|
|
505
|
-
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
506
|
-
// The plugin's name is mandatory and must be unique
|
|
507
|
-
const pluginName = plugin.name;
|
|
508
|
-
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
|
-
assert(pluginName, 'A name is required.');
|
|
521
|
-
const plugin = this.registerPlugins[pluginName];
|
|
522
|
-
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
|
-
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
|
-
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 _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 (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
|
-
|
|
1072
|
-
|
|
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
|
-
|
|
1083
|
-
|
|
1084
|
-
globalSnapshot: globalSnapshotRes
|
|
1085
|
-
};
|
|
879
|
+
mSnapshot = remoteSnapshotRes;
|
|
880
|
+
gSnapshot = globalSnapshotRes;
|
|
1086
881
|
}
|
|
1087
882
|
} else {
|
|
1088
883
|
if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
@@ -1096,10 +891,8 @@ class SnapshotHandler {
|
|
|
1096
891
|
remoteSnapshot: moduleSnapshot,
|
|
1097
892
|
from: 'global'
|
|
1098
893
|
});
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
globalSnapshot: globalSnapshotRes
|
|
1102
|
-
};
|
|
894
|
+
mSnapshot = remoteSnapshotRes;
|
|
895
|
+
gSnapshot = globalSnapshotRes;
|
|
1103
896
|
} else {
|
|
1104
897
|
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);
|
|
@@ -1161,7 +963,8 @@ class SnapshotHandler {
|
|
|
1161
963
|
this.hooks = new PluginSystem({
|
|
1162
964
|
beforeLoadRemoteSnapshot: new AsyncHook('beforeLoadRemoteSnapshot'),
|
|
1163
965
|
loadSnapshot: new AsyncWaterfallHook('loadGlobalSnapshot'),
|
|
1164
|
-
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot')
|
|
966
|
+
loadRemoteSnapshot: new AsyncWaterfallHook('loadRemoteSnapshot'),
|
|
967
|
+
afterLoadSnapshot: new AsyncWaterfallHook('afterLoadSnapshot')
|
|
1165
968
|
});
|
|
1166
969
|
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
1167
970
|
this.HostInstance = HostInstance;
|
|
@@ -1763,7 +1566,7 @@ class RemoteHandler {
|
|
|
1763
1566
|
origin: host
|
|
1764
1567
|
});
|
|
1765
1568
|
}
|
|
1766
|
-
warn
|
|
1569
|
+
warn(messages.join(' '));
|
|
1767
1570
|
}
|
|
1768
1571
|
}
|
|
1769
1572
|
removeRemote(remote) {
|
package/dist/polyfills.esm.js
CHANGED
package/dist/share.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var polyfills = require('./polyfills.cjs.js');
|
|
4
3
|
var sdk = require('@module-federation/sdk');
|
|
4
|
+
var polyfills = require('./polyfills.cjs.js');
|
|
5
5
|
|
|
6
6
|
function getBuilderId() {
|
|
7
7
|
//@ts-ignore
|
|
@@ -87,6 +87,20 @@ function getRemoteEntryInfoFromSnapshot(snapshot) {
|
|
|
87
87
|
}
|
|
88
88
|
return defaultRemoteEntryInfo;
|
|
89
89
|
}
|
|
90
|
+
const processModuleAlias = (name, subPath)=>{
|
|
91
|
+
// @host/ ./button -> @host/button
|
|
92
|
+
let moduleName;
|
|
93
|
+
if (name.endsWith('/')) {
|
|
94
|
+
moduleName = name.slice(0, -1);
|
|
95
|
+
} else {
|
|
96
|
+
moduleName = name;
|
|
97
|
+
}
|
|
98
|
+
if (subPath.startsWith('.')) {
|
|
99
|
+
subPath = subPath.slice(1);
|
|
100
|
+
}
|
|
101
|
+
moduleName = moduleName + subPath;
|
|
102
|
+
return moduleName;
|
|
103
|
+
};
|
|
90
104
|
|
|
91
105
|
const nativeGlobal = (()=>{
|
|
92
106
|
try {
|
|
@@ -275,9 +289,214 @@ const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
|
|
|
275
289
|
const getPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id);
|
|
276
290
|
const setPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
|
|
277
291
|
|
|
292
|
+
function registerPlugins(plugins, hookInstances) {
|
|
293
|
+
const globalPlugins = getGlobalHostPlugins();
|
|
294
|
+
// Incorporate global plugins
|
|
295
|
+
if (globalPlugins.length > 0) {
|
|
296
|
+
globalPlugins.forEach((plugin)=>{
|
|
297
|
+
if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
|
|
298
|
+
plugins.push(plugin);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
if (plugins && plugins.length > 0) {
|
|
303
|
+
plugins.forEach((plugin)=>{
|
|
304
|
+
hookInstances.forEach((hookInstance)=>{
|
|
305
|
+
hookInstance.applyPlugin(plugin);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
return plugins;
|
|
310
|
+
}
|
|
311
|
+
|
|
278
312
|
const DEFAULT_SCOPE = 'default';
|
|
279
313
|
const DEFAULT_REMOTE_TYPE = 'global';
|
|
280
314
|
|
|
315
|
+
class SyncHook {
|
|
316
|
+
on(fn) {
|
|
317
|
+
if (typeof fn === 'function') {
|
|
318
|
+
this.listeners.add(fn);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
once(fn) {
|
|
322
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
323
|
+
const self = this;
|
|
324
|
+
this.on(function wrapper(...args) {
|
|
325
|
+
self.remove(wrapper);
|
|
326
|
+
// eslint-disable-next-line prefer-spread
|
|
327
|
+
return fn.apply(null, args);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
emit(...data) {
|
|
331
|
+
let result;
|
|
332
|
+
if (this.listeners.size > 0) {
|
|
333
|
+
// eslint-disable-next-line prefer-spread
|
|
334
|
+
this.listeners.forEach((fn)=>{
|
|
335
|
+
result = fn(...data);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
remove(fn) {
|
|
341
|
+
this.listeners.delete(fn);
|
|
342
|
+
}
|
|
343
|
+
removeAll() {
|
|
344
|
+
this.listeners.clear();
|
|
345
|
+
}
|
|
346
|
+
constructor(type){
|
|
347
|
+
this.type = '';
|
|
348
|
+
this.listeners = new Set();
|
|
349
|
+
if (type) {
|
|
350
|
+
this.type = type;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
class AsyncHook extends SyncHook {
|
|
356
|
+
emit(...data) {
|
|
357
|
+
let result;
|
|
358
|
+
const ls = Array.from(this.listeners);
|
|
359
|
+
if (ls.length > 0) {
|
|
360
|
+
let i = 0;
|
|
361
|
+
const call = (prev)=>{
|
|
362
|
+
if (prev === false) {
|
|
363
|
+
return false; // Abort process
|
|
364
|
+
} else if (i < ls.length) {
|
|
365
|
+
return Promise.resolve(ls[i++].apply(null, data)).then(call);
|
|
366
|
+
} else {
|
|
367
|
+
return prev;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
result = call();
|
|
371
|
+
}
|
|
372
|
+
return Promise.resolve(result);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
377
|
+
function checkReturnData(originalData, returnedData) {
|
|
378
|
+
if (!isObject(returnedData)) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
if (originalData !== returnedData) {
|
|
382
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
383
|
+
for(const key in originalData){
|
|
384
|
+
if (!(key in returnedData)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
class SyncWaterfallHook extends SyncHook {
|
|
392
|
+
emit(data) {
|
|
393
|
+
if (!isObject(data)) {
|
|
394
|
+
error(`The data for the "${this.type}" hook should be an object.`);
|
|
395
|
+
}
|
|
396
|
+
for (const fn of this.listeners){
|
|
397
|
+
try {
|
|
398
|
+
const tempData = fn(data);
|
|
399
|
+
if (checkReturnData(data, tempData)) {
|
|
400
|
+
data = tempData;
|
|
401
|
+
} else {
|
|
402
|
+
this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
} catch (e) {
|
|
406
|
+
warn(e);
|
|
407
|
+
this.onerror(e);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return data;
|
|
411
|
+
}
|
|
412
|
+
constructor(type){
|
|
413
|
+
super(), this.onerror = error;
|
|
414
|
+
this.type = type;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
class AsyncWaterfallHook extends SyncHook {
|
|
419
|
+
emit(data) {
|
|
420
|
+
if (!isObject(data)) {
|
|
421
|
+
error(`The response data for the "${this.type}" hook must be an object.`);
|
|
422
|
+
}
|
|
423
|
+
const ls = Array.from(this.listeners);
|
|
424
|
+
if (ls.length > 0) {
|
|
425
|
+
let i = 0;
|
|
426
|
+
const processError = (e)=>{
|
|
427
|
+
warn(e);
|
|
428
|
+
this.onerror(e);
|
|
429
|
+
return data;
|
|
430
|
+
};
|
|
431
|
+
const call = (prevData)=>{
|
|
432
|
+
if (checkReturnData(data, prevData)) {
|
|
433
|
+
data = prevData;
|
|
434
|
+
if (i < ls.length) {
|
|
435
|
+
try {
|
|
436
|
+
return Promise.resolve(ls[i++](data)).then(call, processError);
|
|
437
|
+
} catch (e) {
|
|
438
|
+
return processError(e);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
} else {
|
|
442
|
+
this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
|
|
443
|
+
}
|
|
444
|
+
return data;
|
|
445
|
+
};
|
|
446
|
+
return Promise.resolve(call(data));
|
|
447
|
+
}
|
|
448
|
+
return Promise.resolve(data);
|
|
449
|
+
}
|
|
450
|
+
constructor(type){
|
|
451
|
+
super(), this.onerror = error;
|
|
452
|
+
this.type = type;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
class PluginSystem {
|
|
457
|
+
applyPlugin(plugin) {
|
|
458
|
+
assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
|
|
459
|
+
// The plugin's name is mandatory and must be unique
|
|
460
|
+
const pluginName = plugin.name;
|
|
461
|
+
assert(pluginName, 'A name must be provided by the plugin.');
|
|
462
|
+
if (!this.registerPlugins[pluginName]) {
|
|
463
|
+
this.registerPlugins[pluginName] = plugin;
|
|
464
|
+
Object.keys(this.lifecycle).forEach((key)=>{
|
|
465
|
+
const pluginLife = plugin[key];
|
|
466
|
+
if (pluginLife) {
|
|
467
|
+
this.lifecycle[key].on(pluginLife);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
removePlugin(pluginName) {
|
|
473
|
+
assert(pluginName, 'A name is required.');
|
|
474
|
+
const plugin = this.registerPlugins[pluginName];
|
|
475
|
+
assert(plugin, `The plugin "${pluginName}" is not registered.`);
|
|
476
|
+
Object.keys(plugin).forEach((key)=>{
|
|
477
|
+
if (key !== 'name') {
|
|
478
|
+
this.lifecycle[key].remove(plugin[key]);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
483
|
+
inherit({ lifecycle, registerPlugins }) {
|
|
484
|
+
Object.keys(lifecycle).forEach((hookName)=>{
|
|
485
|
+
assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
|
|
486
|
+
this.lifecycle[hookName] = lifecycle[hookName];
|
|
487
|
+
});
|
|
488
|
+
Object.keys(registerPlugins).forEach((pluginName)=>{
|
|
489
|
+
assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
|
|
490
|
+
this.applyPlugin(registerPlugins[pluginName]);
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
constructor(lifecycle){
|
|
494
|
+
this.registerPlugins = {};
|
|
495
|
+
this.lifecycle = lifecycle;
|
|
496
|
+
this.lifecycleKeys = Object.keys(lifecycle);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
281
500
|
// fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
|
|
282
501
|
// those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant
|
|
283
502
|
// Copyright (c)
|
|
@@ -867,9 +1086,14 @@ function getTargetSharedOptions(options) {
|
|
|
867
1086
|
return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
|
|
868
1087
|
}
|
|
869
1088
|
|
|
1089
|
+
exports.AsyncHook = AsyncHook;
|
|
1090
|
+
exports.AsyncWaterfallHook = AsyncWaterfallHook;
|
|
870
1091
|
exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE;
|
|
871
1092
|
exports.DEFAULT_SCOPE = DEFAULT_SCOPE;
|
|
872
1093
|
exports.Global = Global;
|
|
1094
|
+
exports.PluginSystem = PluginSystem;
|
|
1095
|
+
exports.SyncHook = SyncHook;
|
|
1096
|
+
exports.SyncWaterfallHook = SyncWaterfallHook;
|
|
873
1097
|
exports.addGlobalSnapshot = addGlobalSnapshot;
|
|
874
1098
|
exports.addUniqueItem = addUniqueItem;
|
|
875
1099
|
exports.arrayOptions = arrayOptions;
|
|
@@ -892,15 +1116,14 @@ exports.getRemoteEntryInfoFromSnapshot = getRemoteEntryInfoFromSnapshot;
|
|
|
892
1116
|
exports.getTargetSharedOptions = getTargetSharedOptions;
|
|
893
1117
|
exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
|
|
894
1118
|
exports.globalLoading = globalLoading;
|
|
895
|
-
exports.isObject = isObject;
|
|
896
|
-
exports.isPlainObject = isPlainObject;
|
|
897
1119
|
exports.isPureRemoteEntry = isPureRemoteEntry;
|
|
898
1120
|
exports.isRemoteInfoWithEntry = isRemoteInfoWithEntry;
|
|
899
1121
|
exports.nativeGlobal = nativeGlobal;
|
|
1122
|
+
exports.processModuleAlias = processModuleAlias;
|
|
900
1123
|
exports.registerGlobalPlugins = registerGlobalPlugins;
|
|
1124
|
+
exports.registerPlugins = registerPlugins;
|
|
901
1125
|
exports.resetFederationGlobalInfo = resetFederationGlobalInfo;
|
|
902
1126
|
exports.setGlobalFederationConstructor = setGlobalFederationConstructor;
|
|
903
1127
|
exports.setGlobalFederationInstance = setGlobalFederationInstance;
|
|
904
1128
|
exports.setGlobalSnapshotInfoByModuleInfo = setGlobalSnapshotInfoByModuleInfo;
|
|
905
1129
|
exports.setPreloaded = setPreloaded;
|
|
906
|
-
exports.warn = warn;
|