2

Ampersand doesn't work in styles (postcss)

package npm i --dev autoprefixer postcss-nested postcss-cli npm-run-all

commands

"scripts": {
    "start": "npm-run-all -p watch:css start-js",
    "build": "npm-run-all build:css build-js",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "build:css": "postcss src/styles/main.css -o src/index.css",
    "watch:css": "postcss src/styles/main.css -o src/index.css -w",
    "start-js": "react-scripts start",
    "build-js": "react-scripts build"
}

postcss.config.js in the root

module.exports = {
    plugins: [require("postcss-nested"), require("autoprefixer")],
};

code:

  • index.css
.App {
    background-color: #ffffff;

    &-header {
      background-color: yellow;
    }
}
  • App.js
<div className="App">
    <header className="App-header">test</header>
</div>

why ampersand in code doesn't work?

1
  • 1
    Even though you have separate npm scripts for building CSS, you may be actually using react-scripts to compile the CSS, in which case, it has its own PostCSS configuration that you would not be able to override with a postcss.config.js file and this configuration does not include the postcss-nested plugin.
    – Wongjn
    May 28 at 15:06

1 Answer 1

0

When using CRA, you need to write npm eject and only then write changes in the config that appears.

If you don't want to get all the configs when you write npm eject, then use CRACO - it allows you to create a file for configs separately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.