【问题标题】:Using classnames with purgecss使用带有 purgecss 的类名
【发布时间】:2020-11-11 21:04:24
【问题描述】:

我将classnamespurgeCSS 和Webpack 一起使用。

显然 PurgeCSS 不能自己处理这些类名。 如何实现一个提取器来过滤classnames() 函数中定义的类名?

我的 webpack.config.js 在答案中,因为我无法将其保存在这里

【问题讨论】:

    标签: webpack class-names css-purge


    【解决方案1】:
    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',
                        ],
                    },
                ],
            },
        },
    ];
    
    

    【讨论】:

    • 请考虑发布 Github gist、Pastebin 或其他分发代码 sn-ps 的方式,而不是发布答案。谢谢!
    猜你喜欢
    • 2021-01-14
    • 2020-07-14
    • 2021-06-15
    • 2021-08-03
    • 2019-01-07
    • 2016-10-16
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多