From 0fa1e3419ffba573156791a39ad5d9b6b0bb656b Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:06:36 -0500 Subject: [PATCH] fix(@angular-devkit/build-angular): ensure empty optimized Sass stylesheets stay empty When an optimized Sass stylesheet becomes an empty string the AOT Angular host adapter was previously treating this as a falsy value and using the original content of the stylesheet. Empty strings are now considered valid values and will be passed to the AOT compiler as such. --- .../behavior/component-stylesheets_spec.ts | 21 +++++++++++++++++++ .../src/tools/esbuild/angular/angular-host.ts | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/component-stylesheets_spec.ts b/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/component-stylesheets_spec.ts index 037ff4c9d1..3cbb5d9463 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/component-stylesheets_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/tests/behavior/component-stylesheets_spec.ts @@ -23,5 +23,26 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { const { result } = await harness.executeOnce(); expect(result?.success).toBeTrue(); }); + + it('should maintain optimized empty Sass stylesheet when original has content', async () => { + await harness.modifyFile('src/app/app.component.ts', (content) => { + return content.replace('./app.component.css', './app.component.scss'); + }); + await harness.removeFile('src/app/app.component.css'); + await harness.writeFile('src/app/app.component.scss', '@import "variables";'); + await harness.writeFile('src/app/_variables.scss', '$value: blue;'); + + harness.useTarget('build', { + ...BASE_OPTIONS, + optimization: { + styles: true, + }, + }); + + const { result } = await harness.executeOnce(); + expect(result?.success).toBeTrue(); + + harness.expectFile('dist/browser/main.js').content.not.toContain('variables'); + }); }); }); diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/angular-host.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/angular-host.ts index 0ce3d0f9bf..66108aebb3 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/angular-host.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/angular-host.ts @@ -74,7 +74,7 @@ export function createAngularCompilerHost( context.resourceFile ?? undefined, ); - return result ? { content: result } : null; + return typeof result === 'string' ? { content: result } : null; }; // Allow the AOT compiler to request the set of changed templates and styles