npm install --save-dev gulp-minify-css
// including plugins var gulp = require('gulp'); var minifyCss = require("gulp-minify-css"); // task gulp.task('minify-css', function () { gulp.src('./css/one.css') // path to your file .pipe(minifyCss()) .pipe(gulp.dest('path/to/destination')); }); // task gulp.task('minify-multi-css', function () { gulp.src(__dirname+'/css/*.css') // path to your file .pipe(minifyCss()) .pipe(gulp.dest('path/to/destination')); });
gulp minify-css gulp minify-multi-css
var gulp = require('gulp'); var minifycss = require('gulp-clean-css'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); var rename = require('gulp-rename'); gulp.task('minify-css', function () { gulp.src('./css/*.css') .pipe(concat("finally.css")) .pipe(rename({ suffix: '.min' })) .pipe(minifycss()) .pipe(gulp.dest('build/css')); }); gulp.task('minify-js', function(){ gulp.src(__dirname + "/js/*.js") .pipe(concat("finally.js")) .pipe(rename({ suffix: '.min' })) .pipe(uglify()) .pipe(gulp.dest('build/js')) });
compatibility
var gulp = require('gulp'); var cleanCSS = require('gulp-clean-css'); gulp.task('minify-css', function() { return gulp.src('styles/*.css') .pipe(cleanCSS({compatibility: 'ie8'})) .pipe(gulp.dest('dist')); });
callback
var gulp = require('gulp'); var cleanCSS = require('gulp-clean-css'); gulp.task('minify-css', function() { return gulp.src('styles/*.css') .pipe(cleanCSS({debug: true}, function(details) { console.log(details.name + ': ' + details.stats.originalSize); console.log(details.name + ': ' + details.stats.minifiedSize); })) .pipe(gulp.dest('dist')); });
给css文件里引用url加版本号(md5sum),像这样:
background: url(../images/pc-banner-bg.jpg?v=4facbd0914639f296faec4dba4d358f0) no-repeat;}
npm install --save-dev gulp-make-css-url-version
var gulp = require('gulp'), minifier = require('gulp-minify-css'); cssver = require('gulp-make-css-url-version'); gulp.task('testCssmin', function () { gulp.src('src/css/*.css') .pipe(cssver()) //给css文件里引用文件加版本号(文件MD5) .pipe(minifier()) .pipe(gulp.dest('dist/css')); });
检查出重复定义的CSS
Installation
npm install gulp-csscss --save-dev
Example
var gulp = require('gulp'); var csscss = require('gulp-csscss'); gulp.task('default', function() { gulp.src('src/style.css') .pipe(csscss()) });
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。