Babel import css syntax error

2.1k views Asked by At

I want to be able to use import in my react application for not only js/jsx files but also for css files. From what I've read, the best way to do that is to use the extract-text-webpack-plugin which will take your imported css files and bundle them together.

I've set it up so that its generating my bundled css file, but for some reason every time I load my page I get a syntax error:

SyntaxError: MyWebpage/views/global.css: Unexpected token, expected ; (1:5)
> 1 | body {
    |      ^
  2 |     margin: 0;
  3 | }

My setup looks like this:

webpack.config.js

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');


const config = {
  entry: ['babel-polyfill', './views/Index.jsx'],
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: 'bundle.js',
    publicPath: '/public'
  },
  module: {
    rules: [
      { test: /\.(jsx|js)$/, exclude: /node_modules/ , use: 'babel-loader' },
      { 
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ]
};

module.exports = config;

The entry point ./views/Index.js is where I'm importing my css file:

Index.js

import React from 'react';
import Layout from './Layout.jsx';
import PageContent from './PageContent.jsx';
import './global.css';

class Index extends React.Component {
  render() {
    return (
        <Layout title={this.props.title}>
            <PageContent />
        </Layout>
    );
  }
}

export default Index;

Inside the imported ./Layout.jsx file I'm using a <link> to include the bundled css file in my page:

Layout.jsx

import React from 'react';

class Layout extends React.Component {
    render() {
        return (
            <html>
            <head>
                <title>{this.props.title}</title>
                <link rel="stylesheet" href="styles.css" />
            </head>
            <body> 
                <div id="root">
                    {this.props.children}
                </div>
                <script type="text/javascript" src="./bundle.js"></script>
            </body>
            </html>
        );
    }
}

export default Layout;

I'm pretty confused because it seems like my app is building fine, but when I try to access my webpage I keep getting a syntax error.

Can anyone please help me understand what I'm doing wrong?

2

There are 2 answers

0
RVCoder On

It seems problem with loaders below is example of webpack.config.js file working for jsx and css loaders :

module.exports = {
    entry: './app/index.js',
    output: {
        path: __dirname,
        filename: 'dist/bundle.js'
    },
    devServer: {
        inline: true,
        port: 3000
    },
    module: {
        loaders: [{
            test: /.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
                presets: ['es2015', 'react', 'react-hmre']
            }
        },
        {
                test: /\.scss$/,
                loaders: [ 'style', 'css', 'sass' ]
            }]
    }
}; 
0
andrew I. On

it seems like babel or webpack is not loading the loaders.

This variant helps me with it (just include into webpack.config.js):

    require.extensions['.css'] = () => {
      return;
    };

More here... [link]