From ccd16b10dd3795b82e19e878c1e45ffc188a907c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 3 Jun 2024 13:23:22 +0200 Subject: [PATCH] fix(angular): normalize and handle dev remotes correctly (#26320) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #26169 --- .../module-federation-dev-ssr.impl.ts | 14 +++++++------- .../src/builders/utilities/module-federation.ts | 14 ++++++-------- .../lib/normalize-options.ts | 1 + .../module-federation-dev-server.impl.ts | 2 +- .../module-federation-dev-server/schema.d.ts | 11 ++++------- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/angular/src/builders/module-federation-dev-ssr/module-federation-dev-ssr.impl.ts b/packages/angular/src/builders/module-federation-dev-ssr/module-federation-dev-ssr.impl.ts index 0371e8630e72b..7310db9dc17e5 100644 --- a/packages/angular/src/builders/module-federation-dev-ssr/module-federation-dev-ssr.impl.ts +++ b/packages/angular/src/builders/module-federation-dev-ssr/module-federation-dev-ssr.impl.ts @@ -54,7 +54,13 @@ export function executeModuleFederationDevSSRBuilder( ); } - validateDevRemotes(options, workspaceProjects); + const devServeRemotes = !options.devRemotes + ? [] + : Array.isArray(options.devRemotes) + ? options.devRemotes + : [options.devRemotes]; + + validateDevRemotes({ devRemotes: devServeRemotes }, workspaceProjects); const remotesToSkip = new Set(options.skipRemotes ?? []); const staticRemotes = getStaticRemotes( @@ -72,12 +78,6 @@ export function executeModuleFederationDevSSRBuilder( ); const remotes = [...staticRemotes, ...dynamicRemotes]; - const devServeRemotes = !options.devRemotes - ? [] - : Array.isArray(options.devRemotes) - ? options.devRemotes - : [options.devRemotes]; - const remoteProcessPromises = []; for (const remote of remotes) { const isDev = devServeRemotes.includes(remote); diff --git a/packages/angular/src/builders/utilities/module-federation.ts b/packages/angular/src/builders/utilities/module-federation.ts index 7dfead3ba1a99..a5b8007c2df5b 100644 --- a/packages/angular/src/builders/utilities/module-federation.ts +++ b/packages/angular/src/builders/utilities/module-federation.ts @@ -3,6 +3,10 @@ import { existsSync, readFileSync } from 'fs'; import { logger, ProjectConfiguration } from '@nx/devkit'; import { registerTsProject } from '@nx/js/src/internal'; +export type DevRemoteDefinition = + | string + | { remoteName: string; configuration: string }; + export function getDynamicRemotes( project: ProjectConfiguration, context: import('@angular-devkit/architect').BuilderContext, @@ -157,18 +161,12 @@ export function getStaticRemotes( export function validateDevRemotes( options: { - devRemotes?: ( - | string - | { - remoteName: string; - configuration: string; - } - )[]; + devRemotes: DevRemoteDefinition[]; }, workspaceProjects: Record ): void { const invalidDevRemotes = - options.devRemotes?.filter( + options.devRemotes.filter( (remote) => !(typeof remote === 'string' ? workspaceProjects[remote] diff --git a/packages/angular/src/executors/module-federation-dev-server/lib/normalize-options.ts b/packages/angular/src/executors/module-federation-dev-server/lib/normalize-options.ts index c942bcc373f19..bb30d2032c038 100644 --- a/packages/angular/src/executors/module-federation-dev-server/lib/normalize-options.ts +++ b/packages/angular/src/executors/module-federation-dev-server/lib/normalize-options.ts @@ -18,6 +18,7 @@ export function normalizeOptions(schema: Schema): NormalizedSchema { return { ...schema, buildTarget, + devRemotes: schema.devRemotes ?? [], host: schema.host ?? 'localhost', port: schema.port ?? 4200, liveReload: schema.liveReload ?? true, diff --git a/packages/angular/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts b/packages/angular/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts index 6cea689118810..8c41564ed0230 100644 --- a/packages/angular/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts +++ b/packages/angular/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts @@ -107,7 +107,7 @@ export async function* moduleFederationDevServerExecutor( 'angular' ); - const remoteNames = options.devRemotes?.map((r) => + const remoteNames = options.devRemotes.map((r) => typeof r === 'string' ? r : r.remoteName ); diff --git a/packages/angular/src/executors/module-federation-dev-server/schema.d.ts b/packages/angular/src/executors/module-federation-dev-server/schema.d.ts index 3c2d6b0715b2f..c2e5f77430021 100644 --- a/packages/angular/src/executors/module-federation-dev-server/schema.d.ts +++ b/packages/angular/src/executors/module-federation-dev-server/schema.d.ts @@ -1,3 +1,5 @@ +import type { DevRemoteDefinition } from '../../builders/utilities/module-federation'; + interface BaseSchema { port?: number; host?: string; @@ -16,13 +18,7 @@ interface BaseSchema { hmr?: boolean; watch?: boolean; poll?: number; - devRemotes?: ( - | string - | { - remoteName: string; - configuration: string; - } - )[]; + devRemotes?: DevRemoteDefinition[]; skipRemotes?: string[]; pathToManifestFile?: string; static?: boolean; @@ -43,6 +39,7 @@ export type SchemaWithBuildTarget = BaseSchema & { export type Schema = SchemaWithBrowserTarget | SchemaWithBuildTarget; export type NormalizedSchema = SchemaWithBuildTarget & { + devRemotes: DevRemoteDefinition[]; liveReload: boolean; open: boolean; ssl: boolean;