Minify HTML
之前的文章里我们介绍了如何压缩CSS和JavaScript,这里将对博客发布前的HTML文件进行压缩,这里我们选择使用htmlnano对HTML进行压缩。
htmlnano基于PostHTML,可作为PostHTML的插件使用,灵感来自于cssnano。
首先安装htmlnano:
npm install --save-dev htmlnano terser posthtml posthtml-cli
添加PostHTML的配置文件 posthtmlrc.json
:
{ "root": "public", "input": "**/*.html", "output": "public", "allInOutput": true, "plugins": { "htmlnano": {} } }
添加htmlnano的配置文件 htmlnanorc.json
:
{ "preset": "safe" }
htmlnano支持一些预设值:
- safe - a default preset for minifying a regular HTML in a safe way (without breaking anything)
- ampSafe - same as safe preset but for AMP pages
- max - maximal minification (might break some pages)
此时我们可以使用命令行对public文件夹下的所有html文件进行压缩:
npx posthtml -c posthtmlrc.json