gulp-htmlmin压缩html的gulp插件实例代码_Javascript教程-查字典教程网
gulp-htmlmin压缩html的gulp插件实例代码
gulp-htmlmin压缩html的gulp插件实例代码
发布时间:2016-12-30 来源:查字典编辑
摘要:通过一条命令用Npm安装gulp-htmlmin:npminstallgulp-htmlmin--save-dev安装完毕后,打开gulpf...

通过一条命令用Npm安装gulp-htmlmin:

npm install gulp-htmlmin --save-dev

安装完毕后,打开gulpfile.js文件,我们里面编写一个task用来专门压缩html,并对html进行一系列的处理:

var gulp = require('gulp'); var htmlmin = require('gulp-htmlmin'); gulp.task('html',function(){ var options = { collapseWhitespace:true, collapseBooleanAttributes:true, removeComments:true, removeEmptyAttributes:true, removeScriptTypeAttributes:true, removeStyleLinkTypeAttributes:true, minifyJS:true, minifyCSS:true }; gulp.src('app/**/*.html') .pipe(htmlmin(options)) .pipe(gulp.dest('dest/')); });

我们看到task里面有个设置选项,分别介绍一下他们的属性的作用:

1.collapseWhitespace:从字面意思应该可以看出来,清除空格,压缩html,这一条比较重要,作用比较大,引起的改变压缩量也特别大;

2.collapseBooleanAttributes:省略布尔属性的值,比如:<input checked="checked"/>,那么设置这个属性后,就会变成 <input checked/>;

3.removeComments:清除html中注释的部分,我们应该减少html页面中的注释。

4.removeEmptyAttributes:清除所有的空属性,

5.removeSciptTypeAttributes:清除所有script标签中的type="text/javascript"属性。

6.removeStyleLinkTypeAttributes:清楚所有Link标签上的type属性。

7.minifyJS:压缩html中的javascript代码。

8.minifyCSS:压缩html中的css代码。

总之,压缩Html的原则就是清除没用的代码,删除本就默认值的属性,将html压缩的最小,这样才能提高项目运行的性能。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类