When purifycss cleans up useless css, it causes a lot of useful css to be deleted.

want to add purifycss function, but after build, a lot of useful css have not been packaged?

webpack.build.js
var webpack = require("webpack");
var path = require("path");
var glob = require("glob");

let HtmlWebpackPlugin = require("html-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");

const CleanWebpackPlugin = require("clean-webpack-plugin");

const CopyWebpackPlugin = require("copy-webpack-plugin");

const PurifyCSSPlugin = require("purifycss-webpack");

//
var ROOT_PATH = path.resolve(__dirname);
var SRC_PATH = path.resolve(ROOT_PATH, "src");
var BUILD_PATH = path.resolve(ROOT_PATH, "dist");
var NODE_MODULES_PATH = path.resolve(ROOT_PATH, "node_modules");

console.log(1121212)
console.log(glob.sync(path.join(SRC_PATH, "/*.html")))

module.exports = {
    entry: {
        app: path.resolve(__dirname, "src/app.js")
    },
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "[name].js",
        publicPath: "/",
        chunkFilename: "[name].js"
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ["css-loader"]
                })
            },
            {
                test: /\.scss$/,
                // loader: "style-loader!css-loader!sass-loader!postcss-loader"
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    // resolve-url-loader may be chained before sass-loader if necessary
                    use: ["css-loader?minimize", "postcss-loader", "sass-loader"]
                })
            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: "style-loader"
                    }, {
                        loader: "css-loader"
                    }, {
                        loader: "less-loader"
                    },
                ]
            },
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                },
                
            },
            {
                test: /\.(png|jpg|gif|svg)/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 8192,
                            name: "imgs/[name].[hash:4].[ext]"
                        }
                    }
                ]
            },
            {
                test: /\.(htm|html)/,
                use: ["html-withimg-loader"]
            },
        ]
    },
    resolve: {
        extensions: [".js", ".jsx", ".json", "jsonp", ".scss", ".less"]
    },
    plugins: [
        new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: ""production"",
            },
        }),

        // 
        new CleanWebpackPlugin(BUILD_PATH),

        // 
        new CopyWebpackPlugin([
            {
                from: __dirname + "/src/components",
                to: __dirname + "/dist/components",
                ignore: "*.jsx"
            }
        ]),

        // 
        // new FriendlyErrorsPlugin(),

        // 
        // new webpack.NoEmitOnErrorsPlugin(),

        //  CSS
        new ExtractTextPlugin("css/style.css"),
        
        //  css
        new PurifyCSSPlugin({
            paths: glob.sync(path.join(SRC_PATH, "/index.html"))
        })

    ],
    devServer: {
        // contentBase: SRC_PATH,
        inline: true,
        port: 7000
    },
    devtool: "source-map"
};
Mar.12,2021

I have the same problem


clipboard.png


glob.sync (path.join (SRC_PATH,' / .html')) the file directory here needs to contain all the places you use the style, and don't write it as index.html


I have the same problem.
tested

    new ExtractTextPlugin("css/index.css"), 
    new PurifyCSSPlugin({ 
      paths: glob.sync(path.join(__dirname, '../src/*.html')),
    }),
    
   

workaround:
1.new ExtractTextPlugin comes before new PurifyCSSPlugin.
2. Check to see if there is a problem with the'.. / src/*.html' path.


has the problem of landlord been solved? I also encountered the same problem. Ask for advice


if you encounter the same problem, the solution provided on the third floor can solve
new ExtractTextPlugin ({

.
filename: "css/[name].css?[hash:8]"

}),
new PurifyCSSPlugin ({

paths: glob.sync(path.join(__dirname, "../src/pages/*/*.html"))

})


Today, I also encountered the same problem. The answer upstairs gave me a lot of hints. In the end, I solved it like this:

const glob = require('glob-all');
plugins: [
    new ExtractTextPlugin({
          filename: "css/[name].css?[hash:8]"
        }),
    new PurifyCSSPlugin({
      paths: glob.sync([
        path.join(__dirname, '../index.html'),
        path.join(__dirname, '../src/*/*.jsx')
      ])
    })
]

solution:
1. You can only pass one string parameter to install glob.sync that comes with glob-all,.
1.new ExtractTextPlugin comes before new PurifyCSSPlugin.
2.glob.sync configure all files that you have referenced styles (note the path), such as html,jsx.

Menu