PurgeCSS 删除 next.js 中的 Tailwind 字体

PurgeCSS removing Tailwind font in next.js

我正在建设一个 next.js 网站,该网站使用如下特定文本,

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      fontFamily: {
        sans: ['SFMono-Regular', 'Menlo', ...defaultTheme.fontFamily.sans],
      },
      colors: {
        // indigo: '#7D00FF',
        blue: '#51B1E8',
        red: '#FF0E00',
      },
    },
  },
  plugins: [
    require('@tailwindcss/ui'),
  ]
}

由于某种原因,文本样式在部署到 Vercel 时被清除。这是清除 css 配置。

module.exports = {
    plugins: [
      "postcss-import",
      "tailwindcss",
      "autoprefixer"
    ]
  };

  const purgecss = [
    "@fullhuman/postcss-purgecss",
    {
      content: [
        './pages/**/**/*.{js,jsx,ts,tsx}',
        './pages/**/*.{js,jsx,ts,tsx}',
        './pages/*.{js,jsx,ts,tsx}',

        './components/**/**/*.{js,jsx,ts,tsx}',
        './components/**/*.{js,jsx,ts,tsx}',
        './components/*.{js,jsx,ts,tsx}',
        ],
      defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
    }
  ];
  module.exports = {
    plugins: [
      "postcss-import",
      "tailwindcss",
      "autoprefixer",
      ...(process.env.NODE_ENV === "production" ? [purgecss] : [])
    ]
  };

这是怎么回事?

提前致谢,

我可以通过在设置中的 safelist 中添加 htmlbody 来解决这个问题。

const purgecss = require('@fullhuman/postcss-purgecss')({
  // Specify the paths to all of the template files in your project
  content: [
    // './src/**/*.html',
    './pages/**/*.vue',
    './layouts/**/*.vue',
    './components/**/*.vue'
  ],
  safelist: ['html', 'body'],

  // Include any special characters you're using in this regular expression
  defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || []
})

注意您使用的是哪个版本的 purgecss(检查 package.json): 从 whitelistPatternssafelist 发生了变化,我花了一些时间才发现

我在我的 Vue 项目中设置了这个:

module.exports = {
  content: [
    "./src/**/*.vue",
  ],
  safelist: [
    "body",
    "html",
    "img",
    "a",
    "g-image",
    "g-image--lazy",
    "g-image--loaded",
    /-(leave|enter|appear)(|-(to|from|active))$/,
    /^(?!(|.*?:)cursor-move).+-move$/,
    /^router-link(|-exact)-active$/,
    /data-v-.*/,
  ],
  extractors: [
    {
      extractor: (content) => content.match(/[A-z0-9-:\/]+/g),
      extensions: ["vue"],
    },
  ],
}

根据您使用的 PurgeCSS 版本,(我的是:v3.1.3),safelist 用于排除模式,在旧版本中您可能必须使用 whitelist 代替。