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) {
|
||||
const hexo = this
|
||||
const options = hexo.config.minify.html
|
||||
|
@ -34,11 +40,8 @@ function logicHtml (str, data) {
|
|||
if (isMatch(path, exclude, globOptions)) return str
|
||||
|
||||
const result = Htmlminifier(str, options)
|
||||
const saved = ((str.length - result.length) / str.length * 100).toFixed(2)
|
||||
if (options.logger) {
|
||||
const log = hexo.log || console.log
|
||||
log.log('Minify the html: %s [%s saved]', path, saved + '%')
|
||||
}
|
||||
if (options.logger) verbose.call(this, str, result, path, 'html')
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -60,12 +63,8 @@ function logicCss (str, data) {
|
|||
return new Promise((resolve, reject) => {
|
||||
new CleanCSS(options).minify(str, (err, result) => {
|
||||
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)
|
||||
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
|
||||
|
||||
const result = Terser.minify(str, jsOptions)
|
||||
const saved = ((str.length - result.code.length) / str.length * 100).toFixed(2)
|
||||
if (options.logger) {
|
||||
const log = hexo.log || console.log
|
||||
log.log('Minify the js: %s [%s saved]', path, saved + '%')
|
||||
}
|
||||
if (options.logger) verbose.call(this, str, result.code, path, 'js')
|
||||
|
||||
return result.code
|
||||
}
|
||||
|
||||
|
@ -129,12 +125,9 @@ function logicSvg () {
|
|||
new Svgo(options).optimize(assetTxt).then((result) => {
|
||||
// Replace the original file with the minified.
|
||||
route.set(path, result.data)
|
||||
// Logging
|
||||
const saved = ((assetTxt.length - result.data.length) / assetTxt.length * 100).toFixed(2)
|
||||
if (options.logger) {
|
||||
const log = hexo.log || console.log
|
||||
log.log('Minify the svg: %s [%s saved]', path, saved + '%')
|
||||
}
|
||||
|
||||
if (options.logger) verbose.call(this, assetTxt, result.data, path, 'svg')
|
||||
|
||||
resolve(assetTxt)
|
||||
})
|
||||
}
|
||||
|
@ -172,12 +165,9 @@ function logicGzip () {
|
|||
if (!err) {
|
||||
// Save the compressed file to .gz
|
||||
route.set(path + '.gz', Input)
|
||||
// Logging
|
||||
const saved = ((assetTxt.length - Input.toString().length) / assetTxt.length * 100).toFixed(2)
|
||||
if (options.logger) {
|
||||
const log = hexo.log || console.log
|
||||
log.log('Gzip-compressed %s [%s saved]', path, saved + '%')
|
||||
}
|
||||
|
||||
if (options.logger) verbose.call(this, assetTxt, Input.toString(), path, 'gzip')
|
||||
|
||||
resolve(assetTxt)
|
||||
} else {
|
||||
reject(err)
|
||||
|
@ -220,12 +210,9 @@ function logicBrotli () {
|
|||
if (!err) {
|
||||
// Save the compressed file to .br
|
||||
route.set(path + '.br', output)
|
||||
// Logging
|
||||
const saved = ((input.length - output.toString().length) / input.length * 100).toFixed(2)
|
||||
if (options.logger) {
|
||||
const log = hexo.log || console.log
|
||||
log.log('Brotli-compressed %s [%s saved]', path, saved + '%')
|
||||
}
|
||||
|
||||
if (options.logger) verbose.call(this, input, output.toString(), path, 'brotli')
|
||||
|
||||
resolve(assetTxt)
|
||||
} else {
|
||||
reject(err)
|
||||
|
|
Loading…
Reference in New Issue