undefined css module class names
Asked Answered
R

2

5

I have the the following component, imported css and webpack config - clearly I have imported the relevant css module, but the connection between my react output className={css__main.container} and the imported css-loader seems not to have been made by react - css_main.container is not mapping to .main__container___2MJY (it returns undefined and therefore react/webpack does not include in the output) - I'm sure, being new to css modules, I've just missed a step, can anyone point out what it might be? ("css-loader": "^0.28.11" is installed in package.json).

import React from 'react';
import {Route, browserHistory, Switch, Link, BrowserRouter, HashRouter} from 'react-router-dom';
import css__main from "../css/main.css";

import NoMatch from "./no-match.js";
import ButtonLink from "../components/buttonLink.js";
import List from "./lists/list-base.js";
import Home from "./subs/home.js";


class Main extends React.Component {
  render(){
    console.log( JSON.stringify( css__main ));
    //[["./src/css/main.css",".main__container___2MJY_{border:1px solid #d0d0d0;-moz-box-shadow:0 0 8px #d0d0d0;box-shadow:0 0 8px #d0d0d0;overflow:hidden;height:80vh;display:flex;flex-direction:column;justify-content:flex-start}.main__header___3ZGBv{color:#444;background-color:transparent;border-bottom:1px solid #d0d0d0;font-size:1.2em;height:65px;display:flex;flex-direction:row;align-items:stretch;justify-content:space-between;padding:15px 20px}",""]]
    return (
      <div id="container" className={css__main.container}>
        <div className={css__main.header}>
          Flix AB Testing
        </div>
        <HashRouter history={browserHistory}>
          <Switch>
            <Route exact path="/" component={Home}/>
            <Route path="/active" component={List}/>
            <Route path="/inactive" component={List}/>
            <Route path="/expire" component={List}/>
            <Route path="/delete" component={List}/>
            <Route component={NoMatch}/>
          </Switch>
        </HashRouter>
      </div>
    );
  };
} 

export default Main;

importing this css (which I can output to the console as shown in code block above):

.container {
  border: 1px solid #D0D0D0;
  -moz-box-shadow: 0 0 8px #D0D0D0;
  box-shadow: 0 0 8px #D0D0D0;
  overflow: hidden;
  height: 80vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.header {
  color: #444;
  background-color: transparent;
  border-bottom: 1px solid #D0D0D0;
  font-size: 1.2em;
  height: 65px;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  justify-content: space-between;
  padding: 15px 20px;
}

my webpack config. is as follows:

module.exports = {
  entry: [
    './src/index.js'
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.css$/,
        loader: 'css-loader',
        options: {
          minimize: true,
          camelCase: 'dashes',
          modules: true,
          localIdentName: '[name]__[local]___[hash:base64:5]'
        }
      }
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.css']
  },
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: './dist'
  }
};
Roger answered 21/6, 2018 at 16:11 Comment(0)
R
5

OK - so got it working in the following way. The final step was having style-loader included as well as css-loader included. I had read else where that it wasn't required with modules set to true, so will have to do further research.

I think someone with more knowledge (or me when I've come up to speed further) will be able to tie some of the loose ends together here - but this will work if you are having the same issues as I was.

I have surrounded my css classes with :local(myclass) {/some css here/} to get local scoping for the time being until I understand better.

{
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              minimize: true,
              camelCase: 'dashes',
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]'
            }
          }
        ]
      }

The alternative below is easiest if you are not worried about the options.

{
 use: ['style-loader', 'css-loader']
}
Roger answered 22/6, 2018 at 14:39 Comment(0)
A
1

Try to use namedExport: false for css-loader in webpack.config.js

 {
    test: /\.css$/,
    use: [
      'style-loader',
      {
        loader: 'css-loader',
        options: {
          modules: {
            namedExport: false,
          },
        },
      },
    ],
  },
Aftereffect answered 10/8, 2024 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.