From b10d2a7bb0694ff385d6d0f26ce1db2f5733f61d Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:47:12 -0500 Subject: [PATCH] perf(@angular-devkit/build-angular): only enable advanced optimizations with script optimizations When using the `application` or `browser-esbuild` builders, the internal advanced optimizations can only be applied when in AOT mode. However, they were previously only checking the AOT mode and not whether the project was configured to use script optimizations. The advanced optimizations are now conditional on both AOT mode and the `optimization.scripts` option. This can greatly improve the performance of builds in development since the Babel related processing can be skipped for all TypeScript application code. --- .../build_angular/src/builders/application/options.ts | 2 +- .../tests/behavior/angular-aot-metadata_spec.ts | 2 ++ .../e2e/tests/build/prerender/error-with-sourcemaps.ts | 7 ++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/application/options.ts b/packages/angular_devkit/build_angular/src/builders/application/options.ts index 94d55e10b8..914a302997 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/options.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/options.ts @@ -284,7 +284,7 @@ export async function normalizeOptions( // Return all the normalized options return { - advancedOptimizations: !!aot, + advancedOptimizations: !!aot && optimizationOptions.scripts, allowedCommonJsDependencies, baseHref, cacheOptions, diff --git a/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/angular-aot-metadata_spec.ts b/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/angular-aot-metadata_spec.ts index fa136b1059..be84649bbe 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/angular-aot-metadata_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/angular-aot-metadata_spec.ts @@ -14,6 +14,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { it('should not emit any AOT class metadata functions', async () => { harness.useTarget('build', { ...BASE_OPTIONS, + optimization: true, }); const { result } = await harness.executeOnce(); @@ -25,6 +26,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { it('should not emit any AOT NgModule scope metadata functions', async () => { harness.useTarget('build', { ...BASE_OPTIONS, + optimization: true, }); const { result } = await harness.executeOnce(); diff --git a/tests/legacy-cli/e2e/tests/build/prerender/error-with-sourcemaps.ts b/tests/legacy-cli/e2e/tests/build/prerender/error-with-sourcemaps.ts index 5661608d06..9937064070 100644 --- a/tests/legacy-cli/e2e/tests/build/prerender/error-with-sourcemaps.ts +++ b/tests/legacy-cli/e2e/tests/build/prerender/error-with-sourcemaps.ts @@ -44,5 +44,10 @@ export default async function () { const { message } = await expectToFail(() => ng('build', '--configuration', 'development', '--prerender'), ); - match(message, /window is not defined[.\s\S]*constructor \(.*app\.component\.ts\:\d+:\d+\)/); + match( + message, + // When babel is used it will add names to the sourcemap and `constructor` will be used in the stack trace. + // This will currently only happen if AOT and script optimizations are set which enables advanced optimizations. + /window is not defined[.\s\S]*(?:constructor|_AppComponent) \(.*app\.component\.ts\:\d+:\d+\)/, + ); }