From 1dd5c28f8d32e382d271a3da2bed25f16e88153a Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 3 Jun 2021 18:13:38 -0400 Subject: [PATCH] fix(@angular-devkit/build-angular): dispose Sass worker resources on Webpack shutdown Sass Worker instances and resource requests are now cleaned up when the Webpack compiler is shutdown. This removes the need to unreference the workers upon creation. Fixes #20985 --- .../build_angular/src/sass/sass-service.ts | 8 +++----- .../build_angular/src/webpack/configs/styles.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 6 deletions(-) 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);