Skip to content
This repository was archived by the owner on Dec 25, 2018. It is now read-only.

webpack-contrib/purifycss-webpack

Repository files navigation

build status bitHound Score codecov

PurifyCSS Webpack Plugin

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.

Usage

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>]) to paths if you want to control the behavior per entry.

Options

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!

License

ISC.