Skip to content

Commit ac66e40

Browse files
alan-agius4filipesilva
authored andcommittedDec 6, 2021
fix(@angular-devkit/build-angular): Sass compilation in StackBlitz webcontainers
When `process.versions.webcontainer` is truthy it means that we are running in a StackBlitz webcontainer. `SassWorkerImplementation` uses `receiveMessageOnPort` Node.js `worker_thread` API to ensure sync behavior which is ~2x faster. However, it is non trivial to support this in a webcontainer and while slower we choose to use `dart-sass` which in Webpack uses the slower async path.
1 parent 9e22d7a commit ac66e40

File tree

1 file changed

+21
-8
lines changed
  • packages/angular_devkit/build_angular/src/webpack/configs

1 file changed

+21
-8
lines changed
 

Diff for: ‎packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,16 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
107107
);
108108
}
109109

110-
const sassImplementation = new SassWorkerImplementation();
111-
extraPlugins.push({
112-
apply(compiler) {
113-
compiler.hooks.shutdown.tap('sass-worker', () => {
114-
sassImplementation?.close();
115-
});
116-
},
117-
});
110+
const sassImplementation = getSassImplementation();
111+
if (sassImplementation instanceof SassWorkerImplementation) {
112+
extraPlugins.push({
113+
apply(compiler) {
114+
compiler.hooks.shutdown.tap('sass-worker', () => {
115+
sassImplementation?.close();
116+
});
117+
},
118+
});
119+
}
118120

119121
const assetNameTemplate = assetNameTemplateFactory(hashFormat);
120122

@@ -404,3 +406,14 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
404406
plugins: extraPlugins,
405407
};
406408
}
409+
410+
function getSassImplementation(): SassWorkerImplementation | typeof import('sass') {
411+
const { webcontainer } = process.versions as unknown as Record<string, unknown>;
412+
413+
// When `webcontainer` is a truthy it means that we are running in a StackBlitz webcontainer.
414+
// `SassWorkerImplementation` uses `receiveMessageOnPort` Node.js `worker_thread` API to ensure sync behavior which is ~2x faster.
415+
// However, it is non trivial to support this in a webcontainer and while slower we choose to use `dart-sass`
416+
// which in Webpack uses the slower async path.
417+
// We should periodically check with StackBlitz folks (Mark Whitfeld / Dominic Elm) to determine if this workaround is still needed.
418+
return webcontainer ? require('sass') : new SassWorkerImplementation();
419+
}

0 commit comments

Comments
 (0)
Please sign in to comment.