2016-05-26 11:09:41 +00:00
|
|
|
/* global hexo */
|
2018-10-25 09:35:59 +00:00
|
|
|
'use strict'
|
2018-10-26 04:57:23 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
|
|
|
const UglifyJS = require('uglify-js')
|
|
|
|
const Htmlminifier = require('html-minifier').minify
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const minimatch = require('minimatch')
|
|
|
|
const zlib = require('zlib')
|
|
|
|
const br = require('iltorb')
|
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
|
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let path = data.path
|
|
|
|
let exclude = options.exclude
|
2018-10-25 09:35:59 +00:00
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude]
|
|
|
|
|
|
|
|
if (path && exclude && exclude.length) {
|
2018-10-26 04:57:23 +00:00
|
|
|
for (let i = 0, len = exclude.length; i < len; i++) {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (minimatch(path, exclude[i], { matchBase: true })) return str
|
2018-06-29 08:08:47 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let result = Htmlminifier(str, options)
|
|
|
|
let saved = ((str.length - result.length) / str.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let path = data.path
|
|
|
|
let exclude = options.exclude
|
2018-10-25 09:35:59 +00:00
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude]
|
2016-06-22 11:15:47 +00:00
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
if (path && exclude && exclude.length) {
|
2018-10-26 04:57:23 +00:00
|
|
|
for (let i = 0, len = exclude.length; i < len; i++) {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (minimatch(path, exclude[i], { matchBase: true })) return str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
new CleanCSS({ level: 2 }).minify(str, function (err, result) {
|
|
|
|
if (err) return reject(err)
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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) {
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
|
|
// Return if disabled.
|
|
|
|
if (options.enable === false) return
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let path = data.path
|
|
|
|
let exclude = options.exclude
|
2018-10-25 09:35:59 +00:00
|
|
|
if (exclude && !Array.isArray(exclude)) exclude = [exclude]
|
2016-05-26 11:09:41 +00:00
|
|
|
|
2018-10-25 09:35:59 +00:00
|
|
|
if (path && exclude && exclude.length) {
|
2018-10-26 04:57:23 +00:00
|
|
|
for (let i = 0, len = exclude.length; i < len; i++) {
|
2018-10-25 09:35:59 +00:00
|
|
|
if (minimatch(path, exclude[i], { matchBase: true })) return str
|
2018-06-29 08:08:47 +00:00
|
|
|
}
|
2018-10-25 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uglifyjs doesn't like unsupported options
|
|
|
|
delete options.enable
|
|
|
|
delete options.exclude
|
2018-10-26 04:57:23 +00:00
|
|
|
let jsLogger = options.logger
|
2018-10-25 09:35:59 +00:00
|
|
|
delete options.logger
|
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let result = UglifyJS.minify(str, options)
|
|
|
|
let saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (jsLogger) {
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let route = hexo.route
|
|
|
|
let routeList = route.list()
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-03-08 10:37:51 +00:00
|
|
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css') || path.endsWith('.xml') || path.endsWith('.json'))).map(path => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
|
|
|
zlib.gzip(assetTxt, { level: zlib.Z_BEST_COMPRESSION }, (err, Input) => {
|
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .gz
|
|
|
|
route.set(path + '.gz', Input)
|
|
|
|
// Logging
|
2018-10-26 04:57:23 +00:00
|
|
|
let saved = ((assetTxt.length - Input.toString().length) / assetTxt.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
|
|
|
|
2018-10-26 04:57:23 +00:00
|
|
|
let route = hexo.route
|
|
|
|
let routeList = route.list()
|
2018-10-25 09:35:59 +00:00
|
|
|
|
2019-03-08 10:37:51 +00:00
|
|
|
return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css') || path.endsWith('.xml') || path.endsWith('.json'))).map(path => {
|
2018-10-25 09:35:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Grab all assets using hexo router
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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
|
2018-10-26 04:57:23 +00:00
|
|
|
let input = new Buffer.from(assetTxt, 'utf-8')
|
2018-10-25 09:35:59 +00:00
|
|
|
// brotli compress using highest level
|
|
|
|
br.compress(input, { quality: br.BROTLI_MAX_QUALITY }, (err, output) => {
|
|
|
|
if (!err) {
|
|
|
|
// Save the compressed file to .br
|
|
|
|
route.set(path + '.br', output)
|
|
|
|
// Logging
|
2018-10-26 04:57:23 +00:00
|
|
|
let saved = ((input.length - output.toString().length) / input.length * 100).toFixed(2)
|
2018-10-25 09:35:59 +00:00
|
|
|
if (options.logger) {
|
2018-10-26 04:57:23 +00:00
|
|
|
let 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,
|
|
|
|
logicGzip: logicGzip,
|
|
|
|
logicBrotli: logicBrotli
|
|
|
|
}
|