3

我正在构建一个 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] : [])
    ]
  };

到底是怎么回事?

提前致谢,

4

2 回答 2

4

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

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花了一些时间才知道

于 2020-10-09T01:40:19.227 回答
0

我在我的 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

于 2021-03-18T17:06:52.817 回答