Skip to content

Commit

Permalink
feat(react): use shared helpers from devkit (#13061)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored Nov 8, 2022
1 parent cb0ccea commit 000eebd
Showing 1 changed file with 9 additions and 98 deletions.
107 changes: 9 additions & 98 deletions packages/react/src/module-federation/with-module-federation.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,22 @@
import {
getNpmPackageSharedConfig,
sharePackages,
shareWorkspaceLibraries,
} from './webpack-utils';
import {
AdditionalSharedConfig,
createProjectGraphAsync,
getDependentPackagesForProject,
getNpmPackageSharedConfig,
ModuleFederationConfig,
ProjectConfiguration,
ProjectGraph,
readCachedProjectGraph,
} from '@nrwl/devkit';
import {
getRootTsConfigPath,
readTsConfig,
} from '@nrwl/workspace/src/utilities/typescript';
import { ParsedCommandLine } from 'typescript';
import {
AdditionalSharedConfig,
ModuleFederationConfig,
readRootPackageJson,
Remotes,
SharedFunction,
SharedLibraryConfig,
} from './models';
import { readRootPackageJson } from './package-json';
sharePackages,
shareWorkspaceLibraries,
} from '@nrwl/devkit';
import { extname } from 'path';
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) {
const remoteConfiguration = projectGraph.nodes[remote].data;
const serveTarget = remoteConfiguration?.targets?.serve;
Expand Down Expand Up @@ -232,7 +143,7 @@ export async function withModuleFederation(options: ModuleFederationConfig) {
);
}

const dependencies = await getDependentPackagesForProject(
const dependencies = getDependentPackagesForProject(
projectGraph,
options.name
);
Expand Down

0 comments on commit 000eebd

Please sign in to comment.