Clean-CSS 基于 Node.js 的 CSS 代码压缩工具
Clean-CSS 是一个非常简单的 CSS 压缩程序,基于 Node.js 环境开发使用,可以删除 CSS 中一些无用的空格、评论以及每个选择器后面的分号,还可以压缩通过 @import 引入的 CSS 文件。
环境依赖
Node.js 4.0+ (tested on CentOS, Ubuntu, OS X, and Windows)
通过 Npm 安装
npm install clean-css
如何使用命令行
Clean-CSS 接受下列命令行参数,使用时请将需要优化的 CSS 文件<source-file>放置到最后一个参数后面,避免遇到潜在的不可预想的问题。
cleancss [options] source-file, [source-file, ...] -h, --help output usage information -v, --version output the version number -b, --keep-line-breaks Keep line breaks -c, --compatibility [ie7|ie8] Force compatibility mode (see Readme for advanced examples) -d, --debug Shows debug information (minification time & compression efficiency) -o, --output [output-file] Use [output-file] as output instead of STDOUT -r, --root [root-path] Set a root path to which resolve absolute @import rules -s, --skip-import Disable @import processing -t, --timeout [seconds] Per connection timeout when fetching remote @imports (defaults to 5 seconds) --rounding-precision [n] Rounds to `N` decimal places. Defaults to 2. -1 disables rounding --s0 Remove all special comments, i.e. /*! comment */ --s1 Remove all special comments but the first one --semantic-merging Enables unsafe mode by assuming BEM-like semantic stylesheets (warning, this may break your styling!) --skip-advanced Disable advanced optimizations - ruleset reordering & merging --skip-aggressive-merging Disable properties merging based on their order --skip-import-from [rules] Disable @import processing for specified rules --skip-media-merging Disable @media merging --skip-rebase Disable URLs rebasing --skip-restructuring Disable restructuring optimizations --skip-shorthand-compacting Disable shorthand compacting --source-map Enables building input's source map --source-map-inline-sources Enables inlining sources inside source maps
使用举例
To minify a public.css file into public-min.css do:
cleancss -o public-min.css public.cssTo minify the same public.css into the standard output skip the -o parameter:
cleancss public.cssMore likely you would like to concatenate a couple of files. If you are on a Unix-like system:
cat one.css two.css three.css | cleancss -o merged-and-minified.css
On Windows:
type one.css two.css three.css | cleancss -o merged-and-minified.css
Or even gzip the result at once:
cat one.css two.css three.css | cleancss | gzip -9 -c > merged-minified-and-gzipped.css.gz
相关链接
Github 地址: https://github.com/jakubpawlowicz/clean-css
基于的在线服务:
如何使用 Clean-CSS API?
var CleanCSS = require('clean-css');
var source = 'a{font-weight:bold;}';
var minified = new CleanCSS().minify(source).styles;CleanCSS constructor accepts a hash as a parameter, i.e., new CleanCSS(options) with the following options available:
advanced- set to false to disable advanced optimizations - selector & property merging, reduction, etc.aggressiveMerging- set to false to disable aggressive merging of properties.benchmark- turns on benchmarking mode measuring time spent on cleaning up (runnpm run benchto see example)compatibility- enables compatibility mode, see below for more examplesdebug- set to true to get minification statistics understatsproperty (seetest/custom-test.jsfor examples)inliner- a hash of options for@importinliner, see test/protocol-imports-test.js for examples, or this comment for a proxy use case.keepBreaks- whether to keep line breaks (default is false)keepSpecialComments-*for keeping all (default),1for keeping first one only,0for removing allmediaMerging- whether to merge@mediaat-rules (default is true)processImport- whether to process@importrulesprocessImportFrom- a list of@importrules, can be['all'](default),['local'],['remote'], or a blacklisted path e.g.['!fonts.googleapis.com']rebase- set to false to skip URL rebasingrelativeTo- path to resolve relative@importrules and URLsrestructuring- set to false to disable restructuring in advanced optimizationsroot- path to resolve absolute@importrules and rebase relative URLsroundingPrecision- rounding precision; defaults to2;-1disables roundingsemanticMerging- set to true to enable semantic merging mode which assumes BEM-like content (default is false as it's highly likely this will break your stylesheets - use with caution !)shorthandCompacting- set to false to skip shorthand compacting (default is true unless sourceMap is set when it's false)sourceMap- exposes source map undersourceMapproperty, e.g.new CleanCSS().minify(source).sourceMap(default is false) If input styles are a product of CSS preprocessor (Less, Sass) an input source map can be passed as a string.sourceMapInlineSources- set to true to inline sources inside a source map'ssourcesContentfield (defaults to false) It is also required to process inlined sources from input source maps.target- path to a folder or an output file to which rebase all URLs
The output of minify method (or the 2nd argument to passed callback) is a hash containing the following fields:
styles- optimized output CSS as a stringsourceMap- output source map (if requested withsourceMapoption)errors- a list of errors raisedwarnings- a list of warnings raisedstats- a hash of statistic information (if requested withdebugoption):originalSize- original content size (after import inlining)minifiedSize- optimized content sizetimeSpent- time spent on optimizationsefficiency- a ratio of output size to input size (e.g. 25% if content was reduced from 100 bytes to 75 bytes)
如何处理通过 @import s 引入的文件?
In order to inline remote @import statements you need to provide a callback to minify method, e.g.:
var CleanCSS = require('clean-css');
var source = '@import url(http://path/to/remote/styles);';
new CleanCSS().minify(source, function (error, minified) {
// minified.styles
});This is due to a fact, that, while local files can be read synchronously, remote resources can only be processed asynchronously. If you don't provide a callback, then remote @import s will be left intact.
如何保留注释块?
Use the /*! notation instead of the standard one /* :
/*!
Important comments included in minified output.
*/如何改变相关的图像的 URL?
Clean-css will handle it automatically for you (since version 1.1) in the following cases:
- When using the CLI:
- Use an output path via
-o/--outputto rebase URLs as relative to the output file. - Use a root path via
-r/--rootto rebase URLs as absolute from the given root path. - If you specify both then
-r/--roottakes precedence.
- Use an output path via
- When using clean-css as a library:
- Use a combination of
relativeToandtargetoptions for relative rebase (same as 1 in CLI). - Use a combination of
relativeToandrootoptions for absolute rebase (same as 2 in CLI). roottakes precedence overtargetas in CLI.
- Use a combination of
如何生成 source maps?
Source maps are supported since version 3.0. Additionally to mapping original CSS files, clean-css also supports input source maps, so minified styles can be mapped into their Less or Sass sources directly. Source maps are generated using source-map module from Mozilla.
使用命令行
To generate a source map, use --source-map switch, e.g.:
cleancss --source-map --output styles.min.css styles.cssName of the output file is required, so a map file, named by adding .map suffix to output file name, can be created (styles.min.css.map in this case).
使用 API
To generate a source map, use sourceMap: true option, e.g.:
new CleanCSS({ sourceMap: true, target: pathToOutputDirectory })
.minify(source, function (error, minified) {
// access minified.sourceMap for SourceMapGenerator object
// see https://github.com/mozilla/source-map/#sourcemapgenerator for more details
// see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L114 on how it's used in clean-css' CLI
});Using API you can also pass an input source map directly:
new CleanCSS({ sourceMap: inputSourceMapAsString, target: pathToOutputDirectory })
.minify(source, function (error, minified) {
// access minified.sourceMap to access SourceMapGenerator object
// see https://github.com/mozilla/source-map/#sourcemapgenerator for more details
// see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L114 on how it's used in clean-css' CLI
});Or even multiple input source maps at once (available since version 3.1):
new CleanCSS({ sourceMap: true, target: pathToOutputDirectory }).minify({
'path/to/source/1': {
styles: '...styles...',
sourceMap: '...source-map...'
},
'path/to/source/2': {
styles: '...styles...',
sourceMap: '...source-map...'
}
}, function (error, minified) {
// access minified.sourceMap as above
});如何通过 API 压缩多个文件?
通过传递数据
new CleanCSS().minify(['path/to/file/one', 'path/to/file/two']);通过传递 Hash
new CleanCSS().minify({
'path/to/file/one': {
styles: 'contents of file one'
},
'path/to/file/two': {
styles: 'contents of file two'
}
});如何设置兼容模式?
Compatibility settings are controlled by --compatibility switch (CLI) and compatibility option (library mode). In both modes the following values are allowed:
'ie7'- Internet Explorer 7 compatibility mode'ie8'- Internet Explorer 8 compatibility mode''or'*'(default) - Internet Explorer 9+ compatibility mode
Since clean-css 3 a fine grained control is available over those settings , with the following options available:
'[+-]colors.opacity'- - turn on (+) / off (-)rgba()/hsla()declarations removal'[+-]properties.backgroundClipMerging'- turn on / off background-clip merging into shorthand'[+-]properties.backgroundOriginMerging'- turn on / off background-origin merging into shorthand'[+-]properties.backgroundSizeMerging'- turn on / off background-size merging into shorthand'[+-]properties.colors'- turn on / off any color optimizations'[+-]properties.ieBangHack'- turn on / off IE bang hack removal'[+-]properties.iePrefixHack'- turn on / off IE prefix hack removal'[+-]properties.ieSuffixHack'- turn on / off IE suffix hack removal'[+-]properties.merging'- turn on / off property merging based on understandability'[+-]properties.shorterLengthUnits'- turn on / off shortening pixel units intopc,pt, orinunits'[+-]properties.spaceAfterClosingBrace'- turn on / off removing space after closing brace -url() no-repeatintourl()no-repeat'[+-]properties.urlQuotes'- turn on / offurl()quoting'[+-]properties.zeroUnits'- turn on / off units removal after a0value'[+-]selectors.adjacentSpace'- turn on / off extra space beforenavelement'[+-]selectors.ie7Hack'- turn on / off IE7 selector hack removal (*+html...)'[+-]selectors.special'- a regular expression with all special, unmergeable selectors (leave it empty unless you know what you are doing)'[+-]units.ch'- turn on / off treatingchas a proper unit'[+-]units.in'- turn on / off treatinginas a proper unit'[+-]units.pc'- turn on / off treatingpcas a proper unit'[+-]units.pt'- turn on / off treatingptas a proper unit'[+-]units.rem'- turn on / off treatingremas a proper unit'[+-]units.vh'- turn on / off treatingvhas a proper unit'[+-]units.vm'- turn on / off treatingvmas a proper unit'[+-]units.vmax'- turn on / off treatingvmaxas a proper unit'[+-]units.vmin'- turn on / off treatingvminas a proper unit
For example, using --compatibility 'ie8,+units.rem' will ensure IE8 compatibility while enabling rem units so the following style margin:0px 0rem can be shortened to margin:0 , while in pure IE8 mode it can't be. To pass a single off (-) switch in CLI please use the following syntax --compatibility *,-units.rem . In library mode you can also pass compatibility as a hash of options.
什么是高级压缩模式?
All advanced optimizations are dispatched here , and this is what they do:
recursivelyOptimizeBlocks- does all the following operations on a block (think@mediaor@keyframeat-rules);recursivelyOptimizeProperties- optimizes properties in rulesets and "flat at-rules" (like @font-face) by splitting them into components (e.g.marginintomargin-(*)), optimizing, and rebuilding them back. You may want to useshorthandCompactingoption to control whether you want to turn multiple (long-hand) properties into a shorthand ones;removeDuplicates- gets rid of duplicate rulesets with exactly the same set of properties (think of including the same Sass / Less partial twice for no good reason);mergeAdjacent- merges adjacent rulesets with the same selector or rules;reduceNonAdjacent- identifies which properties are overridden in same-selector non-adjacent rulesets, and removes them;mergeNonAdjacentBySelector- identifies same-selector non-adjacent rulesets which can be moved (!) to be merged, requires all intermediate rulesets to not redefine the moved properties, or if redefined to be either more coarse grained (e.g.marginvsmargin-top) or have the same value;mergeNonAdjacentByBody- same as the one above but for same-rules non-adjacent rulesets;restructure- tries to reorganize different-selector different-rules rulesets so they take less space, e.g..one{padding:0}.two{margin:0}.one{margin-bottom:3px}into.two{margin:0}.one{padding:0;margin-bottom:3px};removeDuplicateMediaQueries- removes duplicated@mediaat-rules;mergeMediaQueries- merges non-adjacent@mediaat-rules by same rules asmergeNonAdjacentBy*above;





