简体   繁体   English

Nuxt 中的 Tailwind PostCss 配置

[英]Tailwind PostCss Config in Nuxt

Error Message错误信息

"Please use build.postcss in your nuxt.config.js instead of an external config file. Support for such files will be removed in Nuxt 3 as they remove all defaults set by Nuxt and can cause severe problems with features like alias resolving inside your CSS." “请在你的 nuxt.config.js 中使用 build.postcss 而不是外部配置文件。在 Nuxt 3 中将删除对此类文件的支持,因为它们会删除 Nuxt 设置的所有默认值,并可能导致严重的问题,例如在你的内部解析别名等功能CSS。”

Question

How do I change my postcss.config.js file to be in my nuxt.config.js file?如何将我的 postcss.config.js 文件更改为在我的 nuxt.config.js 文件中?

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/**/*.html', './src/**/*.vue'],

    // 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] : []),
    ],
};

As the error says, please use build.postcss for your configuration.如错误所述,请使用 build.postcss 进行配置。 This means that you should place your plugins within nuxt.config.js > build > postcss .这意味着您应该将插件放在nuxt.config.js > build > postcss中。 For you case, this would look something like:对于您的情况,这看起来像:

export default {
    // other config here like head & target
    build: {
        // other build config like babel
        postcss: {
            plugins: {
                require('tailwindcss'),
                require('autoprefixer'),

                ...(process.env.NODE_ENV === 'production' ? 
                    [require('@fullhuman/postcss-purgecss')
                        ({
                            content: ...,
                            defaultExtractor: ...,
                        })
                    ] : []
                ),
            }
        }
    }
}

Most info you will need is documented on the Nuxt docs for the build configuration property您需要的大部分信息都记录在构建配置属性的 Nuxt 文档中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM