Minify CSS class names with Angular CLI

Viewed 360

I'm looking for a way to improve performance by minifying my app's CSS class names. This approach is used by large websites and is also described in this article.

Does anybody have an idea on how to do this with Angular CLI v10+ ? Ideally I'd want to add a webpack plugin while keeping the CLI for compilation, or a similar approach with minimal footprint, obviously for production builds only.

1 Answers

You can achieve something using the ViewEncapsulation API. By default it uses Emulated which generate large CSS class names. If you change that in your components to ShadowDom. This will encapsulate the styles and will shift everything to use Shadow DOM. With Shadow DOM the styles won't be leaked outside the components. You have to test it though and check for browsers support because it's not supported everywhere. Also, global styles might not work as you expect.

Edit: I also found this interesting article that explain something similar using Angular.

Related