@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.
@@ -1,5 +1,6 @@
1
- import { _ as _object_without_properties_loose, a as _extends } from './polyfills.esm.mjs';
2
1
  import { createLogger, isBrowserEnv, isDebugMode } from '@module-federation/sdk';
2
+ import '@module-federation/error-codes';
3
+ import { a as _object_without_properties_loose, _ as _extends } from './polyfills.esm.mjs';
3
4
 
4
5
  function getBuilderId() {
5
6
  //@ts-ignore
@@ -87,6 +88,20 @@ function getRemoteEntryInfoFromSnapshot(snapshot) {
87
88
  }
88
89
  return defaultRemoteEntryInfo;
89
90
  }
91
+ const processModuleAlias = (name, subPath)=>{
92
+ // @host/ ./button -> @host/button
93
+ let moduleName;
94
+ if (name.endsWith('/')) {
95
+ moduleName = name.slice(0, -1);
96
+ } else {
97
+ moduleName = name;
98
+ }
99
+ if (subPath.startsWith('.')) {
100
+ subPath = subPath.slice(1);
101
+ }
102
+ moduleName = moduleName + subPath;
103
+ return moduleName;
104
+ };
90
105
 
91
106
  const CurrentGlobal = typeof globalThis === 'object' ? globalThis : window;
92
107
  const nativeGlobal = (()=>{
@@ -278,9 +293,214 @@ const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
278
293
  const getPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);
279
294
  const setPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
280
295
 
296
+ function registerPlugins(plugins, hookInstances) {
297
+ const globalPlugins = getGlobalHostPlugins();
298
+ // Incorporate global plugins
299
+ if (globalPlugins.length > 0) {
300
+ globalPlugins.forEach((plugin)=>{
301
+ if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
302
+ plugins.push(plugin);
303
+ }
304
+ });
305
+ }
306
+ if (plugins && plugins.length > 0) {
307
+ plugins.forEach((plugin)=>{
308
+ hookInstances.forEach((hookInstance)=>{
309
+ hookInstance.applyPlugin(plugin);
310
+ });
311
+ });
312
+ }
313
+ return plugins;
314
+ }
315
+
281
316
  const DEFAULT_SCOPE = 'default';
282
317
  const DEFAULT_REMOTE_TYPE = 'global';
283
318
 
319
+ class SyncHook {
320
+ on(fn) {
321
+ if (typeof fn === 'function') {
322
+ this.listeners.add(fn);
323
+ }
324
+ }
325
+ once(fn) {
326
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
327
+ const self = this;
328
+ this.on(function wrapper(...args) {
329
+ self.remove(wrapper);
330
+ // eslint-disable-next-line prefer-spread
331
+ return fn.apply(null, args);
332
+ });
333
+ }
334
+ emit(...data) {
335
+ let result;
336
+ if (this.listeners.size > 0) {
337
+ // eslint-disable-next-line prefer-spread
338
+ this.listeners.forEach((fn)=>{
339
+ result = fn(...data);
340
+ });
341
+ }
342
+ return result;
343
+ }
344
+ remove(fn) {
345
+ this.listeners.delete(fn);
346
+ }
347
+ removeAll() {
348
+ this.listeners.clear();
349
+ }
350
+ constructor(type){
351
+ this.type = '';
352
+ this.listeners = new Set();
353
+ if (type) {
354
+ this.type = type;
355
+ }
356
+ }
357
+ }
358
+
359
+ class AsyncHook extends SyncHook {
360
+ emit(...data) {
361
+ let result;
362
+ const ls = Array.from(this.listeners);
363
+ if (ls.length > 0) {
364
+ let i = 0;
365
+ const call = (prev)=>{
366
+ if (prev === false) {
367
+ return false; // Abort process
368
+ } else if (i < ls.length) {
369
+ return Promise.resolve(ls[i++].apply(null, data)).then(call);
370
+ } else {
371
+ return prev;
372
+ }
373
+ };
374
+ result = call();
375
+ }
376
+ return Promise.resolve(result);
377
+ }
378
+ }
379
+
380
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
381
+ function checkReturnData(originalData, returnedData) {
382
+ if (!isObject(returnedData)) {
383
+ return false;
384
+ }
385
+ if (originalData !== returnedData) {
386
+ // eslint-disable-next-line no-restricted-syntax
387
+ for(const key in originalData){
388
+ if (!(key in returnedData)) {
389
+ return false;
390
+ }
391
+ }
392
+ }
393
+ return true;
394
+ }
395
+ class SyncWaterfallHook extends SyncHook {
396
+ emit(data) {
397
+ if (!isObject(data)) {
398
+ error(`The data for the "${this.type}" hook should be an object.`);
399
+ }
400
+ for (const fn of this.listeners){
401
+ try {
402
+ const tempData = fn(data);
403
+ if (checkReturnData(data, tempData)) {
404
+ data = tempData;
405
+ } else {
406
+ this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
407
+ break;
408
+ }
409
+ } catch (e) {
410
+ warn(e);
411
+ this.onerror(e);
412
+ }
413
+ }
414
+ return data;
415
+ }
416
+ constructor(type){
417
+ super(), this.onerror = error;
418
+ this.type = type;
419
+ }
420
+ }
421
+
422
+ class AsyncWaterfallHook extends SyncHook {
423
+ emit(data) {
424
+ if (!isObject(data)) {
425
+ error(`The response data for the "${this.type}" hook must be an object.`);
426
+ }
427
+ const ls = Array.from(this.listeners);
428
+ if (ls.length > 0) {
429
+ let i = 0;
430
+ const processError = (e)=>{
431
+ warn(e);
432
+ this.onerror(e);
433
+ return data;
434
+ };
435
+ const call = (prevData)=>{
436
+ if (checkReturnData(data, prevData)) {
437
+ data = prevData;
438
+ if (i < ls.length) {
439
+ try {
440
+ return Promise.resolve(ls[i++](data)).then(call, processError);
441
+ } catch (e) {
442
+ return processError(e);
443
+ }
444
+ }
445
+ } else {
446
+ this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
447
+ }
448
+ return data;
449
+ };
450
+ return Promise.resolve(call(data));
451
+ }
452
+ return Promise.resolve(data);
453
+ }
454
+ constructor(type){
455
+ super(), this.onerror = error;
456
+ this.type = type;
457
+ }
458
+ }
459
+
460
+ class PluginSystem {
461
+ applyPlugin(plugin) {
462
+ assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
463
+ // The plugin's name is mandatory and must be unique
464
+ const pluginName = plugin.name;
465
+ assert(pluginName, 'A name must be provided by the plugin.');
466
+ if (!this.registerPlugins[pluginName]) {
467
+ this.registerPlugins[pluginName] = plugin;
468
+ Object.keys(this.lifecycle).forEach((key)=>{
469
+ const pluginLife = plugin[key];
470
+ if (pluginLife) {
471
+ this.lifecycle[key].on(pluginLife);
472
+ }
473
+ });
474
+ }
475
+ }
476
+ removePlugin(pluginName) {
477
+ assert(pluginName, 'A name is required.');
478
+ const plugin = this.registerPlugins[pluginName];
479
+ assert(plugin, `The plugin "${pluginName}" is not registered.`);
480
+ Object.keys(plugin).forEach((key)=>{
481
+ if (key !== 'name') {
482
+ this.lifecycle[key].remove(plugin[key]);
483
+ }
484
+ });
485
+ }
486
+ // eslint-disable-next-line @typescript-eslint/no-shadow
487
+ inherit({ lifecycle, registerPlugins }) {
488
+ Object.keys(lifecycle).forEach((hookName)=>{
489
+ assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
490
+ this.lifecycle[hookName] = lifecycle[hookName];
491
+ });
492
+ Object.keys(registerPlugins).forEach((pluginName)=>{
493
+ assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
494
+ this.applyPlugin(registerPlugins[pluginName]);
495
+ });
496
+ }
497
+ constructor(lifecycle){
498
+ this.registerPlugins = {};
499
+ this.lifecycle = lifecycle;
500
+ this.lifecycleKeys = Object.keys(lifecycle);
501
+ }
502
+ }
503
+
284
504
  // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
285
505
  // those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant
286
506
  // Copyright (c)
@@ -876,4 +1096,4 @@ function getTargetSharedOptions(options) {
876
1096
  return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
877
1097
  }
878
1098
 
879
- export { isPlainObject as A, isRemoteInfoWithEntry as B, isPureRemoteEntry as C, DEFAULT_REMOTE_TYPE as D, getRemoteEntryInfoFromSnapshot as E, arrayOptions as F, Global as G, formatShareConfigs as H, getTargetSharedOptions as I, addUniqueItem as J, CurrentGlobal as K, logger as L, getBuilderId as M, getGlobalShareScope as a, getGlobalFederationInstance as b, getGlobalFederationConstructor as c, setGlobalFederationConstructor as d, getInfoWithoutType as e, getGlobalSnapshot as f, getRegisteredShare as g, getTargetSnapshotInfoByModuleInfo as h, getGlobalSnapshotInfoByModuleInfo as i, setGlobalSnapshotInfoByModuleInfo as j, addGlobalSnapshot as k, getRemoteEntryExports as l, registerGlobalPlugins as m, nativeGlobal as n, getGlobalHostPlugins as o, getPreloaded as p, setPreloaded as q, resetFederationGlobalInfo as r, setGlobalFederationInstance as s, globalLoading as t, DEFAULT_SCOPE as u, assert as v, error as w, getFMId as x, isObject as y, warn as z };
1099
+ export { AsyncHook as A, getFMId as B, processModuleAlias as C, DEFAULT_REMOTE_TYPE as D, isRemoteInfoWithEntry as E, isPureRemoteEntry as F, Global as G, getRemoteEntryInfoFromSnapshot as H, arrayOptions as I, formatShareConfigs as J, getTargetSharedOptions as K, addUniqueItem as L, CurrentGlobal as M, logger as N, getBuilderId as O, PluginSystem as P, SyncHook as S, AsyncWaterfallHook as a, SyncWaterfallHook as b, getGlobalShareScope as c, getGlobalFederationInstance as d, getGlobalFederationConstructor as e, setGlobalFederationConstructor as f, getRegisteredShare as g, getInfoWithoutType as h, getGlobalSnapshot as i, getTargetSnapshotInfoByModuleInfo as j, getGlobalSnapshotInfoByModuleInfo as k, setGlobalSnapshotInfoByModuleInfo as l, addGlobalSnapshot as m, nativeGlobal as n, getRemoteEntryExports as o, registerGlobalPlugins as p, getGlobalHostPlugins as q, resetFederationGlobalInfo as r, setGlobalFederationInstance as s, getPreloaded as t, setPreloaded as u, registerPlugins as v, globalLoading as w, DEFAULT_SCOPE as x, assert as y, error as z };
@@ -66,6 +66,12 @@ export declare class FederationHost {
66
66
  moduleInfo: RemoteInfo;
67
67
  }], Promise<(() => Promise<Module>) | undefined>>;
68
68
  }>;
69
+ bridgeHook: PluginSystem<{
70
+ beforeBridgeRender: SyncHook<[Record<string, any>], any>;
71
+ afterBridgeRender: SyncHook<[Record<string, any>], any>;
72
+ beforeBridgeDestroy: SyncHook<[Record<string, any>], any>;
73
+ afterBridgeDestroy: SyncHook<[Record<string, any>], any>;
74
+ }>;
69
75
  constructor(userOptions: UserOptions);
70
76
  initOptions(userOptions: UserOptions): Options;
71
77
  loadShare<T>(pkgName: string, extraOptions?: {
@@ -78,6 +78,12 @@ export declare class FederationHost implements IndexModule.FederationHost {
78
78
  moduleInfo: import("./type").RemoteInfo;
79
79
  }], Promise<(() => Promise<IndexModule.Module>) | undefined>>;
80
80
  }>;
81
+ get bridgeHook(): import("./utils/hooks").PluginSystem<{
82
+ beforeBridgeRender: import("./utils/hooks").SyncHook<[Record<string, any>], any>;
83
+ afterBridgeRender: import("./utils/hooks").SyncHook<[Record<string, any>], any>;
84
+ beforeBridgeDestroy: import("./utils/hooks").SyncHook<[Record<string, any>], any>;
85
+ afterBridgeDestroy: import("./utils/hooks").SyncHook<[Record<string, any>], any>;
86
+ }>;
81
87
  initOptions(...args: Parameters<IndexModule.FederationHost['initOptions']>): import("./type").Options;
82
88
  loadShare<T>(...args: Parameters<IndexModule.FederationHost['loadShare']>): Promise<false | (() => T | undefined)>;
83
89
  loadShareSync<T>(...args: Parameters<IndexModule.FederationHost['loadShareSync']>): () => T | never;
@@ -1,5 +1,7 @@
1
1
  import { resetFederationGlobalInfo, getGlobalFederationInstance, setGlobalFederationInstance, getGlobalFederationConstructor, setGlobalFederationConstructor, getInfoWithoutType, getGlobalSnapshot, getTargetSnapshotInfoByModuleInfo, getGlobalSnapshotInfoByModuleInfo, setGlobalSnapshotInfoByModuleInfo, addGlobalSnapshot, getRemoteEntryExports, registerGlobalPlugins, getGlobalHostPlugins, getPreloaded, setPreloaded, Global } from './global';
2
2
  import { getRegisteredShare, getGlobalShareScope } from './utils/share';
3
+ import * as pluginHelper from './utils/hooks';
4
+ import { registerPlugins } from './utils';
3
5
  interface IShareUtils {
4
6
  getRegisteredShare: typeof getRegisteredShare;
5
7
  getGlobalShareScope: typeof getGlobalShareScope;
@@ -23,6 +25,8 @@ interface IGlobalUtils {
23
25
  getGlobalHostPlugins: typeof getGlobalHostPlugins;
24
26
  getPreloaded: typeof getPreloaded;
25
27
  setPreloaded: typeof setPreloaded;
28
+ registerPlugins: typeof registerPlugins;
29
+ pluginHelper: typeof pluginHelper;
26
30
  }
27
31
  declare const _default: {
28
32
  global: IGlobalUtils;
@@ -32,6 +32,11 @@ export declare class SnapshotHandler {
32
32
  remoteSnapshot: ModuleInfo;
33
33
  from: "global" | "manifest";
34
34
  }>;
35
+ afterLoadSnapshot: AsyncWaterfallHook<{
36
+ options: Options;
37
+ moduleInfo: Remote;
38
+ remoteSnapshot: ModuleInfo;
39
+ }>;
35
40
  }>;
36
41
  loaderHook: FederationHost['loaderHook'];
37
42
  manifestLoading: Record<string, Promise<ModuleInfo>>;
@@ -4,7 +4,7 @@ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
4
4
  export type PartialOptional<T, K extends keyof T> = Omit<T, K> & {
5
5
  [P in K]-?: T[P];
6
6
  };
7
- interface RemoteInfoCommon {
7
+ export interface RemoteInfoCommon {
8
8
  alias?: string;
9
9
  shareScope?: string;
10
10
  type?: RemoteEntryType;
@@ -23,8 +23,9 @@ type RemoteLifeCycle = RemoteHandler['hooks']['lifecycle'];
23
23
  type RemoteLifeCycleCyclePartial = Partial<{
24
24
  [k in keyof RemoteLifeCycle]: Parameters<RemoteLifeCycle[k]['on']>[0];
25
25
  }>;
26
- export type FederationRuntimePlugin = CoreLifeCyclePartial & SnapshotLifeCycleCyclePartial & SharedLifeCycleCyclePartial & RemoteLifeCycleCyclePartial & ModuleLifeCycleCyclePartial & {
26
+ type LifeCycle = CoreLifeCyclePartial & SnapshotLifeCycleCyclePartial & SharedLifeCycleCyclePartial & RemoteLifeCycleCyclePartial & ModuleLifeCycleCyclePartial & {
27
27
  name: string;
28
28
  version?: string;
29
29
  };
30
+ export type FederationRuntimePlugin<T = LifeCycle> = LifeCycle & T;
30
31
  export {};
@@ -1,4 +1,5 @@
1
1
  import { FederationHost } from '../core';
2
2
  import { UserOptions } from '../type';
3
3
  import { Module } from '../module';
4
- export declare function registerPlugins(plugins: UserOptions['plugins'], hookInstances: Array<FederationHost['hooks'] | FederationHost['snapshotHandler']['hooks'] | FederationHost['sharedHandler']['hooks'] | FederationHost['remoteHandler']['hooks'] | Module['host']['loaderHook']>): import("../type").FederationRuntimePlugin[] | undefined;
4
+ import { PluginSystem } from './hooks';
5
+ export declare function registerPlugins<Y extends Record<string, any>, T extends PluginSystem<Y>>(plugins: UserOptions['plugins'], hookInstances: Array<T | FederationHost['hooks'] | FederationHost['snapshotHandler']['hooks'] | FederationHost['sharedHandler']['hooks'] | FederationHost['remoteHandler']['hooks'] | Module['host']['loaderHook'] | Module['host']['bridgeHook']>): import("../type").FederationRuntimePlugin[] | undefined;
@@ -15,3 +15,4 @@ export declare function getRemoteEntryInfoFromSnapshot(snapshot: ModuleInfo): {
15
15
  type: RemoteEntryType;
16
16
  globalName: string;
17
17
  };
18
+ export declare const processModuleAlias: (name: string, subPath: string) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.0.0-next-20241106033302",
3
+ "version": "0.0.0-next-20241106104434",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.mjs",
@@ -50,7 +50,7 @@
50
50
  }
51
51
  },
52
52
  "dependencies": {
53
- "@module-federation/sdk": "0.0.0-next-20241106033302",
54
- "@module-federation/error-codes": "0.0.0-next-20241106033302"
53
+ "@module-federation/sdk": "0.0.0-next-20241106104434",
54
+ "@module-federation/error-codes": "0.0.0-next-20241106104434"
55
55
  }
56
56
  }