压缩htmljscss模块 html-minifier

在线压缩地址:http://kangax.github.io/html-minifier/

git地址:https://github.com/kangax/html-minifier

安装:

npm install html-minifier

使用:

1、node命令行进入要压缩的项目的根目录后,执行:

npm i html-minifier

2、在此根目录下新建一个文件命名为test.js,其内容如下:

var fs = require('fs');
var minify = require('html-minifier').minify;
fs.readFile('./test.htm', 'utf8', function (err, data) {
    if (err) {
        throw err;
    }
    fs.writeFile('./test_result.html', minify(data,{removeComments: true,collapseWhitespace: true,minifyJS:true, minifyCSS:true}),function(){
        console.log('success');
    });
});

执行命令:

node test.js

minify函数参数说明

第一个参数
String类型, 一段html代码

第二个参数 options
这里只列了几个常用的

    • removeComments 默认值false;是否去掉注释
    • collapseWhitespace 默认值false;是否去掉空格
    • minifyJS 默认值false;是否压缩html里的js(使用uglify-js进行的压缩)
    • minifyCSS 默认值false;是否压缩html里的css(使用clean-css进行的压缩)
原文地址:https://www.cnblogs.com/yanliangnh/p/5680224.html