@module-federation/runtime 0.0.0-next-20240411081414 → 0.0.0-next-20240411115459

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/README.md CHANGED
@@ -50,9 +50,25 @@ type InitOptions {
50
50
  // List of dependencies that need to be shared by the current host
51
51
  // When using the build plugin, users can configure the dependencies that need to be shared in the build plugin, and the build plugin will inject the dependencies that need to be shared into the runtime shared configuration.
52
52
  // Shared must be manually passed in the version instance reference when it is passed in at runtime, because it cannot be directly passed in at runtime.
53
- shared?: ShareInfos;
53
+ shared?: {
54
+ [pkgName: string]: ShareArgs | ShareArgs[];
55
+ };
54
56
  };
55
57
 
58
+ type ShareArgs =
59
+ | (SharedBaseArgs & { get: SharedGetter })
60
+ | (SharedBaseArgs & { lib: () => Module });
61
+
62
+ type SharedBaseArgs = {
63
+ version: string;
64
+ shareConfig?: SharedConfig;
65
+ scope?: string | Array<string>;
66
+ deps?: Array<string>;
67
+ strategy?: 'version-first' | 'loaded-first';
68
+ };
69
+
70
+ type SharedGetter = (() => () => Module) | (() => Promise<() => Module>);
71
+
56
72
  type RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {
57
73
  alias?: string;
58
74
  };
@@ -120,7 +136,7 @@ loadRemote('app2/util').then((m) => m.add(1, 2, 3));
120
136
 
121
137
  ### loadShare
122
138
 
123
- - Type: `loadShare(pkgName: string)`
139
+ - Type: `loadShare(pkgName: string, extraOptions?: { customShareInfo?: Partial<Shared>;resolver?: (sharedOptions: ShareInfos[string]) => Shared;})`
124
140
  - Gets the `share` dependency. When there are `share` dependencies that match the current `host` in the global environment, the existing and satisfying `share` dependencies will be reused first. Otherwise, load its own dependencies and store them in the global cache.
125
141
  - This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.
126
142
 
@@ -161,6 +177,50 @@ loadShare('react').then((reactFactory) => {
161
177
  });
162
178
  ```
163
179
 
180
+ If has set multiple version shared, `loadShare` will return the loaded and has max version shared. The behavior can be controlled by set `extraOptions.resolver`:
181
+
182
+ ```js
183
+ import { init, loadRemote, loadShare } from '@module-federation/runtime';
184
+
185
+ init({
186
+ name: '@demo/main-app',
187
+ remotes: [],
188
+ shared: {
189
+ react: [
190
+ {
191
+ version: '17.0.0',
192
+ scope: 'default',
193
+ get: async ()=>() => ({ version: '17.0.0)' }),
194
+ shareConfig: {
195
+ singleton: true,
196
+ requiredVersion: '^17.0.0',
197
+ },
198
+ },
199
+ {
200
+ version: '18.0.0',
201
+ scope: 'default',
202
+ // pass lib means the shared has loaded
203
+ lib: () => ({ version: '18.0.0)' }),
204
+ shareConfig: {
205
+ singleton: true,
206
+ requiredVersion: '^18.0.0',
207
+ },
208
+ },
209
+ ],
210
+ },
211
+ });
212
+
213
+ loadShare('react', {
214
+ resolver: (sharedOptions) => {
215
+ return (
216
+ sharedOptions.find((i) => i.version === '17.0.0') ?? sharedOptions[0]
217
+ );
218
+ },
219
+ }).then((reactFactory) => {
220
+ console.log(reactFactory()); // { version: '17.0.0)' }
221
+ });
222
+ ```
223
+
164
224
  ### preloadRemote
165
225
 
166
226
  - Type: `preloadRemote(preloadOptions: Array<PreloadRemoteArgs>)`
@@ -1,4 +1,4 @@
1
- import { l as getRegisteredShare, u as getGlobalShareScope, G as Global, H as nativeGlobal, I as resetFederationGlobalInfo, B as getGlobalFederationInstance, E as setGlobalFederationInstance, C as getGlobalFederationConstructor, A as setGlobalFederationConstructor, m as getInfoWithoutType, t as getGlobalSnapshot, J as getTargetSnapshotInfoByModuleInfo, p as getGlobalSnapshotInfoByModuleInfo, r as setGlobalSnapshotInfoByModuleInfo, q as addGlobalSnapshot, c as getRemoteEntryExports, F as registerGlobalPlugins, g as getGlobalHostPlugins, n as getPreloaded, o as setPreloaded } from './share.esm.js';
1
+ import { o as getRegisteredShare, v as getGlobalShareScope, G as Global, J as nativeGlobal, K as resetFederationGlobalInfo, E as getGlobalFederationInstance, H as setGlobalFederationInstance, F as getGlobalFederationConstructor, C as setGlobalFederationConstructor, l as getInfoWithoutType, u as getGlobalSnapshot, L as getTargetSnapshotInfoByModuleInfo, q as getGlobalSnapshotInfoByModuleInfo, t as setGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, c as getRemoteEntryExports, I as registerGlobalPlugins, g as getGlobalHostPlugins, m as getPreloaded, n as setPreloaded } from './share.esm.js';
2
2
 
3
3
  const ShareUtils = {
4
4
  getRegisteredShare,
package/dist/index.cjs.js CHANGED
@@ -813,23 +813,31 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
813
813
  }
814
814
  }, true, memo, remoteSnapshot);
815
815
  if (remoteSnapshot.shared) {
816
- remoteSnapshot.shared.forEach((shared)=>{
817
- var _options_shared;
818
- const shareInfo = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
819
- // When data is downgraded, the shared configuration may be different.
820
- if (!shareInfo) {
821
- return;
822
- }
823
- const registeredShared = share.getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
816
+ const collectSharedAssets = (shareInfo, snapshotShared)=>{
817
+ const registeredShared = share.getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
824
818
  // If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
825
819
  if (registeredShared && typeof registeredShared.lib === 'function') {
826
- shared.assets.js.sync.forEach((asset)=>{
820
+ snapshotShared.assets.js.sync.forEach((asset)=>{
827
821
  loadedSharedJsAssets.add(asset);
828
822
  });
829
- shared.assets.css.sync.forEach((asset)=>{
823
+ snapshotShared.assets.css.sync.forEach((asset)=>{
830
824
  loadedSharedCssAssets.add(asset);
831
825
  });
832
826
  }
827
+ };
828
+ remoteSnapshot.shared.forEach((shared)=>{
829
+ var _options_shared;
830
+ const shareInfos = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
831
+ // if no version, preload all shared
832
+ const shareInfo = shared.version ? shareInfos.find((s)=>s.version === shared.version) : shareInfos;
833
+ // When data is downgraded, the shared configuration may be different.
834
+ if (!shareInfo) {
835
+ return;
836
+ }
837
+ const arrayShareInfo = share.arrayOptions(shareInfo);
838
+ arrayShareInfo.forEach((s)=>{
839
+ collectSharedAssets(s, shared);
840
+ });
833
841
  });
834
842
  }
835
843
  const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
@@ -1114,13 +1122,16 @@ class FederationHost {
1114
1122
  this.options = options;
1115
1123
  return options;
1116
1124
  }
1117
- async loadShare(pkgName, customShareInfo) {
1118
- var _this_options_shared;
1125
+ async loadShare(pkgName, extraOptions) {
1119
1126
  // This function performs the following steps:
1120
1127
  // 1. Checks if the currently loaded share already exists, if not, it throws an error
1121
1128
  // 2. Searches globally for a matching share, if found, it uses it directly
1122
1129
  // 3. If not found, it retrieves it from the current share and stores the obtained share globally.
1123
- const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
1130
+ const shareInfo = share.getTargetSharedOptions({
1131
+ pkgName,
1132
+ extraOptions,
1133
+ shareInfos: this.options.shared
1134
+ });
1124
1135
  if (shareInfo == null ? void 0 : shareInfo.scope) {
1125
1136
  await Promise.all(shareInfo.scope.map(async (shareScope)=>{
1126
1137
  await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
@@ -1179,7 +1190,7 @@ class FederationHost {
1179
1190
  });
1180
1191
  return loading;
1181
1192
  } else {
1182
- if (customShareInfo) {
1193
+ if (extraOptions == null ? void 0 : extraOptions.customShareInfo) {
1183
1194
  return false;
1184
1195
  }
1185
1196
  const asyncLoadProcess = async ()=>{
@@ -1210,9 +1221,12 @@ class FederationHost {
1210
1221
  // 1. If the loaded shared already exists globally, then it will be reused
1211
1222
  // 2. If lib exists in local shared, it will be used directly
1212
1223
  // 3. If the local get returns something other than Promise, then it will be used directly
1213
- loadShareSync(pkgName, customShareInfo) {
1214
- var _this_options_shared;
1215
- const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
1224
+ loadShareSync(pkgName, extraOptions) {
1225
+ const shareInfo = share.getTargetSharedOptions({
1226
+ pkgName,
1227
+ extraOptions,
1228
+ shareInfos: this.options.shared
1229
+ });
1216
1230
  if (shareInfo == null ? void 0 : shareInfo.scope) {
1217
1231
  shareInfo.scope.forEach((shareScope)=>{
1218
1232
  this.initializeSharing(shareScope, shareInfo.strategy);
@@ -1452,9 +1466,11 @@ class FederationHost {
1452
1466
  };
1453
1467
  Object.keys(this.options.shared).forEach((shareName)=>{
1454
1468
  const shared = this.options.shared[shareName];
1455
- if (shared.scope.includes(shareScopeName)) {
1456
- register(shareName, shared);
1457
- }
1469
+ shared.forEach((s)=>{
1470
+ if (s.scope.includes(shareScopeName)) {
1471
+ register(shareName, s);
1472
+ }
1473
+ });
1458
1474
  });
1459
1475
  if (strategy === 'version-first') {
1460
1476
  this.options.remotes.forEach((remote)=>{
@@ -1475,7 +1491,19 @@ class FederationHost {
1475
1491
  }
1476
1492
  formatOptions(globalOptions, userOptions) {
1477
1493
  const formatShareOptions = share.formatShareConfigs(userOptions.shared || {}, userOptions.name);
1478
- const shared = _extends({}, globalOptions.shared, formatShareOptions);
1494
+ const shared = _extends({}, globalOptions.shared);
1495
+ Object.keys(formatShareOptions).forEach((shareKey)=>{
1496
+ if (!shared[shareKey]) {
1497
+ shared[shareKey] = formatShareOptions[shareKey];
1498
+ } else {
1499
+ formatShareOptions[shareKey].forEach((newUserSharedOptions)=>{
1500
+ const isSameVersion = shared[shareKey].find((s)=>s.version === newUserSharedOptions.version);
1501
+ if (!isSameVersion) {
1502
+ shared[shareKey].push(newUserSharedOptions);
1503
+ }
1504
+ });
1505
+ }
1506
+ });
1479
1507
  const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
1480
1508
  origin: this,
1481
1509
  userOptions,
@@ -1493,17 +1521,19 @@ class FederationHost {
1493
1521
  const sharedKeys = Object.keys(formatShareOptions);
1494
1522
  sharedKeys.forEach((sharedKey)=>{
1495
1523
  const sharedVal = formatShareOptions[sharedKey];
1496
- const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
1497
- if (!registeredShared && sharedVal && sharedVal.lib) {
1498
- this.setShared({
1499
- pkgName: sharedKey,
1500
- lib: sharedVal.lib,
1501
- get: sharedVal.get,
1502
- loaded: true,
1503
- shared: sharedVal,
1504
- from: userOptions.name
1505
- });
1506
- }
1524
+ sharedVal.forEach((s)=>{
1525
+ const registeredShared = share.getRegisteredShare(this.shareScopeMap, sharedKey, s, this.hooks.lifecycle.resolveShare);
1526
+ if (!registeredShared && s && s.lib) {
1527
+ this.setShared({
1528
+ pkgName: sharedKey,
1529
+ lib: s.lib,
1530
+ get: s.get,
1531
+ loaded: true,
1532
+ shared: s,
1533
+ from: userOptions.name
1534
+ });
1535
+ }
1536
+ });
1507
1537
  });
1508
1538
  const plugins = [
1509
1539
  ...globalOptionsRes.plugins
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getRegisteredShare, m as getInfoWithoutType, n as getPreloaded, o as setPreloaded, p as getGlobalSnapshotInfoByModuleInfo, q as addGlobalSnapshot, r as setGlobalSnapshotInfoByModuleInfo, t as getGlobalSnapshot, G as Global, u as getGlobalShareScope, v as formatShareConfigs, x as getBuilderId, y as isBrowserEnv, z as addUniqueItem, A as setGlobalFederationConstructor, B as getGlobalFederationInstance, C as getGlobalFederationConstructor, E as setGlobalFederationInstance } from './share.esm.js';
2
- export { F as registerGlobalPlugins } from './share.esm.js';
1
+ import { g as getGlobalHostPlugins, a as globalLoading, D as DEFAULT_REMOTE_TYPE, b as DEFAULT_SCOPE, c as getRemoteEntryExports, d as assert, s as safeToString, e as getFMId, i as isObject, f as error, w as warn, h as isPlainObject, j as isRemoteInfoWithEntry, k as isPureRemoteEntry, l as getInfoWithoutType, m as getPreloaded, n as setPreloaded, o as getRegisteredShare, p as arrayOptions, q as getGlobalSnapshotInfoByModuleInfo, r as addGlobalSnapshot, t as setGlobalSnapshotInfoByModuleInfo, u as getGlobalSnapshot, G as Global, v as getGlobalShareScope, x as getTargetSharedOptions, y as formatShareConfigs, z as getBuilderId, A as isBrowserEnv, B as addUniqueItem, C as setGlobalFederationConstructor, E as getGlobalFederationInstance, F as getGlobalFederationConstructor, H as setGlobalFederationInstance } from './share.esm.js';
2
+ export { I as registerGlobalPlugins } from './share.esm.js';
3
3
  import { loadScriptNode, loadScript, composeKeyWithSeparator, createLink, getResourceUrl, isManifestProvider, generateSnapshotFromManifest } from '@module-federation/sdk';
4
4
  export { loadScript, loadScriptNode } from '@module-federation/sdk';
5
5
 
@@ -811,23 +811,31 @@ function generatePreloadAssets(origin, preloadOptions, remote, globalSnapshot, r
811
811
  }
812
812
  }, true, memo, remoteSnapshot);
813
813
  if (remoteSnapshot.shared) {
814
- remoteSnapshot.shared.forEach((shared)=>{
815
- var _options_shared;
816
- const shareInfo = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
817
- // When data is downgraded, the shared configuration may be different.
818
- if (!shareInfo) {
819
- return;
820
- }
821
- const registeredShared = getRegisteredShare(origin.shareScopeMap, shared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
814
+ const collectSharedAssets = (shareInfo, snapshotShared)=>{
815
+ const registeredShared = getRegisteredShare(origin.shareScopeMap, snapshotShared.sharedName, shareInfo, origin.hooks.lifecycle.resolveShare);
822
816
  // If the global share does not exist, or the lib function does not exist, it means that the shared has not been loaded yet and can be preloaded.
823
817
  if (registeredShared && typeof registeredShared.lib === 'function') {
824
- shared.assets.js.sync.forEach((asset)=>{
818
+ snapshotShared.assets.js.sync.forEach((asset)=>{
825
819
  loadedSharedJsAssets.add(asset);
826
820
  });
827
- shared.assets.css.sync.forEach((asset)=>{
821
+ snapshotShared.assets.css.sync.forEach((asset)=>{
828
822
  loadedSharedCssAssets.add(asset);
829
823
  });
830
824
  }
825
+ };
826
+ remoteSnapshot.shared.forEach((shared)=>{
827
+ var _options_shared;
828
+ const shareInfos = (_options_shared = options.shared) == null ? void 0 : _options_shared[shared.sharedName];
829
+ // if no version, preload all shared
830
+ const shareInfo = shared.version ? shareInfos.find((s)=>s.version === shared.version) : shareInfos;
831
+ // When data is downgraded, the shared configuration may be different.
832
+ if (!shareInfo) {
833
+ return;
834
+ }
835
+ const arrayShareInfo = arrayOptions(shareInfo);
836
+ arrayShareInfo.forEach((s)=>{
837
+ collectSharedAssets(s, shared);
838
+ });
831
839
  });
832
840
  }
833
841
  const needPreloadJsAssets = jsAssets.filter((asset)=>!loadedSharedJsAssets.has(asset));
@@ -1112,13 +1120,16 @@ class FederationHost {
1112
1120
  this.options = options;
1113
1121
  return options;
1114
1122
  }
1115
- async loadShare(pkgName, customShareInfo) {
1116
- var _this_options_shared;
1123
+ async loadShare(pkgName, extraOptions) {
1117
1124
  // This function performs the following steps:
1118
1125
  // 1. Checks if the currently loaded share already exists, if not, it throws an error
1119
1126
  // 2. Searches globally for a matching share, if found, it uses it directly
1120
1127
  // 3. If not found, it retrieves it from the current share and stores the obtained share globally.
1121
- const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
1128
+ const shareInfo = getTargetSharedOptions({
1129
+ pkgName,
1130
+ extraOptions,
1131
+ shareInfos: this.options.shared
1132
+ });
1122
1133
  if (shareInfo == null ? void 0 : shareInfo.scope) {
1123
1134
  await Promise.all(shareInfo.scope.map(async (shareScope)=>{
1124
1135
  await Promise.all(this.initializeSharing(shareScope, shareInfo.strategy));
@@ -1177,7 +1188,7 @@ class FederationHost {
1177
1188
  });
1178
1189
  return loading;
1179
1190
  } else {
1180
- if (customShareInfo) {
1191
+ if (extraOptions == null ? void 0 : extraOptions.customShareInfo) {
1181
1192
  return false;
1182
1193
  }
1183
1194
  const asyncLoadProcess = async ()=>{
@@ -1208,9 +1219,12 @@ class FederationHost {
1208
1219
  // 1. If the loaded shared already exists globally, then it will be reused
1209
1220
  // 2. If lib exists in local shared, it will be used directly
1210
1221
  // 3. If the local get returns something other than Promise, then it will be used directly
1211
- loadShareSync(pkgName, customShareInfo) {
1212
- var _this_options_shared;
1213
- const shareInfo = Object.assign({}, (_this_options_shared = this.options.shared) == null ? void 0 : _this_options_shared[pkgName], customShareInfo);
1222
+ loadShareSync(pkgName, extraOptions) {
1223
+ const shareInfo = getTargetSharedOptions({
1224
+ pkgName,
1225
+ extraOptions,
1226
+ shareInfos: this.options.shared
1227
+ });
1214
1228
  if (shareInfo == null ? void 0 : shareInfo.scope) {
1215
1229
  shareInfo.scope.forEach((shareScope)=>{
1216
1230
  this.initializeSharing(shareScope, shareInfo.strategy);
@@ -1450,9 +1464,11 @@ class FederationHost {
1450
1464
  };
1451
1465
  Object.keys(this.options.shared).forEach((shareName)=>{
1452
1466
  const shared = this.options.shared[shareName];
1453
- if (shared.scope.includes(shareScopeName)) {
1454
- register(shareName, shared);
1455
- }
1467
+ shared.forEach((s)=>{
1468
+ if (s.scope.includes(shareScopeName)) {
1469
+ register(shareName, s);
1470
+ }
1471
+ });
1456
1472
  });
1457
1473
  if (strategy === 'version-first') {
1458
1474
  this.options.remotes.forEach((remote)=>{
@@ -1473,7 +1489,19 @@ class FederationHost {
1473
1489
  }
1474
1490
  formatOptions(globalOptions, userOptions) {
1475
1491
  const formatShareOptions = formatShareConfigs(userOptions.shared || {}, userOptions.name);
1476
- const shared = _extends({}, globalOptions.shared, formatShareOptions);
1492
+ const shared = _extends({}, globalOptions.shared);
1493
+ Object.keys(formatShareOptions).forEach((shareKey)=>{
1494
+ if (!shared[shareKey]) {
1495
+ shared[shareKey] = formatShareOptions[shareKey];
1496
+ } else {
1497
+ formatShareOptions[shareKey].forEach((newUserSharedOptions)=>{
1498
+ const isSameVersion = shared[shareKey].find((s)=>s.version === newUserSharedOptions.version);
1499
+ if (!isSameVersion) {
1500
+ shared[shareKey].push(newUserSharedOptions);
1501
+ }
1502
+ });
1503
+ }
1504
+ });
1477
1505
  const { userOptions: userOptionsRes, options: globalOptionsRes } = this.hooks.lifecycle.beforeInit.emit({
1478
1506
  origin: this,
1479
1507
  userOptions,
@@ -1491,17 +1519,19 @@ class FederationHost {
1491
1519
  const sharedKeys = Object.keys(formatShareOptions);
1492
1520
  sharedKeys.forEach((sharedKey)=>{
1493
1521
  const sharedVal = formatShareOptions[sharedKey];
1494
- const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, sharedVal, this.hooks.lifecycle.resolveShare);
1495
- if (!registeredShared && sharedVal && sharedVal.lib) {
1496
- this.setShared({
1497
- pkgName: sharedKey,
1498
- lib: sharedVal.lib,
1499
- get: sharedVal.get,
1500
- loaded: true,
1501
- shared: sharedVal,
1502
- from: userOptions.name
1503
- });
1504
- }
1522
+ sharedVal.forEach((s)=>{
1523
+ const registeredShared = getRegisteredShare(this.shareScopeMap, sharedKey, s, this.hooks.lifecycle.resolveShare);
1524
+ if (!registeredShared && s && s.lib) {
1525
+ this.setShared({
1526
+ pkgName: sharedKey,
1527
+ lib: s.lib,
1528
+ get: s.get,
1529
+ loaded: true,
1530
+ shared: s,
1531
+ from: userOptions.name
1532
+ });
1533
+ }
1534
+ });
1505
1535
  });
1506
1536
  const plugins = [
1507
1537
  ...globalOptionsRes.plugins
package/dist/package.json CHANGED
@@ -15,19 +15,19 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.cjs.d.ts",
19
18
  "import": "./dist/index.esm.js",
20
- "require": "./dist/index.cjs.js"
19
+ "require": "./dist/index.cjs.js",
20
+ "types": "./dist/index.cjs.d.ts"
21
21
  },
22
22
  "./helpers": {
23
- "types": "./dist/helpers.cjs.d.ts",
24
23
  "import": "./dist/helpers.esm.js",
25
- "require": "./dist/helpers.cjs.js"
24
+ "require": "./dist/helpers.cjs.js",
25
+ "types": "./dist/helpers.cjs.d.ts"
26
26
  },
27
27
  "./types": {
28
- "types": "./dist/types.cjs.d.ts",
29
28
  "import": "./dist/types.esm.js",
30
- "require": "./dist/types.cjs.js"
29
+ "require": "./dist/types.cjs.js",
30
+ "types": "./dist/types.cjs.d.ts"
31
31
  },
32
32
  "./*": "./*"
33
33
  },
package/dist/share.cjs.js CHANGED
@@ -71,6 +71,11 @@ const objectToString = Object.prototype.toString;
71
71
  function isPlainObject(val) {
72
72
  return objectToString.call(val) === '[object Object]';
73
73
  }
74
+ function arrayOptions(options) {
75
+ return Array.isArray(options) ? options : [
76
+ options
77
+ ];
78
+ }
74
79
 
75
80
  function _extends$1() {
76
81
  _extends$1 = Object.assign || function(target) {
@@ -674,7 +679,11 @@ function formatShareConfigs(shareArgs, from) {
674
679
  return {};
675
680
  }
676
681
  return Object.keys(shareArgs).reduce((res, pkgName)=>{
677
- res[pkgName] = formatShare(shareArgs[pkgName], from);
682
+ const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
683
+ res[pkgName] = res[pkgName] || [];
684
+ arrayShareArgs.forEach((shareConfig)=>{
685
+ res[pkgName].push(formatShare(shareConfig, from));
686
+ });
678
687
  return res;
679
688
  }, {});
680
689
  }
@@ -697,12 +706,11 @@ function versionLt(a, b) {
697
706
  return false;
698
707
  }
699
708
  }
700
- const findVersion = (shareScopeMap, scope, pkgName, cb)=>{
701
- const versions = shareScopeMap[scope][pkgName];
709
+ const findVersion = (shareVersionMap, cb)=>{
702
710
  const callback = cb || function(prev, cur) {
703
711
  return versionLt(prev, cur);
704
712
  };
705
- return Object.keys(versions).reduce((prev, cur)=>{
713
+ return Object.keys(shareVersionMap).reduce((prev, cur)=>{
706
714
  if (!prev) {
707
715
  return cur;
708
716
  }
@@ -724,7 +732,7 @@ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
724
732
  const callback = function(prev, cur) {
725
733
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
726
734
  };
727
- return findVersion(shareScopeMap, scope, pkgName, callback);
735
+ return findVersion(shareScopeMap[scope][pkgName], callback);
728
736
  }
729
737
  function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
730
738
  const versions = shareScopeMap[scope][pkgName];
@@ -741,7 +749,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
741
749
  }
742
750
  return versionLt(prev, cur);
743
751
  };
744
- return findVersion(shareScopeMap, scope, pkgName, callback);
752
+ return findVersion(shareScopeMap[scope][pkgName], callback);
745
753
  }
746
754
  function getFindShareFunction(strategy) {
747
755
  if (strategy === 'loaded-first') {
@@ -804,12 +812,33 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
804
812
  function getGlobalShareScope() {
805
813
  return Global.__FEDERATION__.__SHARE__;
806
814
  }
815
+ function getTargetSharedOptions(options) {
816
+ const { pkgName, extraOptions, shareInfos } = options;
817
+ const defaultResolver = (sharedOptions)=>{
818
+ if (!sharedOptions) {
819
+ return undefined;
820
+ }
821
+ const shareVersionMap = {};
822
+ sharedOptions.forEach((shared)=>{
823
+ shareVersionMap[shared.version] = shared;
824
+ });
825
+ const callback = function(prev, cur) {
826
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
827
+ };
828
+ const maxVersion = findVersion(shareVersionMap, callback);
829
+ return shareVersionMap[maxVersion];
830
+ };
831
+ var _extraOptions_resolver;
832
+ const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
833
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
834
+ }
807
835
 
808
836
  exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE;
809
837
  exports.DEFAULT_SCOPE = DEFAULT_SCOPE;
810
838
  exports.Global = Global;
811
839
  exports.addGlobalSnapshot = addGlobalSnapshot;
812
840
  exports.addUniqueItem = addUniqueItem;
841
+ exports.arrayOptions = arrayOptions;
813
842
  exports.assert = assert;
814
843
  exports.error = error;
815
844
  exports.formatShareConfigs = formatShareConfigs;
@@ -825,6 +854,7 @@ exports.getInfoWithoutType = getInfoWithoutType;
825
854
  exports.getPreloaded = getPreloaded;
826
855
  exports.getRegisteredShare = getRegisteredShare;
827
856
  exports.getRemoteEntryExports = getRemoteEntryExports;
857
+ exports.getTargetSharedOptions = getTargetSharedOptions;
828
858
  exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
829
859
  exports.globalLoading = globalLoading;
830
860
  exports.isBrowserEnv = isBrowserEnv;
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) {
@@ -672,7 +677,11 @@ function formatShareConfigs(shareArgs, from) {
672
677
  return {};
673
678
  }
674
679
  return Object.keys(shareArgs).reduce((res, pkgName)=>{
675
- res[pkgName] = formatShare(shareArgs[pkgName], from);
680
+ const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
681
+ res[pkgName] = res[pkgName] || [];
682
+ arrayShareArgs.forEach((shareConfig)=>{
683
+ res[pkgName].push(formatShare(shareConfig, from));
684
+ });
676
685
  return res;
677
686
  }, {});
678
687
  }
@@ -695,12 +704,11 @@ function versionLt(a, b) {
695
704
  return false;
696
705
  }
697
706
  }
698
- const findVersion = (shareScopeMap, scope, pkgName, cb)=>{
699
- const versions = shareScopeMap[scope][pkgName];
707
+ const findVersion = (shareVersionMap, cb)=>{
700
708
  const callback = cb || function(prev, cur) {
701
709
  return versionLt(prev, cur);
702
710
  };
703
- return Object.keys(versions).reduce((prev, cur)=>{
711
+ return Object.keys(shareVersionMap).reduce((prev, cur)=>{
704
712
  if (!prev) {
705
713
  return cur;
706
714
  }
@@ -722,7 +730,7 @@ function findSingletonVersionOrderByVersion(shareScopeMap, scope, pkgName) {
722
730
  const callback = function(prev, cur) {
723
731
  return !isLoaded(versions[prev]) && versionLt(prev, cur);
724
732
  };
725
- return findVersion(shareScopeMap, scope, pkgName, callback);
733
+ return findVersion(shareScopeMap[scope][pkgName], callback);
726
734
  }
727
735
  function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
728
736
  const versions = shareScopeMap[scope][pkgName];
@@ -739,7 +747,7 @@ function findSingletonVersionOrderByLoaded(shareScopeMap, scope, pkgName) {
739
747
  }
740
748
  return versionLt(prev, cur);
741
749
  };
742
- return findVersion(shareScopeMap, scope, pkgName, callback);
750
+ return findVersion(shareScopeMap[scope][pkgName], callback);
743
751
  }
744
752
  function getFindShareFunction(strategy) {
745
753
  if (strategy === 'loaded-first') {
@@ -802,5 +810,25 @@ function getRegisteredShare(localShareScopeMap, pkgName, shareInfo, resolveShare
802
810
  function getGlobalShareScope() {
803
811
  return Global.__FEDERATION__.__SHARE__;
804
812
  }
813
+ function getTargetSharedOptions(options) {
814
+ const { pkgName, extraOptions, shareInfos } = options;
815
+ const defaultResolver = (sharedOptions)=>{
816
+ if (!sharedOptions) {
817
+ return undefined;
818
+ }
819
+ const shareVersionMap = {};
820
+ sharedOptions.forEach((shared)=>{
821
+ shareVersionMap[shared.version] = shared;
822
+ });
823
+ const callback = function(prev, cur) {
824
+ return !isLoaded(shareVersionMap[prev]) && versionLt(prev, cur);
825
+ };
826
+ const maxVersion = findVersion(shareVersionMap, callback);
827
+ return shareVersionMap[maxVersion];
828
+ };
829
+ var _extraOptions_resolver;
830
+ const resolver = (_extraOptions_resolver = extraOptions == null ? void 0 : extraOptions.resolver) != null ? _extraOptions_resolver : defaultResolver;
831
+ return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
832
+ }
805
833
 
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 };
834
+ 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;
@@ -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,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
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<{
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.0.0-next-20240411081414",
3
+ "version": "0.0.0-next-20240411115459",
4
4
  "author": "zhouxiao <codingzx@gmail.com>",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.esm.js",
@@ -15,19 +15,19 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.cjs.d.ts",
19
18
  "import": "./dist/index.esm.js",
20
- "require": "./dist/index.cjs.js"
19
+ "require": "./dist/index.cjs.js",
20
+ "types": "./dist/index.cjs.d.ts"
21
21
  },
22
22
  "./helpers": {
23
- "types": "./dist/helpers.cjs.d.ts",
24
23
  "import": "./dist/helpers.esm.js",
25
- "require": "./dist/helpers.cjs.js"
24
+ "require": "./dist/helpers.cjs.js",
25
+ "types": "./dist/helpers.cjs.d.ts"
26
26
  },
27
27
  "./types": {
28
- "types": "./dist/types.cjs.d.ts",
29
28
  "import": "./dist/types.esm.js",
30
- "require": "./dist/types.cjs.js"
29
+ "require": "./dist/types.cjs.js",
30
+ "types": "./dist/types.cjs.d.ts"
31
31
  },
32
32
  "./*": "./*"
33
33
  },
@@ -45,6 +45,6 @@
45
45
  }
46
46
  },
47
47
  "dependencies": {
48
- "@module-federation/sdk": "0.0.0-next-20240411081414"
48
+ "@module-federation/sdk": "0.0.0-next-20240411115459"
49
49
  }
50
50
  }