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

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

由于某些原因,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"
  }
}

谢谢!

zzzyeukh

zzzyeukh1#

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

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

bvn4nwqk

bvn4nwqk2#

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

kqlmhetl3#

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

tpxzln5u4#

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

wixjitnu

wixjitnu5#

另一个可以在下一个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;
f0ofjuux

f0ofjuux6#

适用于仍对以下版本有问题的用户:

"autoprefixer": "^10.4.7",
"postcss": "^8.4.13",
"tailwindcss": "^3.0.24",

在NX+Next.js环境中,您只需使用以下配置文件:

// postcss.config.js
const { join } = require('path');

module.exports = {
  plugins: {
    tailwindcss: {
      config: join(__dirname, 'tailwind.config.js'),
    },
    autoprefixer: {},
  },
};
// tailwind.config.js
const { join } = require('path');

module.exports = {
  content: [
    join(__dirname, './pages/**/*.{js,ts,jsx,tsx}'),
    join(__dirname, './src/**/*.{js,ts,jsx,tsx}'),
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};
t3irkdon

t3irkdon7#

在摆弄了几个小时之后,我让它以最奇怪的方式工作;我只是在我的一个组件中的一个元素中添加了一个类,然后在global al.css文件中为那个类编写了定制的css,然后所有顺风类都会反映出来。这可能是顺风的代码中的一个错误,他们需要跟踪和修复。

ne5o7dgx

ne5o7dgx8#

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

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

up9lanfz

up9lanfz9#

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

import '../styles/globals.css'

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

export default MyApp
dfty9e19

dfty9e1910#

我刚刚从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}",
],
plupiseo

plupiseo11#

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

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

vuv7lop312#

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

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

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

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

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

yftpprvb

yftpprvb13#

我也经历过这种情况,但我的解决方案是编辑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: [],
}
hmmo2u0o

hmmo2u0o14#

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"-->
pcww981p

pcww981p15#

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

相关问题