@module-federation/vite 1.16.15 → 1.16.16

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/lib/index.d.ts CHANGED
@@ -49,7 +49,7 @@ type ModuleFederationOptions = {
49
49
  version?: string;
50
50
  shareScope?: string;
51
51
  singleton?: boolean;
52
- requiredVersion?: string;
52
+ requiredVersion?: moduleFederationPlugin.SharedConfig['requiredVersion'];
53
53
  strictVersion?: boolean;
54
54
  import?: moduleFederationPlugin.SharedConfig['import'];
55
55
  }> | undefined;
package/lib/index.js CHANGED
@@ -360,7 +360,7 @@ function searchPackageVersion(sharedName) {
360
360
  return typeof version === "string" ? version : void 0;
361
361
  }
362
362
  function inferVersionFromRequiredVersion(requiredVersion) {
363
- if (!requiredVersion) return void 0;
363
+ if (typeof requiredVersion !== "string") return void 0;
364
364
  return requiredVersion.match(/\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?/)?.[0];
365
365
  }
366
366
  function getLitExportSubpathShares(sharedName) {
@@ -393,7 +393,7 @@ function normalizeShareItem(key, shareItem) {
393
393
  shareConfig: {
394
394
  import: shareItem.import,
395
395
  singleton: shareItem.singleton || false,
396
- requiredVersion: shareItem.requiredVersion || (isImportFalse || shareItem.version ? "*" : version ? `^${version}` : "*"),
396
+ requiredVersion: shareItem.requiredVersion !== void 0 ? shareItem.requiredVersion : isImportFalse || shareItem.version ? "*" : version ? `^${version}` : "*",
397
397
  strictVersion: !!shareItem.strictVersion
398
398
  }
399
399
  };
@@ -658,7 +658,7 @@ function getSsrCapabilities(viteMajor, command, hasRemotes) {
658
658
  * @returns {string} The resulting JavaScript source code string.
659
659
  */
660
660
  function serializeRuntimeOptions(options) {
661
- const seenObjects = /* @__PURE__ */ new WeakSet();
661
+ const ancestors = /* @__PURE__ */ new WeakSet();
662
662
  /**
663
663
  * Recursive inner function to serialize any value into a source code string.
664
664
  */
@@ -676,16 +676,18 @@ function serializeRuntimeOptions(options) {
676
676
  if (val instanceof Date) return `new Date(${JSON.stringify(val.toISOString())})`;
677
677
  if (val instanceof RegExp) return `new RegExp(${JSON.stringify(val.source)}, ${JSON.stringify(val.flags)})`;
678
678
  if (type === "object") {
679
- if (seenObjects.has(val)) return `"__circular__"`;
680
- seenObjects.add(val);
681
- }
682
- if (Array.isArray(val)) return `[${val.map(valueToCode).join(", ")}]`;
683
- if (val instanceof Map) return `new Map([${Array.from(val.entries()).map(([k, v]) => `[${valueToCode(k)}, ${valueToCode(v)}]`).join(", ")}])`;
684
- if (val instanceof Set) return `new Set([${Array.from(val.values()).map(valueToCode).join(", ")}])`;
685
- if (type === "object") {
686
- const properties = [];
687
- for (const key in val) if (Object.prototype.hasOwnProperty.call(val, key)) properties.push(`${JSON.stringify(key)}: ${valueToCode(val[key])}`);
688
- return `{${properties.join(", ")}}`;
679
+ if (ancestors.has(val)) return `"__circular__"`;
680
+ ancestors.add(val);
681
+ try {
682
+ if (Array.isArray(val)) return `[${val.map(valueToCode).join(", ")}]`;
683
+ if (val instanceof Map) return `new Map([${Array.from(val.entries()).map(([k, v]) => `[${valueToCode(k)}, ${valueToCode(v)}]`).join(", ")}])`;
684
+ if (val instanceof Set) return `new Set([${Array.from(val.values()).map(valueToCode).join(", ")}])`;
685
+ const properties = [];
686
+ for (const key in val) if (Object.prototype.hasOwnProperty.call(val, key)) properties.push(`${JSON.stringify(key)}: ${valueToCode(val[key])}`);
687
+ return `{${properties.join(", ")}}`;
688
+ } finally {
689
+ ancestors.delete(val);
690
+ }
689
691
  }
690
692
  return JSON.stringify(String(val));
691
693
  }
@@ -1619,7 +1621,7 @@ function generateLocalSharedImportMap() {
1619
1621
  return `
1620
1622
  import {loadShare} from "@module-federation/runtime";
1621
1623
  const importMap = {
1622
- ${Array.from(getUsedShares()).sort().map((pkg) => {
1624
+ ${getOrderedUsedShares().map((pkg) => {
1623
1625
  const shareItem = getNormalizeShareItem(pkg);
1624
1626
  return `
1625
1627
  ${JSON.stringify(pkg)}: async () => {
@@ -1630,7 +1632,7 @@ function generateLocalSharedImportMap() {
1630
1632
  }).join(",")}
1631
1633
  }
1632
1634
  const usedShared = {
1633
- ${Array.from(getUsedShares()).sort().map((key) => {
1635
+ ${getOrderedUsedShares().map((key) => {
1634
1636
  const shareItem = getNormalizeShareItem(key);
1635
1637
  if (!shareItem) return null;
1636
1638
  return `
@@ -1706,9 +1708,15 @@ function getOrderedUsedShares() {
1706
1708
  }
1707
1709
  function orderSharedDependenciesFirst(sharedPackages) {
1708
1710
  const sharedKeyByPackageName = /* @__PURE__ */ new Map();
1711
+ const subpathKeysByPackageName = /* @__PURE__ */ new Map();
1709
1712
  sharedPackages.forEach((pkg) => {
1710
1713
  const packageName = getPackageName(pkg);
1711
1714
  if (!sharedKeyByPackageName.get(packageName) || pkg === packageName) sharedKeyByPackageName.set(packageName, pkg);
1715
+ if (pkg !== packageName) {
1716
+ const subpaths = subpathKeysByPackageName.get(packageName);
1717
+ if (subpaths) subpaths.push(pkg);
1718
+ else subpathKeysByPackageName.set(packageName, [pkg]);
1719
+ }
1712
1720
  });
1713
1721
  const visiting = /* @__PURE__ */ new Set();
1714
1722
  const visited = /* @__PURE__ */ new Set();
@@ -1717,7 +1725,8 @@ function orderSharedDependenciesFirst(sharedPackages) {
1717
1725
  if (visited.has(pkg)) return;
1718
1726
  if (visiting.has(pkg)) return;
1719
1727
  visiting.add(pkg);
1720
- const packageJson = getInstalledPackageJson(pkg)?.packageJson;
1728
+ const packageName = getPackageName(pkg);
1729
+ const packageJson = getInstalledPackageJson(pkg)?.packageJson ?? (pkg !== packageName ? getInstalledPackageJson(packageName)?.packageJson : void 0);
1721
1730
  const dependencies = {
1722
1731
  ...packageJson?.dependencies || {},
1723
1732
  ...packageJson?.peerDependencies || {},
@@ -1727,6 +1736,7 @@ function orderSharedDependenciesFirst(sharedPackages) {
1727
1736
  const sharedDependency = sharedKeyByPackageName.get(dependency);
1728
1737
  if (sharedDependency) visit(sharedDependency);
1729
1738
  });
1739
+ if (pkg === packageName) (subpathKeysByPackageName.get(packageName) || []).forEach(visit);
1730
1740
  visiting.delete(pkg);
1731
1741
  visited.add(pkg);
1732
1742
  ordered.push(pkg);
@@ -1800,8 +1810,33 @@ function hasImportFalseShared$1(options) {
1800
1810
  return Object.values(options.shared ?? {}).some((share) => share?.shareConfig?.import === false);
1801
1811
  }
1802
1812
  function generateRuntimeSharedCacheSeedCode() {
1813
+ const seedOrder = getOrderedUsedShares();
1803
1814
  return `
1804
- for (const [pkg, share] of Object.entries(usedShared)) {
1815
+ const __mfSeedOrder = ${JSON.stringify(seedOrder)};
1816
+ const __mfSeedKeys = __mfSeedOrder.filter((pkg) => usedShared[pkg] !== undefined);
1817
+ const __mfSeedPackageName = (pkg) => pkg.startsWith('@')
1818
+ ? pkg.split('/').slice(0, 2).join('/')
1819
+ : pkg.split('/')[0];
1820
+ for (const pkg of Object.keys(usedShared)) {
1821
+ if (__mfSeedKeys.includes(pkg)) continue;
1822
+ const packageName = __mfSeedPackageName(pkg);
1823
+ const rootIndex = __mfSeedKeys.indexOf(packageName);
1824
+ if (rootIndex === -1) {
1825
+ __mfSeedKeys.push(pkg);
1826
+ continue;
1827
+ }
1828
+ let insertIndex = rootIndex;
1829
+ while (
1830
+ insertIndex > 0 &&
1831
+ __mfSeedPackageName(__mfSeedKeys[insertIndex - 1]) === packageName &&
1832
+ __mfSeedKeys[insertIndex - 1] !== packageName
1833
+ ) {
1834
+ insertIndex--;
1835
+ }
1836
+ __mfSeedKeys.splice(insertIndex, 0, pkg);
1837
+ }
1838
+ for (const pkg of __mfSeedKeys) {
1839
+ const share = usedShared[pkg];
1805
1840
  const cacheDescriptor = __mfGetSharedCacheDescriptor(pkg, share.shareConfig?.singleton, share.version, share.scope);
1806
1841
  if (share.shareConfig?.import === false || __mfReadSharedCache(__mfModuleCache.share, cacheDescriptor) !== undefined) {
1807
1842
  continue;
@@ -1833,7 +1868,7 @@ function getHostAutoInitSharedSeedItems() {
1833
1868
  return getOrderedUsedShares().map((pkg) => ({
1834
1869
  pkg,
1835
1870
  shareItem: getShareItemForPreload(pkg)
1836
- })).filter(({ shareItem }) => shareItem?.shareConfig.import === false).sort((a, b) => {
1871
+ })).filter(({ shareItem }) => shareItem?.shareConfig?.import === false).sort((a, b) => {
1837
1872
  const priority = (pkg) => pkg === "vue" ? 0 : pkg === "pinia" ? 1 : 2;
1838
1873
  const aIsLocal = !!getLocalProviderImportPath(a.pkg);
1839
1874
  const bIsLocal = !!getLocalProviderImportPath(b.pkg);
@@ -2042,6 +2077,7 @@ let currentHostAutoInitRemoteEntryId = REMOTE_ENTRY_ID;
2042
2077
  let currentHostAutoInitCommand = "build";
2043
2078
  function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
2044
2079
  const shouldPreloadShares = getNormalizeModuleFederationOptions().shareStrategy !== "loaded-first";
2080
+ const hostInitShareOrder = JSON.stringify(getOrderedUsedShares());
2045
2081
  return `
2046
2082
  ${getRuntimeModuleCacheBootstrapCode()}
2047
2083
  let hostInitPromise;
@@ -2055,7 +2091,11 @@ function generateHostAutoInitCode(remoteEntryImport, _command = "build") {
2055
2091
  const {usedShared} = await import("${getLocalSharedImportMapPath()}");
2056
2092
  ${normalizeRuntimeShareCode}
2057
2093
  ${shouldPreloadShares ? `
2058
- for (const [pkg, share] of Object.entries(usedShared)) {
2094
+ const __mfHostInitShareOrder = ${hostInitShareOrder}
2095
+ .concat(Object.keys(usedShared).filter((pkg) => !${hostInitShareOrder}.includes(pkg)));
2096
+ for (const pkg of __mfHostInitShareOrder) {
2097
+ const share = usedShared[pkg];
2098
+ if (!share) continue;
2059
2099
  const cacheDescriptor = __mfGetSharedCacheDescriptor(pkg, share.shareConfig?.singleton, share.version, share.scope);
2060
2100
  if (__mfReadSharedCache(__mfModuleCache.share, cacheDescriptor) !== undefined) {
2061
2101
  continue;
@@ -4010,7 +4050,7 @@ const Manifest = () => {
4010
4050
  const varRemoteEntry = varFilename ? {
4011
4051
  name: varFilename,
4012
4052
  path: "",
4013
- type: "module"
4053
+ type: "var"
4014
4054
  } : void 0;
4015
4055
  const remotes = Array.from(Object.entries(getUsedRemotesMap())).flatMap(([remoteKey, modules]) => Array.from(modules).map((moduleKey) => ({
4016
4056
  federationContainerName: options.remotes[remoteKey].entry,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.16.15",
3
+ "version": "1.16.16",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",