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?
react-scripts
to compile the CSS, in which case, it has its own PostCSS configuration that you would not be able to override with apostcss.config.js
file and this configuration does not include thepostcss-nested
plugin.