介绍

一款 Hexo 插件,通过压缩 HTML、CSS、JS 和图片来优化博客访问速度。

hexo-all-minifier 库

该文档出处

安装

插件安装命令

1
2
3

npm install hexo-all-minifier --save

增加配置信息到_config.yml

将这行代码填写到 Hexo 博客根目录下的博客配置文件 _config.yml 中就可以了。

1
all_minifier: true

默认添加以上代码就可以了,比较简单,但仅添加以上代码的情况下,如果在 hexo g 生成静态网页时遇到了问题,或者说有更进阶的需求,那么可以使用以下更进阶一些的设置。

进阶设置

如果使用默认的 all_minifier: true 遇到了问题,将对应出问题的某一项关闭即可(enable: false)。

HTML 优化

1
2
3
4
5
html_minifier:
enable: true
ignore_error: false
silent: false
exclude:

enable - 开启 HTML minifier. 默认为 true 。
ignore_error - 忽视 HTML 优化时遇到的错误。
silent - 禁用日志优化,默认为 false 。
exclude - 排除文件。

CSS 优化

1
2
3
4
5
css_minifier:
enable: true
silent: false
exclude:
- '*.min.css'

enable - 开启 CSS minifier,默认为 true 。
silent - 禁用日志优化,默认为 false 。
exclude - 排除文件。

JS 优化

1
2
3
4
5
6
7
8
js_minifier:
enable: true
mangle: true
silent: false
output:
compress:
exclude:
- '*.min.js'

enable - 开启 JS minifier,默认为 true 。
mangle - Mangle file names
output - 输出选项。如果为空,请从 .yml 文件中将其删除!否则,它将被设置为 null,null 不同于 undefined。
compress - 压缩选项。同上。
silent - 禁用日志优化,默认为 false 。
exclude - 排除文件。

image优化

1
2
3
4
5
6
7
8
image_minifier:
enable: true
interlaced: false
multipass: false
optimizationLevel: 2
pngquant: false
progressive: false
silent: false

enable - 开启 image minifier. 默认为 true 。
interlaced - Interlace gif for progressive rendering. 默认为 false 。
multipass - 多次优化 svg 图片,直到完全优化。默认为 false 。
optimizationLevel - 选择一个 0 - 7 之间的图片优化等级,默认为 2 。
pngquant - 开启 imagemin-pngquant 插件。默认为 false 。
progressive - Lossless conversion to progressive.默认为 false 。
silent - 禁用日志优化,默认为 false 。
exclude - 排除特定类型的图片文件。可选的值有 gif,jpg, png, or svg 。默认为 null ,不支持 Glob 。

一些错误信息

ERR1: pngquant failed to build, make sure that libpng-dev is installed

例如:

1
2
3
4
Error: pngquant failed to build, make sure that libpng-dev is installed
Error: autoreconf -fiv && ./configure --disable-shared --prefix="/i066/node_modules/mozjpeg/vendor" --bindir="/i066/node_modules/mozjpeg/vendor" --libdir="/i066/node_modules/mozjpeg/vendor" && make --jobs=2 && make install --jobs=2
Command failed: autoreconf -fiv
/bin/sh: 1: autoreconf: not found

解决办法:

1
apt install libpng-dev libtool automake autoconf nasm pkg-config