Version 1
[yaffs-website] / node_modules / grunt-uncss / README.md
1 # grunt-uncss [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)\r
2 \r
3 [![NPM version](https://img.shields.io/npm/v/grunt-uncss.svg?)](https://www.npmjs.com/package/grunt-uncss)\r
4 [![Linux Build Status](https://img.shields.io/travis/addyosmani/grunt-uncss/master.svg?label=Linux%20build)](https://travis-ci.org/addyosmani/grunt-uncss)\r
5 [![Windows Build status](https://img.shields.io/appveyor/ci/addyosmani/grunt-uncss/master.svg?label=Windows%20build)](https://ci.appveyor.com/project/addyosmani/grunt-uncss/branch/master)\r
6 [![dependencies Status](https://img.shields.io/david/addyosmani/grunt-uncss.svg)](https://david-dm.org/addyosmani/grunt-uncss)\r
7 [![devDependencies Status](https://img.shields.io/david/dev/addyosmani/grunt-uncss.svg)](https://david-dm.org/addyosmani/grunt-uncss?type=dev)\r
8 \r
9 >A grunt task for removing unused CSS from your projects with [UnCSS](https://github.com/giakki/uncss). Works across multiple files and supports dynamically injected CSS via PhantomJS.\r
10 \r
11 ## Getting Started\r
12 \r
13 If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the\r
14 [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create\r
15 a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins.\r
16 Once you're familiar with that process, you may install this plugin with this command:\r
17 \r
18 ```shell\r
19 npm install grunt-uncss --save-dev\r
20 ```\r
21 \r
22 Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:\r
23 \r
24 ```js\r
25 grunt.loadNpmTasks('grunt-uncss');\r
26 ```\r
27 \r
28 **Issues with the output should be reported on the UnCSS [issue tracker](https://github.com/giakki/uncss/issues).**\r
29 \r
30 ## Preview\r
31 \r
32 Taking a multi-page project using Bootstrap with >120KB of CSS down to 11KB.\r
33 \r
34 ![Demo](https://i.imgur.com/uhWMALH.gif)\r
35 \r
36 ## Uncss task\r
37 \r
38 *Run this task with the `grunt uncss` command.*\r
39 \r
40 Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.\r
41 \r
42 ## Options\r
43 \r
44 Options are passed to [UnCSS](https://github.com/giakki/uncss#within-nodejs). In addition this task defines an extra option:\r
45 \r
46 ### report\r
47 \r
48 * Choices: `'min'`, `'gzip'`\r
49 * Default: `'min'`\r
50 \r
51 Report minification result or both minification and gzip results.\r
52 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](https://github.com/sindresorhus/maxmin#readme).\r
53 \r
54 ## Usage examples\r
55 \r
56 Use the `grunt-uncss` task by specifying a target destination (file) for your cleaned CSS.\r
57 Below this is `dist/css/tidy.css`.\r
58 \r
59 Along-side, specify the input HTML files you would like scanned for used selectors.\r
60 In this case `app/index.html` and `app/about.html` are the two files we would like checked.\r
61 \r
62 ```js\r
63 uncss: {\r
64   dist: {\r
65     files: {\r
66       'dist/css/tidy.css': ['app/index.html', 'app/about.html']\r
67     }\r
68   }\r
69 }\r
70 ```\r
71 \r
72 Which you can then use alongside a processor like\r
73 [processhtml](https://github.com/dciccale/grunt-processhtml) to\r
74 rewrite the location of your stylesheet to `tidy.css` using a block like:\r
75 \r
76 ```html\r
77 <!-- build:css css/tidy.css -->\r
78 <link rel="stylesheet" href="css/normalize.css">\r
79 <link rel="stylesheet" href="css/main.css">\r
80 <link rel="stylesheet" href="css/bootstrap.css">\r
81 <!-- /build -->\r
82 ```\r
83 \r
84 and some configuration like:\r
85 \r
86 ```js\r
87 processhtml: {\r
88   dist: {\r
89     files: {\r
90       'dist/index.html': ['app/index.html'],\r
91       'dist/about.html': ['app/about.html']\r
92     }\r
93   }\r
94 }\r
95 ```\r
96 \r
97 ```js\r
98 // Remove unused CSS across multiple files\r
99 uncss: {\r
100   dist: {\r
101     files: {\r
102       'dist/css/tidy.css': ['app/index.html', 'app/about.html']\r
103     }\r
104   }\r
105 }\r
106 ```\r
107 \r
108 ```js\r
109 // Remove unused CSS across multiple files and ignore specific selectors\r
110 uncss: {\r
111   dist: {\r
112     options: {\r
113       ignore: ['#added_at_runtime', '.created_by_jQuery']\r
114     },\r
115     files: {\r
116       'dist/css/tidy.css': ['app/index.html', 'app/about.html']\r
117     }\r
118   }\r
119 }\r
120 ```\r
121 \r
122 ```js\r
123 // Remove unused CSS from URLs (php, node, etc.)\r
124 // (Note that`nonull` must be true, or else Grunt\r
125 // removes remote paths that it can't find locally)\r
126 uncss: {\r
127   dist: {\r
128     files: [{\r
129       nonull: true,\r
130       src: ['http://localhost:8080/path1', 'http://localhost:8080/path2'],\r
131       dest: 'dist/css/tidy.css'\r
132     }]\r
133   }\r
134 }\r
135 ```\r
136 \r
137 ### Test project\r
138 \r
139 There is a test project included under the `tests/app` directory which you can build by running `grunt` after an `npm install`.\r
140 It also includes a `grunt compare_size` task for getting a feel of the before and after CSS sizes:\r
141 \r
142 ![grunt compare_size](https://i.imgur.com/bUseCPh.png)\r
143 \r
144 ## Examples\r
145 \r
146 ### Sites\r
147 \r
148 * [HTML5Boilerplate.com website](https://github.com/h5bp/html5boilerplate.com)\r
149 * [Mozilla's DXR documentation viewer](https://github.com/mozilla/dxr)\r
150 * [Vanilla forums docs site](https://github.com/vanilla/vanilla-docs)\r
151 \r
152 ### Apps\r
153 \r
154 * [GitHub Team Viewer - Angular app](https://github.com/vinitkumar/github-team-viewer)\r
155 * [Angular Todo application](https://github.com/JeremyCarlsten/grunt-uncss-angular-example)\r
156 * [CashSplitter - Angular app with PouchDB, Bootstrap](https://github.com/carlo-colombo/CashSplitter)\r
157 * [This Week's Comics Express app](https://github.com/WillsonSmith/thisWeeksComics)\r
158 * [Sample grunt-uncss in a Sass project](https://github.com/addyosmani/grunt-uncss-sass-example)\r
159 \r
160 ### Other\r
161 \r
162 * [Using grunt-uncss with Wordpress](https://github.com/sboodhoo/Grunt-UnCSS-WordPress)\r
163 * [JSON Sitemap generator for grunt-uncss](https://github.com/phoenixMag00/JSON-Sitemap-Generator-for-Grunt-UnCSS-with-WordPress)\r
164 \r
165 ## The problem\r
166 \r
167 User-interface libraries like [Bootstrap](https://getbootstrap.com), [TopCoat](http://topcoat.io)\r
168 and so on are fairly prolific, however many developers use less than 10% of the CSS they provide\r
169 (when opting for the full build, which most do). As a result, they can end up with fairly bloated\r
170 stylesheets which can significantly increase page load time and affect performance.\r
171 `grunt-uncss` is an attempt to help with by generating a CSS file containing only the CSS used\r
172 in your project, based on selector testing.\r
173 \r
174 ## Research and alternative solutions\r
175 \r
176 There have been many efforts to try solving the problem of finding unused CSS in the past. Opera created\r
177 [ucss](https://github.com/oyvindeh/ucss), @aanand created <https://github.com/aanand/deadweight>,\r
178 Brian Le Roux [CSS Slap Chop](https://github.com/brianleroux/css-slap-chop) and there were a number of\r
179 client-side solutions also crafted, such as [Helium-CSS](https://github.com/geuis/helium-css),\r
180 [CSSESS](https://github.com/driverdan/cssess) and the Python [mincss](https://www.peterbe.com/plog/mincss).\r
181 \r
182 Unfortunately, most of these solutions don't actually generate what you're really after - a leaner build\r
183 of your project CSS containing only those rules you used. Finding that a more recent project called\r
184 [UnCSS](https://github.com/giakki/uncss) did try tackling this, I set out to share some of the problems we\r
185 need to solve in this space with the developer and build a Grunt task to enable usage of it in builds more\r
186 easily.\r
187 \r
188 Huge thanks go out to Giacomo Martino for his help with the Node module this task uses.\r
189 \r
190 ## Coverage\r
191 \r
192 * [Spring-cleaning unused CSS with Grunt, Gulp & other build systems](https://addyosmani.com/blog/removing-unused-css/)\r
193 * [Automating the removal of unused CSS - VelocityConf](https://www.youtube.com/watch?v=833xr1MyE30)\r
194 * [Use Grunt and UnCSS to speed up the load time of your site](http://xdamman.com/website-optimization-grunt-uncss)\r
195 * [Foundation 5, Sass and Grunt UnCSS](https://corydowdy.com/blog/foundation-5-sass-and-grunt-uncss)\r
196 * [Automating Front-end Workflow (slides)](https://speakerdeck.com/addyosmani/automating-front-end-workflow)\r
197 * [Automatically removing unused CSS - Windows](http://deanhume.com/Home/BlogPost/automatically-removing-unused-css-using-grunt/6101)\r
198 * [Workflow for responsive email with Grunt and UnCSS](https://medium.com/p/32d607879082)\r
199 \r
200 ## WordPress\r
201 \r
202 While UnCSS works best (and quickest) with static HTML files, it is possible to pass in\r
203 a URL array that contains all the URLs on your website, and process all used selectors that way.\r
204 [@lgladdy](https://github.com/lgladdy) wrote a guide on how to do this\r
205 [on his blog](https://gladdy.uk/blog/2014/04/13/using-uncss-and-grunt-uncss-with-wordpress/)\r
206 \r
207 ## Yeoman Generator\r
208 \r
209 If you're looking for a webapp starting point with grunt-uncss integrated, see [generator-webapp-uncss](https://github.com/addyosmani/generator-webapp-uncss).\r
210 \r
211 ## Limitations\r
212 \r
213 Please note that the CSS parser used in the `uncss` module we rely on currently isn't able to work with complex selectors.\r
214 For example `[data-section=''] > section > [data-section-title] a`. This may mean that at build time you run into exceptions\r
215 such as `TypeError: Cannot convert undefined or null to object`. If this is the case, please consider moving these selectors\r
216 to a separate stylesheet which the task does not run on.\r
217 \r
218 We are actively looking at how to improve the CSS parsers used and will update this notice once this problem has been solved.\r
219 \r
220 ## Maintainers\r
221 \r
222 * [@addyosmani](https://github.com/addyosmani)\r
223 * [@XhmikosR](https://github.com/XhmikosR)\r
224 \r
225 ## License\r
226 \r
227 (C) Addy Osmani 2016, released under the MIT license\r