This is a plugin uses PurifyCSS to clean your CSS. You should use the extract-text-webpack-plugin with this.
Without any CSS file being emitted as an asset, this plugin will not do a thing except idle about inside the compiler. You can also use the file
plugin to drop a special CSS file into your output folder, but it is highly recommend these two plugins together.
First, install it:
npm install purifycss-webpack-plugin --save-dev
Then configure as follows:
const path = require('path');
const glob = require('glob');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PurifyPlugin = require('purifycss-webpack-plugin');
module.exports = {
entry: {...},
output: {...},
module: {
rules: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallbackLoader: 'style-loader',
loader: 'css-loader'
})
}
]
},
plugins: [
new ExtractTextPlugin('[name].[contenthash].css'),
// Make sure this is after ExtractTextPlugin!
new PurifyPlugin({
// Give paths to parse for rules. These should be absolute!
paths: glob.sync(path.join(__dirname, 'app/*.html')),
})
]
};
And, that's it! Your scripts and view files will be scanned for classes, and those that are unused will be stripped off your CSS - aka. "purified".
You can pass an object (
<entry> -> [<absolute path>]
) topaths
if you want to control the behavior per entry.
This plugin, unlike the original PurifyCSS plugin, provides special features, such as scanning the dependency files. You can configure using the following fields:
Property | Description |
---|---|
extensions |
An array of extensions that should be given to PurifyCSS when determining classes. This defaults to webpack resolve.extensions configuration. Often it's a good idea to override this with ['.html'] so it won't traverse node_modules .js files. |
paths |
An array of absolute paths or a path to traverse. This also accepts an object (<entry name> -> <paths> ). It can be a good idea glob these. |
purifyOptions |
Pass custom options to PurifyCSS. |
purifycss-webpack-plugin does not emit sourcemaps even if you enable
sourceMap
option on loaders!
ISC.