prv*_*ist 5 css reactjs next.js tailwind-css
顺风版: v9.3.5
PostCSS 配置:
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
...(process.env.NODE_ENV === 'production'
? {
'@fullhuman/postcss-purgecss': {
content: ['./components/**/*.js', './pages/**/*.js'],
defaultExtractor: content =>
content.match(/[\w-/:]+(?<!:)/g) || [],
},
}
: {}),
},
}
Run Code Online (Sandbox Code Playgroud)
顺风配置:
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
tint: 'rgba(0,0,0,0.3)',
},
},
},
variants: {},
plugins: [],
}
Run Code Online (Sandbox Code Playgroud)
样式在开发中完美运行,但在生产中只有一些样式有效。在检查 build 文件夹中的 CSS 文件时,看起来某些 CSS 类没有被提取或可能被清除,因此导致应用程序的部分样式。
编辑:PurgeCSS 版本 3.0 添加安全列表选项,替换白名单
当我将某些类的名称动态注入到我的 html 模板中时,我遇到了同样的问题。
我使用的是nuxt.js/tailwindcss所以你需要先阅读文档来解决你的问题。
这是生成生产中缺少的类的代码
computed: {
axeY() {
return this.y < 0 ? `-translate-y${this.y}` + ' ' : `translate-y-1` + ' '
},
axeX() {
return this.x < 0 ? `-translate-x${this.x}` : `translate-x-${this.x}`
},
Run Code Online (Sandbox Code Playgroud)
PostCSS 会将所有文件分析到内容表中(在配置文件中声明),
请注意,我的文件不包含带有翻译前缀
的类
,如您所见,我缺少的类是:[translate-x-1,-translate-x -1, translate-y-1, -translate-y-1] ...数字 1 是一个变量。
/*
** TailwindCSS Configuration File
**
** Docs: https://tailwindcss.com/docs/configuration
** Default: https://github.com/tailwindcss/tailwindcss/blob/master/stubs/defaultConfig.stub.js
*/
const num = [1, 2, 3, 4, 5, 6, 8, 10, 12]
const whitelist = []
num.map((x) => {
whitelist.push('translate-x-' + x)
whitelist.push('-translate-x-' + x)
whitelist.push('translate-y-' + x)
whitelist.push('-translate-y-' + x)
})
module.exports = {
future: {
removeDeprecatedGapUtilities: true,
},
theme: {},
variants: {
backgroundColor: ['hover', 'focus', 'active'],
},
plugins: [],
purge: {
// Learn more on https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css
enabled: process.env.NODE_ENV === 'production',
content: [
'components/**/*.vue',
'layouts/**/*.vue',
'pages/**/*.vue',
'plugins/**/*.js',
'nuxt.config.js',
],
options: {
whitelist,
},
},
}
Run Code Online (Sandbox Code Playgroud)
发现问题,postcss配置在内容数组中缺少部分文件夹,而且由于我的 js 文件有jsx,我也需要添加它。
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
...(process.env.NODE_ENV === 'production'
? {
'@fullhuman/postcss-purgecss': {
// added sections folder and changed extension to jsx
content: ['./components/**/*.jsx', './pages/**/*.js', './sections/**/**/*.jsx'],
defaultExtractor: content =>
content.match(/[\w-/:]+(?<!:)/g) || [],
},
}
: {}),
},
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4672 次 |
| 最近记录: |