Tailwind CSS在next.js v13中未应用于app文件夹,但在Pages和Components文件夹中可以正常工作。 在tailwind.config文件中,我已经添加了
然而,在app文件夹中的组件上没有应用任何CSS!
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
"./app/**/*.{js,ts,jsx,tsx}",
],
Tailwind CSS在next.js v13中未应用于app文件夹,但在Pages和Components文件夹中可以正常工作。 在tailwind.config文件中,我已经添加了
然而,在app文件夹中的组件上没有应用任何CSS!
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
"./app/**/*.{js,ts,jsx,tsx}",
],
尝试检查以下内容(这对我起作用):
设置
experimental.appDir: true
以启用应用程序目录功能
const nextConfig = {
experimental: {
appDir: true,
},
}
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx}",
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
...
}
@tailwind base;
@tailwind components;
@tailwind utilities;
在layout中导入CSS => 可以正常工作
在页面中导入CSS => 不起作用
import './globals.css';
...
我遇到了相同的问题。
我将globals.css从/app
移动到/styles
,然后在app/layout.tsx
中进行导入。
现在可以正常工作了。
npm install -D tailwindcss postcss autoprefixer concurrently
npx tailwindcss init -p
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx}", // Note the addition of the `app` directory.
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
experimental: {
appDir: true,
},
}
module.exports = nextConfig
package.json
// ...
"scripts": {
"dev": "concurrently \"next dev --turbo\" \"tailwindcss --input ./styles/input.css --output ./styles/output.css --watch\"",
"build": "tailwindcss ./styles/input.css --output ./styles/output.css && next build",
"start": "next start",
"lint": "next lint"
},
// ...
./styles/input.css
@tailwind base;
@tailwind components;
@tailwind utilities;
./styles/output.css
文件
./app/layout.tsx
文件import "../styles/output.css";
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
./app/page.tsx
中添加样式export default function Home() {
return (
<h1 className="m-12 text-4xl text-red-600">
Welcome to NextJS 13 with tailwindcss and turbopack
</h1>
);
}
npm run dev
啊……我找到了解决我的评论问题的方法。
在我的情况下,我需要对我的next配置进行额外的更新,因为我正在使用Turborepo。
在NextJS 12中,以下是我在next.config.js
中的内容。
const withTM = require("next-transpile-modules")(["ui"]);
module.exports = withTM({
reactStrictMode: true,
experimental: { appDir: true }
});
然而,我通过从最新版本开始创建一个新的turborepo,意识到代码转译的方式已经改变。现在,这也需要在实验性块中声明。
module.exports = {
reactStrictMode: true,
experimental: {
transpilePackages: ["ui"],
appDir: true
}
};
如果您按照Next.js文档中列出的步骤操作,再执行此操作,那么您就能在Next.js + Turborepo中使用Tailwind了 :)
我认为@lorekkusu是正确的,但他漏掉了一些东西。
假设我正在处理/app/blog/page.tsx,即使它已经在/app/layout.tsx中被导入,globals.css仍需要重新导入。
import '../globals.css' //at /app/blog/page.tsx
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./src/app/**/*.{js,ts,jsx,tsx}',
'./src/pages/**/*.{js,ts,jsx,tsx}',
'./src/components/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [],
}
postcss.config.js
文件并在其中添加 tailwind 插件module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
content: [
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/app/**/*.{js,ts,jsx,tsx,mdx}', ]
记得在 Nextjs 项目中将文件夹结构更改为 /app 样式时,您需要将此行添加到 tailwind.config.js 中。
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx}", // <-------------------
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
...
}
接下来请检查以下内容:
在 next.config.js 文件中
const nextConfig = {
experimental: {
appDir: true,
},
}
在globals.css中
@tailwind base;
@tailwind components;
@tailwind utilities;
最后,无论您在何处导入globals.css
都要这样做:
import './globals.css';