Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ neat_html:
```
- **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`.
- **exclude** - Exclude files. Support [wildcard](https://github.com/micromatch/nanomatch#features) glob pattern.
- **exclude** - Exclude files.
- Support one-liner, `exclude: [*.min.html, *.note.html]`.
- To exclude a file, double asterisk and the full path must be specified, `**/themes/typing/source/js/source.js`.
- `*source.js` also works, but it also excludes `resource.js`.
- Test glob pattern on the web using [Globtester](http://www.globtester.com/).
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

For more options, see [HTMLMinifier](https://github.com/kangax/html-minifier).

Expand All @@ -56,6 +57,7 @@ neat_css:
- **logger** - Verbose output. Defaults to `false`.
- **exclude** - Exclude files. Support wildcard pattern.
- **level** - Optimization level. Defaults to `2`.
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

For more options, see [clean-css](https://github.com/jakubpawlowicz/clean-css).

Expand All @@ -74,6 +76,7 @@ neat_js:
- **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.
- To retain comments, `output: {comments: true}`.
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

For more options, see [Terser](https://github.com/terser-js/terser).

Expand All @@ -92,6 +95,7 @@ neat_svg:
- Exclude `*.min.svg` by default.
- **plugins** - Plugin options.
- To retain comments, `plugins: [{removeComments: false}]`.
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

For more options, see [svgo](https://github.com/svg/svgo).

Expand All @@ -118,6 +122,7 @@ neat_gzip:
- **include** - Include files. Support wildcard pattern.
- Support one-liner, `include: ['*.html','*.css','*.js']`.
- Must include asterisk and single quotes. `.html` is invalid. `'*.html'` is valid.
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

----------

Expand All @@ -140,6 +145,7 @@ neat_brotli:
- **enable** - Enable the plugin. Defaults to `true`.
- **logger** - Verbose output. Defaults to `false`.
- **include** - Include files. Support wildcard pattern.
- **globOptions** - [micromatch options](https://github.com/micromatch/micromatch#options) to customise how glob patterns match files. Defaults to `{ matchBase: true }`.

## 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.
Expand Down
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ if (hexo.config.neat_enable === true) {
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true
minifyCSS: true,
globOptions: { matchBase: true }
}, hexo.config.neat_html)

// CSS minifier
hexo.config.neat_css = Object.assign({
enable: true,
logger: false,
exclude: ['*.min.css'],
level: 2
level: 2,
globOptions: { matchBase: true }
}, hexo.config.neat_css)

// Javascript minifier
Expand All @@ -34,29 +36,33 @@ if (hexo.config.neat_enable === true) {
exclude: ['*.min.js'],
compress: {},
mangle: true,
output: {}
output: {},
globOptions: { matchBase: true }
}, hexo.config.neat_js)

// SVG minifier
hexo.config.neat_svg = Object.assign({
enable: true,
logger: false,
include: ['*.svg', '!*.min.svg'],
plugins: []
plugins: [],
globOptions: { matchBase: true }
}, hexo.config.neat_svg)

// gzip compression
hexo.config.neat_gzip = Object.assign({
enable: true,
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: { matchBase: true }
}, hexo.config.neat_gzip)

// brotli compression
hexo.config.neat_brotli = Object.assign({
enable: true,
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: { matchBase: true }
}, hexo.config.neat_brotli)

const filter = require('./lib/filter')
Expand Down
56 changes: 30 additions & 26 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
/* global hexo */
'use strict'

const Htmlminifier = require('html-minifier').minify
const CleanCSS = require('clean-css')
const Terser = require('terser')
const svgo = require('svgo')
const Svgo = require('svgo')
const zlib = require('zlib')
const br = require('iltorb')
const nanomatch = require('nanomatch')
const micromatch = require('micromatch')

function isMatch (path, patterns, options) {
if (path && patterns && patterns.length) {
return micromatch.isMatch(path, patterns, options)
} else {
return false
}
}

function logicHtml (str, data) {
const hexo = this
const options = hexo.config.neat_html
// Return if disabled.
if (options.enable === false) return

let path = data.path
let exclude = options.exclude
const path = data.path
const exclude = options.exclude
const globOptions = options.globOptions

// Return if a path matches exclusion pattern
if (path && exclude && exclude.length) {
if (nanomatch.some(path, exclude, { matchBase: true })) return str
}
if (isMatch(path, exclude, globOptions)) return str

let result = Htmlminifier(str, options)
let saved = ((str.length - result.length) / str.length * 100).toFixed(2)
Expand All @@ -35,16 +41,13 @@ function logicHtml (str, data) {
function logicCss (str, data) {
const hexo = this
const options = hexo.config.neat_css
// Return if disabled.
if (options.enable === false) return

let path = data.path
let exclude = options.exclude
const path = data.path
const exclude = options.exclude
const globOptions = options.globOptions

// Return if a path matches exclusion pattern
if (path && exclude && exclude.length) {
if (nanomatch.some(path, exclude, { matchBase: true })) return str
}
if (isMatch(path, exclude, globOptions)) return str

return new Promise((resolve, reject) => {
new CleanCSS(options).minify(str, (err, result) => {
Expand All @@ -62,22 +65,20 @@ function logicCss (str, data) {
function logicJs (str, data) {
const hexo = this
const options = hexo.config.neat_js
// Return if disabled.
if (options.enable === false) return

let path = data.path
let exclude = options.exclude
const path = data.path
const exclude = options.exclude
const globOptions = options.globOptions

// Return if a path matches exclusion pattern
if (path && exclude && exclude.length) {
if (nanomatch.some(path, exclude, { matchBase: true })) return str
}
if (isMatch(path, exclude, globOptions)) return str

// Terser doesn't like unsupported options
const jsOptions = Object.assign({}, options)
delete jsOptions.enable
delete jsOptions.exclude
delete jsOptions.logger
delete jsOptions.globOptions

let result = Terser.minify(str, jsOptions)
let saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
Expand All @@ -97,8 +98,9 @@ function logicSvg () {
let route = hexo.route
let routeList = route.list()
let include = options.include
const globOptions = options.globOptions

return Promise.all((nanomatch(routeList, include, { matchBase: true })).map(path => {
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
let assetPath = route.get(path)
Expand All @@ -108,7 +110,7 @@ function logicSvg () {
assetPath.on('end', () => {
if (assetTxt.length) {
// Minify using svgo
new svgo(options).optimize(assetTxt).then((result) => {
new Svgo(options).optimize(assetTxt).then((result) => {
// Replace the original file with the minified.
route.set(path, result.data)
// Logging
Expand All @@ -134,8 +136,9 @@ function logicGzip () {
let route = hexo.route
let routeList = route.list()
let include = options.include
const globOptions = options.globOptions

return Promise.all((nanomatch(routeList, include, { matchBase: true })).map(path => {
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
let assetPath = route.get(path)
Expand Down Expand Up @@ -175,8 +178,9 @@ function logicBrotli () {
let route = hexo.route
let routeList = route.list()
let include = options.include
const globOptions = options.globOptions

return Promise.all((nanomatch(routeList, include, { matchBase: true })).map(path => {
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
return new Promise((resolve, reject) => {
// Grab all assets using hexo router
let assetPath = route.get(path)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"clean-css": "^4.2.1",
"html-minifier": "^4.0.0",
"iltorb": "^2.4.2",
"nanomatch": "^1.2.13",
"micromatch": "^4.0.2",
"svgo": "^1.2.2",
"terser": "^4.0.0"
},
Expand Down