feat: rename 'logger:' option to 'verbose:'

BREAKING CHANGE
This commit is contained in:
curbengh 2019-12-27 22:54:09 +00:00
parent e59e3866b9
commit 440b9ccd8c
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
3 changed files with 67 additions and 22 deletions

View File

@ -5,7 +5,7 @@ const minifyDefault = { enable: true }
const htmlDefault = {
enable: true,
priority: 10,
logger: false,
verbose: false,
exclude: [],
collapseBooleanAttributes: true,
collapseWhitespace: true,
@ -22,8 +22,7 @@ const htmlDefault = {
const cssDefault = {
enable: true,
priority: 10,
// TODO: rename to verbose
logger: false,
verbose: false,
exclude: ['*.min.css'],
level: 2,
globOptions: { basename: true }
@ -31,7 +30,7 @@ const cssDefault = {
const jsDefault = {
enable: true,
priority: 10,
logger: false,
verbose: false,
exclude: ['*.min.js'],
compress: {},
mangle: true,
@ -41,7 +40,7 @@ const jsDefault = {
const svgDefault = {
enable: true,
priority: 10,
logger: false,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
globOptions: { basename: true }
@ -49,14 +48,14 @@ const svgDefault = {
const gzipDefault = {
enable: true,
priority: 10,
logger: false,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}
const brotliDefault = {
enable: true,
priority: 10,
logger: false,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}

View File

@ -28,7 +28,7 @@ const isMatch = (path = '', patterns = [], options = {}) => {
return false
}
function verbose (original, minified, path, ext) {
function logFn (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]`)
@ -40,13 +40,13 @@ function minifyHtml (str, data) {
if (options.enable === false) return
const { path } = data
const { exclude, globOptions } = options
const { exclude, globOptions, verbose } = options
// Return if a path matches exclusion pattern
if (isMatch(path, exclude, globOptions)) return str
const result = Htmlminifier(str, options)
if (options.logger) verbose.call(this, str, result, path, 'html')
if (verbose) logFn.call(this, str, result, path, 'html')
return result
}
@ -57,13 +57,13 @@ async function minifyCss (str, data) {
if (options.enable === false) return
const { path } = data
const { exclude, globOptions } = options
const { exclude, globOptions, verbose } = options
if (isMatch(path, exclude, globOptions)) return str
try {
const { styles } = await new CleanCSS(options).minify(str)
if (options.logger) verbose.call(this, str, styles, path, 'css')
if (verbose) logFn.call(this, str, styles, path, 'css')
return styles
} catch (err) {
throw new Error(err)
@ -76,7 +76,7 @@ function minifyJs (str, data) {
if (options.enable === false) return
const { path } = data
const { exclude, globOptions } = options
const { exclude, globOptions, verbose } = options
if (isMatch(path, exclude, globOptions)) return str
@ -84,13 +84,15 @@ function minifyJs (str, data) {
const jsOptions = Object.assign({}, options)
delete jsOptions.enable
delete jsOptions.priority
delete jsOptions.verbose
// Old option, retained to avoid crash when upgrading to v4
delete jsOptions.logger
delete jsOptions.exclude
delete jsOptions.globOptions
const { code, error } = Terser.minify(str, jsOptions)
if (error) throw new Error(error)
if (options.logger) verbose.call(this, str, code, path, 'js')
if (verbose) logFn.call(this, str, code, path, 'js')
return code
}
@ -102,7 +104,7 @@ function minifySvg () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include } = options
const { globOptions, include, verbose } = options
let includeString = include || ''
if (include && Array.isArray(include)) includeString = include.join('')
@ -117,7 +119,7 @@ function minifySvg () {
if (assetTxt.length) {
try {
const { data } = await new Svgo(options).optimize(assetTxt)
if (options.logger) verbose.call(this, assetTxt, data, path, 'svg')
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
resolve(route.set(path, data))
} catch (err) {
reject(new Error(err))
@ -135,7 +137,7 @@ function gzipFn () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include } = options
const { globOptions, include, verbose } = options
let { level } = options
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION
@ -152,7 +154,7 @@ function gzipFn () {
if (assetTxt.length) {
try {
const result = await gzip(assetTxt, { level })
if (options.logger) verbose.call(this, assetTxt, result, path, 'gzip')
if (verbose) logFn.call(this, assetTxt, result, path, 'gzip')
resolve(route.set(path + '.gz', result))
} catch (err) {
reject(new Error(err))
@ -170,7 +172,7 @@ function brotliFn () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include } = options
const { globOptions, include, verbose } = options
let { level } = options
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY
@ -187,7 +189,7 @@ function brotliFn () {
if (assetTxt.length) {
try {
const result = await br(assetTxt, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
if (options.logger) verbose.call(this, assetTxt, result, path, 'brotli')
if (verbose) logFn.call(this, assetTxt, result, path, 'brotli')
resolve(route.set(path + '.br', result))
} catch (err) {
reject(new Error(err))

View File

@ -44,8 +44,8 @@ describe('html', () => {
expect(result).toBe(expected)
})
it('option - logger', () => {
hexo.config.minify.html.logger = true
it('option - verbose', () => {
hexo.config.minify.html.verbose = true
const path = 'foo'
hexo.log.log = jest.fn()
const input = '<p>foo</p>'
@ -150,6 +150,16 @@ describe('css', () => {
expect(result).toBe(styles)
})
it('option - verbose', async () => {
hexo.config.minify.css.verbose = true
const path = 'foo'
hexo.log.log = jest.fn()
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
await c(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`css: ${path}`)
})
test('option - invalid', async () => {
const customOpt = {
level: 9000
@ -238,6 +248,16 @@ describe('js', () => {
expect(result).toBe(code)
})
it('option - verbose', () => {
hexo.config.minify.js.verbose = true
const path = 'foo'
hexo.log.log = jest.fn()
const input = 'var o = { "foo": 1, bar: 3 };'
j(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`)
})
test('option - invalid', () => {
const customOpt = {
mangle: {
@ -328,6 +348,14 @@ describe('svg', () => {
})
})
it('option - verbose', async () => {
hexo.config.minify.svg.verbose = true
hexo.log.log = jest.fn()
await s()
expect(hexo.log.log.mock.calls[0][0]).toContain(`svg: ${path}`)
})
test('invalid svg', async () => {
const input = '{}'
hexo.route.set(path, input)
@ -450,6 +478,14 @@ describe('gzip', () => {
})
})
it('option - verbose', async () => {
hexo.config.minify.gzip.verbose = true
hexo.log.log = jest.fn()
await g()
expect(hexo.log.log.mock.calls[0][0]).toContain(`gzip: ${path}`)
})
test('option - invalid', async () => {
const customOpt = {
level: 9000
@ -559,6 +595,14 @@ describe('brotli', () => {
})
})
it('option - verbose', async () => {
hexo.config.minify.brotli.verbose = true
hexo.log.log = jest.fn()
await b()
expect(hexo.log.log.mock.calls[0][0]).toContain(`brotli: ${path}`)
})
test('option - level is string', async () => {
const level = 'foo'
hexo.config.minify.brotli.level = level