From 70f402d976de63018fe74f035e6a85c6c48b1311 Mon Sep 17 00:00:00 2001
From: Alan Agius <17563226+alan-agius4@users.noreply.github.com>
Date: Mon, 10 Mar 2025 08:19:20 +0000
Subject: [PATCH] fix(@angular/build): handle postcss compilation errors
 gracefully

Improves error handling in the PostCSS compilation process to prevent crashes and allow recovery.

Closes #29789

(cherry picked from commit 3e35167855b3eacb9f45948ef75e999956819490)
---
 .../stylesheets/stylesheet-plugin-factory.ts        | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/packages/angular/build/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.ts b/packages/angular/build/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.ts
index ac03c602e658..cb10e65892f8 100644
--- a/packages/angular/build/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.ts
+++ b/packages/angular/build/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.ts
@@ -12,6 +12,7 @@ import assert from 'node:assert';
 import { readFile } from 'node:fs/promises';
 import { extname } from 'node:path';
 import type { Options } from 'sass';
+import { assertIsError } from '../../../utils/error';
 import type { PostcssConfiguration } from '../../../utils/postcss-configuration';
 import { LoadResultCache, createCachedLoad } from '../load-result-cache';
 
@@ -422,8 +423,16 @@ async function compileString(
           },
         ],
       };
-    }
+    } else {
+      assertIsError(error);
 
-    throw error;
+      return {
+        errors: [
+          {
+            text: error.message,
+          },
+        ],
+      };
+    }
   }
 }