@module-federation/vite 1.21.2 → 1.21.4

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
@@ -304,6 +304,18 @@ federation({
304
304
 
305
305
  `runtime-infer` is useful for local development and falls back to the full dependency when the required exports are not available. `server-calc` is recommended for deployments because it can use aggregated export metadata from all consumers.
306
306
 
307
+ Use `eager: true` when a shared dependency must provide exports during module evaluation. For example, icon libraries may call `jsx()` at module scope before deferred singleton initialization completes. Configure the remote's React share as follows; discovered React subpaths such as `react/jsx-runtime` inherit this setting:
308
+
309
+ ```ts
310
+ federation({
311
+ shared: {
312
+ react: { singleton: true, eager: true },
313
+ },
314
+ });
315
+ ```
316
+
317
+ This bundles the local fallback into the initial entry, so enable it only when synchronous evaluation requires it.
318
+
307
319
  Do not combine `eager: true` with `treeShaking`; eager shared dependencies are bundled into the initial entry and cannot use the on-demand tree-shaking path. Choose eager loading for small dependencies, or tree shaking for larger dependencies such as component libraries.
308
320
 
309
321
  With `server-calc`, the Vite build records the exports used by each application in its generated Module Federation metadata. A deployment service must then collect that metadata for all applications that share the same dependency and version, merge their `usedExports` lists, and use the resulting union to create one optimized secondary shared artifact. For example, if one application uses `Button` and another uses `Input`, the secondary artifact must contain both exports.
@@ -363,7 +363,6 @@ function resolveInstalledPackageJson(pkg, cwd, packageName, opts) {
363
363
  };
364
364
  const findPackageInPnpmStore = (startDir) => {
365
365
  let currentDir = startDir;
366
- const rootDir = path$1.parse(currentDir).root;
367
366
  while (true) {
368
367
  const pnpmStoreDir = path$1.join(currentDir, "node_modules", ".pnpm");
369
368
  if (existsSync(pnpmStoreDir)) try {
@@ -373,50 +372,61 @@ function resolveInstalledPackageJson(pkg, cwd, packageName, opts) {
373
372
  if (candidate?.packageJson.name === packageName) return candidate;
374
373
  }
375
374
  } catch {}
376
- if (currentDir === rootDir) break;
377
- currentDir = path$1.dirname(currentDir);
375
+ const parentDir = path$1.dirname(currentDir);
376
+ if (parentDir === currentDir) break;
377
+ currentDir = parentDir;
378
378
  }
379
379
  };
380
+ let resolvedPath;
380
381
  try {
381
382
  const projectRequire = createRequire(pathToFileURL(path$1.join(cwd, "package.json")));
382
- let resolvedPath;
383
383
  if (opts?.fromResolvedEntry) resolvedPath = opts.fromResolvedEntry;
384
384
  else try {
385
385
  resolvedPath = projectRequire.resolve(pkg);
386
386
  } catch {
387
387
  resolvedPath = projectRequire.resolve(packageName);
388
388
  }
389
+ } catch {
390
+ resolvedPath = void 0;
391
+ }
392
+ if (resolvedPath !== void 0 && !path$1.isAbsolute(resolvedPath)) resolvedPath = void 0;
393
+ if (resolvedPath !== void 0) try {
389
394
  let currentDir = path$1.dirname(resolvedPath);
390
- const rootDir = path$1.parse(currentDir).root;
395
+ let matchingPackage;
391
396
  while (true) {
392
397
  const packageJsonPath = path$1.join(currentDir, "package.json");
393
398
  if (existsSync(packageJsonPath)) {
394
399
  const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
395
400
  try {
396
401
  const packageJson = JSON.parse(packageJsonContent);
397
- if (packageJson.name === packageName) return {
398
- path: packageJsonPath,
399
- dir: currentDir,
400
- packageJson
401
- };
402
+ if (packageJson.name === packageName) {
403
+ const packageInfo = {
404
+ path: packageJsonPath,
405
+ dir: currentDir,
406
+ packageJson
407
+ };
408
+ if (currentDir.endsWith(path$1.join("node_modules", packageName))) return packageInfo;
409
+ matchingPackage ??= packageInfo;
410
+ }
402
411
  } catch (error) {
403
412
  if (!(error instanceof SyntaxError)) throw error;
404
413
  }
405
414
  }
406
- if (currentDir === rootDir) break;
407
- currentDir = path$1.dirname(currentDir);
408
- }
409
- } catch {
410
- let currentDir = cwd;
411
- const rootDir = path$1.parse(currentDir).root;
412
- while (true) {
413
- const directCandidate = tryReadPackageJson(path$1.join(currentDir, "node_modules", packageName, "package.json"));
414
- if (directCandidate?.packageJson.name === packageName) return directCandidate;
415
- if (currentDir === rootDir) break;
416
- currentDir = path$1.dirname(currentDir);
415
+ const parentDir = path$1.dirname(currentDir);
416
+ if (parentDir === currentDir) break;
417
+ currentDir = parentDir;
417
418
  }
418
- return findPackageInPnpmStore(cwd);
419
+ return matchingPackage;
420
+ } catch {}
421
+ let currentDir = cwd;
422
+ while (true) {
423
+ const directCandidate = tryReadPackageJson(path$1.join(currentDir, "node_modules", packageName, "package.json"));
424
+ if (directCandidate?.packageJson.name === packageName) return directCandidate;
425
+ const parentDir = path$1.dirname(currentDir);
426
+ if (parentDir === currentDir) break;
427
+ currentDir = parentDir;
419
428
  }
429
+ return findPackageInPnpmStore(cwd);
420
430
  }
421
431
  function getInstalledPackageEntry(pkg, opts) {
422
432
  const installed = getInstalledPackageJson(pkg, opts);