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

View File

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

View File

@ -44,8 +44,8 @@ describe('html', () => {
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
it('option - logger', () => { it('option - verbose', () => {
hexo.config.minify.html.logger = true hexo.config.minify.html.verbose = true
const path = 'foo' const path = 'foo'
hexo.log.log = jest.fn() hexo.log.log = jest.fn()
const input = '<p>foo</p>' const input = '<p>foo</p>'
@ -150,6 +150,16 @@ describe('css', () => {
expect(result).toBe(styles) 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 () => { test('option - invalid', async () => {
const customOpt = { const customOpt = {
level: 9000 level: 9000
@ -238,6 +248,16 @@ describe('js', () => {
expect(result).toBe(code) 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', () => { test('option - invalid', () => {
const customOpt = { const customOpt = {
mangle: { 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 () => { test('invalid svg', async () => {
const input = '{}' const input = '{}'
hexo.route.set(path, 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 () => { test('option - invalid', async () => {
const customOpt = { const customOpt = {
level: 9000 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 () => { test('option - level is string', async () => {
const level = 'foo' const level = 'foo'
hexo.config.minify.brotli.level = level hexo.config.minify.brotli.level = level