I am trying to implement CSS modules to my project which is using React and Webpack. But, I want to keep using all the global css that I have created.
For example, previously I imported css in React like this
import './styles.scss'
And then, there will be a html element using the class .button
which exists inside ./styles.scss
<button className='button'>Click me</button>
Now since I want to implement CSS modules, I modified the css-loader
config in webpack like this
module: {
rules: [{
test: /\.s?css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
// This is where I added the config for css modules
modules: true,
localIdentName: '[hash:base32:5]-[name]-[local]',
importLoaders: 2,
}
},
{
loader: 'postcss-loader',
options: {
config: {
path: './postcss.config.js',
},
},
},
'sass-loader',
]
}]
}
However, now I cannot use the button
classname when I import like this
import './styles.scss'
Because the classnames in ./styles.scss
are all converted to hash-based classnames like 32osj-home-button
Basically, how do I configure the css-loader to load css normally when I import this way
import './styles.scss'
But use css modules when I import this way?
import styles from './styles.scss'
OR
Is there any configuration available to set css modules to load all css in :global
by default, and only load css in :local
when I specify it?
FYI, I know I can make 2 loader configs to apply css modules for css file named this way
styles.modules.scss
and apply normal css-loader for default named css
styles.scss
But, I prefer not to do so since it will create more files after Webpack bundles them.