如何在 Next 13 的应用程序文件夹中应用 tailwind css

Ate*_*han 23 next.js tailwind-css

Tailwind CSS 不适用于 next.js v13 中的应用程序文件夹,但它适用于 Pages 和 Components 文件夹。在 tailwind.config 文件中,我添加了

但是,没有 CSS 应用于应用程序文件夹中的组件!

content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
    "./app/**/*.{js,ts,jsx,tsx}",
],
Run Code Online (Sandbox Code Playgroud)

lor*_*usu 26

try to check the following: (works for me)

  • in next.config.js

set experimental.appDir: true to enable app directory feature

const nextConfig = {
  experimental: {
    appDir: true,
  },
}
Run Code Online (Sandbox Code Playgroud)
  • in tailwind.config.js
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx}",
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  ...
}
Run Code Online (Sandbox Code Playgroud)
  • in ./app/globals.css
@tailwind base;
@tailwind components;
@tailwind utilities;
Run Code Online (Sandbox Code Playgroud)
  • in ./app/layout.tsx

import css in layout => works fine

import css in page => not working

import './globals.css';
...
Run Code Online (Sandbox Code Playgroud)

  • 在下一版本 13.1.0 上,Tailwind 在与应用程序目录一起使用时似乎仍然无法正常工作。我的最后一个工作版本是 13.0.6。我面临着同样的问题,这对我有用。 (3认同)
  • 遵循官方指南(测试版)[Beta-Next.js-Tailwind](https://beta.nextjs.org/docs/styling/tailwind-css) (2认同)

小智 8

有同样的问题。

我将 globals.css 从 移至/app/styles然后导入app/layout.tsx

现在工作正常。


mik*_*ajs 7

将 tailwindcss 与 Nextjs 13 和 Turbopack 结合使用

更新依赖项

npm install -D tailwindcss postcss autoprefixer concurrently
npx tailwindcss init -p
Run Code Online (Sandbox Code Playgroud)

配置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: [],
}
Run Code Online (Sandbox Code Playgroud)

配置next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  experimental: {
    appDir: true,
  },
}

module.exports = nextConfig
Run Code Online (Sandbox Code Playgroud)

更新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"
},
// ...
Run Code Online (Sandbox Code Playgroud)

创造./styles/input.css

@tailwind base;
@tailwind components;
@tailwind utilities;
Run Code Online (Sandbox Code Playgroud)

创建空./styles/output.css文件


Run Code Online (Sandbox Code Playgroud)

创造./app/layout.tsx

import "../styles/output.css";

export default function Root({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
Run Code Online (Sandbox Code Playgroud)

添加样式到./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>
  );
}
Run Code Online (Sandbox Code Playgroud)

运行项目

npm run dev
Run Code Online (Sandbox Code Playgroud)

参考:


Rya*_*ink 5

啊...找出问题来解决我的评论。

就我而言,我必须对下一个配置进行额外的更新,因为我正在使用 Turborepo。

在 NextJS 12 中,以下是我的next.config.js

const withTM = require("next-transpile-modules")(["ui"]);

module.exports = withTM({
  reactStrictMode: true,
  experimental: { appDir: true }
});
Run Code Online (Sandbox Code Playgroud)

然而,我意识到通过从最新开始启动一个新的turborepo,你转换代码的方式已经改变了。现在这也需要在实验块中声明。

module.exports = {
  reactStrictMode: true,
  experimental: {
    transpilePackages: ["ui"],
    appDir: true
  }
};
Run Code Online (Sandbox Code Playgroud)

一旦完成此操作,以及 nextjs 文档中列出的步骤,您应该能够在 Next + Turborepo 中使用 tailwind :)