From 5f837e0ad72827d6446a77609f53c400c035243b Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 15 Mar 2022 15:25:21 +0000 Subject: [PATCH] cleanup(angular): address PR comments --- .../angular/src/utils/mfe/mfe-webpack.spec.ts | 4 +- packages/angular/src/utils/mfe/mfe-webpack.ts | 6 ++- .../utils/mfe/with-module-federation.spec.ts | 14 ++++--- .../src/utils/mfe/with-module-federation.ts | 37 ++++++++++++------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/packages/angular/src/utils/mfe/mfe-webpack.spec.ts b/packages/angular/src/utils/mfe/mfe-webpack.spec.ts index 9a51bf931d6d5b..ce864f82c197f9 100644 --- a/packages/angular/src/utils/mfe/mfe-webpack.spec.ts +++ b/packages/angular/src/utils/mfe/mfe-webpack.spec.ts @@ -1,7 +1,7 @@ jest.mock('fs'); -jest.mock('@nrwl/workspace'); +jest.mock('@nrwl/workspace/src/utilities/typescript'); import * as fs from 'fs'; -import * as workspace from '@nrwl/workspace'; +import * as workspace from '@nrwl/workspace/src/utilities/typescript'; import { sharePackages, shareWorkspaceLibraries } from './mfe-webpack'; diff --git a/packages/angular/src/utils/mfe/mfe-webpack.ts b/packages/angular/src/utils/mfe/mfe-webpack.ts index b60447ef404468..314680e0a6538b 100644 --- a/packages/angular/src/utils/mfe/mfe-webpack.ts +++ b/packages/angular/src/utils/mfe/mfe-webpack.ts @@ -1,11 +1,13 @@ -import { readTsConfig } from '@nrwl/workspace'; import { existsSync, readFileSync } from 'fs'; import { NormalModuleReplacementPlugin } from 'webpack'; import { appRootPath as rootPath } from 'nx/src/utils/app-root'; import { normalizePath, joinPathFragments } from '@nrwl/devkit'; import { dirname } from 'path'; import { ParsedCommandLine } from 'typescript'; -import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript'; +import { + getRootTsConfigPath, + readTsConfig, +} from '@nrwl/workspace/src/utilities/typescript'; export interface SharedLibraryConfig { singleton: boolean; diff --git a/packages/angular/src/utils/mfe/with-module-federation.spec.ts b/packages/angular/src/utils/mfe/with-module-federation.spec.ts index 57a2d49a393213..d58c70bfce6384 100644 --- a/packages/angular/src/utils/mfe/with-module-federation.spec.ts +++ b/packages/angular/src/utils/mfe/with-module-federation.spec.ts @@ -1,9 +1,11 @@ jest.mock('fs'); jest.mock('@nrwl/workspace/src/core/project-graph'); -jest.mock('@nrwl/workspace'); +jest.mock('@nrwl/workspace/src/utilities/typescript'); +jest.mock('@nrwl/workspace/src/core/file-utils'); jest.mock('nx/src/shared/workspace'); import * as graph from '@nrwl/workspace/src/core/project-graph'; -import * as workspace from '@nrwl/workspace'; +import * as typescriptUtils from '@nrwl/workspace/src/utilities/typescript'; +import * as workspace from '@nrwl/workspace/src/core/file-utils'; import * as taoWorkspace from 'nx/src/shared/workspace'; import * as fs from 'fs'; @@ -46,7 +48,7 @@ describe('withModuleFederation', () => { }) ); - (workspace.readTsConfig as jest.Mock).mockReturnValue({ + (typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({ options: { paths: { shared: ['/libs/shared/src/index.ts'], @@ -97,7 +99,7 @@ describe('withModuleFederation', () => { }) ); - (workspace.readTsConfig as jest.Mock).mockReturnValue({ + (typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({ options: { paths: { shared: ['/libs/shared/src/index.ts'], @@ -149,7 +151,7 @@ describe('withModuleFederation', () => { }) ); - (workspace.readTsConfig as jest.Mock).mockReturnValue({ + (typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({ options: { paths: { shared: ['/libs/shared/src/index.ts'], @@ -205,7 +207,7 @@ describe('withModuleFederation', () => { }) ); - (workspace.readTsConfig as jest.Mock).mockImplementation(() => ({ + (typescriptUtils.readTsConfig as jest.Mock).mockImplementation(() => ({ options: { paths: { shared: ['/libs/shared/src/index.ts'], diff --git a/packages/angular/src/utils/mfe/with-module-federation.ts b/packages/angular/src/utils/mfe/with-module-federation.ts index ae628c5306e7be..8666d9a1de829c 100644 --- a/packages/angular/src/utils/mfe/with-module-federation.ts +++ b/packages/angular/src/utils/mfe/with-module-federation.ts @@ -7,13 +7,15 @@ import { createProjectGraphAsync, readCachedProjectGraph, } from '@nrwl/workspace/src/core/project-graph'; -import { readWorkspaceJson } from '@nrwl/workspace'; +import { readWorkspaceJson } from '@nrwl/workspace/src/core/file-utils'; import { joinPathFragments, ProjectGraph } from '@nrwl/devkit'; import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin'); import { Workspaces } from 'nx/src/shared/workspace'; import { appRootPath } from 'nx/src/utils/app-root'; -import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript'; -import { readTsConfig } from '@nrwl/workspace'; +import { + getRootTsConfigPath, + readTsConfig, +} from '@nrwl/workspace/src/utilities/typescript'; import { ParsedCommandLine } from 'typescript'; export type MFERemotes = string[] | [remoteName: string, remoteUrl: string][]; @@ -30,8 +32,12 @@ export interface MFEConfig { function recursivelyResolveWorkspaceDependents( projectGraph: ProjectGraph, - target: string + target: string, + collectedDeps: Set = new Set() ) { + if (collectedDeps.has(target)) { + return []; + } let dependencies = [target]; const workspaceDependencies = ( @@ -41,11 +47,16 @@ function recursivelyResolveWorkspaceDependents( for (const dep of workspaceDependencies) { dependencies = [ ...dependencies, - ...recursivelyResolveWorkspaceDependents(projectGraph, dep.target), + ...recursivelyResolveWorkspaceDependents( + projectGraph, + dep.target, + collectedDeps + ), ]; } } - return dependencies; + collectedDeps = new Set([...collectedDeps, ...dependencies]); + return [...collectedDeps]; } function mapWorkspaceLibrariesToTsConfigImport(workspaceLibraries: string[]) { @@ -90,18 +101,18 @@ async function getDependentPackagesForProject(name: string) { const deps = projectGraph.dependencies[name].reduce( (dependencies, dependency) => { - const workspaceLibraries = dependencies.workspaceLibraries; - const npmPackages = dependencies.npmPackages; + const workspaceLibraries = new Set(dependencies.workspaceLibraries); + const npmPackages = new Set(dependencies.npmPackages); - if (dependency.target.startsWith('npm')) { - npmPackages.push(dependency.target.replace('npm:', '')); + if (dependency.target.startsWith('npm:')) { + npmPackages.add(dependency.target.replace('npm:', '')); } else { - workspaceLibraries.push(dependency.target); + workspaceLibraries.add(dependency.target); } return { - workspaceLibraries, - npmPackages, + workspaceLibraries: [...workspaceLibraries], + npmPackages: [...npmPackages], }; }, { workspaceLibraries: [], npmPackages: [] }