Detalhes do pacote

grunt-uncss

uncss4.7kMIT0.10.1

A grunt task for generating CSS files containing only those styles used in your project.

gruntplugin, uncss, css

readme (leia-me)

grunt-uncss Built with Grunt

Build Status dependencies Status devDependencies Status

A grunt task for removing unused CSS from your projects with UnCSS.

Getting Started

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-uncss --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-uncss');

Issues with the output should be reported on the UnCSS issue tracker.

Preview

Taking a multi-page project using Bootstrap with >120KB of CSS down to 11KB.

Demo

Uncss task

Run this task with the grunt uncss command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Options

Options are passed to UnCSS. In addition this task defines an extra option:

report

  • Choices: 'min', 'gzip'
  • Default: 'min'

Report minification result or both minification and gzip results. This is useful to see exactly how well clean-css is performing but using 'gzip' will make the task take 5-10x longer to complete. Example output.

Usage examples

Use the grunt-uncss task by specifying a target destination (file) for your cleaned CSS. Below this is dist/css/tidy.css.

Along-side, specify the input HTML files you would like scanned for used selectors. In this case app/index.html and app/about.html are the two files we would like checked.

uncss: {
    dist: {
        files: {
            'dist/css/tidy.css': ['app/index.html', 'app/about.html']
        }
    }
}

Which you can then use alongside a processor like processhtml to rewrite the location of your stylesheet to tidy.css using a block like:

<!-- build:css css/tidy.css -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/bootstrap.css">
<!-- /build -->

and some configuration like:

processhtml: {
    dist: {
        files: {
            'dist/index.html': ['app/index.html'],
            'dist/about.html': ['app/about.html']
        }
    }
}
// Remove unused CSS across multiple files
uncss: {
    dist: {
        files: {
            'dist/css/tidy.css': ['app/index.html', 'app/about.html']
        }
    }
}
// Remove unused CSS across multiple files and ignore specific selectors
uncss: {
    dist: {
        options: {
            ignore: ['#added_at_runtime', '.created_by_jQuery']
        },
        files: {
            'dist/css/tidy.css': ['app/index.html', 'app/about.html']
        }
    }
}
// Remove unused CSS from URLs (php, node, etc.)
// (Note that`nonull` must be true, or else Grunt
// removes remote paths that it can't find locally)
uncss: {
    dist: {
        files: [{
            nonull: true,
            src: ['http://localhost:8080/path1', 'http://localhost:8080/path2'],
            dest: 'dist/css/tidy.css'
        }]
    }
}

Examples

Sites

Apps

Other

The problem

User-interface libraries like Bootstrap, TopCoat and so on are fairly prolific, however many developers use less than 10% of the CSS they provide (when opting for the full build, which most do). As a result, they can end up with fairly bloated stylesheets which can significantly increase page load time and affect performance. grunt-uncss is an attempt to help with by generating a CSS file containing only the CSS used in your project, based on selector testing.

Research and alternative solutions

There have been many efforts to try solving the problem of finding unused CSS in the past. Opera created ucss, @aanand created https://github.com/aanand/deadweight, Brian Le Roux CSS Slap Chop and there were a number of client-side solutions also crafted, such as Helium-CSS, CSSESS and the Python mincss.

Unfortunately, most of these solutions don't actually generate what you're really after - a leaner build of your project CSS containing only those rules you used. Finding that a more recent project called UnCSS did try tackling this, I set out to share some of the problems we need to solve in this space with the developer and build a Grunt task to enable usage of it in builds more easily.

Huge thanks go out to Giacomo Martino for his help with the Node module this task uses.

Coverage

WordPress

While UnCSS works best (and quickest) with static HTML files, it is possible to pass in a URL array that contains all the URLs on your website, and process all used selectors that way. @lgladdy wrote a guide on how to do this on his blog

Yeoman Generator

If you're looking for a webapp starting point with grunt-uncss integrated, see generator-webapp-uncss.

Limitations

Please note that the CSS parser used in the uncss module we rely on currently isn't able to work with complex selectors. For example [data-section=''] > section > [data-section-title] a. This may mean that at build time you run into exceptions such as TypeError: Cannot convert undefined or null to object. If this is the case, please consider moving these selectors to a separate stylesheet which the task does not run on.

We are actively looking at how to improve the CSS parsers used and will update this notice once this problem has been solved.

Maintainers

License

(C) Addy Osmani 2020, released under the MIT license

changelog (log de mudanças)

grunt-uncss Changelog

v0.10.1

  • Update (dev)dependencies.

v0.10.0

  • Drop Node.js < 10 support.
  • Update (dev)dependencies.

v0.9.1

  • Update UnCSS to v0.17.3.
  • Update (dev)dependencies.
  • Switch to GitHub Actions CI

v0.9.0

  • Update UnCSS to v0.17.2.
  • Update (dev)dependencies.
  • Add Node.js 12 in CI.

v0.8.6

  • Update (dev)dependencies.
  • CI tweaks.

v0.8.5

  • Update dependencies.

v0.8.4

  • Update dependencies.

v0.8.3

  • Update devDependencies.
  • Switch to ES6.

v0.8.1

  • Switch to xo for linting.
  • Update devDependencies.
  • Migrate to the uncss GitHub organization.

v0.8.0

  • Update UnCSS to v0.16.0 (Node.js >= 6 is required)

v0.7.1

  • Switch to ESLint
  • Fix tests issue

v0.7.0

  • Update UnCSS to v0.15.0 (Node.js >= 4 is required)
  • Update chalk to v2.0.1
  • Update devDependencies to their latest versions
  • Fix tests issues

v0.6.2

  • Update devDependencies to their latest versions.

v0.6.1

  • Update async to v2.0.1.

v0.6.0

  • Update UnCSS to v0.14.0

v0.5.2

  • Update devDependencies
  • Fix specifying URLs #142

v0.5.1

  • Update devDependencies
  • Update CI configs

v0.5.0

  • Update devDependencies including UnCSS to v0.13.0
  • Handle multiple files sequentially (#178)
  • Drop Node.js 0.10 support

v0.4.0

  • Update devDependencies including UnCSS to v0.12.0
  • Add Windows testing via AppVeyor

v0.3.8

  • Update devDependencies
  • Fail when no files and URLs are defined

v0.3.7

  • Update all dependencies except for UnCSS
  • Remove the unused underscore dependency

v0.3.5

  • Lock UnCSS to v0.8.1 due to various issues
  • Update the other dependencies

v0.3.4

  • Update dependencies
  • Update README.md

v0.3.3

  • Update UnCSS

v0.3.2

  • Additional cleanup for report option

v0.3.1

  • Fix failure when report wasn't set

v0.3.0

  • Update dependencies including UnCSS to 0.8.0
  • Enable report by default and switch to using maxmin

v0.1.6

  • Upgrades to UnCSS 0.6.2 - brings stable support for PhantomJS and processing dynamically injected CSS via JavaScript, a better selector engine (CSSSelect via cheerio)
  • Now supports csspath, ignore, raw, timeout options
  • Improved support for multiple files, general stability fixes with issues upstream now fixed
  • Added new suite of unit tests. Run with grunt test
  • README.md now includes links to articles, videos, a better Bootstrap 3 example
  • General improvements to sample Gruntfile.js (processhtml, cssmin, load-grunt-tasks)
  • Complimentary plugin for Gulp is now available