Skip to content

Commit 05f31bd

Browse files
bgotinkclydin
authored andcommittedAug 3, 2023
fix(@angular-devkit/build-angular): prevent race condition in setting up sass worker pool
(cherry picked from commit df4d358)
1 parent 6d1da30 commit 05f31bd

File tree

1 file changed

+14
-4
lines changed
  • packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets

1 file changed

+14
-4
lines changed
 

‎packages/angular_devkit/build_angular/src/tools/esbuild/stylesheets/sass-language.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ import type {
1717
import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin-factory';
1818

1919
let sassWorkerPool: SassWorkerImplementation | undefined;
20+
let sassWorkerPoolPromise: Promise<SassWorkerImplementation> | undefined;
2021

2122
function isSassException(error: unknown): error is Exception {
2223
return !!error && typeof error === 'object' && 'sassMessage' in error;
2324
}
2425

2526
export function shutdownSassWorkerPool(): void {
26-
sassWorkerPool?.close();
27-
sassWorkerPool = undefined;
27+
if (sassWorkerPool) {
28+
sassWorkerPool.close();
29+
sassWorkerPool = undefined;
30+
} else if (sassWorkerPoolPromise) {
31+
void sassWorkerPoolPromise.then(shutdownSassWorkerPool);
32+
}
33+
sassWorkerPoolPromise = undefined;
2834
}
2935

3036
export const SassStylesheetLanguage = Object.freeze<StylesheetLanguage>({
@@ -104,8 +110,12 @@ async function compileString(
104110
): Promise<OnLoadResult> {
105111
// Lazily load Sass when a Sass file is found
106112
if (sassWorkerPool === undefined) {
107-
const sassService = await import('../../sass/sass-service');
108-
sassWorkerPool = new sassService.SassWorkerImplementation(true);
113+
if (sassWorkerPoolPromise === undefined) {
114+
sassWorkerPoolPromise = import('../../sass/sass-service').then(
115+
(sassService) => new sassService.SassWorkerImplementation(true),
116+
);
117+
}
118+
sassWorkerPool = await sassWorkerPoolPromise;
109119
}
110120

111121
// Cache is currently local to individual compile requests.

0 commit comments

Comments
 (0)
Please sign in to comment.