How to use css modules with create-react-app?

后端 未结 8 1734
孤独总比滥情好
孤独总比滥情好 2020-12-08 02:38

According to a tweet by Dan Abramov, CSS modules support is there in create-react-app (CRA). One just needs to give extension of module.css to his stylesheets t

8条回答
  •  时光取名叫无心
    2020-12-08 02:59

    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)
    

提交回复
热议问题