1

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;
}
8
  • Inspect the output in the DOM - is there a class name applied at all? If so, is it the mangled on you specified in the css module loader options, or is it just "big"? Commented Mar 16, 2018 at 18:51
  • Also, in dev mode, don't use the extracttextplugin, that loads your css into an external file. if you're not including that file in your html, it won't work. in dev mode let the style-loader inject your css via javascript Commented Mar 16, 2018 at 18:53
  • Check a working example in webpack-demo project. Hope it helps! Commented Mar 16, 2018 at 19:17
  • Where is your compiled CSS going? Are you sure it's being loaded at all? Take a look at the webpack extract-text plugin. Commented Mar 16, 2018 at 19:25
  • I've inspect the output in the DOM - there's no class name applied to div at all. But what is the reason? Commented Mar 16, 2018 at 20:03

5 Answers 5

9

rename

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

Sign up to request clarification or add additional context in comments.

1 Comment

this works for me. fyi im using 16.7 version of react. all of the examples in editing your webpack.config.js is useless now cause i think its built in already
3

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'

Comments

1

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.

Comments

0

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]'
                        }
                    }
                ]
            }
        ]
    }

Comments

0

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',
                              }),
                          ],
                      },
                  },
              ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.