原文链接:http://www.ydcss.com/archives/41
使用gulp-minify-css压缩css文件,减小文件大小,并给引用url添加版本号避免缓存。重要:gulp-minify-css已经被废弃,请使用gulp-clean-css,用法一致。
1.1、gulp基本使用还未掌握?请参看: gulp详细入门教程
1.2、本示例目录结构如下:
2.1、github:https://github.com/jonathanepollack/gulp-minify-css
2.2、安装:命令提示符执行 cnpm install gulp-minify-css --save-dev
2.3、注意:没有安装cnpm请使用 npm install gulp-minify-css --save-dev
什么是cnpm,如何安装?
2.4、说明:--save-dev
保存配置信息至 package.json 的 devDependencies 节点。为什么要保存至package.json?
var gulp = require('gulp'), cssmin = require('gulp-minify-css'); gulp.task('testCssmin', function () { gulp.src('src/css/*.css') .pipe(cssmin()) .pipe(gulp.dest('dist/css')); });3.2、gulp-minify-css 最终是调用clean-css,其他参数 查看这里
var gulp = require('gulp'),
cssmin = require('gulp-minify-css');
gulp.task('testCssmin', function () {
gulp.src('src/css/*.css')
.pipe(cssmin({
advanced: false,//类型:Boolean 默认:true [是否开启高级优化(合并选择器等)]
compatibility: 'ie7',//保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式]
keepBreaks: true,//类型:Boolean 默认:false [是否保留换行]
keepSpecialComments: '*'
//保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀
}))
.pipe(gulp.dest('dist/css'));
});
3.3、给css文件里引用url加版本号(根据引用文件的md5生产版本号),像这样:
var gulp = require('gulp'),
cssmin = require('gulp-minify-css');
//确保已本地安装gulp-make-css-url-version [cnpm install gulp-make-css-url-version --save-dev]
cssver = require('gulp-make-css-url-version');
gulp.task('testCssmin', function () {
gulp.src('src/css/*.css')
.pipe(cssver()) //给css文件里引用文件加版本号(文件MD5)
.pipe(cssmin())
.pipe(gulp.dest('dist/css'));
});
3.3、若想保留注释,这样注释即可:
/*! Important comments included in minified output. */
4.1、命令提示符执行:gulp testCssmin
5.1、本文有任何错误,或有任何疑问,欢迎留言说明。
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 (run npm run bench
to see example)compatibility
- enables compatibility mode, see below for more examplesdebug
- set to true to get minification statistics under stats
property (see test/custom-test.js
for examples)inliner
- a hash of options for @import
inliner, 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), 1
for keeping first one only, 0
for removing allmediaMerging
- whether to merge @media
at-rules (default is true)processImport
- whether to process @import
rulesprocessImportFrom
- a list of @import
rules, 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 @import
rules and URLsrestructuring
- set to false to disable restructuring in advanced optimizationsroot
- path to resolve absolute @import
rules and rebase relative URLsroundingPrecision
- rounding precision; defaults to 2
; -1
disables 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 under sourceMap
property, 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's sourcesContent
field (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 URLsThe 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 with sourceMap
option)errors
- a list of errors raisedwarnings
- a list of warnings raisedstats
- a hash of statistic information (if requested with debug
option):
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)