Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

4.4. CSS Minification

4.4.1. gulp-minify-css

			
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
			
			

4.4.2. gulp-clean-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'));
});			
			
			

4.4.3. gulp-make-css-url-version

給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'));
});			
			
			

4.4.4. 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())
});