@module-federation/vite 1.23.6 → 1.23.8

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,5 @@
1
1
  import { l as isHttpUrl, n as mfError, t as createModuleFederationError, u as normalizePathForImport } from "./logger-D67aJYOe.js";
2
- import { p as hasPackageDependency, r as watchFileEvents, y as resolveImportPath } from "./dtsConstants-CDuC8Bb7.js";
2
+ import { b as resolveImportPath, m as hasPackageDependency, r as watchFileEvents } from "./dtsConstants-286VtFnC.js";
3
3
  import fs from "fs";
4
4
  import * as path$1 from "node:path";
5
5
  import { normalizeOptions } from "@module-federation/sdk";
@@ -193,9 +193,62 @@ function getRunnerCacheKey(remoteOrigin, resolvedShared, fetchTimeoutMs, fetchMa
193
193
  getSortedRecordEntries(resolvedShared)
194
194
  ]);
195
195
  }
196
- async function resolveSharedExternal(id, resolvedShared) {
196
+ function findResolvedSharedKey(id, resolvedShared) {
197
+ if (resolvedShared[id]) return id;
198
+ let matchedKey;
199
+ for (const key of Object.keys(resolvedShared)) {
200
+ if (!key.endsWith("/")) continue;
201
+ const base = key.slice(0, -1);
202
+ if (id !== base && !id.startsWith(`${base}/`)) continue;
203
+ if (!matchedKey || key.length > matchedKey.length) matchedKey = key;
204
+ }
205
+ return matchedKey;
206
+ }
207
+ function getEsmExportTarget(target) {
208
+ if (typeof target === "string") return target;
209
+ if (!target || typeof target !== "object" || Array.isArray(target)) return void 0;
210
+ for (const [condition, value] of Object.entries(target)) {
211
+ if (condition !== "node" && condition !== "import" && condition !== "default") continue;
212
+ const resolved = getEsmExportTarget(value);
213
+ if (resolved) return resolved;
214
+ }
215
+ }
216
+ async function resolveEsmSubpath(id, base) {
217
+ const { dirname, join } = await _path();
218
+ const { existsSync, readFileSync } = await _fs();
219
+ const segments = id.split("/");
220
+ const nameLength = id.startsWith("@") ? 2 : 1;
221
+ const name = segments.slice(0, nameLength).join("/");
222
+ const subpath = [".", ...segments.slice(nameLength)].join("/");
223
+ for (let dir = dirname(base); dirname(dir) !== dir; dir = dirname(dir)) {
224
+ const packageJson = join(dir, "node_modules", name, "package.json");
225
+ if (!existsSync(packageJson)) continue;
226
+ const { exports } = JSON.parse(readFileSync(packageJson, "utf8"));
227
+ const target = getEsmExportTarget(exports?.[subpath]);
228
+ return target && join(dirname(packageJson), target);
229
+ }
230
+ }
231
+ /**
232
+ * Resolve a shared request from the exact or wildcard build-time map.
233
+ * Wildcard entries point at the package entry, so concrete subpaths need a
234
+ * second Node resolution step to preserve package exports and conditions.
235
+ */
236
+ async function resolveSharedPath(id, resolvedShared) {
197
237
  if (typeof id !== "string") return null;
198
- const resolved = resolvedShared[id];
238
+ const matchedKey = findResolvedSharedKey(id, resolvedShared);
239
+ if (!matchedKey) return null;
240
+ const resolved = resolvedShared[matchedKey];
241
+ if (!resolved) return null;
242
+ if (matchedKey === id || EXTERNAL_URL_RE.test(resolved)) return resolved;
243
+ const { createRequire } = await _module();
244
+ const { join } = await _path();
245
+ for (const base of [resolved, join(process.cwd(), "noop.js")]) try {
246
+ return await resolveEsmSubpath(id, base).catch(() => void 0) ?? createRequire(base).resolve(id);
247
+ } catch {}
248
+ return null;
249
+ }
250
+ async function resolveSharedExternal(id, resolvedShared) {
251
+ const resolved = await resolveSharedPath(id, resolvedShared);
199
252
  if (!resolved) return null;
200
253
  if (EXTERNAL_URL_RE.test(resolved)) return {
201
254
  externalize: resolved,
@@ -752,6 +805,15 @@ function transformSsrCode(code, base, specifiers, sharedPkgMap, requireShimUrl,
752
805
  }
753
806
  return neutralizeBrowserPreloadHelpers(rewriter.toString());
754
807
  }
808
+ async function materializeSharedSubpaths(specifiers, sharedPkgMap) {
809
+ const uniqueSpecifiers = new Set(specifiers.map(({ value }) => value));
810
+ const resolvedShared = Object.fromEntries(sharedPkgMap);
811
+ for (const specifier of uniqueSpecifiers) {
812
+ if (sharedPkgMap.has(specifier)) continue;
813
+ const resolved = await resolveSharedPath(specifier, resolvedShared);
814
+ if (resolved) sharedPkgMap.set(specifier, resolved);
815
+ }
816
+ }
755
817
  function isVitePreloadHelperSpecifier(specifier) {
756
818
  return specifier.includes("preload-helper");
757
819
  }
@@ -808,6 +870,7 @@ async function fetchEsmToTempFile(url, graph) {
808
870
  const tmpPath = await fetchEsmToTempFile(u, graph);
809
871
  subMap.set(u, getTempFileImportUrl(tmpPath, versionKey));
810
872
  }));
873
+ if (sharedPkgMap) await materializeSharedSubpaths(specifiers, sharedPkgMap);
811
874
  const requireShimUrl = sharedPkgMap?.size && specifiers.some(({ value }) => isNodeModuleSpecifier(value)) ? await getRequireShimUrl(tmpDir, sharedPkgMap, contextKey) : void 0;
812
875
  code = transformSsrCode(code, base, specifiers, sharedPkgMap, requireShimUrl, subMap);
813
876
  const { writeFileSync } = await _fs();
@@ -853,7 +916,7 @@ async function importTempModule(filePath, versionKey) {
853
916
  }
854
917
  let warnedVmUnavailable = false;
855
918
  function getVmStrategyModule() {
856
- if (!loaderState.vmStrategyModulePromise) loaderState.vmStrategyModulePromise = import("./ssrVmStrategy-BCaiSEbA.js");
919
+ if (!loaderState.vmStrategyModulePromise) loaderState.vmStrategyModulePromise = import("./ssrVmStrategy-D1cSKUV-.js");
857
920
  return loaderState.vmStrategyModulePromise;
858
921
  }
859
922
  /**
@@ -980,8 +1043,9 @@ async function rebaseResolvedShared(resolvedShared) {
980
1043
  const rebased = {};
981
1044
  for (const [specifier, buildPath] of Object.entries(resolvedShared)) {
982
1045
  rebased[specifier] = buildPath;
1046
+ const resolutionSpecifier = specifier.endsWith("/") ? specifier.slice(0, -1) : specifier;
983
1047
  for (const require of requires) try {
984
- rebased[specifier] = require.resolve(specifier);
1048
+ rebased[specifier] = require.resolve(resolutionSpecifier);
985
1049
  break;
986
1050
  } catch {}
987
1051
  }
@@ -1025,4 +1089,4 @@ function ssrEntryLoaderPlugin(options = {}) {
1025
1089
  };
1026
1090
  }
1027
1091
  //#endregion
1028
- export { ssrEntryLoaderPlugin as a, fetchWithTimeout as c, revalidate as i, getBodyPreview as l, SsrEntryHttpError as n, DEFAULT_SSR_FETCH_MAX_BYTES as o, neutralizeBrowserPreloadHelpers as r, DEFAULT_SSR_FETCH_TIMEOUT_MS as s, SSR_ENTRY_LOADER_STATE_KEY as t, readResponseTextBounded as u };
1092
+ export { revalidate as a, DEFAULT_SSR_FETCH_TIMEOUT_MS as c, readResponseTextBounded as d, resolveSharedPath as i, fetchWithTimeout as l, SsrEntryHttpError as n, ssrEntryLoaderPlugin as o, neutralizeBrowserPreloadHelpers as r, DEFAULT_SSR_FETCH_MAX_BYTES as s, SSR_ENTRY_LOADER_STATE_KEY as t, getBodyPreview as u };
@@ -1,6 +1,6 @@
1
- import { l as isHttpUrl } from "./logger-D67aJYOe.js";
1
+ import { l as isHttpUrl, s as EXTERNAL_URL_RE } from "./logger-D67aJYOe.js";
2
2
  import { t as findSharedKey } from "./sharedKeyMatcher-C04bVN1q.js";
3
- import { c as fetchWithTimeout, l as getBodyPreview, n as SsrEntryHttpError, r as neutralizeBrowserPreloadHelpers, u as readResponseTextBounded } from "./ssrEntryLoader-Bh2Tq1cZ.js";
3
+ import { d as readResponseTextBounded, i as resolveSharedPath, l as fetchWithTimeout, n as SsrEntryHttpError, r as neutralizeBrowserPreloadHelpers, u as getBodyPreview } from "./ssrEntryLoader-DKpOrW8D.js";
4
4
  //#region src/utils/ssrVmStrategy.ts
5
5
  /**
6
6
  * vm.SourceTextModule strategy for loading remote SSR entries.
@@ -69,11 +69,8 @@ async function loadBareModule(specifier, options) {
69
69
  }
70
70
  } catch {}
71
71
  }
72
- const resolvedPath = options.resolvedShared[specifier];
73
- if (resolvedPath) return import(
74
- /* @vite-ignore */
75
- `file://${resolvedPath}`
76
- );
72
+ const resolvedPath = await resolveSharedPath(specifier, options.resolvedShared);
73
+ if (resolvedPath) return EXTERNAL_URL_RE.test(resolvedPath) ? import(resolvedPath) : import(`file://${resolvedPath}`);
77
74
  const namespace = await import(
78
75
  /* @vite-ignore */
79
76
  specifier
@@ -1,6 +1,12 @@
1
1
  import { i as SsrEntryLoaderConfig } from "../normalizeModuleFederationOptions-DNUlv-AL.js";
2
2
  //#region src/utils/ssrEntryLoader.d.ts
3
3
  declare const SSR_ENTRY_LOADER_STATE_KEY: unique symbol;
4
+ /**
5
+ * Resolve a shared request from the exact or wildcard build-time map.
6
+ * Wildcard entries point at the package entry, so concrete subpaths need a
7
+ * second Node resolution step to preserve package exports and conditions.
8
+ */
9
+ declare function resolveSharedPath(id: unknown, resolvedShared: Record<string, string>): Promise<string | null>;
4
10
  interface RemoteInfo {
5
11
  name: string;
6
12
  entry: string;
@@ -65,4 +71,4 @@ declare function ssrEntryLoaderPlugin(options?: SsrEntryLoaderOptions): {
65
71
  } | undefined>;
66
72
  };
67
73
  //#endregion
68
- export { SSR_ENTRY_LOADER_STATE_KEY, SsrEntryHttpError, ssrEntryLoaderPlugin as default, neutralizeBrowserPreloadHelpers, revalidate };
74
+ export { SSR_ENTRY_LOADER_STATE_KEY, SsrEntryHttpError, ssrEntryLoaderPlugin as default, neutralizeBrowserPreloadHelpers, resolveSharedPath, revalidate };
@@ -1,2 +1,2 @@
1
- import { a as ssrEntryLoaderPlugin, i as revalidate, n as SsrEntryHttpError, r as neutralizeBrowserPreloadHelpers, t as SSR_ENTRY_LOADER_STATE_KEY } from "../ssrEntryLoader-Bh2Tq1cZ.js";
2
- export { SSR_ENTRY_LOADER_STATE_KEY, SsrEntryHttpError, ssrEntryLoaderPlugin as default, neutralizeBrowserPreloadHelpers, revalidate };
1
+ import { a as revalidate, i as resolveSharedPath, n as SsrEntryHttpError, o as ssrEntryLoaderPlugin, r as neutralizeBrowserPreloadHelpers, t as SSR_ENTRY_LOADER_STATE_KEY } from "../ssrEntryLoader-DKpOrW8D.js";
2
+ export { SSR_ENTRY_LOADER_STATE_KEY, SsrEntryHttpError, ssrEntryLoaderPlugin as default, neutralizeBrowserPreloadHelpers, resolveSharedPath, revalidate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.23.6",
3
+ "version": "1.23.8",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",