From b5639aba85ad487ff299d0bfa8281b5e3407a61e Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 15 Sep 2019 16:51:15 +0100 Subject: [PATCH] refactor: logger function --- lib/filter.js | 53 +++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/lib/filter.js b/lib/filter.js index 13093ff..194bb6b 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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)