From c17e256f9b2f78353122bd73ef52b2bb413a4de5 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 8 Jun 2023 12:05:53 +0100 Subject: [PATCH] fix(angular): skipping remotes should not only look at the current workspace #17473 --- .../module-federation-dev-server.impl.ts | 29 +++++++++++++++++-- .../src/utils/find-matching-projects.spec.ts | 2 +- .../nx/src/utils/find-matching-projects.ts | 2 +- .../module-federation-dev-server.impl.ts | 22 ++++++++++++-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/packages/angular/src/builders/module-federation-dev-server/module-federation-dev-server.impl.ts b/packages/angular/src/builders/module-federation-dev-server/module-federation-dev-server.impl.ts index 9cf8342544535c..da27f9e6bbf280 100644 --- a/packages/angular/src/builders/module-federation-dev-server/module-federation-dev-server.impl.ts +++ b/packages/angular/src/builders/module-federation-dev-server/module-federation-dev-server.impl.ts @@ -1,5 +1,10 @@ import type { Schema } from './schema'; -import { readCachedProjectGraph, workspaceRoot, Workspaces } from '@nx/devkit'; +import { + logger, + readCachedProjectGraph, + workspaceRoot, + Workspaces, +} from '@nx/devkit'; import { scheduleTarget } from 'nx/src/adapter/ngcli-adapter'; import { executeWebpackDevServerBuilder } from '../webpack-dev-server/webpack-dev-server.impl'; import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph'; @@ -10,7 +15,10 @@ import { } from '../utilities/module-federation'; import { existsSync } from 'fs'; import { extname, join } from 'path'; -import { findMatchingProjects } from 'nx/src/utils/find-matching-projects'; +import { + findMatchingProjects, + parseStringPattern, +} from 'nx/src/utils/find-matching-projects'; export function executeModuleFederationDevServerBuilder( schema: Schema, @@ -48,9 +56,24 @@ export function executeModuleFederationDevServerBuilder( validateDevRemotes(options, workspaceProjects); + const patternMatchedRemotesToSkip = new Set( + findMatchingProjects(options.skipRemotes, projectGraph.nodes) + ); + const remotesNotInGraph = options.skipRemotes.filter((r) => { + const { exclude, type } = parseStringPattern(r, projectGraph.nodes); + return exclude === false && type === 'unlabeled'; + }); const remotesToSkip = new Set( - findMatchingProjects(options.skipRemotes, projectGraph.nodes) ?? [] + [...patternMatchedRemotesToSkip, ...remotesNotInGraph] ?? [] ); + + if (remotesToSkip.size > 0) { + logger.info( + `Not automatically serving remotes: ${[...remotesToSkip.values()].join( + ', ' + )}` + ); + } const staticRemotes = getStaticRemotes( project, context, diff --git a/packages/nx/src/utils/find-matching-projects.spec.ts b/packages/nx/src/utils/find-matching-projects.spec.ts index 0ad750ce5645e8..72c2b57bfe7a8a 100644 --- a/packages/nx/src/utils/find-matching-projects.spec.ts +++ b/packages/nx/src/utils/find-matching-projects.spec.ts @@ -2,8 +2,8 @@ import { findMatchingProjects, getMatchingStringsWithCache, } from './find-matching-projects'; -import minimatch = require('minimatch'); import type { ProjectGraphProjectNode } from '../config/project-graph'; +import minimatch = require('minimatch'); describe('findMatchingProjects', () => { let projectGraph: Record = { diff --git a/packages/nx/src/utils/find-matching-projects.ts b/packages/nx/src/utils/find-matching-projects.ts index b2888047f2cf8d..2858ba52d0155c 100644 --- a/packages/nx/src/utils/find-matching-projects.ts +++ b/packages/nx/src/utils/find-matching-projects.ts @@ -202,7 +202,7 @@ function addMatchingProjectsByTag( } } -function parseStringPattern( +export function parseStringPattern( pattern: string, projects: Record ): ProjectPattern { diff --git a/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts b/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts index 892ebc92b52e60..47682756d18a8e 100644 --- a/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts +++ b/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts @@ -8,7 +8,10 @@ import { } from '@nx/devkit/src/utils/async-iterable'; import * as chalk from 'chalk'; import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open'; -import { findMatchingProjects } from 'nx/src/utils/find-matching-projects'; +import { + findMatchingProjects, + parseStringPattern, +} from 'nx/src/utils/find-matching-projects'; import { fork } from 'child_process'; type ModuleFederationDevServerOptions = WebDevServerOptions & { @@ -38,9 +41,24 @@ export default async function* moduleFederationDevServer( ); } + const patternMatchedRemotesToSkip = new Set( + findMatchingProjects(options.skipRemotes, context.projectGraph.nodes) + ); + const remotesNotInGraph = options.skipRemotes.filter((r) => { + const { exclude, type } = parseStringPattern(r, context.projectGraph.nodes); + return exclude === false && type === 'unlabeled'; + }); const remotesToSkip = new Set( - findMatchingProjects(options.skipRemotes ?? [], context.projectGraph.nodes) + [...patternMatchedRemotesToSkip, ...remotesNotInGraph] ?? [] ); + + if (remotesToSkip.size > 0) { + logger.info( + `Not automatically serving remotes: ${[...remotesToSkip.values()].join( + ', ' + )}` + ); + } const knownRemotes = (moduleFederationConfig.remotes ?? []).filter((r) => { const validRemote = Array.isArray(r) ? r[0] : r; return !remotesToSkip.has(validRemote);