itgit
itgit

Reputation: 11

CSS Modules doesn't work, React 16

I've installed css-loader and style-loader. Then I configured webpack.dev and webpack.prod. I suppose there's a problem with webpack because component's syntax isn't complicated. The component ignores styles. Why?

package.json

"dependencies": {
    "css-loader": "^0.28.11",
    "prop-types": "^15.6.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.1",
    "style-loader": "^0.20.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject"`enter code here`: "react-scripts eject"  
  }

webpack.config.dev.js

 test: /\.css$/,
 use: [
    require.resolve('style-loader'),
    {
        loader: require.resolve('css-loader'),
        options: {
            importLoaders: 1,
            modules: true,
            localIdentName: '[name]__[local]__[hash:base64:5]'
        },
    },

webpack.config.prod.js

test: /\.css$/,
loader: ExtractTextPlugin.extract(
    Object.assign(
    {
        fallback: {
            loader: require.resolve('style-loader'),
            options: {
                hmr: false,
            },
        },
        use: [
        {
            loader: require.resolve('css-loader'),
            options: {
                importLoaders: 1,
                modules: true,
                localIdentName: '[name]__[local]__[hash:base64:5]',
                minimize: true,
                sourceMap: shouldUseSourceMap,
            },
        },

component

import React, { Component } from 'react';
import styles from './addTask.css';

export default class AddTask extends Component {
  render() {
    return (
      <div className={styles.big}>
        this should be a big font
      </div>
    );
  }
}

css for the component

.big {
    font-size: 111px;
}

Upvotes: 1

Views: 9752

Answers (5)

Tolga Evcimen
Tolga Evcimen

Reputation: 7352

In order for react to understand your css file is a css-module, you need to add the suffix .module to the file name while using a react-script version newer than 2.0.0.

Simply rename addTask.css to addTask.module.css and change the import to:

import styles from './addTask.module.css'

Upvotes: 3

Akin Aguda
Akin Aguda

Reputation: 463

rename

addTask.css
to
addTask.module.css
and change the import to
import styles from './addTask.module.css'

Upvotes: 9

Bohdan
Bohdan

Reputation: 1

Try to paste it below the " test: /.css$/, ". It helped me.

use: [
                  require.resolve('style-loader'),
                  {
                      loader: require.resolve('css-loader'),
                      options: {
                          importLoaders: 1,
                          modules:true,
                          localIdentName:'[name]__[local]__[hash:base64:5]'
                      },
                  },
                  {
                      loader: require.resolve('postcss-loader'),
                      options: {
                          // Necessary for external CSS imports to work
                          // https://github.com/facebookincubator/create-react-app/issues/2677
                          ident: 'postcss',
                          plugins: () => [
                              require('postcss-flexbugs-fixes'),
                              autoprefixer({
                                  browsers: [
                                      '>1%',
                                      'last 4 versions',
                                      'Firefox ESR',
                                      'not ie < 9', // React doesn't support IE8 anyway
                                  ],
                                  flexbox: 'no-2009',
                              }),
                          ],
                      },
                  },
              ],

Upvotes: 0

Shriney Malekar
Shriney Malekar

Reputation: 11

I had same issue. Was using a git to track the files.

Before running npm run eject to edit in the webpack.config file I have added and commit the react package into the repo.

Then I run npm run eject and added above mentioned changes. and Again I restart the npm, it worked for me.

Upvotes: 1

Pranita
Pranita

Reputation: 339

Can you try the following syntax for your webpack config file?

https://webpack.js.org/concepts/loaders/#configuration
https://github.com/webpack-contrib/css-loader

   module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    { loader: 'style-loader' },
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1,
                            modules: true,
                            localIdentName: '[name]__[local]__[hash:base64:5]'
                        }
                    }
                ]
            }
        ]
    }

Upvotes: 0

Related Questions