diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets/sass-language.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets/sass-language.ts index d3e86398732c..ebe5d3f7f02f 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets/sass-language.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets/sass-language.ts @@ -17,14 +17,20 @@ import type { import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin-factory'; let sassWorkerPool: SassWorkerImplementation | undefined; +let sassWorkerPoolPromise: Promise | undefined; function isSassException(error: unknown): error is Exception { return !!error && typeof error === 'object' && 'sassMessage' in error; } export function shutdownSassWorkerPool(): void { - sassWorkerPool?.close(); - sassWorkerPool = undefined; + if (sassWorkerPool) { + sassWorkerPool.close(); + sassWorkerPool = undefined; + } else if (sassWorkerPoolPromise) { + void sassWorkerPoolPromise.then(shutdownSassWorkerPool); + } + sassWorkerPoolPromise = undefined; } export const SassStylesheetLanguage = Object.freeze({ @@ -104,8 +110,12 @@ async function compileString( ): Promise { // Lazily load Sass when a Sass file is found if (sassWorkerPool === undefined) { - const sassService = await import('../../sass/sass-service'); - sassWorkerPool = new sassService.SassWorkerImplementation(true); + if (sassWorkerPoolPromise === undefined) { + sassWorkerPoolPromise = import('../../sass/sass-service').then( + (sassService) => new sassService.SassWorkerImplementation(true), + ); + } + sassWorkerPool = await sassWorkerPoolPromise; } // Cache is currently local to individual compile requests.