mirror of https://github.com/curbengh/hexo-yam
feat(gz & br): support custom compression level
This commit is contained in:
parent
ab023da82e
commit
48868bfa7e
|
@ -136,6 +136,8 @@ function gzipFn () {
|
||||||
const { route } = hexo
|
const { route } = hexo
|
||||||
const routeList = route.list()
|
const routeList = route.list()
|
||||||
const { globOptions, include } = options
|
const { globOptions, include } = options
|
||||||
|
let { level } = options
|
||||||
|
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION
|
||||||
|
|
||||||
let includeString = include || ''
|
let includeString = include || ''
|
||||||
if (include && Array.isArray(include)) includeString = include.join('')
|
if (include && Array.isArray(include)) includeString = include.join('')
|
||||||
|
@ -151,7 +153,7 @@ function gzipFn () {
|
||||||
try {
|
try {
|
||||||
// TODO: Drop Buffer
|
// TODO: Drop Buffer
|
||||||
const input = Buffer.from(assetTxt, 'utf-8')
|
const input = Buffer.from(assetTxt, 'utf-8')
|
||||||
const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION })
|
const result = await gzip(input, { level })
|
||||||
if (options.logger) verbose.call(this, input, result, path, 'gzip')
|
if (options.logger) verbose.call(this, input, result, path, 'gzip')
|
||||||
resolve(route.set(path + '.gz', result))
|
resolve(route.set(path + '.gz', result))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -171,6 +173,8 @@ function brotliFn () {
|
||||||
const { route } = hexo
|
const { route } = hexo
|
||||||
const routeList = route.list()
|
const routeList = route.list()
|
||||||
const { globOptions, include } = options
|
const { globOptions, include } = options
|
||||||
|
let { level } = options
|
||||||
|
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY
|
||||||
|
|
||||||
let includeString = include || ''
|
let includeString = include || ''
|
||||||
if (include && Array.isArray(include)) includeString = include.join('')
|
if (include && Array.isArray(include)) includeString = include.join('')
|
||||||
|
@ -185,7 +189,7 @@ function brotliFn () {
|
||||||
if (assetTxt.length) {
|
if (assetTxt.length) {
|
||||||
try {
|
try {
|
||||||
const input = Buffer.from(assetTxt, 'utf-8')
|
const input = Buffer.from(assetTxt, 'utf-8')
|
||||||
const result = await br(input)
|
const result = await br(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
|
||||||
if (options.logger) verbose.call(this, input, result, path, 'brotli')
|
if (options.logger) verbose.call(this, input, result, path, 'brotli')
|
||||||
resolve(route.set(path + '.br', result))
|
resolve(route.set(path + '.br', result))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -422,6 +422,43 @@ describe('gzip', () => {
|
||||||
expect(result).toBeUndefined()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('option', async () => {
|
||||||
|
const customOpt = {
|
||||||
|
level: 1
|
||||||
|
}
|
||||||
|
hexo.config.minify.gzip.level = customOpt.level
|
||||||
|
await g()
|
||||||
|
|
||||||
|
const output = hexo.route.get(path.concat('.gz'))
|
||||||
|
const buf = []
|
||||||
|
output.on('data', (chunk) => (buf.push(chunk)))
|
||||||
|
output.on('end', async () => {
|
||||||
|
const result = Buffer.concat(buf)
|
||||||
|
const expected = await gzip(input, customOpt)
|
||||||
|
|
||||||
|
expect(result.toString('base64')).toBe(Buffer.from(expected, 'binary').toString('base64'))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('option - invalid', async () => {
|
||||||
|
const customOpt = {
|
||||||
|
level: 9000
|
||||||
|
}
|
||||||
|
hexo.config.minify.gzip.level = customOpt.level
|
||||||
|
|
||||||
|
let expected
|
||||||
|
try {
|
||||||
|
await gzip(input, customOpt)
|
||||||
|
} catch (err) {
|
||||||
|
expected = err.message
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await g()
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.message).toContain(expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
test('include - exclude non-text file by default', async () => {
|
test('include - exclude non-text file by default', async () => {
|
||||||
const path = 'foo.jpg'
|
const path = 'foo.jpg'
|
||||||
hexo.route.set(path, input)
|
hexo.route.set(path, input)
|
||||||
|
@ -496,6 +533,23 @@ describe('brotli', () => {
|
||||||
expect(result).toBeUndefined()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('option - invalid', async () => {
|
||||||
|
const level = 'foo'
|
||||||
|
hexo.config.minify.brotli.level = level
|
||||||
|
|
||||||
|
let expected
|
||||||
|
try {
|
||||||
|
await brotli(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
|
||||||
|
} catch (err) {
|
||||||
|
expected = err.message
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await b()
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.message).toContain(expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
test('include - exclude non-text file by default', async () => {
|
test('include - exclude non-text file by default', async () => {
|
||||||
const path = 'foo.jpg'
|
const path = 'foo.jpg'
|
||||||
hexo.route.set(path, input)
|
hexo.route.set(path, input)
|
||||||
|
|
Loading…
Reference in New Issue