reactjs - 将 tailwindcss 与 React 一起使用,清除后大小没有减小

标签 reactjs tailwind-css

我尝试使用弹出的 create-react-app 设置顺风。我成功地让它工作但未能清除大小。这是我的设置

./src/assets/styles/tailwind.css

@tailwind base;

@tailwind components;

@tailwind utilities;

postcss.config.js

module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer')
    ],
};

tailwind.config.js

module.exports = {
  purge: ["./src/**/*.js"],
  theme: {
    extend: {}
  },
  variants: {},
  plugins: []
};

package.json

"scripts": {
    "start": "node scripts/start.js && postcss src/assets/styles/tailwind.css -o src/assets/styles/main.css -w",
    "build": "node scripts/build.js && postcss src/assets/styles/tailwind.css -o src/assets/styles/main.css"
}

index.js

import "./assets/styles/main.css";
// ...

我尝试创建这样的组件及其工作

<div className="w-64 h-64 bg-red-200">hai</div>

但是当我构建时,即使我在配置中给出了清除路径,大小也没有减少。无论我是否添加清除路径,它都是恒定的 143kb。我也曾在 postcss.config.js 尝试过像这样手动清除,但没有用

// postcss.config.js
const purgecss = require("@fullhuman/postcss-purgecss")({
  // Specify the paths to all of the template files in your project
  content: [
    "./src/**/*.js"
    // etc.
  ],

  // This is the function used to extract class names from your templates
  defaultExtractor: (content) => {
    // Capture as liberally as possible, including things like `h-(screen-1.5)`
    const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || [];

    // Capture classes within other delimiters like .block(class="w-1/2") in Pug
    const innerMatches = content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || [];

    return broadMatches.concat(innerMatches);
  }
});

module.exports = {
  plugins: [
    require("tailwindcss"),
    require("autoprefixer"),
    ...(process.env.NODE_ENV === "production" ? [purgecss] : [])
  ]
};

我的设置有什么问题?

最佳答案

使用最新版本,从 1.4.0 开始,您不需要手动配置 PurgeCSS。

// postcss.config.js
module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer')
    ]
}
//tailwind.config.js
module.exports = {
  purge: [
      './src/**/*.js'
  ],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}
// package.json
  "scripts": {
    "start": "npm run build:css && react-scripts start",
    "build": "NODE_ENV=production npm run build:css && react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build:css": "postcss tailwind.css -o src/main.css"
  },

注意 NODE_ENV=production

documentation说:

Now whenever you compile your CSS with NODE_ENV set to production, Tailwind will automatically purge unused styles from your CSS.

关于reactjs - 将 tailwindcss 与 React 一起使用,清除后大小没有减小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63259023/

相关文章:

javascript - react 状态更新不正确

reactjs - react typescript : Configure router to allow username based URL's without conflicting with other routes

css - 如何在react类组件中使用react-jss?

javascript - React.js 中的 Spread Notation 渲染函数和变量作用域

reactjs - Nextjs 热重载每次更改顺风 css 需要 8-10 秒

ember.js - Ember、PostCSS、SASS 和 @apply 的问题

javascript - 无法在 Next 中传递 props

reactjs - 如何使用 TailwindCSS 设置光标热点

css - 为什么 @screen xl 和 @screen lg 在 tailwindcss 中被 @screen md 覆盖?

html - 在 React 中添加组件会导致水平滚动条