+0
日增长
9,387
stars
401
forks
133
watchers
197
prs
303
issues
18
releases
README
UnCSS
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:
- UnCSS cannot be run on non-HTML pages, such as templates or PHP files. If you need to run UnCSS against your templates, you should probably generate example HTML pages from your templates, and run uncss on those generated files; or run a live local dev server, and point uncss at that.
- UnCSS only runs the Javascript that is run on page load. It does not (and cannot) handle Javascript that runs on user interactions like button clicks. You must use the ignore option to preserve classes that are added by Javascript on user interaction.
Installation
shell
npm install -g uncss
Usage
Online Server
- https://uncss-online.com/ - Unofficial server, very convenient for testing or one-off usage!
Within Node.js
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!
- grunt-uncss – Thanks to @addyosmani
- gulp-uncss – Thanks to @ben-eb
- broccoli-uncss - Thanks to @sindresorhus
From the command line
txt
Usage: uncss [options]
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 Top issues
#321Ability to ignore js script in html23 comments · 2017-07-14#339JavaScript heap out of memory19 comments · 2017-10-18#331With -n avoid injecting blank lines (and unnecessary newlines)18 comments · 2017-09-16#69Parse inline CSS in HTML18 comments · 2014-06-07#459Add more collaborators to the organization9 comments · 2021-06-10#359Add option to ignore TLS/SSL errors9 comments · 2018-01-31#353Doesn't remove :not selectors6 comments · 2017-12-12#431Postcss export object will only run uncss on a single css file piped in4 comments · 2020-05-20