【发布时间】:2022-01-11 10:35:35
【问题描述】:
我一直在将我的 CRA 项目升级到 TailwindCSS 3,但现在 CSS 嵌套不再起作用。启动服务器后,控制台吐出:
(8:3) Nested CSS was detected, but CSS nesting has not been configured correctly.
Please enable a CSS nesting plugin *before* Tailwind in your configuration.
See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting
但是,我看不出必须做些什么来纠正这个问题。我尝试使用 Tailwind(遵循 this guide)设置一个普通的 CRA 项目,以确保我没有冲突,但仍然没有成功。
postcss.config.js:
module.exports = {
plugins: {
"tailwindcss/nesting": {},
tailwindcss: {},
autoprefixer: {},
},
};
如您所见,我在 Tailwind 之前添加了嵌套插件。在我看来,好像插件没有被检测到。我也尝试用postcss-nesting 替换它,结果相同。
注意:我也尝试过使用 require('tailwind/nesting') 的数组语法,就像指南建议的那样。
有趣的是,从 postcss.config.js 中删除所有插件(或使用无法解析的 require)仍然会输出相同的错误,这意味着不需要此文件来加载 Tailwind。也许我遗漏了一些导致整个 postcss.config.js 文件一开始就没有被加载的东西?
index.js:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
ReactDOM.render(
<React.StrictMode>
<div className="a">
aaa
<div className="b">bbb</div>
</div>
</React.StrictMode>,
document.getElementById("root")
);
index.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
.a {
@apply text-blue-500;
.b {
@apply text-green-500;
}
}
package.json:(为简洁起见省略)
{
"name": "tailwindtest",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.12"
}
}
【问题讨论】:
-
你的 package.json 不应该包含 @tailwindcss/nesting 吗?
-
@ClémentBaconnier 根据错误中的链接:
It’s included directly in the tailwindcss package itself, so to use it all you need to do is add it to your PostCSS configuration, somewhere before Tailwind -
从我之前的 github 链接,基于 adam 的 #5896 评论和 detect-nesting.test.js 文件,我认为可以安全地假设 tailwind 不期望嵌套 CSS。即使您删除插件,它也会警告该错误消息。在我看来,这意味着不知何故,
postcss-nested或tailwindcss/nesting插件没有工作或部分工作,对吧? -
@ClémentBaconnier 感谢您的意见,他们肯定会有所帮助。恐怕问题仍然存在于另一个类中的单个类选择器中,因此 at-rules 没有直接关系。不过,我发现最近发布的 Create React App v5 已正式支持 Tailwind 并作为依赖项。也许问题是这个版本覆盖了我安装的版本,并且我的配置由于相关原因从未加载。
标签: reactjs tailwind-css postcss