0

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?

1
  • try install style-loader and then add it to ExtractTextPlugin.extract like this ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]') Commented Aug 13, 2016 at 5:44

1 Answer 1

1

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" />
Sign up to request clarification or add additional context in comments.

2 Comments

Please read up on how to format your posts, especially the code parts.
why did you add the font loader?

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.