I am learning react looking videos but in one video they are enabling css module by eject>edit webpack.config.dev.js but i can't find the same file in my react , later i came to know that in 16.7 its different so can anyone tell the steps to enable css module in react 16.7
-
It's a webpack configuration, so it shouldn't matter which react version you have. After ejecting, search die sub directories of your project for files named like webpack*.jsPatrick Hund– Patrick Hund2019-02-11 10:01:30 +00:00Commented Feb 11, 2019 at 10:01
-
i found one but in that also there is different structure , so where to add Modules:true, LocalIdentName: etccyrus– cyrus2019-02-11 10:03:22 +00:00Commented Feb 11, 2019 at 10:03
Add a comment
|
3 Answers
To enable CSS module, first of all go to your project directory then open command line and run npm run eject
inside the config folder you will find webpack.config.dev.js and webpack.config.prod.js files.open those files
and add this code to webpack.config.dev.js
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}),
},
and in filewebpack.config.prod.js add
test: cssRegex,
exclude: cssModuleRegex,
loader: getStyleLoaders({
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]',
sourceMap: shouldUseSourceMap,
}),
After saving this you can now use CSS module
1 Comment
cyrus
ok i found first one but not the second one i.e config.prod.js , but it helped me thanks
For react version 16.13
In webpack.config.js file find this keyword 'css-loader'.
You will find below code in line number 82 for react version-16.13
{
loader: require.resolve('css-loader'),
options: cssOptions,
}
Replace above object with
{
loader: require.resolve('css-loader'),
options: {
modules: {
mode: "local",
localIdentName: "[name]_[local]_[hash:base64:5]"
},
import: true,
importLoaders: true
}
}
Start the server again by npm start(If changes are not reflected)