From 940e382db27474dba6479f57e4ffefee04cfca66 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 25 Apr 2024 12:08:57 +0000 Subject: [PATCH] fix(@angular/build): disable Vite prebundling when script optimizations are enabled This change ensures that `ngDevMode` is replaced in node packages, aligning the behavior of the Vite server more closely with a production environment. (cherry picked from commit 60752fdff0891d2eb15b0ce330938a69e8d3e19a) --- goldens/circular-deps/packages.json | 10 +++++-- .../build/src/builders/dev-server/options.ts | 29 +++++++++++++++++-- .../build/src/builders/dev-server/schema.json | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/goldens/circular-deps/packages.json b/goldens/circular-deps/packages.json index 1291c38df2a0..ba9e8df1ecc4 100644 --- a/goldens/circular-deps/packages.json +++ b/goldens/circular-deps/packages.json @@ -1,10 +1,10 @@ [ [ - "packages/angular/build/src/tools/esbuild/bundler-context.ts", - "packages/angular/build/src/tools/esbuild/utils.ts" + "packages/angular_devkit/build_angular/src/builders/dev-server/builder.ts", + "packages/angular_devkit/build_angular/src/builders/dev-server/options.ts" ], [ - "packages/angular/build/src/tools/esbuild/bundler-execution-result.ts", + "packages/angular/build/src/tools/esbuild/bundler-context.ts", "packages/angular/build/src/tools/esbuild/utils.ts" ], [ @@ -12,6 +12,10 @@ "packages/angular/build/src/tools/esbuild/utils.ts", "packages/angular/build/src/tools/esbuild/bundler-execution-result.ts" ], + [ + "packages/angular/build/src/tools/esbuild/bundler-execution-result.ts", + "packages/angular/build/src/tools/esbuild/utils.ts" + ], [ "packages/angular/cli/src/analytics/analytics-collector.ts", "packages/angular/cli/src/command-builder/command-module.ts" diff --git a/packages/angular/build/src/builders/dev-server/options.ts b/packages/angular/build/src/builders/dev-server/options.ts index d08b783472b5..3ddd991a215d 100644 --- a/packages/angular/build/src/builders/dev-server/options.ts +++ b/packages/angular/build/src/builders/dev-server/options.ts @@ -8,7 +8,9 @@ import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect'; import path from 'node:path'; +import { normalizeOptimization } from '../../utils'; import { normalizeCacheOptions } from '../../utils/normalize-cache'; +import { ApplicationBuilderOptions } from '../application'; import { Schema as DevServerOptions } from './schema'; export type NormalizedDevServerOptions = Awaited>; @@ -28,7 +30,7 @@ export async function normalizeOptions( projectName: string, options: DevServerOptions, ) { - const workspaceRoot = context.workspaceRoot; + const { workspaceRoot, logger } = context; const projectMetadata = await context.getProjectMetadata(projectName); const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? ''); @@ -38,6 +40,29 @@ export async function normalizeOptions( const buildTargetSpecifier = options.buildTarget ?? `::development`; const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build'); + // Get the application builder options. + const browserBuilderName = await context.getBuilderNameForTarget(buildTarget); + const rawBuildOptions = await context.getTargetOptions(buildTarget); + const buildOptions = (await context.validateOptions( + rawBuildOptions, + browserBuilderName, + )) as unknown as ApplicationBuilderOptions; + const optimization = normalizeOptimization(buildOptions.optimization); + + if (options.prebundle) { + if (!cacheOptions.enabled) { + // Warn if the initial options provided by the user enable prebundling but caching is disabled + logger.warn( + 'Prebundling has been configured but will not be used because caching has been disabled.', + ); + } else if (optimization.scripts) { + // Warn if the initial options provided by the user enable prebundling but script optimization is enabled. + logger.warn( + 'Prebundling has been configured but will not be used because scripts optimization is enabled.', + ); + } + } + // Initial options to keep const { host, @@ -78,6 +103,6 @@ export async function normalizeOptions( sslCert, sslKey, // Prebundling defaults to true but requires caching to function - prebundle: cacheOptions.enabled && (prebundle ?? true), + prebundle: cacheOptions.enabled && !optimization.scripts && prebundle, }; } diff --git a/packages/angular/build/src/builders/dev-server/schema.json b/packages/angular/build/src/builders/dev-server/schema.json index 95ff1bbca18b..0e382f7e6489 100644 --- a/packages/angular/build/src/builders/dev-server/schema.json +++ b/packages/angular/build/src/builders/dev-server/schema.json @@ -81,6 +81,7 @@ }, "prebundle": { "description": "Enable and control the Vite-based development server's prebundling capabilities. To enable prebundling, the Angular CLI cache must also be enabled.", + "default": true, "oneOf": [ { "type": "boolean" }, {