在purgecss中使用类名

时间:2020-07-22 10:32:47

标签: webpack class-names css-purge

我在classnames和Webpack中使用purgeCSS

显然,PurgeCSS本身无法处理这些类名。 如何实现提取器来过滤classnames()函数中定义的类名?

我的webpack.config.js已在答案中,因为我无法将其保存在此处

1 个答案:

答案 0 :(得分:0)

const path = require( 'path' );
const glob = require('glob-all');

// Plugins
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const CssoWebpackPlugin = require( 'csso-webpack-plugin' ).default;
const PurgecssPlugin = require('purgecss-webpack-plugin');
const TerserJSPlugin = require( 'terser-webpack-plugin' );
const OptimizeCSSAssetsPlugin = require( 'optimize-css-assets-webpack-plugin' );

// External Scripts
const externals = require('./config/externals');

const PurgeCssPaths = {
    block: path.join(__dirname, 'src', 'block')
};

module.exports = [
    // Compile Javascript
    {
        mode: 'production',
        entry: {
            blocks: './src/blocks.js',
            accordion: './src/block/accordion/scripts/accordion.js',
        },
        optimization: {
            minimize: true,
            minimizer: [
                new TerserJSPlugin( {
                    sourceMap: true,
                    terserOptions: {
                        output: {
                            comments: false,
                        },
                    },
                    extractComments: false,
                } ),
            ],
        },
        output: {
            path: path.resolve( __dirname, 'dist/js' ),
            filename: '[name].bundle.js',
        },
        module: {
            rules: [
                {
                    test: /\.(jsx?)$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                    },
                },
            ],
        },
        externals: externals
    },
    // Compile CSS
    {
        mode: 'production',
        entry: {
            editor: './src/block/editor.scss',
            style: './src/block/style.scss',
        },
        output: {
            path: path.resolve( __dirname, 'dist/css' ),
            filename: '[name].bundle.js',
        },
        optimization: {
            minimize: true,
            minimizer: [
                new TerserJSPlugin( {
                    sourceMap: true,
                    terserOptions: {
                        output: {
                            comments: false,
                        },
                    },
                    extractComments: false,
                } ),
                new OptimizeCSSAssetsPlugin( {} ),
            ],
        },
        plugins: [
            new MiniCssExtractPlugin( {
                filename: '[name].css',
                chunkFilename: '[id].css',
            } ),
            new CssoWebpackPlugin( {
                pluginOutputPostfix: 'min',
            } ),
      new PurgecssPlugin({
                paths: glob.sync([
                    `${PurgeCssPaths.block}/**/*`
                ], {nodir: true}),
        extractors: [
          {
            extractor: content => content.match(/[A-z0-9-:\/]+/g) || [],
            extensions: ['js', 'ts', 'php']
          }
        ],
        whitelist: [],
        whitelistPatterns: [],
        whitelistPatternsChildren: []
      })
        ],
        module: {
            rules: [
                {
                    test: /\.(png|jpg|jpeg|gif|ico)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[name].[ext]',
                                outputPath: 'images',
                            },
                        },
                    ],
                },
                {
                    test: /\.svgz?$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[name].[ext]',
                                outputPath: 'images',
                            },
                        },
                        {
                            loader: 'svgo-loader',
                        },
                    ],
                },
                {
                    test: /\.(pc|sa|sc|c)ss$/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader,
                            options: {
                                esModule: false,
                            },
                        },
                        'css-loader',
                        'postcss-loader',
                        'sass-loader',
                    ],
                },
            ],
        },
    },
];