From 4baffffe44670f42848cfcfd89574c452c8d2fe0 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 19 Sep 2024 16:38:57 +0100 Subject: [PATCH] fix(module-federation): collect secondary entry points from exports #26878 --- .../secondary-entry-points.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/webpack/src/utils/module-federation/secondary-entry-points.ts b/packages/webpack/src/utils/module-federation/secondary-entry-points.ts index 272613df7e8e04..46ed5443a6f27e 100644 --- a/packages/webpack/src/utils/module-federation/secondary-entry-points.ts +++ b/packages/webpack/src/utils/module-federation/secondary-entry-points.ts @@ -4,6 +4,7 @@ import { dirname, join, relative } from 'path'; import { existsSync, lstatSync, readdirSync } from 'fs'; import { readJsonFile, joinPathFragments, workspaceRoot } from '@nx/devkit'; import { PackageJson, readModulePackageJson } from 'nx/src/utils/package-json'; +import path from 'node:path'; export function collectWorkspaceLibrarySecondaryEntryPoints( library: WorkspaceLibrary, @@ -105,6 +106,36 @@ export function recursivelyCollectSecondaryEntryPointsFromDirectory( } } +function collectPackagesFromExports( + pkgName: string, + pkgVersion: string, + exports: any | undefined, + collectedPackages: { + name: string; + version: string; + }[] +): void { + for (const [relativeEntryPoint, exportOptions] of Object.entries(exports)) { + if ( + exportOptions['default'] && + exportOptions['default'].search(/\.(js|mjs|cjs)$/) + ) { + let entryPointName = joinPathFragments(pkgName, relativeEntryPoint); + if (entryPointName.endsWith('.json')) { + entryPointName = path.dirname(entryPointName); + } + if (entryPointName === '.') { + continue; + } + if (collectedPackages.find((p) => p.name === entryPointName)) { + continue; + } + + collectedPackages.push({ name: entryPointName, version: pkgVersion }); + } + } +} + export function collectPackageSecondaryEntryPoints( pkgName: string, pkgVersion: string, @@ -130,6 +161,9 @@ export function collectPackageSecondaryEntryPoints( } const { exports } = packageJson; + if (exports) { + collectPackagesFromExports(pkgName, pkgVersion, exports, collectedPackages); + } const subDirs = getNonNodeModulesSubDirs(pathToPackage); recursivelyCollectSecondaryEntryPointsFromDirectory( pkgName,