Skip to content

Commit

Permalink
fix(module-federation): collect secondary entry points from exports #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Sep 19, 2024
1 parent 7d0d834 commit 1f8c66b
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -130,6 +161,9 @@ export function collectPackageSecondaryEntryPoints(
}

const { exports } = packageJson;
if (exports) {
collectPackagesFromExports(pkgName, pkgVersion, exports, collectedPackages);
}
const subDirs = getNonNodeModulesSubDirs(pathToPackage);
recursivelyCollectSecondaryEntryPointsFromDirectory(
pkgName,
Expand Down

0 comments on commit 1f8c66b

Please sign in to comment.