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

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

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

谢谢!

t3irkdon

t3irkdon16#

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

f0ofjuux

f0ofjuux17#

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

"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: [],
};
1bqhqjot

1bqhqjot18#

我的项目也有同样的问题,但我尝试这样更改globals.css

在此之前

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900');

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

之后

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import './base.css';
@import 'tailwindcss/utilities';
kuuvgm7e

kuuvgm7e19#

我的一个项目安装了这些程序包版本

"next": "11.1.0",
"autoprefixer": "^10.3.3",
"postcss": "^8.3.6",
"tailwindcss": "^2.2.8"

tailwind.config.js中的此设置正在运行:

module.exports = {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
};

最近,我用这些包启动了一个新项目:

"next": "12.0.8",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.15"

以前的配置设置不起作用。因此我将其更改为:

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};
vc9ivgsu

vc9ivgsu20#

在我的例子中,我已经正确地设置了尾风配置文件,但是顺风样式不适用。删除.next文件夹并运行next dev有帮助。

dohp0rv5

dohp0rv521#

对我来说,解决方案是将./src/...添加到tailwind.config.js中的内容源中。官方的Next.js+Tailwincss示例不支持src文件夹。

tv6aics1

tv6aics122#

适用于使用nextJS创建项目的开发人员。

请注意,tailwind.config.js的内容需要指向文件的正确路径。

module.exports = {
  content: ["./pages/*.{html,js,jsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

例如,如果您使用nextjs创建一个项目,则您有一个pages文件夹,您的index.js文件位于该文件夹中。因此,https://tailwindcss.com/docs/installation/using-postcss上的代码片段(见下文)不完全匹配。把它改成上面的或者你自己喜欢的。

module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

相关问题