If you want to hash the CSS class names you can do it with CSS Modules.
I was able to implement CSS Modules in an Angular application that uses webpack (no angular-cli) thanks to postcss-modules and posthtml-css-modules.
postcss-modules hash all the styles, then posthtml-css-modules replaces the class names on the html files by the hashed class names.
In order to use postcss-modules and posthtml-css-modules you need to configure your rules for (css|scss|sass) and html files like this:
module: {
rules: [
...
{
test: /\.html$/,
use: [
{
loader: 'html-loader'
},
{
loader: 'posthtml-loader',
options: {
config: {
ctx: {
include: {...options},
content: {...options}
}
}
}
}
]
},
{
test: /\.(css|sass|scss)$/,
use: [
'to-string-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => {
return [require("postcss-modules")({generateScopedName: "[hash:base64:8]"})]
}
}
},
{
loader: 'sass-loader',
}
]
},
...
]
...
The posthtml-loader will search for the posthtml.config.js file in the root of the project:
module.exports = ({ file, options, env }) => {
return ({
plugins: [
require('posthtml-css-modules')(file.dirname.concat('/').concat(file.basename.replace('.html', '.scss.json')))
]
})
};
With that, you should be able to use CSS Modules in Angular with Webpack.
CSS Modules in Angular (with Angular CLI)
You can use CSS Modules in Angular with Angular CLI too.
The approach is the same, the only difference is that you need to use @angular-builders/custom-webpack to customize the build so you can add postcss-modules and posthtml-css-modules.
I documented CSS Modules in Angular (with Angular CLI) here:
I hope it will be useful for you.