我正在用Svelte和Tailwindcss做一个项目,我用Webpack作为我的捆绑包,我显然用postcss和purgess来减少我的css的大小。
在开发模式中,我不清除任何东西,它工作得很好,但当我想构建项目时,一切都坏了。
我直接在模板中使用的tailwind类,它们最终会出现在最终的css包中。我在其中编写的自定义css被清除。
postcss.config.js
const purgecss = require('@fullhuman/postcss-purgecss');
const production = process.env.NODE_ENV === 'production';
module.exports = ({ file, options, env }) => ({
plugins: [
require('postcss-import'),
require('postcss-preset-env'),
require('tailwindcss'),
require('autoprefixer'),
production &&
purgecss({
content: ['./src/**/*.svelte'],
defaultExtractor: content => {
return content.match(/[A-Za-z0-9-_:/]+/g) || [];
}
}),
production && require('cssnano')
]
});
webpack.common.js
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const PATHS = require('./paths');
module.exports = {
entry: {
app: PATHS.src,
background: PATHS.src + '/background.js'
},
output: {
path: PATHS.build,
filename: '[name].js'
},
resolve: {
alias: {
svelte: PATHS.modules + '/svelte'
},
extensions: ['.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
},
module: {
rules: [
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true
}
}
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
name: '[name].[ext]'
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{
from: PATHS.public,
to: PATHS.build
}
]),
new HtmlWebpackPlugin({
title: 'Hanzi xiaobai',
inject: false,
template: require('html-webpack-template'),
appMountId: 'root',
lang: 'en',
mobile: true
})
]
};
webpack.production.js
const merge = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'production',
devtool: 'source-map',
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
'postcss-loader'
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css'
})
]
});
exemple.svelte
<style type="type/postcss">
h1 {
@apply text-gray-200 font-bold uppercase text-2xl antialiased
}
</style>
<div class="h-48 flex flex-col items-center">
<h1>Title</h1>
</div>
在这个例子中,h-48 flex flex-col items-center
被正确地输出和应用,但是text-gray-200 font-bold uppercase text-2xl antialiased
没有被正确输出和应用。
我试着使用svelte-preprocess-postcss
和其他东西,但我或多或少调整的东西没有线索,我在做什么。
1条答案
按热度按时间hc8w905p1#
这是一种猜测;但是您是否需要在purgecss声明中使用一个whitelist属性来保护生成的类呢?