@module-federation/runtime 0.1.4 → 0.1.6

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/share.esm.js CHANGED
@@ -69,6 +69,11 @@ const objectToString = Object.prototype.toString;
69
69
  function isPlainObject(val) {
70
70
  return objectToString.call(val) === '[object Object]';
71
71
  }
72
+ function arrayOptions(options) {
73
+ return Array.isArray(options) ? options : [
74
+ options
75
+ ];
76
+ }
72
77
 
73
78
  function _extends$1() {
74
79
  _extends$1 = Object.assign || function(target) {
@@ -183,7 +188,7 @@ function getGlobalFederationConstructor() {
183
188
  function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
184
189
  if (isDebug) {
185
190
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
186
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.4";
191
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.1.6";
187
192
  }
188
193
  }
189
194
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -638,15 +643,19 @@ function _extends() {
638
643
  };
639
644
  return _extends.apply(this, arguments);
640
645
  }
641
- function formatShare(shareArgs, from) {
646
+ function formatShare(shareArgs, from, name) {
642
647
  let get;
643
648
  if ('get' in shareArgs) {
644
649
  // eslint-disable-next-line prefer-destructuring
645
650
  get = shareArgs.get;
646
- } else {
647
- // @ts-ignore ignore
651
+ } else if ('lib' in shareArgs) {
648
652
  get = ()=>Promise.resolve(shareArgs.lib);
653
+ } else {
654
+ get = ()=>Promise.resolve(()=>{
655
+ throw new Error(`Can not get shared '${name}'!`);
656
+ });
649
657
  }
658
+ var _shareArgs_version;
650
659
  return _extends({
651
660
  deps: [],
652
661
  useIn: [],
@@ -661,6 +670,7 @@ function formatShare(shareArgs, from) {
661
670
  }, shareArgs.shareConfig),
662
671
  get,
663
672
  loaded: 'lib' in shareArgs ? true : undefined,
673
+ version: (_shareArgs_version = shareArgs.version) != null ? _shareArgs_version : '0',
664
674
  scope: Array.isArray(shareArgs.scope) ? shareArgs.scope : [
665
675
  'default'
666
676
  ],
@@ -672,7 +682,11 @@ function formatShareConfigs(shareArgs, from) {
672
682
  return {};
673
683
  }
674
684
  return Object.keys(shareArgs).reduce((res, pkgName)=>{
675
- res[pkgName] = formatShare(shareArgs[pkgName], from);
685
+ const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
686
+ res[pkgName] = res[pkgName] || [];
687
+ arrayShareArgs.forEach((shareConfig)=>{
688
+ res[pkgName].push(formatShare(shareConfig, from, pkgName));
689
+ });
676
690
  return res;
677
691
  }, {});
678
692
  }
@@ -695,12 +709,11 @@ function versionLt(a, b) {
695
709
  return false;
696
710
  }
697
711
  }
698
- const findVersion = (shareScopeMap, scope, pkgName, cb)=>{
699
- const versions = shareScopeMap[scope][pkgName];
712
+ const findVersion = (shareVersionMap, cb)=>{
700
713
  const callback = cb || function(prev, cur) {
701
714
  return versionLt(prev, cur);
702
715
  };
703
- return Object.keys(versions).reduce((prev, cur)=>{
716
+ return Object.keys(shareVersionMap).reduce((prev, cur)=>{
704
717
  if (!prev) {
705
718
  return cur;
706
719
  }
@@ -722,7 +735,7 @@ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
722
735
  const callback = function(prev, cur) {
723
736
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
724
737
  };
725
- return findVersion(shareScopeMap, scope, pkgName, callback);
738
+ return findVersion(shareScopeMap[scope][pkgName], callback);
726
739
  }
727
740
  function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
728
741
  const versions = shareScopeMap[scope][pkgName];
@@ -739,7 +752,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
739
752
  }
740
753
  return versionLt(prev, cur);
741
754
  };
742
- return findVersion(shareScopeMap, scope, pkgName, callback);
755
+ return findVersion(shareScopeMap[scope][pkgName], callback);
743
756
  }
744
757
  function getFindShareFunction(strategy) {
745
758
  if (strategy === 'loaded-first') {
@@ -802,5 +815,25 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
802
815
  function getGlobalShareScope() {
803
816
  return Global.__FEDERATION__.__SHARE__;
804
817
  }
818
+ function getTargetSharedOptions(options) {
819
+ const { pkgName, extraOptions, shareInfos } = options;
820
+ const defaultResolver = (sharedOptions)=>{
821
+ if (!sharedOptions) {
822
+ return undefined;
823
+ }
824
+ const shareVersionMap = {};
825
+ sharedOptions.forEach((shared)=>{
826
+ shareVersionMap[shared.version] = shared;
827
+ });
828
+ const callback = function(prev, cur) {
829
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
830
+ };
831
+ const maxVersion = findVersion(shareVersionMap, callback);
832
+ return shareVersionMap[maxVersion];
833
+ };
834
+ var _extraOptions_resolver;
835
+ const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
836
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
837
+ }
805
838
 
806
- export { setGlobalFederationConstructor as A, getGlobalFederationInstance as B, getGlobalFederationConstructor as C, DEFAULT_REMOTE_TYPE as D, setGlobalFederationInstance as E, registerGlobalPlugins as F, Global as G, nativeGlobal as H, resetFederationGlobalInfo as I, getTargetSnapshotInfoByModuleInfo as J, globalLoading as a, DEFAULT_SCOPE as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getRegisteredShare as l, getInfoWithoutType as m, getPreloaded as n, setPreloaded as o, getGlobalSnapshotInfoByModuleInfo as p, addGlobalSnapshot as q, setGlobalSnapshotInfoByModuleInfo as r, safeToString as s, getGlobalSnapshot as t, getGlobalShareScope as u, formatShareConfigs as v, warn as w, getBuilderId as x, isBrowserEnv as y, addUniqueItem as z };
839
+ export { isBrowserEnv as A, addUniqueItem as B, setGlobalFederationConstructor as C, DEFAULT_REMOTE_TYPE as D, getGlobalFederationInstance as E, getGlobalFederationConstructor as F, Global as G, setGlobalFederationInstance as H, registerGlobalPlugins as I, nativeGlobal as J, resetFederationGlobalInfo as K, getTargetSnapshotInfoByModuleInfo as L, globalLoading as a, DEFAULT_SCOPE as b, getRemoteEntryExports as c, assert as d, getFMId as e, error as f, getGlobalHostPlugins as g, isPlainObject as h, isObject as i, isRemoteInfoWithEntry as j, isPureRemoteEntry as k, getInfoWithoutType as l, getPreloaded as m, setPreloaded as n, getRegisteredShare as o, arrayOptions as p, getGlobalSnapshotInfoByModuleInfo as q, addGlobalSnapshot as r, safeToString as s, setGlobalSnapshotInfoByModuleInfo as t, getGlobalSnapshot as u, getGlobalShareScope as v, warn as w, getTargetSharedOptions as x, formatShareConfigs as y, getBuilderId as z };
@@ -136,8 +136,14 @@ export declare class FederationHost {
136
136
  constructor(userOptions: UserOptions);
137
137
  private _setGlobalShareScopeMap;
138
138
  initOptions(userOptions: UserOptions): Options;
139
- loadShare<T>(pkgName: string, customShareInfo?: Partial<Shared>): Promise<false | (() => T | undefined)>;
140
- loadShareSync<T>(pkgName: string, customShareInfo?: Partial<Shared>): () => T | never;
139
+ loadShare<T>(pkgName: string, extraOptions?: {
140
+ customShareInfo?: Partial<Shared>;
141
+ resolver?: (sharedOptions: ShareInfos[string]) => Shared;
142
+ }): Promise<false | (() => T | undefined)>;
143
+ loadShareSync<T>(pkgName: string, extraOptions?: {
144
+ customShareInfo?: Partial<Shared>;
145
+ resolver?: (sharedOptions: ShareInfos[string]) => Shared;
146
+ }): () => T | never;
141
147
  initRawContainer(name: string, url: string, container: RemoteEntryExports): Module;
142
148
  private _getRemoteModuleAndOptions;
143
149
  loadRemote<T>(id: string, options?: {
@@ -15,6 +15,10 @@ export type RemoteInfoOptionalVersion = {
15
15
  version?: string;
16
16
  } & RemoteInfoCommon;
17
17
  export type Remote = (RemoteWithEntry | RemoteWithVersion) & RemoteInfoCommon;
18
+ export type LoadShareExtraOptions = {
19
+ customShareInfo?: Partial<Shared>;
20
+ resolver?: (sharedOptions: ShareInfos[string]) => Shared;
21
+ };
18
22
  export interface RemoteInfo {
19
23
  name: string;
20
24
  version?: string;
@@ -32,7 +36,7 @@ export interface SharedConfig {
32
36
  strictVersion?: boolean;
33
37
  }
34
38
  type SharedBaseArgs = {
35
- version: string;
39
+ version?: string;
36
40
  shareConfig?: SharedConfig;
37
41
  scope?: string | Array<string>;
38
42
  deps?: Array<string>;
@@ -43,7 +47,7 @@ export type ShareArgs = (SharedBaseArgs & {
43
47
  get: SharedGetter;
44
48
  }) | (SharedBaseArgs & {
45
49
  lib: () => Module;
46
- });
50
+ }) | SharedBaseArgs;
47
51
  export type Shared = {
48
52
  version: string;
49
53
  get: SharedGetter;
@@ -69,7 +73,7 @@ export type GlobalShareScopeMap = {
69
73
  [instanceName: string]: ShareScopeMap;
70
74
  };
71
75
  export type ShareInfos = {
72
- [pkgName: string]: Shared;
76
+ [pkgName: string]: Shared[];
73
77
  };
74
78
  export interface Options {
75
79
  id?: string;
@@ -82,7 +86,7 @@ export interface Options {
82
86
  }
83
87
  export type UserOptions = Omit<Optional<Options, 'plugins'>, 'shared' | 'inBrowser'> & {
84
88
  shared?: {
85
- [pkgName: string]: ShareArgs;
89
+ [pkgName: string]: ShareArgs | ShareArgs[];
86
90
  };
87
91
  };
88
92
  export type LoadModuleOptions = {
@@ -1,4 +1,4 @@
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'] | Module['host']['loaderHook']>): void;
4
+ export declare function registerPlugins(plugins: UserOptions['plugins'], hookInstances: Array<FederationHost['hooks'] | FederationHost['snapshotHandler']['hooks'] | Module['host']['loaderHook']>): import("../type").FederationRuntimePlugin[] | undefined;
@@ -1,11 +1,12 @@
1
1
  import { Federation } from '../global';
2
- import { GlobalShareScopeMap, Shared, ShareArgs, ShareInfos, ShareScopeMap } from '../type';
2
+ import { GlobalShareScopeMap, Shared, ShareArgs, ShareInfos, ShareScopeMap, LoadShareExtraOptions, UserOptions } from '../type';
3
3
  import { SyncWaterfallHook } from './hooks';
4
- export declare function formatShare(shareArgs: ShareArgs, from: string): Shared;
5
- export declare function formatShareConfigs(shareArgs: {
6
- [pkgName: string]: ShareArgs;
7
- }, from: string): ShareInfos;
8
- export declare function getRegisteredShare(localShareScopeMap: ShareScopeMap, pkgName: string, shareInfo: ShareInfos[keyof ShareInfos], resolveShare: SyncWaterfallHook<{
4
+ export declare function formatShare(shareArgs: ShareArgs, from: string, name: string): Shared;
5
+ export declare function formatShareConfigs(shareArgs: UserOptions['shared'], from: string): ShareInfos;
6
+ export declare function versionLt(a: string, b: string): boolean;
7
+ export declare const findVersion: (shareVersionMap: ShareScopeMap[string][string], cb?: ((prev: string, cur: string) => boolean) | undefined) => string;
8
+ export declare const isLoaded: (shared: Shared) => boolean;
9
+ export declare function getRegisteredShare(localShareScopeMap: ShareScopeMap, pkgName: string, shareInfo: Shared, resolveShare: SyncWaterfallHook<{
9
10
  shareScopeMap: ShareScopeMap;
10
11
  scope: string;
11
12
  pkgName: string;
@@ -14,3 +15,8 @@ export declare function getRegisteredShare(localShareScopeMap: ShareScopeMap, pk
14
15
  resolver: () => Shared | undefined;
15
16
  }>): Shared | void;
16
17
  export declare function getGlobalShareScope(): GlobalShareScopeMap;
18
+ export declare function getTargetSharedOptions(options: {
19
+ pkgName: string;
20
+ extraOptions?: LoadShareExtraOptions;
21
+ shareInfos: ShareInfos;
22
+ }): Shared & Partial<Shared>;
@@ -10,3 +10,4 @@ export declare function isObject(val: any): boolean;
10
10
  export declare const objectToString: () => string;
11
11
  export declare function isPlainObject(val: any): val is object;
12
12
  export declare function isStaticResourcesEqual(url1: string, url2: string): boolean;
13
+ export declare function arrayOptions<T>(options: T | Array<T>): Array<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/runtime",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.esm.js",
@@ -45,6 +45,6 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "@module-federation/sdk": "0.1.4"
48
+ "@module-federation/sdk": "0.1.6"
49
49
  }
50
50
  }