1

I have a react project that uses styled components, and I'm trying to include a CSS file as part of react-image-gallery

I followed the steps to include css-loader and style-loader in my project and tried importing the file like this

import 'react-image-gallery/styles/css/image-gallery.css

and included the following in Webpack config rules

{
  test: /\.css$/i,
  use: ['style-loader', 'css-loader'],
}

When running the server I'm getting the below error message

SyntaxError: Invalid or unexpected token

(function (exports, require, module, __filename, __dirname) { @charset "UTF-8";

in the above CSS file

From some googling, I understood that this is because the CSS file is included as a JS file by Webpack. But isn't that how it is supposed to be?

Addition info: I have a server side rendered app.

What am I doing wrong?

Update:

My rules look like this

A rules.ts file

import webpack from 'webpack'

const ts: webpack.RuleSetRule = {
  test: /^(?!.*\.spec\.tsx?$).*\.tsx?$/,
  exclude: /node_modules/,
  use: ['babel-loader'],
}

const img: webpack.RuleSetRule = {
  test: /\.(png|jpe?g|gif|svg)$/,
  use: 'file-loader',
}

const css: webpack.RuleSetRule = {
  test: /\.css$/i,
  use: ['style-loader', 'css-loader'],
}

export default {
  client: {
    ts,
    img,
    css,
  },
  server: {
    ts,
    css,
    img: { ...img, use: [{ loader: 'file-loader', options: { emitFile: false } }] },
  },
}

A config file that has the following

const config: webpack.Configuration = {
  context: path.join(__dirname, '../../src/client'),
  resolve: {
    ...resolve.client,
    alias: { 'react-dom': '@hot-loader/react-dom' },
  },
  devtool: false,
  entry: {
    main: ['./index.tsx'],
  },
  mode: 'development',
  module: {
    rules: [rules.client.ts, rules.client.img, rules.client.css],
  },
  output: output.client,
  plugins: [
    ...plugins,
    ...developmentPlugins,
    new ForkTsCheckerWebpackPlugin({
      tsconfig: path.join(__dirname, '../../tsconfig.fork-ts-checker-webpack.plugin.json'),
    }),
    new CleanWebpackPlugin({
      cleanAfterEveryBuildPatterns: ['!webpack.partial.html', '!favicon.ico'],
    }),
  ],
}
4
  • 1
    That looks like it's NOT using the css-loader / style-loader but just using the normal javascript loader, what's your other rules look like.? Commented Jul 8, 2019 at 9:35
  • @Keith updated the question with the rules Commented Jul 8, 2019 at 9:37
  • You have const css:, but your using rules.client.css, so I'm assuming you have assigned these consts to some object literal rules.client, it's not just a simple mistake that your assignments are incorrect. I wonder if it might be easier if you just show your whole webpack config? Commented Jul 8, 2019 at 9:47
  • Yes, I have these rules in a separate file. updated the question again with more context. Commented Jul 8, 2019 at 9:50

1 Answer 1

1

We had 4 co-workers stuck on an issue like that. Actually, we had a plugin "nodemon-webpack-plugin", which we configured for webpack.

This plugin tried to execute files like .css as java-script files.

We finally removed this plugin, because we had an up and running mon already.

Sign up to request clarification or add additional context in comments.

1 Comment

This does not seem to be an answer. Are you suggesting to remove the plugin? If so, is there a recommended replacement? If that's not possible, perhaps a comment is better than an answer. Please review the FAQ section on answering questions: stackoverflow.com/help/how-to-answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.