Skip to content

Commit 8575056

Browse files
clydinalan-agius4
authored andcommitted
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 (cherry picked from commit 1dd5c28)
1 parent 10a263a commit 8575056

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

packages/angular_devkit/build_angular/src/sass/sass-service.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ export class SassWorkerImplementation {
118118
/**
119119
* Shutdown the Sass render worker.
120120
* Executing this method will stop any pending render requests.
121-
*
122-
* The worker is unreferenced upon creation and will not block application exit. This method
123-
* is only needed if early cleanup is needed.
124121
*/
125122
close(): void {
126123
for (const worker of this.workers) {
127-
void worker.terminate();
124+
try {
125+
void worker.terminate();
126+
} catch {}
128127
}
129128
this.requests.clear();
130129
}
@@ -197,7 +196,6 @@ export class SassWorkerImplementation {
197196
},
198197
);
199198

200-
worker.unref();
201199
mainImporterPort.unref();
202200

203201
return worker;

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
108108
);
109109
}
110110

111-
let sassImplementation: {} | undefined;
111+
let sassImplementation: SassWorkerImplementation | undefined;
112112
try {
113113
sassImplementation = require('node-sass');
114114
wco.logger.warn(
@@ -117,6 +117,13 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
117117
);
118118
} catch {
119119
sassImplementation = new SassWorkerImplementation();
120+
extraPlugins.push({
121+
apply(compiler) {
122+
compiler.hooks.shutdown.tap('sass-worker', () => {
123+
sassImplementation?.close();
124+
});
125+
},
126+
});
120127
}
121128

122129
const assetNameTemplate = assetNameTemplateFactory(hashFormat);

0 commit comments

Comments
 (0)