2018-10-25 09:35:59 +00:00
|
|
|
'use strict'
|
2019-05-27 01:14:04 +00:00
|
|
|
|
2019-04-23 08:16:56 +00:00
|
|
|
const Htmlminifier = require('html-minifier').minify
|
2018-10-26 04:57:23 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
2019-04-10 09:54:02 +00:00
|
|
|
const Terser = require('terser')
|
2019-07-10 02:20:23 +00:00
|
|
|
const Svgo = require('svgo')
|
2018-10-26 04:57:23 +00:00
|
|
|
const zlib = require('zlib')
|
|
|
|
const br = require('iltorb')
|
2019-07-10 02:20:23 +00:00
|
|
|
const micromatch = require('micromatch')
|
|
|
|
|
|
|
|
function isMatch (path, patterns, options) {
|
|
|
|
if (path && patterns && patterns.length) {
|
|
|
|
return micromatch.isMatch(path, patterns, options)
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
function logicHtml (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_html
|
2018-10-25 09:35:59 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
const path = data.path
|
|
|
|
const exclude = options.exclude
|
|
|
|
const globOptions = options.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let excludeString = exclude
|
|
|
|
if (Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-04-23 04:05:59 +00:00
|
|
|
// Return if a path matches exclusion pattern
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const result = Htmlminifier(str, options)
|
|
|
|
const saved = ((str.length - result.length) / str.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2018-10-25 09:35:59 +00:00
|
|
|
log.log('Minify the html: %s [%s saved]', path, saved + '%')
|
|
|
|
}
|
|
|
|
return result
|
2018-09-29 05:38:45 +00:00
|
|
|
}
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
function logicCss (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_css
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
const path = data.path
|
|
|
|
const exclude = options.exclude
|
|
|
|
const globOptions = options.globOptions
|
2016-06-22 11:15:47 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let excludeString = exclude
|
|
|
|
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString && excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-05-21 01:46:14 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
new CleanCSS(options).minify(str, (err, result) => {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (err) return reject(err)
|
2019-08-06 02:27:44 +00:00
|
|
|
const saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
resolve(result.styles)
|
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2018-10-25 09:35:59 +00:00
|
|
|
log.log('Minify the css: %s [%s saved]', path, saved + '%')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
function logicJs (str, data) {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_js
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
const path = data.path
|
|
|
|
const exclude = options.exclude
|
|
|
|
const globOptions = options.globOptions
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let excludeString = exclude
|
|
|
|
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
|
|
|
|
if (excludeString && excludeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
if (isMatch(path, exclude, globOptions)) return str
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-04-10 09:54:02 +00:00
|
|
|
// Terser doesn't like unsupported options
|
2019-04-23 04:05:59 +00:00
|
|
|
const jsOptions = Object.assign({}, options)
|
2019-04-23 03:00:47 +00:00
|
|
|
delete jsOptions.enable
|
|
|
|
delete jsOptions.exclude
|
|
|
|
delete jsOptions.logger
|
2019-07-10 02:20:23 +00:00
|
|
|
delete jsOptions.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const result = Terser.minify(str, jsOptions)
|
|
|
|
const saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
|
2019-04-23 03:00:47 +00:00
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2018-10-25 09:35:59 +00:00
|
|
|
log.log('Minify the js: %s [%s saved]', path, saved + '%')
|
|
|
|
}
|
|
|
|
return result.code
|
2016-05-26 11:09:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 07:59:35 +00:00
|
|
|
function logicSvg () {
|
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_svg
|
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const route = hexo.route
|
|
|
|
const routeList = route.list()
|
|
|
|
const include = options.include
|
2019-07-10 02:20:23 +00:00
|
|
|
const globOptions = options.globOptions
|
2019-04-23 07:59:35 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let includeString = include
|
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
|
2019-04-23 07:59:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2019-04-23 07:59:35 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
// Extract the content
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
2019-04-23 08:27:52 +00:00
|
|
|
// Minify using svgo
|
2019-07-10 02:20:23 +00:00
|
|
|
new Svgo(options).optimize(assetTxt).then((result) => {
|
2019-05-02 06:25:43 +00:00
|
|
|
// Replace the original file with the minified.
|
|
|
|
route.set(path, result.data)
|
|
|
|
// Logging
|
2019-08-06 02:27:44 +00:00
|
|
|
const saved = ((assetTxt.length - result.data.length) / assetTxt.length * 100).toFixed(2)
|
2019-05-02 06:25:43 +00:00
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2019-05-02 06:25:43 +00:00
|
|
|
log.log('Minify the svg: %s [%s saved]', path, saved + '%')
|
|
|
|
}
|
|
|
|
resolve(assetTxt)
|
2019-04-23 07:59:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
function logicGzip () {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_gzip
|
2018-10-25 09:35:59 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const route = hexo.route
|
|
|
|
const routeList = route.list()
|
|
|
|
const include = options.include
|
2019-07-10 02:20:23 +00:00
|
|
|
const globOptions = options.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let includeString = include
|
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2018-10-25 09:35:59 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
// Extract the content
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
// gzip compress using highest level
|
2019-04-22 10:38:50 +00:00
|
|
|
zlib.gzip(assetTxt, { level: zlib.constants.Z_BEST_COMPRESSION }, (err, Input) => {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .gz
|
|
|
|
route.set(path + '.gz', Input)
|
|
|
|
// Logging
|
2019-08-06 02:27:44 +00:00
|
|
|
const saved = ((assetTxt.length - Input.toString().length) / assetTxt.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2018-10-25 09:35:59 +00:00
|
|
|
log.log('Gzip-compressed %s [%s saved]', path, saved + '%')
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
resolve(assetTxt)
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
2018-09-30 07:30:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
function logicBrotli () {
|
2018-10-26 04:57:23 +00:00
|
|
|
const hexo = this
|
|
|
|
const options = hexo.config.neat_brotli
|
2018-10-25 09:35:59 +00:00
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
|
|
|
|
2019-08-06 02:27:44 +00:00
|
|
|
const route = hexo.route
|
|
|
|
const routeList = route.list()
|
|
|
|
const include = options.include
|
2019-07-10 02:20:23 +00:00
|
|
|
const globOptions = options.globOptions
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-08-06 01:56:11 +00:00
|
|
|
let includeString = include
|
|
|
|
if (include && Array.isArray(include)) includeString = include.join('')
|
|
|
|
if (includeString && includeString.includes('/')) globOptions.basename = false
|
|
|
|
|
2019-07-10 02:20:23 +00:00
|
|
|
return Promise.all((micromatch(routeList, include, globOptions)).map(path => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
2019-08-06 02:27:44 +00:00
|
|
|
const assetPath = route.get(path)
|
2018-10-25 09:35:59 +00:00
|
|
|
let assetTxt = ''
|
|
|
|
// Extract the content
|
|
|
|
assetPath.on('data', (chunk) => (assetTxt += chunk))
|
|
|
|
assetPath.on('end', () => {
|
|
|
|
if (assetTxt.length) {
|
|
|
|
// Input has to be buffer for brotli
|
2019-08-06 02:27:44 +00:00
|
|
|
const input = Buffer.from(assetTxt, 'utf-8')
|
2019-04-22 08:58:00 +00:00
|
|
|
// brotli defaults to max compression level
|
|
|
|
br.compress(input, (err, output) => {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .br
|
|
|
|
route.set(path + '.br', output)
|
|
|
|
// Logging
|
2019-08-06 02:27:44 +00:00
|
|
|
const saved = ((input.length - output.toString().length) / input.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2019-08-06 02:27:44 +00:00
|
|
|
const log = hexo.log || console.log
|
2018-10-25 09:35:59 +00:00
|
|
|
log.log('Brotli-compressed %s [%s saved]', path, saved + '%')
|
2018-09-29 05:38:45 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
resolve(assetTxt)
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}))
|
2018-09-28 07:43:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 11:09:41 +00:00
|
|
|
module.exports = {
|
2018-10-25 09:35:59 +00:00
|
|
|
logicHtml: logicHtml,
|
|
|
|
logicCss: logicCss,
|
|
|
|
logicJs: logicJs,
|
2019-04-23 07:59:35 +00:00
|
|
|
logicSvg: logicSvg,
|
2018-10-25 09:35:59 +00:00
|
|
|
logicGzip: logicGzip,
|
|
|
|
logicBrotli: logicBrotli
|
|
|
|
}
|