From 1c161c61d5cf0c5d2db7c2941d8de7db2d8092ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Fri, 9 Feb 2024 17:29:44 +0100 Subject: [PATCH] fix(angular): add missing forceEsbuild option to dev-server executor (#21753) (cherry picked from commit 3e193b94b691099eb46cd65afdf5d6b7dc9deca0) --- .../angular/executors/dev-server.json | 5 +++++ .../builders/dev-server/dev-server.impl.ts | 12 ++---------- .../src/builders/dev-server/lib/index.ts | 1 + .../dev-server/lib/validate-options.ts | 19 +++++++++++++++++++ .../src/builders/dev-server/schema.d.ts | 1 + .../src/builders/dev-server/schema.json | 5 +++++ 6 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 packages/angular/src/builders/dev-server/lib/validate-options.ts diff --git a/docs/generated/packages/angular/executors/dev-server.json b/docs/generated/packages/angular/executors/dev-server.json index cc4e906d0a731..308168e403f61 100644 --- a/docs/generated/packages/angular/executors/dev-server.json +++ b/docs/generated/packages/angular/executors/dev-server.json @@ -105,6 +105,11 @@ "type": "number", "description": "Enable and define the file watching poll time period in milliseconds." }, + "forceEsbuild": { + "type": "boolean", + "description": "Force the development server to use the 'browser-esbuild' builder when building. This is a developer preview option for the esbuild-based build system. _Note: this is only supported in Angular versions >= 16.1.0_.", + "default": false + }, "buildLibsFromSource": { "type": "boolean", "description": "Read buildable libraries from source instead of building them separately. If not set, it will take the value specified in the `browserTarget` options, or it will default to `true` if it's also not set in the `browserTarget` options.", diff --git a/packages/angular/src/builders/dev-server/dev-server.impl.ts b/packages/angular/src/builders/dev-server/dev-server.impl.ts index b3aa2587e1d79..0c52b48a9d134 100644 --- a/packages/angular/src/builders/dev-server/dev-server.impl.ts +++ b/packages/angular/src/builders/dev-server/dev-server.impl.ts @@ -10,7 +10,6 @@ import { normalizePath, parseTargetString, readCachedProjectGraph, - stripIndents, type Target, } from '@nx/devkit'; import { getRootTsConfigPath } from '@nx/js'; @@ -34,7 +33,7 @@ import { mergeCustomWebpackConfig, resolveIndexHtmlTransformer, } from '../utilities/webpack'; -import { normalizeOptions } from './lib'; +import { normalizeOptions, validateOptions } from './lib'; import type { NormalizedSchema, Schema, @@ -55,14 +54,7 @@ export function executeDevServerBuilder( rawOptions: Schema, context: import('@angular-devkit/architect').BuilderContext ) { - if (rawOptions.esbuildMiddleware?.length > 0) { - const { major: angularMajorVersion, version: angularVersion } = - getInstalledAngularVersionInfo(); - if (angularMajorVersion < 17) { - throw new Error(stripIndents`The "esbuildMiddleware" option is only supported in Angular >= 17.0.0. You are currently using "${angularVersion}". - You can resolve this error by removing the "esbuildMiddleware" option or by migrating to Angular 17.0.0.`); - } - } + validateOptions(rawOptions); process.env.NX_TSCONFIG_PATH = getRootTsConfigPath(); diff --git a/packages/angular/src/builders/dev-server/lib/index.ts b/packages/angular/src/builders/dev-server/lib/index.ts index f9451963f55a2..da94c6f89ca24 100644 --- a/packages/angular/src/builders/dev-server/lib/index.ts +++ b/packages/angular/src/builders/dev-server/lib/index.ts @@ -1 +1,2 @@ export * from './normalize-options'; +export * from './validate-options'; diff --git a/packages/angular/src/builders/dev-server/lib/validate-options.ts b/packages/angular/src/builders/dev-server/lib/validate-options.ts new file mode 100644 index 0000000000000..d4317491c42c0 --- /dev/null +++ b/packages/angular/src/builders/dev-server/lib/validate-options.ts @@ -0,0 +1,19 @@ +import { stripIndents } from '@nx/devkit'; +import { lt } from 'semver'; +import { getInstalledAngularVersionInfo } from '../../../executors/utilities/angular-version-utils'; +import type { Schema } from '../schema'; + +export function validateOptions(options: Schema): void { + const { major: angularMajorVersion, version: angularVersion } = + getInstalledAngularVersionInfo(); + + if (lt(angularVersion, '16.1.0') && options.forceEsbuild !== undefined) { + throw new Error(stripIndents`The "forceEsbuild" option is only supported in Angular >= 16.1.0. You are currently using "${angularVersion}". + You can resolve this error by removing the "forceEsbuild" option or by migrating to Angular 16.1.0.`); + } + + if (angularMajorVersion < 17 && options.esbuildMiddleware?.length > 0) { + throw new Error(stripIndents`The "esbuildMiddleware" option is only supported in Angular >= 17.0.0. You are currently using "${angularVersion}". + You can resolve this error by removing the "esbuildMiddleware" option or by migrating to Angular 17.0.0.`); + } +} diff --git a/packages/angular/src/builders/dev-server/schema.d.ts b/packages/angular/src/builders/dev-server/schema.d.ts index 6ca097e205a3f..93bd98f0e478d 100644 --- a/packages/angular/src/builders/dev-server/schema.d.ts +++ b/packages/angular/src/builders/dev-server/schema.d.ts @@ -16,6 +16,7 @@ interface BaseSchema { hmr?: boolean; watch?: boolean; poll?: number; + forceEsbuild?: boolean; buildLibsFromSource?: boolean; esbuildMiddleware?: string[]; } diff --git a/packages/angular/src/builders/dev-server/schema.json b/packages/angular/src/builders/dev-server/schema.json index 86e88261b5444..3fa94550184e7 100644 --- a/packages/angular/src/builders/dev-server/schema.json +++ b/packages/angular/src/builders/dev-server/schema.json @@ -111,6 +111,11 @@ "type": "number", "description": "Enable and define the file watching poll time period in milliseconds." }, + "forceEsbuild": { + "type": "boolean", + "description": "Force the development server to use the 'browser-esbuild' builder when building. This is a developer preview option for the esbuild-based build system. _Note: this is only supported in Angular versions >= 16.1.0_.", + "default": false + }, "buildLibsFromSource": { "type": "boolean", "description": "Read buildable libraries from source instead of building them separately. If not set, it will take the value specified in the `browserTarget` options, or it will default to `true` if it's also not set in the `browserTarget` options.",