0

I am trying to use css modules inside react, but it is not working.

The log of this code:

import React from 'react'
import styles from '../../css/test.css'

class Test extends React.Component {
    render() {
        console.log(styles)
        return (
            <div className={styles.app}>
                <p>This text will be blue</p>
            </div>
        );
    }
}

export default Test

returns Object {}

and the rendered code are tags with no class:

<div><p>This text will be blue</p></div>

The css code is available at site, here is my test.css:

.test p {
  color: blue;
} 

If I changed the div to have class='test', the color of p changes to blue

Here is my webpack.config.js

var path = require('path')
var webpack = require('webpack')
var HappyPack = require('happypack')
var BundleTracker = require('webpack-bundle-tracker')
var path = require('path')
var ExtractTextPlugin = require("extract-text-webpack-plugin")

function _path(p) {
  return path.join(__dirname, p);
}

module.exports = {

    context: __dirname,
    entry: [
        './assets/js/index'
    ], 

    output: {
        path: path.resolve('./assets/bundles/'), 
        filename: '[name].js'
    },

    devtool: 'inline-eval-cheap-source-map',

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}), 
        new webpack.ProvidePlugin({ 
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery' 
        }),
        new HappyPack({
            id: 'jsx',
            threads: 4,
            loaders: ["babel-loader"]
        }),
        new ExtractTextPlugin("[name].css", { allChunks: true })

    ],

    module: {
        loaders: [

             {
                test: /\.css$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!resolve-url-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]")
            },

            {
                test: /\.scss$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!resolve-url-loader!sass-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]")
            },

            {
                test: /\.jsx?$/, 
                include: path.resolve(__dirname, './assets/js/'),
                exclude: /node_modules/, 
                loaders: ["happypack/loader?id=jsx"]
            },
            {
                test: /\.png$/,
                loader: 'file-loader',
                query: {
                    name: '/static/img/[name].[ext]'
                }
            }
        ]
    },

    resolve: {
        modulesDirectories: ['node_modules'],
        extensions: ['', '.js', '.jsx'],
        alias: {
          'inputmask' : _path('node_modules/jquery-mask-plugin/dist/jquery.mask')
        }, 
    }   
}

Can anyone help me?

Thanks in advance.

2
  • Did you find the solution? Commented Nov 14, 2017 at 18:16
  • Yes, I will update ASAP Commented Nov 14, 2017 at 18:21

3 Answers 3

0

Looks like your passing the css-loader params to the resolve-url-loader:

"css-loader!resolve-url-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]"

Should be:

"css-loader?modules=true&localIdentName=[name]__[local]___[hash:base64:5]&importLoaders=1!resolve-url-loader"
Sign up to request clarification or add additional context in comments.

Comments

0

A lot of time passed since this question, so my webpack was update many times with another technologies.

This webpack config is working:

...
module.exports = {
  entry,
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  devtool:
    process.env.NODE_ENV === 'production' ? 'source-map' : 'inline-source-map',

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: path.resolve(__dirname, './app/view/'),
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.pcss$/,
        include: path.resolve(__dirname, './app/view/'),
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader:
              'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: function() {
                return [
                  require('postcss-import'),
                  require('postcss-mixins'),
                  require('postcss-cssnext')({
                    browsers: ['last 2 versions']
                  }),
                  require('postcss-nested'),
                  require('postcss-brand-colors')
                ];
              }
            }
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(png|svg|jpg|webp)$/,
        use: {
          loader: 'file-loader'
        }
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    modules: [path.resolve(__dirname, 'node_modules')]
  },
  plugins
};

I guess that there is an issue with older versions at webpack with this line:

'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'

Try importLoaders and importLoader

You can see my repo too.

1 Comment

I have this problem in Jest. See stackoverflow.com/questions/47292537/…
-2

In my specific case, I'm using official utility facebook/create-react-app. You have to run the following command to get access to the webpack configuration:

npm run eject

You will then need to edit config/webpack.config.js and set the css-loader modules option to true.

use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction && shouldUseSourceMap,
                modules:true <-----
              }),

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.