ampersand postcss-nested 在 create-react-app 中不起作用。

huangapple go评论66阅读模式
英文:

ampersand postcss-nested not working in create-react-app

问题

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?

英文:

Ampersand doesn't work in styles (postcss)

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

commands

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

postcss.config.js in the root

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

code:

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

    &amp;-header {
      background-color: yellow;
    }
}
  • App.js
&lt;div className=&quot;App&quot;&gt;
    &lt;header className=&quot;App-header&quot;&gt;test&lt;/header&gt;
&lt;/div&gt;

why ampersand in code doesn't work?

答案1

得分: 0

使用CRA时,需要编写npm eject,然后才能编辑出现的配置更改。

如果您不想在运行npm eject时获取所有配置,那么可以使用CRACO - 它允许您单独创建配置文件。

英文:

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

huangapple
  • 本文由 发表于 2023年5月28日 10:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定