feat(react): use shared helpers from devkit (#13061)
This commit is contained in:
parent
cb0ccea9a8
commit
000eebdbe1
@ -1,111 +1,22 @@
|
|||||||
import {
|
import {
|
||||||
getNpmPackageSharedConfig,
|
AdditionalSharedConfig,
|
||||||
sharePackages,
|
|
||||||
shareWorkspaceLibraries,
|
|
||||||
} from './webpack-utils';
|
|
||||||
import {
|
|
||||||
createProjectGraphAsync,
|
createProjectGraphAsync,
|
||||||
|
getDependentPackagesForProject,
|
||||||
|
getNpmPackageSharedConfig,
|
||||||
|
ModuleFederationConfig,
|
||||||
ProjectConfiguration,
|
ProjectConfiguration,
|
||||||
ProjectGraph,
|
ProjectGraph,
|
||||||
readCachedProjectGraph,
|
readCachedProjectGraph,
|
||||||
} from '@nrwl/devkit';
|
readRootPackageJson,
|
||||||
import {
|
|
||||||
getRootTsConfigPath,
|
|
||||||
readTsConfig,
|
|
||||||
} from '@nrwl/workspace/src/utilities/typescript';
|
|
||||||
import { ParsedCommandLine } from 'typescript';
|
|
||||||
import {
|
|
||||||
AdditionalSharedConfig,
|
|
||||||
ModuleFederationConfig,
|
|
||||||
Remotes,
|
Remotes,
|
||||||
SharedFunction,
|
SharedFunction,
|
||||||
SharedLibraryConfig,
|
SharedLibraryConfig,
|
||||||
} from './models';
|
sharePackages,
|
||||||
import { readRootPackageJson } from './package-json';
|
shareWorkspaceLibraries,
|
||||||
|
} from '@nrwl/devkit';
|
||||||
import { extname } from 'path';
|
import { extname } from 'path';
|
||||||
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
|
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
|
||||||
|
|
||||||
function collectDependencies(
|
|
||||||
projectGraph: ProjectGraph,
|
|
||||||
name: string,
|
|
||||||
dependencies = {
|
|
||||||
workspaceLibraries: new Set<string>(),
|
|
||||||
npmPackages: new Set<string>(),
|
|
||||||
},
|
|
||||||
seen: Set<string> = new Set()
|
|
||||||
): {
|
|
||||||
workspaceLibraries: Set<string>;
|
|
||||||
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.add(dependency.target);
|
|
||||||
collectDependencies(projectGraph, dependency.target, dependencies, seen);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return dependencies;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mapWorkspaceLibrariesToTsConfigImport(
|
|
||||||
workspaceLibraries: string[],
|
|
||||||
{ nodes }: ProjectGraph
|
|
||||||
) {
|
|
||||||
const tsConfigPath = process.env.NX_TSCONFIG_PATH ?? getRootTsConfigPath();
|
|
||||||
const tsConfig: ParsedCommandLine = readTsConfig(tsConfigPath);
|
|
||||||
|
|
||||||
const tsconfigPathAliases: Record<string, string[]> = tsConfig.options?.paths;
|
|
||||||
|
|
||||||
if (!tsconfigPathAliases) {
|
|
||||||
return workspaceLibraries;
|
|
||||||
}
|
|
||||||
|
|
||||||
const mappedLibraries = [];
|
|
||||||
for (const lib of workspaceLibraries) {
|
|
||||||
const sourceRoot = nodes[lib].data.sourceRoot;
|
|
||||||
let found = false;
|
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(tsconfigPathAliases)) {
|
|
||||||
if (value.find((p) => p.startsWith(sourceRoot))) {
|
|
||||||
mappedLibraries.push(key);
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
mappedLibraries.push(lib);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return mappedLibraries;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getDependentPackagesForProject(
|
|
||||||
projectGraph: ProjectGraph,
|
|
||||||
name: string
|
|
||||||
) {
|
|
||||||
const { npmPackages, workspaceLibraries } = collectDependencies(
|
|
||||||
projectGraph,
|
|
||||||
name
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
workspaceLibraries: mapWorkspaceLibrariesToTsConfigImport(
|
|
||||||
[...workspaceLibraries],
|
|
||||||
projectGraph
|
|
||||||
),
|
|
||||||
npmPackages: [...npmPackages],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function determineRemoteUrl(remote: string, projectGraph: ProjectGraph) {
|
function determineRemoteUrl(remote: string, projectGraph: ProjectGraph) {
|
||||||
const remoteConfiguration = projectGraph.nodes[remote].data;
|
const remoteConfiguration = projectGraph.nodes[remote].data;
|
||||||
const serveTarget = remoteConfiguration?.targets?.serve;
|
const serveTarget = remoteConfiguration?.targets?.serve;
|
||||||
@ -232,7 +143,7 @@ export async function withModuleFederation(options: ModuleFederationConfig) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const dependencies = await getDependentPackagesForProject(
|
const dependencies = getDependentPackagesForProject(
|
||||||
projectGraph,
|
projectGraph,
|
||||||
options.name
|
options.name
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user