1

This is the style.css file:

.cake {
  color: red;
}

This is index.js:

Fails:

import React from "react";
import ReactDOM from "react-dom";

import './style.css';

function App() {
  return (
    <div className="cake">Hello!</div>
  );
}

ReactDOM.render(<App />, document.querySelector("#root"));

The CSS styling fails to render and the text is just black in browser.

However, this does work:

Succeeds:

import React from "react";
import ReactDOM from "react-dom";

import styles from './style.css';

function App() {
  return (
    <div className={styles.cake}>Hello!!</div>
  );
}

ReactDOM.render(<App />, document.querySelector("#root"));

This is the relevant section of webpack.config.js:

      {
        test: /\.css$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              modules: true
            }
          }
        ]
      },

I would like to have the option of using the string literal version, and I'm a bit perplexed that many people online seem to have it available. Is there some webpack config change I can apply? Or am I missing something else here?

2
  • Are you using create-react-app? Commented Jan 30, 2020 at 14:23
  • No I spun up my own package. Commented Jan 30, 2020 at 14:24

1 Answer 1

1

This will do the magic:

{
        test: /\.css$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              modules: false
            }
          }
        ]
      },

Actually you've enabled css-modules, which in my opinion are really great to have. You can read more about css-modules here.

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

Comments

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.