Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): Sass compilation in StackBlitz we…
Browse files Browse the repository at this point in the history
…bcontainers

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.
  • Loading branch information
alan-agius4 authored and filipesilva committed Dec 6, 2021
1 parent 9e22d7a commit ac66e40
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
);
}

const sassImplementation = new SassWorkerImplementation();
extraPlugins.push({
apply(compiler) {
compiler.hooks.shutdown.tap('sass-worker', () => {
sassImplementation?.close();
});
},
});
const sassImplementation = getSassImplementation();
if (sassImplementation instanceof SassWorkerImplementation) {
extraPlugins.push({
apply(compiler) {
compiler.hooks.shutdown.tap('sass-worker', () => {
sassImplementation?.close();
});
},
});
}

const assetNameTemplate = assetNameTemplateFactory(hashFormat);

Expand Down Expand Up @@ -404,3 +406,14 @@ export function getStylesConfig(wco: WebpackConfigOptions): Configuration {
plugins: extraPlugins,
};
}

function getSassImplementation(): SassWorkerImplementation | typeof import('sass') {
const { webcontainer } = process.versions as unknown as Record<string, unknown>;

// When `webcontainer` is a 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.
// We should periodically check with StackBlitz folks (Mark Whitfeld / Dominic Elm) to determine if this workaround is still needed.
return webcontainer ? require('sass') : new SassWorkerImplementation();
}

0 comments on commit ac66e40

Please sign in to comment.