hehongwei44 / my-blog

我的博客
MIT License
136 stars 12 forks source link

gulp插件之gulp-htmlmin #123

Open hehongwei44 opened 7 years ago

hehongwei44 commented 7 years ago

目的

使用gulp-htmlmin压缩html,可以压缩页面javascript、css,去除页面空格、注释,删除多余属性等操作

代码

var gulp = require('gulp'),
    htmlmin = require('gulp-htmlmin');

gulp.task('testHtmlmin', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    gulp.src('src/html/*.html')
        .pipe(htmlmin(options))
        .pipe(gulp.dest('dist/html'));
});

更多压缩【参数】请查看 https://github.com/kangax/html-minifier [html-minifer docs]

hehongwei44 commented 7 years ago

相关链接:https://www.npmjs.com/package/gulp-htmlmin 相关链接:http://www.ydcss.com/archives/20