Reputation:
I have a React component that I am trying to import the css.
import './Example.css';
class Example extends Component {
return <div className="example-class">Example</div>
}
My file structure looks like this
build
-index.js
src
-index.js
-Example.css
My webpack.config.js
looks like the following.
Currently everything builds, but the css doesnt appear to be getting picked up.
Can anyone see what I would need to do to access my classnames after build?
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js',
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules|bower_components|build)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
},
{
test: /\.css$/,
use: [ 'css-loader' ]
}
]
},
externals: {
'react': 'commonjs react'
}
};
Upvotes: 4
Views: 588
Reputation: 32972
The css-loader
only processes the CSS but does not make it available in the browser. You can use style-loader
for this, which injects the CSS into a <style>
tag. (see also Loading CSS)
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
Another possibility is to extract the CSS with extract-text-webpack-plugin
. It will extract the CSS and create a .css
file in addition to your JavaScript bundle. You can then include the CSS in your HTML.
Upvotes: 2
Reputation: 2732
You can try this:
import styles from './Example.css';
class Example extends Component {
return <div className={styles.example-class}>Example</div>
}
Upvotes: -1