diff --git a/packages/angular_devkit/build_angular/src/sass/sass-service.ts b/packages/angular_devkit/build_angular/src/sass/sass-service.ts index 794cca0e963c..7a85370293d1 100644 --- a/packages/angular_devkit/build_angular/src/sass/sass-service.ts +++ b/packages/angular_devkit/build_angular/src/sass/sass-service.ts @@ -118,13 +118,12 @@ export class SassWorkerImplementation { /** * Shutdown the Sass render worker. * Executing this method will stop any pending render requests. - * - * The worker is unreferenced upon creation and will not block application exit. This method - * is only needed if early cleanup is needed. */ close(): void { for (const worker of this.workers) { - void worker.terminate(); + try { + void worker.terminate(); + } catch {} } this.requests.clear(); } @@ -207,7 +206,6 @@ export class SassWorkerImplementation { }, ); - worker.unref(); mainImporterPort.unref(); return worker; diff --git a/packages/angular_devkit/build_angular/src/webpack/configs/styles.ts b/packages/angular_devkit/build_angular/src/webpack/configs/styles.ts index 8791b93aa1fc..21782faac3fe 100644 --- a/packages/angular_devkit/build_angular/src/webpack/configs/styles.ts +++ b/packages/angular_devkit/build_angular/src/webpack/configs/styles.ts @@ -108,7 +108,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio ); } - let sassImplementation: {} | undefined; + let sassImplementation: SassWorkerImplementation | undefined; try { sassImplementation = require('node-sass'); wco.logger.warn( @@ -117,6 +117,13 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio ); } catch { sassImplementation = new SassWorkerImplementation(); + extraPlugins.push({ + apply(compiler) { + compiler.hooks.shutdown.tap('sass-worker', () => { + sassImplementation?.close(); + }); + }, + }); } const assetNameTemplate = assetNameTemplateFactory(hashFormat);