Tailwincss不能与next.js一起使用;配置有什么问题?

6kkfgxo0  于 2022-09-18  发布在  Java
关注(0)|答案(22)|浏览(409)

由于某些原因,tailwind无法在next.js中正确呈现。

我想知道我的设置是否有问题?

Style文件夹-trawind.css

@顺风基地;

/* Write your own custom base styles here */

/* Start purging... */
@tailwind components;
/* Stop purging. */

/* Write you own custom component styles here */
.btn-blue {
  @apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}

/* Start purging... */
@tailwind utilities;
/* Stop purging. */

/* Your own custom utilities */

……

_app.js

import React from "react";
// import "styles/global.scss";

import 'styles/tailwind.css'

import NavbarCustom from "components/Layout/NavbarCustom";
import Footer from "components/Layout/Footer";
import "util/analytics.js";
import { ProvideAuth } from "util/auth.js";

function MyApp({ Component, pageProps }) {
  return (
    <ProvideAuth>
      <>
        <NavbarCustom
          bg="white"
          variant="light"
          expand="md"
          logo="icons/Logo_512px.png"
        />

        <Component {...pageProps} />

我做错了什么?如此困惑,通常这种设置都是很好的。

这是网站btw-https://moodmap.app/

使用下面的信息,做出了奇怪的更改,但仍然是同一个问题。

https://moodmap.app/是站点示例。

Tailwind.config.js

module.exports = {
  future: {
    removeDeprecatedGapUtilities: true,
  },
  purge: ['./components/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {
      colors: {
        'accent-1': '#333',
      },
    },
  },
  variants: {},
  plugins: [],
}

Postcss.config.js

module.exports = {
    plugins: [
      'tailwindcss',
      'postcss-flexbugs-fixes',
      [
        'postcss-preset-env',
        {
          autoprefixer: {
            flexbox: 'no-2009',
          },
          stage: 3,
          features: {
            'custom-properties': false,
          },
        },
      ],
    ],
  }

{
  "name": "MoodMap",
  "version": "0.1.0",
  "private": true,
  "keywords": [
    "MoodMap"
  ],
  "dependencies": {
    "@analytics/google-analytics": "0.2.2",
    "@stripe/stripe-js": "^1.5.0",
    "analytics": "0.3.1",
    "fake-auth": "0.1.7",
    "mailchimp-api-v3": "1.13.1",
    "next": "9.5.3",
    "query-string": "6.9.0",
    "raw-body": "^2.4.1",
    "rc-year-calendar": "^1.0.2",
    "react": "16.12.0",
    "react-dom": "16.12.0",
    "react-hook-form": "4.10.1",
    "react-query": "2.12.1",
    "react-transition-group": "^4.4.1",
    "stripe": "^8.52.0"
  },
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "stripe-webhook": "stripe listen --forward-to localhost:3000/api/stripe-webhook"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "postcss-flexbugs-fixes": "^4.2.1",
    "postcss-preset-env": "^6.7.0",
    "stylelint": "^13.7.1",
    "stylelint-config-standard": "^20.0.0",
    "tailwindcss": "^1.8.9"
  }
}

谢谢!

vuv7lop3

vuv7lop31#

我觉得你的设置太大了。现在,你可以用简单得多的东西来实现这一点。

首先,我认为css不再需要加载到nextjs中,并且模块是本地支持的。(所以你可以用css的东西把它删除)

其次,如果你使用的是较新的版本,顺风不再需要如此复杂的设置。

因此,您将需要安装postcss-preset-env,但它现在确实消除了对大型配置的需要。

查看此示例https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss

zzzyeukh

zzzyeukh2#

检查是否在_app.js文件上导入了global als.css文件,如果没有添加此文件

导入‘../Style/lobals.css’

iyfjxgzm

iyfjxgzm3#

通过以下方式在global als.css中将导入更改为顺风样式

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

而不是

@tailwind base;
@tailwind components;
@tailwind utilities;
bvn4nwqk

bvn4nwqk4#

postcss.config.js

module.exports = {
      plugins: {
        tailwindcss: {},
        autoprefixer: {},
      },
    }

taviwind.config.js

module.exports = {
      mode: 'jit',
      content: [
        "./**/*.{js,ts,jsx,tsx}",
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    }

lobal.css

@tailwind base;
    @tailwind components;
    @tailwind utilities;
kqlmhetl

kqlmhetl5#

The issue for me was with a 'create-next-app' I did not have /pages/_app.js and therefore the global.css was not parsed.

Creating /pages/_app.js with the following content resolved the issue

import '../styles/globals.css'

// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

See https://nextjs.org/docs/basic-features/built-in-css-support

tpxzln5u

tpxzln5u6#

在尝试了这个问题中的所有方法之后,有效的方法是更新nextjs:
NPM Install Next@Latest

wixjitnu

wixjitnu7#

另一个可以在下一个js中解决问题的解决方案是

在trawind.config.js中添加以下配置:

/**@type {import('tailwindcss').Config} */
module.exports = {
  purge: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  subject: {
    extend: {},
  },
  plugins: [require("@tailwindcss/typography"),require('@tailwindcss/forms'),],

};

和postcss.config.js中

module.exports = {
  plugins: ['tailwindcss','postcss-preset-env'],
};

最后,在下一个js配置中,您将添加此命令以使其适用于您

/**@type {import('next').NextConfig} */
const nextConfig = {
  swcMinify: true,
  reactStrictMode: true,
  experimental: {
    concurrentFeatures: false, // <- Set this option to false.
    serverComponents: true,
  },
}

module.exports = nextConfig

并且您需要在样式中创建,并在global al.css中添加样式

@tailwind base;
@tailwind utilities;
@tailwind components;
w3nuxt5m

w3nuxt5m8#

我刚刚经历了这一切。我想,我的主要问题是‘缺少postcss文件配置’,因为我使用npx tailwind init进行顺风初始化,而不是npx tailwind init -p(这也会生成postcss文件)。

pcww981p

pcww981p9#

另一个简单的潜在原因是Components/Pages文件夹拼写错误--确保拼写正确。

hmmo2u0o

hmmo2u0o10#

module.exports = {
  content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}', './containers/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

<!-- You have to add paths to folders in which you are using 
   tailwindcss in the tailwind.config.js file -->

<!-- Example: I add './containers/**/*.{js,ts,jsx,tsx}' to the "content"-->
yftpprvb

yftpprvb11#

我也经历过这种情况,但我的解决方案是编辑trawind.Connfig.js

取决于版本

适用于版本2

module.exports = {
 purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
 theme: {
   extend: {},
 },
 variants: {
   extend: {},
 },
 plugins: [],
}

适用于版本3

module.exports = {
  content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
plupiseo

plupiseo12#

像这样在global al.css中导入为我解决了这个问题:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
dfty9e19

dfty9e1913#

我刚刚从CRA迁移了我的应用程序。所以我的CRA应用程序有一个名为features的文件夹,它下面是每个功能的文件夹,是它的Redux切片器和组件等。

我解决这个问题的方法是将features文件夹包含在tailwind.config.js中:

之前:

content: [
  "./pages/**/*.{js,ts,jsx,tsx}",
  "./components/**/*.{js,ts,jsx,tsx}",
],

之后

content: [
  "./pages/**/*.{js,ts,jsx,tsx}",
  "./components/**/*.{js,ts,jsx,tsx}",
  "./features/**/*.{js,ts,jsx,tsx}",
],
up9lanfz

up9lanfz14#

在Pages文件夹中添加一个_app.js文件,然后添加以下代码。Global als.css路径应该是正确的。

import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp
ne5o7dgx

ne5o7dgx15#

我在一些文件中也有同样的问题。

在删除所有内联顺风类并使用@apply将它们放入css文件后,它工作得很好。

相关问题