让前端飞Web前端之路Web 前端开发

压缩html\js\css模块 html-minifier

2019-11-09  本文已影响0人  2o壹9

在线压缩地址: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,其内容如下:

varfs = require('fs');varminify = 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进行的压缩)

上一篇 下一篇

猜你喜欢

热点阅读