gpt4 book ai didi

javascript - NextJs Tailwind build (purge) 移除所有样式

转载 作者:行者123 更新时间:2023-12-04 14:35:18 26 4
gpt4 key购买 nike

在我当前的 NextJS 项目中,我使用 Tailwind 作为 CSS 框架。当我运行 yarn dev ,一切正常,但每当我运行 yarn build后跟 yarn start ,我的所有 CSS 似乎都被清除了,因为我的页面布局完全不同。
前:
enter image description here
后:
enter image description here
我的 tailwind.config.js 文件:

    /* eslint-disable global-require */

const defaultTheme = require('tailwindcss/defaultTheme');

module.exports = {
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
darkMode: false, // or 'media' or 'class'
theme: {
screens: {
sm: '640px',
md: '768px',
max_md: { max: '767px' },
lg: '1024px',
xl: '1536px',
},
colors: {
primary: '#f2a53f',
white: '#fff',
},
fontFamily: {
title: ['Dancing Script', 'Times New Roman', 'sans-serif'],
sans: ['Roboto', ...defaultTheme.fontFamily.sans],
},
textShadow: {
default: '1px 1px 3px #000',
},
zIndex: {
1: -1,
},
extend: {
height: {
128: '32rem',
},
margin: {},
},
},
variants: {
extend: {},
},
plugins: [require('tailwindcss-textshadow')],
};
postcss.config.js 文件:
const purgecss = [
'@fullhuman/postcss-purgecss',
{
content: [
'./pages/**/*.{js,jsx,ts,tsx}',
'./components/**/*.{js,jsx,ts,tsx}',
],
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
},
];

module.exports = {
plugins: [
'tailwindcss',
process.env.NODE_ENV === 'production' ? purgecss : undefined,
],
};
_app.tsx:
import Head from 'next/head';
import { ApolloProvider } from '@apollo/client';
import { AppProps } from 'next/app';
import { useApollo } from '../../apollo/client';

import '../styles/globals.css';

const MyApp = ({ Component, pageProps }: AppProps) => (
<ApolloProvider client={useApollo(pageProps.initialApolloState)}>
<Component {...pageProps} />
</ApolloProvider>
);

export default MyApp;
globals.css:
@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
font-family: 'Dancing Script';
font-style: medium;
font-weight: 500;
font-display: swap;
src: local('Dancing Script'),
url(/fonts/DancingScript-Medium.tff) format('tff');
}
body {
margin: 0 !important;
}
package.json 依赖:
"dependencies": {
"@apollo/client": "3.3.12",
"@apollo/react-hooks": "4.0.0",
"@contentful/rich-text-react-renderer": "14.1.2",
"@contentful/rich-text-types": "14.1.2",
"apollo-cache-inmemory": "1.6.6",
"apollo-client": "2.6.10",
"apollo-link-http": "1.5.17",
"autoprefixer": "10.2.5",
"clsx": "1.1.1",
"contentful": "8.2.0",
"graphql": "15.5.0",
"graphql-tag": "2.11.0",
"next": "10.0.9",
"next-with-apollo": "5.1.1",
"postcss": "8.2.8",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-icons": "4.2.0",
"tailwindcss": "2.0.4",
"tailwindcss-textshadow": "2.1.3"
},
"devDependencies": {
"@commitlint/cli": "12.0.1",
"@commitlint/config-conventional": "12.0.1",
"@fullhuman/postcss-purgecss": "4.0.3",
"@types/node": "14.14.35",
"@types/react": "17.0.3",
"@types/react-dom": "17.0.2",
"@typescript-eslint/eslint-plugin": "4.18.0",
"@typescript-eslint/parser": "4.18.0",
"add": "2.0.6",
"commitizen": "4.2.3",
"cz-conventional-changelog": "3.3.0",
"eslint": "7.22.0",
"eslint-config-airbnb": "18.2.1",
"eslint-config-prettier": "8.1.0",
"eslint-import-resolver-typescript": "2.4.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-prettier": "3.3.1",
"eslint-plugin-react": "7.22.0",
"eslint-plugin-react-hooks": "4.2.0",
"husky": "5.1.3",
"lint-staged": "10.5.4",
"postcss-preset-env": "6.7.0",
"prettier": "2.2.1",
"typescript": "4.2.3",
"yarn": "1.22.10"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
如果有任何问题,请随时告诉我。我在网上查看了其他问题,但没有找到解决我的问题的方法。

最佳答案

仔细检查 tailwind.config.js 中的路径.如果您的组件不在 components目录,则需要更新 purge反射(reflect)这一点的路径。

关于javascript - NextJs Tailwind build (purge) 移除所有样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66789526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com