Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css-loader not importing .css file returning empty object

Tags:

Importing style from css files. Returning empty object. Seems css-loader is not working correctly. Can anyone help me on this. Please find the reference files below

index.js

import React from 'react'   
import style from './header.css'

console.log(style) // Returning empty object

export default class Header extends React.PureComponent {
  render() {
    return(
      <header className = {style.header}>
        This is header component
      </header>
    )
  }
}

./header.css

.header {
  background: #007DC6;
}

./webpack.config.js

{
  test: /\.css$/,
  exclude: /node_modules/,
  loaders: ['style-loader', 'css-loader'],
}, {
  test: /\.css$/,
  include: /node_modules/,
  loaders: ['style-loader', 'css-loader'],
}
like image 314
Gopinath Shiva Avatar asked Dec 23 '16 07:12

Gopinath Shiva


2 Answers

I wonder if this is perhaps you are not using css-modules. The example you are showing there is an example of implementing the css-modules feature of the loader.

Perhaps try adding the ?modules query to your css-loader definition.

like image 74
Sean Larkin Avatar answered Sep 18 '22 09:09

Sean Larkin


You can fix your problem with three ways :

#1: Replace 'css-loader' with 'css-loader?modules=true&camelCase=true'

#2: Do old school like this :

##index.js

import React from 'react'   
import './header.css'

export default class Header extends React.PureComponent {
  render() {
    return(
      <header className="header">
        This is header component
      </header>
    )
  }
}

#3: use babel-plugin-react-css-modules or React CSS Modules

like image 35
Mohammad Rajabloo Avatar answered Sep 19 '22 09:09

Mohammad Rajabloo