- feat(module-federation): consolidate module federation utils into module-federation package - chore(module-federation): fix tests and linting <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Our current support for Module Federation relies on utilities that are spread and duplicated across the `@nx/webpack` package and the `@nx/rspack` package. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Now that we have a `@nx/module-federation` package, dedupe the utils and consolidate them into a single package ## Todo - [x] Migrations for React + Angular to install `@nx/module-federation` and point `ModuleFederationConfig` export to that package from webpack.config and rspack.config files
95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import type { ProjectGraph } from '@nx/devkit';
|
|
import type { WorkspaceLibrary } from './models';
|
|
import { readTsPathMappings } from './typescript';
|
|
import {
|
|
getOutputsForTargetAndConfiguration,
|
|
parseTargetString,
|
|
} from '@nx/devkit';
|
|
|
|
export function getDependentPackagesForProject(
|
|
projectGraph: ProjectGraph,
|
|
name: string
|
|
): {
|
|
workspaceLibraries: WorkspaceLibrary[];
|
|
npmPackages: string[];
|
|
} {
|
|
const { npmPackages, workspaceLibraries } = collectDependencies(
|
|
projectGraph,
|
|
name
|
|
);
|
|
|
|
return {
|
|
workspaceLibraries: [...workspaceLibraries.values()],
|
|
npmPackages: [...npmPackages],
|
|
};
|
|
}
|
|
|
|
function collectDependencies(
|
|
projectGraph: ProjectGraph,
|
|
name: string,
|
|
dependencies = {
|
|
workspaceLibraries: new Map<string, WorkspaceLibrary>(),
|
|
npmPackages: new Set<string>(),
|
|
},
|
|
seen: Set<string> = new Set()
|
|
): {
|
|
workspaceLibraries: Map<string, WorkspaceLibrary>;
|
|
npmPackages: Set<string>;
|
|
} {
|
|
if (seen.has(name)) {
|
|
return dependencies;
|
|
}
|
|
seen.add(name);
|
|
|
|
(projectGraph.dependencies[name] ?? []).forEach((dependency) => {
|
|
if (dependency.target.startsWith('npm:')) {
|
|
dependencies.npmPackages.add(dependency.target.replace('npm:', ''));
|
|
} else {
|
|
dependencies.workspaceLibraries.set(dependency.target, {
|
|
name: dependency.target,
|
|
root: projectGraph.nodes[dependency.target].data.root,
|
|
importKey: getLibraryImportPath(dependency.target, projectGraph),
|
|
});
|
|
collectDependencies(projectGraph, dependency.target, dependencies, seen);
|
|
}
|
|
});
|
|
|
|
return dependencies;
|
|
}
|
|
|
|
function getLibraryImportPath(
|
|
library: string,
|
|
projectGraph: ProjectGraph
|
|
): string | undefined {
|
|
let buildLibsFromSource = true;
|
|
if (process.env.NX_BUILD_LIBS_FROM_SOURCE) {
|
|
buildLibsFromSource = process.env.NX_BUILD_LIBS_FROM_SOURCE === 'true';
|
|
}
|
|
const libraryNode = projectGraph.nodes[library];
|
|
let sourceRoots = [libraryNode.data.sourceRoot];
|
|
|
|
if (!buildLibsFromSource && process.env.NX_BUILD_TARGET) {
|
|
const buildTarget = parseTargetString(
|
|
process.env.NX_BUILD_TARGET,
|
|
projectGraph
|
|
);
|
|
sourceRoots = getOutputsForTargetAndConfiguration(
|
|
buildTarget,
|
|
{},
|
|
libraryNode
|
|
);
|
|
}
|
|
|
|
const tsConfigPathMappings = readTsPathMappings();
|
|
|
|
for (const [key, value] of Object.entries(tsConfigPathMappings)) {
|
|
for (const src of sourceRoots) {
|
|
if (value.find((path) => path.startsWith(src))) {
|
|
return key;
|
|
}
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|