Here is a component importing css...
import React from 'react';
import styles from './sample.css';
class Index extends React.Component {
render(){
return (
<div>
<div className="${styles.bodywrap}"></div>
</div>
);
}
}
export default Index;
My stylesheet sample.css
is the following...
.bodywrap {
color:red;
}
But when I run the web app I get the following error...
SyntaxError: C:/Users/Eric/Desktop/tutorHub/components/index_components/sample.c
ss: Unexpected token (1:0)
> 1 | .bodywrap {
| ^
2 | color:red;
3 | }
I followed the instuctions to setup css modules, so my webpack.config looks as follows...
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: "./main.js",
output: {
path: '/',
filename: 'index.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query:{
presets: ['es2015','react'],
}
},
{
test: /\.css/,
loader: ExtractTextPlugin.extract('css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]')
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
new webpack.optimize.OccurrenceOrderPlugin()
]
};
Any idea why my CSS is not being read properly? Do I need something to convert the code so it is recognized properly?
EDIT
I think its important to node that I'm rendering the component above through the server using node js...
var express = require('express');
var router = express.Router();
var React = require('react');
var reactDom = require('react-dom/server');
var App = React.createFactory(require('../components/index'));
var MasterLayout = React.createFactory(require('../master/links'));
router.get('/', function(req,res) {
var reactHtml = reactDom.renderToString(App({}));
var styles = reactDom.renderToString(MasterLayout({}));
res.render('../../tutorHub/index.jade', {reactOutput: reactHtml, links: styles});
});
Can this be interfering with the css?
In your webpack.config.js add this loader :-
{
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
Add in your component add this :-
import 'yourPath.css';
for use classes of this css file just write
<div className="yourClassName" />