Home

Awesome

UnCSS

npm version Build Status Coverage Status

UnCSS is a tool that removes unused CSS from your stylesheets. It works across multiple files and supports Javascript-injected CSS.

How

The process by which UnCSS removes the unused rules is as follows:

  1. The HTML files are loaded by jsdom and JavaScript is executed.
  2. All the stylesheets are parsed by PostCSS.
  3. document.querySelector filters out selectors that are not found in the HTML files.
  4. The remaining rules are converted back to CSS.

Please note:

Installation

npm install -g uncss

Usage

Online Server

Within Node.js

var uncss = require('uncss');

var files = ['my', 'array', 'of', 'HTML', 'files', 'or', 'http://urls.com'],
    options = {
        banner: false,
        csspath: '../public/css/',
        htmlroot: 'public',
        ignore: ['#added_at_runtime', /test\-[0-9]+/],
        ignoreSheets: [/fonts.googleapis/],
        inject: function (window) {
            window.document.querySelector('html').classList.add('no-csscalc', 'csscalc');
        },
        jsdom: {
            userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
        },
        media: ['(min-width: 700px) handheld and (orientation: landscape)'],
        raw: 'h1 { color: green }',
        report: false,
        strictSSL: true,
        stylesheets: ['lib/bootstrap/dist/css/bootstrap.css', 'src/public/css/main.css'],
        timeout: 1000,
        uncssrc: '.uncssrc',
        userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)',
    };

uncss(files, options, function (error, output) {
    console.log(output);
});

/* Look Ma, no options! */
uncss(files, function (error, output) {
    console.log(output);
});

/* Specifying raw HTML */
var rawHtml = '...';

uncss(rawHtml, options, function (error, output) {
    console.log(output);
});

At build-time

UnCSS can also be used in conjunction with other JavaScript build systems, such as Grunt, Broccoli or Gulp!

From the command line

Usage: uncss [options] <file or URL, ...>
    e.g. uncss https://getbootstrap.com/docs/3.3/examples/jumbotron/ > stylesheet.css

Options:

  -h, --help                            output usage information
  -V, --version                         output the version number
  -i, --ignore <selector, ...>          Do not remove given selectors
  -m, --media <media_query, ...>        Process additional media queries
  -C, --csspath <path>                  Relative path where the CSS files are located
  -s, --stylesheets <file, ...>         Specify additional stylesheets to process
  -S, --ignoreSheets <selector, ...>    Do not include specified stylesheets
  -r, --raw <string>                    Pass in a raw string of CSS
  -t, --timeout <milliseconds>          Wait for JS evaluation
  -H, --htmlroot <folder>               Absolute paths' root location
  -u, --uncssrc <file>                  Load these options from <file>
  -n, --noBanner                        Disable banner
  -a, --userAgent <string>              Use a custom user agent string
  -I, --inject <file>                   Path to javascript file to be executed before uncss runs
  -o, --output <file>                   Path to write resulting CSS to

Note that you can pass both local file paths (which are processed by glob) and URLs to the program.

As a PostCSS Plugin

UnCSS can be used as a PostCSS Plugin.

postcss([require('uncss').postcssPlugin]);

See PostCSS docs for examples for your environment.

Note: Depending on your environment, you might not be able to use uncss as a PostCSS plugin since the plugin is not directly exported. In such cases, use the wrapper library postcss-uncss.

Options

Example Configuration
{
  html: ['index.html', 'about.html', 'team/*.html'],
  ignore: ['.fade']
}

License

Copyright (c) 2019 Giacomo Martino. See the LICENSE file for license rights and limitations (MIT).