fix(module-federation): check the remote project targets for buildTarget to use #30808 (#30867)

## Current Behavior
Module Federation collectRemotes util looks for a hardcoded `build`
target.

## Expected Behavior
Read the remote's targets and find the correct `buildTarget` option for
the module-federation-dev-server to use

## Related Issue(s)

Fixes #30808
This commit is contained in:
Colum Ferry 2025-04-25 16:00:00 +01:00 committed by GitHub
parent 7affa87af9
commit 0c63624407
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
import { logger, type ProjectGraph } from '@nx/devkit'; import { logger, parseTargetString, type ProjectGraph } from '@nx/devkit';
import { registerTsProject } from '@nx/js/src/internal'; import { registerTsProject } from '@nx/js/src/internal';
import { findMatchingProjects } from 'nx/src/utils/find-matching-projects'; import { findMatchingProjects } from 'nx/src/utils/find-matching-projects';
import * as pc from 'picocolors'; import * as pc from 'picocolors';
@ -59,8 +59,35 @@ function collectRemoteProjects(
collected.add(remote); collected.add(remote);
const remoteProjectRoot = remoteProject.root; const remoteProjectRoot = remoteProject.root;
// Find the target that uses the module-federation-dev-server executor
let buildTargetName = 'build';
if (remoteProject.targets) {
for (const [targetKey, targetConfig] of Object.entries(
remoteProject.targets
)) {
const executor = targetConfig.executor || '';
// Extract the portion after the `:` in the executor name
const executorParts = executor.split(':');
const executorName =
executorParts.length > 1 ? executorParts[1] : executor;
if (executorName === 'module-federation-dev-server') {
// Extract the buildTarget from the options
if (targetConfig.options?.buildTarget) {
const parsedTarget = parseTargetString(
targetConfig.options.buildTarget,
context.projectGraph
);
buildTargetName = parsedTarget.target;
break;
}
}
}
}
let remoteProjectTsConfig = let remoteProjectTsConfig =
remoteProject.targets['build'].options.tsConfig ?? remoteProject.targets?.[buildTargetName]?.options?.tsConfig ??
[ [
join(remoteProjectRoot, 'tsconfig.app.json'), join(remoteProjectRoot, 'tsconfig.app.json'),
join(remoteProjectRoot, 'tsconfig.json'), join(remoteProjectRoot, 'tsconfig.json'),