Merge pull request #17 from curbengh/options

feat: enable plugin by default and update options naming
This commit is contained in:
curbengh 2019-09-13 11:29:36 +09:30 committed by GitHub
commit 9cb977e2d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 149 additions and 130 deletions

151
README.md
View File

@ -1,4 +1,4 @@
# Hexo-yam # hexo-yam
[![npm version](https://badge.fury.io/js/hexo-yam.svg)](https://www.npmjs.com/package/hexo-yam) [![npm version](https://badge.fury.io/js/hexo-yam.svg)](https://www.npmjs.com/package/hexo-yam)
[![Build Status](https://travis-ci.com/curbengh/hexo-yam.svg?branch=master)](https://travis-ci.com/curbengh/hexo-yam) [![Build Status](https://travis-ci.com/curbengh/hexo-yam.svg?branch=master)](https://travis-ci.com/curbengh/hexo-yam)
@ -6,31 +6,47 @@
[![Known Vulnerabilities](https://snyk.io/test/npm/hexo-yam/badge.svg)](https://snyk.io/test/npm/hexo-yam) [![Known Vulnerabilities](https://snyk.io/test/npm/hexo-yam/badge.svg)](https://snyk.io/test/npm/hexo-yam)
[![Greenkeeper badge](https://badges.greenkeeper.io/curbengh/hexo-yam.svg)](https://greenkeeper.io/) [![Greenkeeper badge](https://badges.greenkeeper.io/curbengh/hexo-yam.svg)](https://greenkeeper.io/)
---
Yet Another Minifier for Hexo. Minify and compress HTML, JS, CSS and SVG. XML, JSON and [many more](https://github.com/curbengh/hexo-yam/blob/ba77db0094a7c07ea9f70f010bfc15541d4105ca/index.js#L64) are also compressed. Support gzip and [brotli](https://en.wikipedia.org/wiki/Brotli) [compressions](https://en.wikipedia.org/wiki/HTTP_compression). Yet Another Minifier for Hexo. Minify and compress HTML, JS, CSS and SVG. XML, JSON and [many more](https://github.com/curbengh/hexo-yam/blob/ba77db0094a7c07ea9f70f010bfc15541d4105ca/index.js#L64) are also compressed. Support gzip and [brotli](https://en.wikipedia.org/wiki/Brotli) [compressions](https://en.wikipedia.org/wiki/HTTP_compression).
This project is based on [hexo-neat](https://github.com/rozbo/hexo-neat). Existing options are the same, so you can use this as a drop-in replacement. ## Version 3
In v3 onwards, this plugin is enabled by default. Most [options](#options) have changed and they are now under `minify:` option.
*Note:* See [HTTP Compression](#http-compression) section below for more info on using brotli. Example:
```
neat_svg:
plugins: [{cleanupIDs: false}]
```
needs to be updated to
```
minify:
svg:
plugins: [{cleanupIDs: false}]
```
## Installation ## Installation
``` bash ``` bash
$ npm install hexo-yam --save $ npm install hexo-yam --save
``` ```
## Usage
To enable this plugin, insert the following to `_config.yml`:
``` yaml
neat_enable: true
```
For further customization, see below.
## Options ## Options
``` yaml ``` yaml
neat_html: minify:
enable: true enable: true
exclude: ```
- **enable** - Enable the plugin. Defaults to `true`.
---
``` yaml
minify:
html:
enable: true
exclude:
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
@ -43,29 +59,31 @@ neat_html:
For more options, see [HTMLMinifier](https://github.com/kangax/html-minifier). For more options, see [HTMLMinifier](https://github.com/kangax/html-minifier).
---------- ---
``` yaml ``` yaml
neat_css: minify:
enable: true css:
exclude: enable: true
- '*.min.css' exclude:
- '*.min.css'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
- **exclude** - Exclude files. Support wildcard pattern. - **exclude** - Exclude files. Support wildcard pattern.
- **level** - Optimization level. Defaults to `2`. - **level** - Optimization level. Defaults to `2`.
- **globOptions** - See [`neat_html`](#options). - **globOptions** - See [`html:`](#options).
For more options, see [clean-css](https://github.com/jakubpawlowicz/clean-css). For more options, see [clean-css](https://github.com/jakubpawlowicz/clean-css).
---------- ---
``` yaml ``` yaml
neat_js: minify:
enable: true js:
exclude: enable: true
- '*.min.js' exclude:
- '*.min.js'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
@ -74,18 +92,19 @@ neat_js:
- **mangle** - Mangle variable names. Defaults to `true`. Pass an object to specify [mangle options](https://github.com/terser-js/terser#mangle-options). - **mangle** - Mangle variable names. Defaults to `true`. Pass an object to specify [mangle options](https://github.com/terser-js/terser#mangle-options).
- **output** - Output options. - **output** - Output options.
- To retain comments, `output: {comments: true}`. - To retain comments, `output: {comments: true}`.
- **globOptions** - See [`neat_html`](#options). - **globOptions** - See [`html:`](#options).
For more options, see [Terser](https://github.com/terser-js/terser). For more options, see [Terser](https://github.com/terser-js/terser).
---------- ---
``` yaml ``` yaml
neat_svg: minify:
enable: true svg:
include: enable: true
- '*.svg' include:
- '!*.min.svg' - '*.svg'
- '!*.min.svg'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
@ -93,57 +112,59 @@ neat_svg:
- Exclude `*.min.svg` by default. - Exclude `*.min.svg` by default.
- **plugins** - Plugin options. - **plugins** - Plugin options.
- To retain comments, `plugins: [{removeComments: false}]`. - To retain comments, `plugins: [{removeComments: false}]`.
- **globOptions** - See [`neat_html`](#options). - **globOptions** - See [`html:`](#options).
For more options, see [svgo](https://github.com/svg/svgo). For more options, see [svgo](https://github.com/svg/svgo).
---------- ---
``` yaml ``` yaml
neat_gzip: minify:
enable: true gzip:
include: enable: true
- '*.html' include:
- '*.css' - '*.html'
- '*.js' - '*.css'
- '*.txt' - '*.js'
- '*.ttf' - '*.txt'
- '*.atom' - '*.ttf'
- '*.stl' - '*.atom'
- '*.xml' - '*.stl'
- '*.svg' - '*.xml'
- '*.eot' - '*.svg'
- '*.json' - '*.eot'
- '*.json'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
- **include** - Include files. Support wildcard pattern. - **include** - Include files. Support wildcard pattern.
- Support one-liner, `include: ['*.html','*.css','*.js']`. - Support one-liner, `include: ['*.html','*.css','*.js']`.
- Must include asterisk and single quotes. `.html` is invalid. `'*.html'` is valid. - Must include asterisk and single quotes. `.html` is invalid. `'*.html'` is valid.
- **globOptions** - See [`neat_html`](#options). - **globOptions** - See [`html:`](#options).
---------- ---
``` yaml ``` yaml
neat_brotli: minify:
enable: true brotli:
include: enable: true
- '*.html' include:
- '*.css' - '*.html'
- '*.js' - '*.css'
- '*.txt' - '*.js'
- '*.ttf' - '*.txt'
- '*.atom' - '*.ttf'
- '*.stl' - '*.atom'
- '*.xml' - '*.stl'
- '*.svg' - '*.xml'
- '*.eot' - '*.svg'
- '*.json' - '*.eot'
- '*.json'
``` ```
- **enable** - Enable the plugin. Defaults to `true`. - **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`. - **logger** - Verbose output. Defaults to `false`.
- **include** - Include files. Support wildcard pattern. - **include** - Include files. Support wildcard pattern.
- **globOptions** - See [`neat_html`](#options). - **globOptions** - See [`html:`](#options).
## HTTP Compression ## HTTP Compression
While most modern web browsers [support Brotli](https://www.caniuse.com/#feat=brotli), you also need to consider whether the web/app server, hosting platform, reverse proxy or CDN (whichever relevant to you) support it. While most modern web browsers [support Brotli](https://www.caniuse.com/#feat=brotli), you also need to consider whether the web/app server, hosting platform, reverse proxy or CDN (whichever relevant to you) support it.

114
index.js
View File

@ -1,70 +1,68 @@
/* global hexo */ /* global hexo */
'use strict' 'use strict'
if (hexo.config.neat_enable === true) { hexo.config.minify = Object.assign({
// HTML minifier enable: true
hexo.config.neat_html = Object.assign({ }, hexo.config.minify)
enable: true,
logger: false,
exclude: [],
collapseBooleanAttributes: true,
collapseWhitespace: true,
// Ignore '<!-- more -->' https://hexo.io/docs/tag-plugins#Post-Excerpt
ignoreCustomComments: [/^\s*more/],
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}, hexo.config.neat_html)
// CSS minifier hexo.config.minify.html = Object.assign({
hexo.config.neat_css = Object.assign({ enable: true,
enable: true, logger: false,
logger: false, exclude: [],
exclude: ['*.min.css'], collapseBooleanAttributes: true,
level: 2, collapseWhitespace: true,
globOptions: { basename: true } // Ignore '<!-- more -->' https://hexo.io/docs/tag-plugins#Post-Excerpt
}, hexo.config.neat_css) ignoreCustomComments: [/^\s*more/],
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}, hexo.config.minify.html)
// Javascript minifier hexo.config.minify.css = Object.assign({
hexo.config.neat_js = Object.assign({ enable: true,
enable: true, logger: false,
logger: false, exclude: ['*.min.css'],
exclude: ['*.min.js'], level: 2,
compress: {}, globOptions: { basename: true }
mangle: true, }, hexo.config.minify.css)
output: {},
globOptions: { basename: true }
}, hexo.config.neat_js)
// SVG minifier hexo.config.minify.js = Object.assign({
hexo.config.neat_svg = Object.assign({ enable: true,
enable: true, logger: false,
logger: false, exclude: ['*.min.js'],
include: ['*.svg', '!*.min.svg'], compress: {},
plugins: [], mangle: true,
globOptions: { basename: true } output: {},
}, hexo.config.neat_svg) globOptions: { basename: true }
}, hexo.config.minify.js)
// gzip compression hexo.config.minify.svg = Object.assign({
hexo.config.neat_gzip = Object.assign({ enable: true,
enable: true, logger: false,
logger: false, include: ['*.svg', '!*.min.svg'],
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], plugins: [],
globOptions: { basename: true } globOptions: { basename: true }
}, hexo.config.neat_gzip) }, hexo.config.minify.svg)
// brotli compression hexo.config.minify.gzip = Object.assign({
hexo.config.neat_brotli = Object.assign({ enable: true,
enable: true, logger: false,
logger: false, include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], globOptions: { basename: true }
globOptions: { basename: true } }, hexo.config.minify.gzip)
}, hexo.config.neat_brotli)
hexo.config.minify.brotli = Object.assign({
enable: true,
logger: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}, hexo.config.minify.brotli)
if (hexo.config.minify.enable === true) {
const filter = require('./lib/filter') const filter = require('./lib/filter')
hexo.extend.filter.register('after_render:html', filter.logicHtml) hexo.extend.filter.register('after_render:html', filter.logicHtml)
hexo.extend.filter.register('after_render:css', filter.logicCss) hexo.extend.filter.register('after_render:css', filter.logicCss)

View File

@ -18,7 +18,7 @@ function isMatch (path, patterns, options) {
function logicHtml (str, data) { function logicHtml (str, data) {
const hexo = this const hexo = this
const options = hexo.config.neat_html const options = hexo.config.minify.html
// Return if disabled. // Return if disabled.
if (options.enable === false) return if (options.enable === false) return
@ -44,7 +44,7 @@ function logicHtml (str, data) {
function logicCss (str, data) { function logicCss (str, data) {
const hexo = this const hexo = this
const options = hexo.config.neat_css const options = hexo.config.minify.css
if (options.enable === false) return if (options.enable === false) return
const path = data.path const path = data.path
@ -72,7 +72,7 @@ function logicCss (str, data) {
function logicJs (str, data) { function logicJs (str, data) {
const hexo = this const hexo = this
const options = hexo.config.neat_js const options = hexo.config.minify.js
if (options.enable === false) return if (options.enable === false) return
const path = data.path const path = data.path
@ -103,7 +103,7 @@ function logicJs (str, data) {
function logicSvg () { function logicSvg () {
const hexo = this const hexo = this
const options = hexo.config.neat_svg const options = hexo.config.minify.svg
// Return if disabled. // Return if disabled.
if (options.enable === false) return if (options.enable === false) return
@ -145,7 +145,7 @@ function logicSvg () {
function logicGzip () { function logicGzip () {
const hexo = this const hexo = this
const options = hexo.config.neat_gzip const options = hexo.config.minify.gzip
// Return if disabled. // Return if disabled.
if (options.enable === false) return if (options.enable === false) return
@ -191,7 +191,7 @@ function logicGzip () {
function logicBrotli () { function logicBrotli () {
const hexo = this const hexo = this
const options = hexo.config.neat_brotli const options = hexo.config.minify.brotli
// Return if disabled. // Return if disabled.
if (options.enable === false) return if (options.enable === false) return

View File

@ -1,7 +1,7 @@
{ {
"name": "hexo-yam", "name": "hexo-yam",
"description": "Yet Another Minifier. Minify and compress html, js, css and svg", "description": "Yet Another Minifier. Minify and compress html, js, css and svg",
"version": "2.2.1", "version": "3.0.0",
"readme": "README.md", "readme": "README.md",
"main": "index.js", "main": "index.js",
"directories": { "directories": {