Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): prevent race condition in setting…
Browse files Browse the repository at this point in the history
… up sass worker pool

(cherry picked from commit df4d358)
  • Loading branch information
bgotink authored and clydin committed Aug 3, 2023
1 parent 6d1da30 commit 05f31bd
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ import type {
import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin-factory';

let sassWorkerPool: SassWorkerImplementation | undefined;
let sassWorkerPoolPromise: Promise<SassWorkerImplementation> | 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<StylesheetLanguage>({
Expand Down Expand Up @@ -104,8 +110,12 @@ async function compileString(
): Promise<OnLoadResult> {
// 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.
Expand Down

0 comments on commit 05f31bd

Please sign in to comment.