mirror of https://github.com/curbengh/hexo-yam
refactor: logger function
This commit is contained in:
parent
36bca1b939
commit
b5639aba85
|
@ -16,6 +16,12 @@ const isMatch = (path, patterns, options) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function verbose (original, minified, path, ext) {
|
||||||
|
const saved = ((original.length - minified.length) / original.length * 100).toFixed(2)
|
||||||
|
const log = this.log || console
|
||||||
|
log.log(`${ext}: ${path} [${saved}% saved]`)
|
||||||
|
}
|
||||||
|
|
||||||
function logicHtml (str, data) {
|
function logicHtml (str, data) {
|
||||||
const hexo = this
|
const hexo = this
|
||||||
const options = hexo.config.minify.html
|
const options = hexo.config.minify.html
|
||||||
|
@ -34,11 +40,8 @@ function logicHtml (str, data) {
|
||||||
if (isMatch(path, exclude, globOptions)) return str
|
if (isMatch(path, exclude, globOptions)) return str
|
||||||
|
|
||||||
const result = Htmlminifier(str, options)
|
const result = Htmlminifier(str, options)
|
||||||
const saved = ((str.length - result.length) / str.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, str, result, path, 'html')
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Minify the html: %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,12 +63,8 @@ function logicCss (str, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
new CleanCSS(options).minify(str, (err, result) => {
|
new CleanCSS(options).minify(str, (err, result) => {
|
||||||
if (err) return reject(err)
|
if (err) return reject(err)
|
||||||
const saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, str, result.styles, path, 'css')
|
||||||
resolve(result.styles)
|
resolve(result.styles)
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Minify the css: %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -93,11 +92,8 @@ function logicJs (str, data) {
|
||||||
delete jsOptions.globOptions
|
delete jsOptions.globOptions
|
||||||
|
|
||||||
const result = Terser.minify(str, jsOptions)
|
const result = Terser.minify(str, jsOptions)
|
||||||
const saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, str, result.code, path, 'js')
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Minify the js: %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
return result.code
|
return result.code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,12 +125,9 @@ function logicSvg () {
|
||||||
new Svgo(options).optimize(assetTxt).then((result) => {
|
new Svgo(options).optimize(assetTxt).then((result) => {
|
||||||
// Replace the original file with the minified.
|
// Replace the original file with the minified.
|
||||||
route.set(path, result.data)
|
route.set(path, result.data)
|
||||||
// Logging
|
|
||||||
const saved = ((assetTxt.length - result.data.length) / assetTxt.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg')
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Minify the svg: %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
resolve(assetTxt)
|
resolve(assetTxt)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -172,12 +165,9 @@ function logicGzip () {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
// Save the compressed file to .gz
|
// Save the compressed file to .gz
|
||||||
route.set(path + '.gz', Input)
|
route.set(path + '.gz', Input)
|
||||||
// Logging
|
|
||||||
const saved = ((assetTxt.length - Input.toString().length) / assetTxt.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, assetTxt, Input.toString(), path, 'gzip')
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Gzip-compressed %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
resolve(assetTxt)
|
resolve(assetTxt)
|
||||||
} else {
|
} else {
|
||||||
reject(err)
|
reject(err)
|
||||||
|
@ -220,12 +210,9 @@ function logicBrotli () {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
// Save the compressed file to .br
|
// Save the compressed file to .br
|
||||||
route.set(path + '.br', output)
|
route.set(path + '.br', output)
|
||||||
// Logging
|
|
||||||
const saved = ((input.length - output.toString().length) / input.length * 100).toFixed(2)
|
if (options.logger) verbose.call(this, input, output.toString(), path, 'brotli')
|
||||||
if (options.logger) {
|
|
||||||
const log = hexo.log || console.log
|
|
||||||
log.log('Brotli-compressed %s [%s saved]', path, saved + '%')
|
|
||||||
}
|
|
||||||
resolve(assetTxt)
|
resolve(assetTxt)
|
||||||
} else {
|
} else {
|
||||||
reject(err)
|
reject(err)
|
||||||
|
|
Loading…
Reference in New Issue