gulp教程之gulp-minify-css【gulp-clean-css】

原文链接:http://www.ydcss.com/archives/41

简介:

使用gulp-minify-css压缩css文件,减小文件大小,并给引用url添加版本号避免缓存。重要:gulp-minify-css已经被废弃,请使用gulp-clean-css,用法一致。

1、安装nodejs/全局安装gulp/本地安装gulp/创建package.json和gulpfile.js文件

1.1、gulp基本使用还未掌握?请参看: gulp详细入门教程

1.2、本示例目录结构如下:

2、本地安装gulp-minify-css

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?

3、配置gulpfile.js

3.1、基本使用

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、执行任务

4.1、命令提示符执行:gulp testCssmin

5、结束语

5.1、本文有任何错误,或有任何疑问,欢迎留言说明。


PS:参数:

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 examples
  • debug - 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 all
  • mediaMerging - whether to merge @media at-rules (default is true)
  • processImport - whether to process @import rules
  • processImportFrom - 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 rebasing
  • relativeTo - path to resolve relative @import rules and URLs
  • restructuring - set to false to disable restructuring in advanced optimizations
  • root - path to resolve absolute @import rules and rebase relative URLs
  • roundingPrecision - rounding precision; defaults to 2-1 disables rounding
  • semanticMerging - 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 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 string
  • sourceMap - output source map (if requested with sourceMap option)
  • errors - a list of errors raised
  • warnings - a list of warnings raised
  • stats - a hash of statistic information (if requested with debug option):
    • originalSize - original content size (after import inlining)
    • minifiedSize - optimized content size
    • timeSpent - time spent on optimizations
    • efficiency - a ratio of output size to input size (e.g. 25% if content was reduced from 100 bytes to 75 bytes)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值