【问题标题】:Tailwindcss not working with next.js; what is wrong with the configuration?Tailwindcss 不适用于 next.js;配置有什么问题?
【发布时间】:2021-01-09 21:40:54
【问题描述】:

由于某种原因,tailwind 在 next.js 中没有正确呈现。

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

样式文件夹 - tailwind.css

@tailwind 基地;

/* 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} />

我做错了什么?好糊涂,一般这种设置就好了。

这是网站顺便说一句 - https://mmap-1xr646x4a.vercel.app/

使用下面的信息,进行了更改,但奇怪的是仍然是同样的问题。

https://mmap-hewlbern.moodmap.vercel.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"
  }
}

谢谢!

【问题讨论】:

    标签: javascript reactjs next.js tailwind-css css-purge


    【解决方案1】:

    我在某些文件中遇到了同样的问题。

    在删除所有内联顺风类并将它们放入带有@apply 的 CSS 文件后,它运行良好。

    【讨论】:

      【解决方案2】:

      我的项目也有同样的问题,但我尝试像这样更改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';
      

      【讨论】:

      • '/base.css'; 呢?是另一个文件还是从顺风导入的?
      • @NicoGulo 你的自定义css
      • 但我的自定义 css 在 globals.css 中。我应该移动到 base.css 吗?
      • 这对我有用。谢谢!
      【解决方案3】:

      我认为你的设置太大了。现在你可以用更简单的东西来实现这一点。

      首先,我认为不再需要将 CSS 加载到 nextjs 中,并且原生支持模块。 (所以你可以用CSS的东西删除这个)

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

      所以你需要安装 postcss-preset-env,但它现在确实不需要大配置。

      看看这个例子https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss

      【讨论】:

      • 您在 _app.js 中的 css 导入是否正常工作?尝试删除 next.config.js :)
      • 这是一个非答案。 “您的设置太复杂”是无用的意见。
      猜你喜欢
      • 2022-12-03
      • 2020-06-09
      • 2018-07-09
      • 2021-11-03
      • 2022-11-30
      • 2021-01-02
      • 2020-07-22
      • 2017-03-08
      • 2014-01-13
      相关资源
      最近更新 更多