简体   繁体   English

Taiwlind CSS:样式不会在生产中加载,在开发中工作。 将 Next.js 与 MDX 结合使用

[英]Taiwlind CSS: Styles don't load in production, works in dev. Using Next.js with MDX

I probably cobbled together this from a bunch of blog posts online.我可能是从网上的一堆博客文章中拼凑出来的。 Some blogs use autoprefixer , some use postcss-preset-env & it's all confusion.一些博客使用autoprefixer ,一些使用postcss-preset-env并且很混乱。

I have installed tailwindcss as a dev dependency & imported the styles properly in pages/_app.tsx .我已经安装了tailwindcss作为开发依赖项并在pages/_app.tsx正确导入了样式。 Here are my relevant files:这是我的相关文件:

package.json包.json

"dependencies": {
    "@mapbox/rehype-prism": "^0.5.0",
    "@mdx-js/react": "^1.6.19",
    "@types/nprogress": "^0.2.0",
    "fast-glob": "^3.2.4",
    "next": "10.0.0",
    "nprogress": "^0.2.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "tinytime": "^0.2.6"
},
"devDependencies": {
    "@fullhuman/postcss-purgecss": "^3.0.0",
    "@mdx-js/loader": "^1.6.19",
    "@next/bundle-analyzer": "^10.0.0",
    "@types/mdx-js__react": "^1.5.3",
    "@types/node": "^14.14.6",
    "@types/react": "^16.9.55",
    "@types/react-dom": "^16.9.9",
    "@types/webpack-env": "^1.15.3",
    "cross-env": "^7.0.2",
    "feed": "^4.2.1",
    "file-loader": "^6.2.0",
    "postcss-flexbugs-fixes": "^4.2.1",
    "postcss-preset-env": "^6.7.0",
    "rimraf": "^3.0.2",
    "simple-functional-loader": "^1.2.1",
    "tailwindcss": "^1.9.6",
    "typescript": "4.0.5"
},

tailwind.config.js tailwind.config.js

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    future: {
        removeDeprecatedGapUtilities: true,
        purgeLayersByDefault: true,
    },
    purge: {
        mode: 'all',
        content: ['./src/**/*.{js,ts,jsx,tsx}', './next.config.js'],
        options: {
            extractors: [
                {
                    extensions: ['mdx'],
                    extractor: (content) => {
                        content = mdx.sync(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)
                    },
                },
            ],
        },
    },
    theme: {
        extend: {
            colors: {
                'accent-1': '#333',
            },
            fontFamily: {
                sans: ['Inter var', ...defaultTheme.fontFamily.sans],
            },
        },
    },
    variants: {},
    plugins: [
        function ({ addBase, addComponents, theme }) {
            addBase([
                {
                    '@font-face': {
                        fontFamily: 'Inter var',
                        fontWeight: '100 900',
                        fontStyle: 'normal',
                        fontNamedInstance: 'Regular',
                        fontDisplay: 'swap',
                        src: 'url("/fonts/Inter-roman.var-latin.woff2?3.13") format("woff2")',
                    },
                },
                {
                    '@font-face': {
                        fontFamily: 'Inter var',
                        fontWeight: '100 900',
                        fontStyle: 'italic',
                        fontNamedInstance: 'Italic',
                        fontDisplay: 'swap',
                        src: 'url("/fonts/Inter-italic.var-latin.woff2?3.13") format("woff2")',
                    },
                },
            ])
        },
    ],
}

postcss.config.js postcss.config.js

module.exports = {
    plugins: [
        'tailwindcss',
        'postcss-flexbugs-fixes',
        process.env.NODE_ENV === 'production'
            ? [
                    '@fullhuman/postcss-purgecss',
                    {
                        content: ['./pages/**/*.{js,jsx,ts,tsx}', './components/**/*.{js,jsx,ts,tsx}'],
                        defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
                    },
              ]
            : undefined,
        [
            'postcss-preset-env',
            {
                autoprefixer: {
                    flexbox: 'no-2009',
                },
                stage: 3,
                features: {
                    'custom-properties': false,
                },
            },
        ],
    ],
}

I am sure I'm doing something wrong but don't know what?我确定我做错了什么,但不知道是什么?

Found the solution.找到了解决办法。

I removed the entire thing below from postcss.config.js :我从postcss.config.js删除了下面的整个postcss.config.js

process.env.NODE_ENV === 'production'
            ? [
                    '@fullhuman/postcss-purgecss',
                    {
                        content: ['./pages/**/*.{js,jsx,ts,tsx}', './components/**/*.{js,jsx,ts,tsx}'],
                        defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
                    },
              ]
            : undefined,

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

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