|
1 |
| -# clean-css-cli |
2 |
| -The command line interface to clean-css CSS optimizer |
| 1 | +[](https://www.npmjs.com/package/clean-css) |
| 2 | +[](https://travis-ci.org/jakubpawlowicz/clean-css) |
| 3 | +[](https://david-dm.org/jakubpawlowicz/clean-css) |
| 4 | +[](https://www.npmjs.com/package/clean-css) |
| 5 | +[](https://twitter.com/cleancss) |
| 6 | + |
| 7 | +clean-css-cli is a command-line interface to [clean-css](https://github.com/jakubpawlowicz/clean-css) - fast and efficient CSS optimizer for [Node.js](http://nodejs.org/). |
| 8 | + |
| 9 | +Previously a part of clean-css it's a separate package since clean-css 4.0. |
| 10 | + |
| 11 | +**Table of Contents** |
| 12 | + |
| 13 | +- [Node.js version support](#nodejs-version-support) |
| 14 | +- [Install](#install) |
| 15 | +- [Use](#use) |
| 16 | + * [Important: 4.0 breaking changes](#important-40-breaking-changes) |
| 17 | + * [CLI options](#cli-options) |
| 18 | + * [Compatibility modes](#compatibility-modes) |
| 19 | + * [Formatting options](#formatting-options) |
| 20 | + * [Inlining options](#inlining-options) |
| 21 | + * [Optimization levels](#optimization-levels) |
| 22 | + + [Level 0 optimizations](#level-0-optimizations) |
| 23 | + + [Level 1 optimizations](#level-1-optimizations) |
| 24 | + + [Level 2 optimizations](#level-2-optimizations) |
| 25 | +- [FAQ](#faq) |
| 26 | + * [How to optimize multiple files?](#how-to-optimize-multiple-files) |
| 27 | + * [How to specify a custom rounding precision?](#how-to-specify-a-custom-rounding-precision) |
| 28 | + * [How to rebase relative image URLs?](#how-to-rebase-relative-image-urls) |
| 29 | + * [How to apply level 1 & 2 optimizations at the same time?](#how-to-apply-level-1--2-optimizations-at-the-same-time) |
| 30 | +- [Contributing](#contributing) |
| 31 | + * [How to get started?](#how-to-get-started) |
| 32 | +- [License](#license) |
| 33 | + |
| 34 | +# Node.js version support |
| 35 | + |
| 36 | +clean-css-cli requires Node.js 4.0+ (tested on Linux, OS X, and Windows) |
| 37 | + |
| 38 | +# Install |
| 39 | + |
| 40 | +```shell |
| 41 | +npm install clean-css-cli |
| 42 | +``` |
| 43 | + |
| 44 | +# Use |
| 45 | + |
| 46 | +```shell |
| 47 | +cleancss -o one.min.css one.css |
| 48 | +``` |
| 49 | + |
| 50 | +## Important: 4.0 breaking changes |
| 51 | + |
| 52 | +clean-css-cli 4.0 introduces some breaking changes: |
| 53 | + |
| 54 | +* API and CLI interfaces are split, so CLI has been moved to this repository while API stays at [clean-css](https://github.com/jakubpawlowicz/clean-css); |
| 55 | +* `--root` and `--relativeTo` options are replaced by a single option taken from `--output` path - this means that rebasing URLs and import inlining is much simpler but may not be (YMMV) as powerful as in 3.x; |
| 56 | +* `--rounding-precision` is disabled by default; |
| 57 | +* `--rounding-precision` applies to **all** units now, not only `px` as in 3.x; |
| 58 | +* `--skip-import` and `--skip-import-from` are merged into `--inline` option which defaults to `local`. Remote `@import` rules are **NOT** inlined by default anymore; |
| 59 | +* renames `--timeout` option to `--inline-timeout`; |
| 60 | +* remote resources without a protocol, e.g. `//fonts.googleapis.com/css?family=Domine:700`, are not inlined anymore; |
| 61 | +* changes default Internet Explorer compatibility from 9+ to 10+, to revert the old default use `--compatibility ie9` option; |
| 62 | +* moves `--rounding-precision`, `--s0`, and `--s1` options to level 1 optimization options, see examples; |
| 63 | +* moves `--skip-media-merging`, `--skip-restructuring`, `--semantic-merging`, and `--skip-shorthand-compacting` to level 2 optimizations options, see examples below; |
| 64 | +* level 1 optimizations are the new default, up to 3.x it was level 2; |
| 65 | +* `--keep-breaks` option is replaced with `--format keep-breaks` to ease transition; |
| 66 | +* `--skip-aggressive-merging` option is removed as aggressive merging is replaced by smarter override merging. |
| 67 | + |
| 68 | +## CLI options |
| 69 | + |
| 70 | +```shell |
| 71 | +-h, --help output usage information |
| 72 | +-v, --version output the version number |
| 73 | +-c, --compatibility [ie7|ie8] Force compatibility mode (see Readme for advanced examples) |
| 74 | +-d, --debug Shows debug information (minification time & compression efficiency) |
| 75 | +-f, --format <options> Controls output formatting, see examples below |
| 76 | +-o, --output [output-file] Use [output-file] as output instead of STDOUT |
| 77 | +-O <n> [optimizations] Turn on level <n> optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below |
| 78 | +--inline [rules] Enables inlining for listed sources (defaults to `local`) |
| 79 | +--inline-timeout [seconds] Per connection timeout when fetching remote stylesheets (defaults to 5 seconds) |
| 80 | +--skip-rebase Disable URLs rebasing |
| 81 | +--source-map Enables building input's source map |
| 82 | +--source-map-inline-sources Enables inlining sources inside source maps |
| 83 | +``` |
| 84 | +
|
| 85 | +## Compatibility modes |
| 86 | +
|
| 87 | +There is a certain number of compatibility mode shortcuts, namely: |
| 88 | +
|
| 89 | +* `--compatibility '*'` (default) - Internet Explorer 10+ compatibility mode |
| 90 | +* `--compatibility ie9` - Internet Explorer 9+ compatibility mode |
| 91 | +* `--compatibility ie8` - Internet Explorer 8+ compatibility mode |
| 92 | +* `--compatibility ie7` - Internet Explorer 7+ compatibility mode |
| 93 | +
|
| 94 | +Each of these modes is an alias to a [fine grained configuration](https://github.com/jakubpawlowicz/clean-css/blob/master/lib/options/compatibility.js), with the following options available: |
| 95 | +
|
| 96 | +```shell |
| 97 | +cleancss --compatibility '*,-properties.urlQuotes' |
| 98 | +cleancss --compatibility '*,+properties.ieBangHack,+properties.ieFilters' |
| 99 | +# [+-]colors.opacity controls `rgba()` / `hsla()` color support; defaults to `on` (+) |
| 100 | +# [+-]properties.backgroundClipMerging controls background-clip merging into shorthand; defaults to `on` (+) |
| 101 | +# [+-]properties.backgroundOriginMerging controls background-origin merging into shorthand; defaults to `on` (+) |
| 102 | +# [+-]properties.backgroundSizeMerging controls background-size merging into shorthand; defaults to `on` (+) |
| 103 | +# [+-]properties.colors controls color optimizations; defaults to `on` (+) |
| 104 | +# [+-]properties.ieBangHack controls keeping IE bang hack; defaults to `off` (-) |
| 105 | +# [+-]properties.ieFilters controls keeping IE `filter` / `-ms-filter`; defaults to `off` (-) |
| 106 | +# [+-]properties.iePrefixHack controls keeping IE prefix hack; defaults to `off` (-) |
| 107 | +# [+-]properties.ieSuffixHack controls keeping IE suffix hack; defaults to `off` (-) |
| 108 | +# [+-]properties.merging controls property merging based on understandability; defaults to `on` (+) |
| 109 | +# [+-]properties.shorterLengthUnits controls shortening pixel units into `pc`, `pt`, or `in` units; defaults to `off` (-) |
| 110 | +# [+-]properties.spaceAfterClosingBrace controls keeping space after closing brace - `url() no-repeat` cleancss --compatibility '*,into `url('roperties.no-repeat`; defaults to `on` (+) |
| 111 | +# [+-]properties.urlQuotes controls keeping quoting inside `url()`; defaults to `off` (-) |
| 112 | +# [+-]properties.zeroUnitsf units `0` value; defaults to `on` (+) |
| 113 | +# [+-]selectors.adjacentSpace controls extra space before `nav` element; defaults to `off` (-) |
| 114 | +# [+-]selectors.ie7Hack controls removal of IE7 selector hacks, e.g. `*+html...`; defaults to `on` (+) |
| 115 | +# [+-]units.ch controls treating `ch` as a supported unit; defaults to `on` (+) |
| 116 | +# [+-]units.in controls treating `in` as a supported unit; defaults to `on` (+) |
| 117 | +# [+-]units.pc controls treating `pc` as a supported unit; defaults to `on` (+) |
| 118 | +# [+-]units.pt controls treating `pt` as a supported unit; defaults to `on` (+) |
| 119 | +# [+-]units.rem controls treating `rem` as a supported unit; defaults to `on` (+) |
| 120 | +# [+-]units.vh controls treating `vh` as a supported unit; defaults to `on` (+) |
| 121 | +# [+-]units.vm controls treating `vm` as a supported unit; defaults to `on` (+) |
| 122 | +# [+-]units.vmax controls treating `vmax` as a supported unit; defaults to `on` (+) |
| 123 | +# [+-]units.vmin controls treating `vmin` as a supported unit; defaults to `on` (+) |
| 124 | +``` |
| 125 | +
|
| 126 | +You can also chain more rules after a shortcut when setting a compatibility: |
| 127 | +
|
| 128 | +```shell |
| 129 | +cleancss --compatibility 'ie9,-colors.opacity,-units.rem' one.css |
| 130 | +``` |
| 131 | +
|
| 132 | +## Formatting options |
| 133 | +
|
| 134 | +The `--format` option accept the following options: |
| 135 | +
|
| 136 | +```shell |
| 137 | +cleancss --format beautify one.css |
| 138 | +cleancss --format keep-breaks one.css |
| 139 | +cleancss --format 'indentBy:1;indentWith:tab' one.css |
| 140 | +cleancss --format 'breaks:afterBlockBegins=on;spaces:aroundSelectorRelation=on' one.css |
| 141 | +# `breaks` controls where to insert breaks |
| 142 | +# `afterAtRule` controls if a line break comes after an at-rule; e.g. `@charset`; defaults to `off` (alias to `false`) |
| 143 | +# `afterBlockBegins` controls if a line break comes after a block begins; e.g. `@media`; defaults to `off` |
| 144 | +# `afterBlockEnds` controls if a line break comes after a block ends, defaults to `off` |
| 145 | +# `afterComment` controls if a line break comes after a comment; defaults to `off` |
| 146 | +# `afterProperty` controls if a line break comes after a property; defaults to `off` |
| 147 | +# `afterRuleBegins` controls if a line break comes after a rule begins; defaults to `off` |
| 148 | +# `afterRuleEnds` controls if a line break comes after a rule ends; defaults to `off` |
| 149 | +# `beforeBlockEnds` controls if a line break comes before a block ends; defaults to `off` |
| 150 | +# `betweenSelectors` controls if a line break comes between selectors; defaults to `off` |
| 151 | +# `indentBy` controls number of characters to indent with; defaults to `0` |
| 152 | +# `indentWith` controls a character to indent with, can be `space` or `tab`; defaults to `space` |
| 153 | +# `spaces` controls where to insert spaces |
| 154 | +# `aroundSelectorRelation` controls if spaces come around selector relations; e.g. `div > a`; defaults to `off` |
| 155 | +# `beforeBlockBegins` controls if a space comes before a block begins; e.g. `.block {`; defaults to `off` |
| 156 | +# `beforeValue` controls if a space comes before a value; e.g. `width: 1rem`; defaults to `off` |
| 157 | +# `wrapAt` controls maximum line length; defaults to `off` |
| 158 | +``` |
| 159 | +
|
| 160 | +## Inlining options |
| 161 | +
|
| 162 | +`--inline` option whitelists which `@import` rules will be processed, e.g. |
| 163 | +
|
| 164 | +```shell |
| 165 | +cleancss --inline local one.css # default |
| 166 | +``` |
| 167 | +
|
| 168 | +```shell |
| 169 | +cleancss --inline all # same as local,remote |
| 170 | +``` |
| 171 | +
|
| 172 | +```shell |
| 173 | +cleancss --inline local,mydomain.example.com one.css |
| 174 | +``` |
| 175 | +
|
| 176 | +```shell |
| 177 | +cleancss --inline 'local,remote,!fonts.googleapis.com' one.css |
| 178 | +``` |
| 179 | +
|
| 180 | +## Optimization levels |
| 181 | +
|
| 182 | +The `--level` option can be either `0`, `1` (default), or `2`, e.g. |
| 183 | +
|
| 184 | +```shell |
| 185 | +cleancss --level 2 one.css |
| 186 | +``` |
| 187 | +
|
| 188 | +or a fine-grained configuration given via a string. |
| 189 | +
|
| 190 | +Please note that level 1 optimization options are generally safe while level 2 optimizations should be safe for most users. |
| 191 | +
|
| 192 | +### Level 0 optimizations |
| 193 | +
|
| 194 | +Level 0 optimizations simply means "no optimizations". Use it when you'd like to inline imports and / or rebase URLs but skip everything else, e.g. |
| 195 | +
|
| 196 | +```shell |
| 197 | +cleancss -O0 one.css |
| 198 | +``` |
| 199 | +
|
| 200 | +### Level 1 optimizations |
| 201 | +
|
| 202 | +Level 1 optimizations (default) operate on single properties only, e.g. can remove units when not required, turn rgb colors to a shorter hex representation, remove comments, etc |
| 203 | +
|
| 204 | +Here is a full list of available options: |
| 205 | +
|
| 206 | +```shell |
| 207 | +cleancss -O1 one.css |
| 208 | +cleancss -O1 removeQuotes:off;roundingPrecision:4;specialComments:1 one.css |
| 209 | +# `cleanupCharsets` controls `@charset` moving to the front of a stylesheet; defaults to `on` |
| 210 | +# `normalizeUrls` controls URL normalzation; default to `on` |
| 211 | +# `optimizeBackground` controls `background` property optimizatons; defaults to `on` |
| 212 | +# `optimizeBorderRadius` controls `border-radius` property optimizatons; defaults to `on` |
| 213 | +# `optimizeFilter` controls `filter` property optimizatons; defaults to `on` |
| 214 | +# `optimizeFont controls `font` property optimizatons; defaults to `on` |
| 215 | +# `optimizeFontWeight` controls `font-weight` property optimizatons; defaults to `on` |
| 216 | +# `optimizeOutline` controls `outline` property optimizatons; defaults to `on` |
| 217 | +# `removeNegativePaddings` controls removing negative paddings; defaults to `on` |
| 218 | +# `removeQuotes` controls removing quotes when unnecessary; defaults to `on` |
| 219 | +# `removeWhitespace` controls removing unused whitespace; defaults to `on` |
| 220 | +# `replaceMultipleZeros` contols removing redundant zeros; defaults to `on` |
| 221 | +# `replaceTimeUnits` controls replacing time units with shorter values; defaults to `on |
| 222 | +# `replaceZeroUnits` controls replacing zero values with units; defaults to `on` |
| 223 | +# `roundingPrecision` rounds pixel values to `N` decimal places; `off` disables rounding; defaults to `off` |
| 224 | +# `selectorsSortingMethod` denotes selector sorting method; can be `natural` or `standard`; defaults to `standard` |
| 225 | +# `specialComments` denotes a number of /*! ... */ comments preserved; defaults to `all` |
| 226 | +# `tidyAtRules` controls at-rules (e.g. `@charset`, `@import`) optimizing; defaults to `on` |
| 227 | +# `tidyBlockScopes` controls block scopes (e.g. `@media`) optimizing; defaults to `on` |
| 228 | +# `tidySelectors` controls selectors optimizing; defaults to `on` |
| 229 | +``` |
| 230 | +
|
| 231 | +There is an `all` shortcut for toggling all options at the same time, e.g. |
| 232 | +
|
| 233 | +```shell |
| 234 | +cleancss -O1 all:off;tidySelectors:on one.css |
| 235 | +``` |
| 236 | +
|
| 237 | +### Level 2 optimizations |
| 238 | +
|
| 239 | +Level 2 optimizations operate at rules or multiple properties level, e.g. can remove duplicate rules, remove properties redefined further down a stylesheet, or restructure rules by moving them around. |
| 240 | +
|
| 241 | +Please note that if level 2 optimizations are turned on then, unless explicitely disabled, level 1 optimizations are applied as well. |
| 242 | +
|
| 243 | +Here is a full list of available options: |
| 244 | +
|
| 245 | +```shell |
| 246 | +cleancss -O2 one.css |
| 247 | +cleancss -O2 mergeMedia:off;restructureRules:off;mergeSemantically:on;mergeIntoShorthands:off one.css |
| 248 | +# `mergeAdjacentRules` controls adjacent rules merging; defaults to `on` |
| 249 | +# `mergeIntoShorthands` controls merging properties into shorthands; defaults to `on` |
| 250 | +# `mergeMedia` controls `@media` merging; defaults to `on` |
| 251 | +# `mergeNonAdjacentRules` controls non-adjacent rule merging; defaults to `on` |
| 252 | +# `mergeSemantically` controls semantic merging; defaults to `off` |
| 253 | +# `overrideProperties` controls property overriding based on understandability; defaults to `on` |
| 254 | +# `reduceNonAdjacentRules` controls non-adjacent rule reducing; defaults to `on` |
| 255 | +# `removeDuplicateFontRules` controls duplicate `@font-face` removing; defaults to `on` |
| 256 | +# `removeDuplicateMediaBlocks` controls duplicate `@media` removing; defaults to `on` |
| 257 | +# `removeDuplicateRules` controls duplicate rules removing; defaults to `on` |
| 258 | +# `restructureRules` controls rule restructuring; defaults to `off` |
| 259 | +``` |
| 260 | +
|
| 261 | +There is an `all` shortcut for toggling all options at the same time, e.g. |
| 262 | +
|
| 263 | +```shell |
| 264 | +cleancss -O2 all:off;removeDuplicateRules:on one.css |
| 265 | +``` |
| 266 | +
|
| 267 | +# FAQ |
| 268 | +
|
| 269 | +More answers can be found in [clean-css FAQ section](https://github.com/jakubpawlowicz/clean-css#faq). |
| 270 | +
|
| 271 | +## How to optimize multiple files? |
| 272 | +
|
| 273 | +It can be done by passing in paths to multiple files, e.g. |
| 274 | +
|
| 275 | +```shell |
| 276 | +cleancss -o merged.min.css one.css two.css three.css |
| 277 | +``` |
| 278 | +
|
| 279 | +## How to specify a custom rounding precision? |
| 280 | +
|
| 281 | +The level 1 `roundingPrecision` optimization option accept a string with per-unit rounding precision settings, e.g. |
| 282 | +
|
| 283 | +```shell |
| 284 | +cleancss -O1 roundingPrecision:all=3,px=5 |
| 285 | +``` |
| 286 | +
|
| 287 | +which sets all units rounding precision to 3 digits except `px` unit precision of 5 digits. |
| 288 | +
|
| 289 | +## How to rebase relative image URLs? |
| 290 | +
|
| 291 | +clean-css-cli will handle it automatically for you when full paths to input files are passed in and `--output` option is used, e.g |
| 292 | +
|
| 293 | +```css |
| 294 | +/*! one.css */ |
| 295 | +a { |
| 296 | + background:url(image.png) |
| 297 | +} |
| 298 | +``` |
| 299 | +
|
| 300 | +```shell |
| 301 | +cleancss -o build/one.min.css one.css |
| 302 | +``` |
| 303 | +
|
| 304 | +```css |
| 305 | +/*! build/one.min.css */ |
| 306 | +a{background:url(../image.png)} |
| 307 | +``` |
| 308 | +
|
| 309 | +## How to apply level 1 & 2 optimizations at the same time? |
| 310 | +
|
| 311 | +Using `-O` option twice and specifying optimization options in each, e.g. |
| 312 | +
|
| 313 | +```shell |
| 314 | +cleancss -O1 all:on,normalizeUrls:off -O2 restructureRules:on one.css |
| 315 | +``` |
| 316 | +
|
| 317 | +will apply level 1 optimizations, except url normalization, and default level 2 optimizations with rule restructuring. |
| 318 | +
|
| 319 | +# Contributing |
| 320 | +
|
| 321 | +See [CONTRIBUTING.md](https://github.com/jakubpawlowicz/clean-css-cli/blob/master/CONTRIBUTING.md). |
| 322 | +
|
| 323 | +## How to get started? |
| 324 | +
|
| 325 | +First clone the sources: |
| 326 | +
|
| 327 | +```shell |
| 328 | +git clone git@github.com:jakubpawlowicz/clean-css-cli.git |
| 329 | +``` |
| 330 | +
|
| 331 | +then install dependencies: |
| 332 | +
|
| 333 | +```shell |
| 334 | +cd clean-css-cli |
| 335 | +npm install |
| 336 | +``` |
| 337 | +
|
| 338 | +then use any of the following commands to verify your copy: |
| 339 | +
|
| 340 | +```shell |
| 341 | +npm run check # to lint JS sources with [JSHint](https://github.com/jshint/jshint/) |
| 342 | +npm test # to run all tests |
| 343 | +``` |
| 344 | +
|
| 345 | +# License |
| 346 | +
|
| 347 | +clean-css-cli is released under the [MIT License](https://github.com/jakubpawlowicz/clean-css-cli/blob/master/LICENSE). |
0 commit comments