2019-12-26 11:11:36 +00:00
|
|
|
/* eslint-env jest */
|
|
|
|
'use strict'
|
|
|
|
|
2019-12-27 05:39:29 +00:00
|
|
|
const { promisify } = require('util')
|
2019-12-26 11:11:36 +00:00
|
|
|
const Hexo = require('hexo')
|
|
|
|
const hexo = new Hexo(__dirname)
|
|
|
|
global.hexo = hexo
|
|
|
|
|
|
|
|
describe('html', () => {
|
|
|
|
const { htmlDefault } = require('../index')
|
|
|
|
const h = require('../lib/filter').minifyHtml.bind(hexo)
|
|
|
|
const Htmlminifier = require('html-minifier').minify
|
2019-12-27 23:12:46 +00:00
|
|
|
const input = '<p id="">foo</p>'
|
|
|
|
const path = 'index.html'
|
2019-12-26 11:11:36 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-26 15:24:54 +00:00
|
|
|
hexo.config.minify.html = Object.assign({}, htmlDefault)
|
2019-12-26 11:11:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('default', () => {
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = h(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const expected = Htmlminifier(input, hexo.config.minify.html)
|
2019-12-26 14:44:42 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2019-12-26 15:24:54 +00:00
|
|
|
test('disable', () => {
|
|
|
|
hexo.config.minify.html.enable = false
|
|
|
|
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = h(input, { path })
|
2019-12-26 15:24:54 +00:00
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('option', () => {
|
|
|
|
const customOpt = { removeEmptyAttributes: false }
|
|
|
|
hexo.config.minify.html = customOpt
|
|
|
|
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = h(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const expected = Htmlminifier(input, customOpt)
|
2019-12-26 14:44:42 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.html.verbose = true
|
2019-12-27 08:55:39 +00:00
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
h(input, { path })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`html: ${path}`)
|
2019-12-27 08:55:39 +00:00
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('exclude', () => {
|
|
|
|
const exclude = '*.min.html'
|
|
|
|
hexo.config.minify.html.exclude = exclude
|
|
|
|
|
|
|
|
const result = h(input, { path: 'foo/bar.min.html' })
|
2019-12-26 14:44:42 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - slash in pattern', () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
const exclude = '**/lectus/**/*.html'
|
2019-12-26 11:11:36 +00:00
|
|
|
hexo.config.minify.html.exclude = exclude
|
|
|
|
|
2019-12-28 10:26:25 +00:00
|
|
|
const result = h(input, { path: 'eleifend/lectus/nullam/dapibus/netus.html' })
|
2019-12-26 14:44:42 +00:00
|
|
|
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - basename is true + slash', () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
const exclude = ['**/lectus/**/*.html', 'bar.html']
|
2019-12-26 14:44:42 +00:00
|
|
|
const globOptions = { basename: true }
|
|
|
|
hexo.config.minify.html.exclude = exclude
|
|
|
|
hexo.config.minify.html.globOptions = globOptions
|
|
|
|
|
|
|
|
const result = h(input, { path: 'foo/bar.html' })
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
2019-12-26 14:44:42 +00:00
|
|
|
|
|
|
|
test('exclude - basename is false + slash', () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
const exclude = ['**/lectus/**/*.html', 'bar.html']
|
2019-12-26 14:44:42 +00:00
|
|
|
const globOptions = { basename: false }
|
|
|
|
hexo.config.minify.html.exclude = exclude
|
|
|
|
hexo.config.minify.html.globOptions = globOptions
|
|
|
|
|
|
|
|
const result = h(input, { path: 'foo/bar.html' })
|
|
|
|
const expected = Htmlminifier(input, hexo.config.minify.html)
|
|
|
|
|
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('null', () => {
|
|
|
|
hexo.config.minify.html.exclude = null
|
|
|
|
hexo.config.minify.html.globOptions = null
|
|
|
|
|
|
|
|
const result = h(input, { path: null })
|
|
|
|
const expected = Htmlminifier(input, hexo.config.minify.html)
|
|
|
|
|
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
2019-12-26 11:11:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('css', () => {
|
|
|
|
const { cssDefault } = require('../index')
|
|
|
|
const c = require('../lib/filter').minifyCss.bind(hexo)
|
|
|
|
const CleanCSS = require('clean-css')
|
2019-12-27 23:12:46 +00:00
|
|
|
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
|
|
|
|
const path = 'foo.css'
|
2019-12-26 11:11:36 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-26 15:24:54 +00:00
|
|
|
hexo.config.minify.css = Object.assign({}, cssDefault)
|
2019-12-26 11:11:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('default', async () => {
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = await c(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const { styles } = await new CleanCSS(hexo.config.minify.css).minify(input)
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(styles)
|
|
|
|
})
|
|
|
|
|
2019-12-26 15:24:54 +00:00
|
|
|
test('disable', async () => {
|
|
|
|
hexo.config.minify.css.enable = false
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = await c(input, { path })
|
|
|
|
|
2019-12-26 15:24:54 +00:00
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('option', async () => {
|
|
|
|
const customOpt = {
|
|
|
|
level: {
|
|
|
|
1: {
|
|
|
|
mergeAdjacentRules: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hexo.config.minify.css = customOpt
|
|
|
|
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = await c(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const { styles } = await new CleanCSS(customOpt).minify(input)
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(styles)
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', async () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.css.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
await c(input, { path })
|
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`css: ${path}`)
|
|
|
|
})
|
|
|
|
|
2019-12-26 15:32:21 +00:00
|
|
|
test('option - invalid', async () => {
|
|
|
|
const customOpt = {
|
|
|
|
level: 9000
|
|
|
|
}
|
|
|
|
hexo.config.minify.css = customOpt
|
|
|
|
|
|
|
|
let result, expected
|
|
|
|
|
|
|
|
try {
|
2019-12-27 23:12:46 +00:00
|
|
|
await c(input, { path })
|
2019-12-26 15:32:21 +00:00
|
|
|
} catch (err) {
|
|
|
|
result = err.message
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await new CleanCSS(customOpt).minify(input)
|
|
|
|
} catch (err) {
|
|
|
|
expected = err.message
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(result).toContain(expected)
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('exclude - *.min.css', async () => {
|
|
|
|
const result = await c(input, { path: 'foo/bar.min.css' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - basename', async () => {
|
|
|
|
const exclude = '*baz.css'
|
|
|
|
hexo.config.minify.css.exclude = exclude
|
|
|
|
const result = await c(input, { path: 'foo/barbaz.css' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - slash in pattern', async () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
const exclude = '**/lectus/**/*.css'
|
2019-12-26 11:11:36 +00:00
|
|
|
hexo.config.minify.css.exclude = exclude
|
2019-12-28 10:26:25 +00:00
|
|
|
const result = await c(input, { path: 'eleifend/lectus/nullam/dapibus/netus.css' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('js', () => {
|
|
|
|
const { jsDefault } = require('../index')
|
|
|
|
const j = require('../lib/filter').minifyJs.bind(hexo)
|
|
|
|
const Terser = require('terser')
|
2019-12-27 23:12:46 +00:00
|
|
|
const input = 'var o = { "foo": 1, bar: 3 };'
|
|
|
|
const path = 'foo.js'
|
2019-12-26 11:11:36 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-26 15:24:54 +00:00
|
|
|
hexo.config.minify.js = Object.assign({}, jsDefault)
|
2019-12-26 11:11:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('default', () => {
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = j(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const { code } = Terser.minify(input, { mangle: jsDefault.mangle })
|
2019-12-27 00:38:44 +00:00
|
|
|
|
|
|
|
expect(result).toBeDefined()
|
|
|
|
expect(code).toBeDefined()
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(code)
|
|
|
|
})
|
|
|
|
|
2019-12-26 15:24:54 +00:00
|
|
|
test('disable', () => {
|
|
|
|
hexo.config.minify.js.enable = false
|
|
|
|
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = j(input, { path })
|
2019-12-26 15:24:54 +00:00
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('option', () => {
|
|
|
|
const customOpt = {
|
|
|
|
mangle: {
|
|
|
|
properties: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hexo.config.minify.js = customOpt
|
|
|
|
|
2019-12-27 23:12:46 +00:00
|
|
|
const result = j(input, { path })
|
2019-12-26 11:11:36 +00:00
|
|
|
const { code } = Terser.minify(input, customOpt)
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(code)
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.js.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
j(input, { path })
|
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`)
|
|
|
|
})
|
|
|
|
|
2019-12-26 15:06:32 +00:00
|
|
|
test('option - invalid', () => {
|
|
|
|
const customOpt = {
|
|
|
|
mangle: {
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hexo.config.minify.js = customOpt
|
|
|
|
|
|
|
|
const { error } = Terser.minify(input, customOpt)
|
|
|
|
try {
|
2019-12-27 23:12:46 +00:00
|
|
|
j(input, { path })
|
2019-12-26 15:06:32 +00:00
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain(error.message)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
test('exclude - *.min.js', () => {
|
|
|
|
const result = j(input, { path: 'foo/bar.min.js' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - basename', () => {
|
|
|
|
const exclude = '*baz.js'
|
|
|
|
hexo.config.minify.js.exclude = exclude
|
|
|
|
const result = j(input, { path: 'foo/barbaz.js' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exclude - slash in pattern', () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
const exclude = '**/lectus/**/*.js'
|
2019-12-26 11:11:36 +00:00
|
|
|
hexo.config.minify.js.exclude = exclude
|
2019-12-28 10:26:25 +00:00
|
|
|
const result = j(input, { path: 'eleifend/lectus/nullam/dapibus/netus.js' })
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-26 11:11:36 +00:00
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
2019-12-27 04:32:03 +00:00
|
|
|
|
|
|
|
describe('svg', () => {
|
|
|
|
const { svgDefault } = require('../index')
|
|
|
|
const s = require('../lib/filter').minifySvg.bind(hexo)
|
|
|
|
const Svgo = require('svgo')
|
|
|
|
const input = '<svg><rect x="1" y="2" width="3" height="4" id="a"/></svg>'
|
2019-12-27 23:12:46 +00:00
|
|
|
const path = 'foo.svg'
|
2019-12-27 04:32:03 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
hexo.config.minify.svg = Object.assign({}, svgDefault)
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
routeList.forEach((path) => hexo.route.remove(path))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('default', async () => {
|
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('disable', async () => {
|
|
|
|
hexo.config.minify.svg.enable = false
|
|
|
|
const result = await s()
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 04:32:03 +00:00
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('option', async () => {
|
|
|
|
const customOpt = [{ cleanupIDs: false }]
|
|
|
|
hexo.config.minify.svg.plugins = customOpt
|
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
expect(result).toContain('id="a"')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', async () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.svg.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
await s()
|
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`svg: ${path}`)
|
|
|
|
})
|
|
|
|
|
2019-12-27 04:32:03 +00:00
|
|
|
test('invalid svg', async () => {
|
|
|
|
const input = '{}'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
let expected
|
|
|
|
try {
|
|
|
|
await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
} catch (err) {
|
|
|
|
expected = err
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await s()
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain(expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - exclude *.min.svg by default', async () => {
|
|
|
|
const path = 'foo.min.svg'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
await s()
|
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - basename', async () => {
|
|
|
|
hexo.config.minify.svg.include = 'bar.svg'
|
2019-12-28 10:10:30 +00:00
|
|
|
const path = 'foo/bar.svg'
|
|
|
|
hexo.route.set(path, input)
|
2019-12-27 04:32:03 +00:00
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const output = hexo.route.get(path)
|
2019-12-27 04:32:03 +00:00
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - slash in pattern', async () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
hexo.config.minify.svg.include = '**/lectus/**/*.svg'
|
|
|
|
const path = 'eleifend/lectus/nullam/dapibus/netus.svg'
|
2019-12-28 10:10:30 +00:00
|
|
|
hexo.route.set(path, input)
|
2019-12-27 04:32:03 +00:00
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const output = hexo.route.get(path)
|
2019-12-27 04:32:03 +00:00
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
2019-12-28 10:10:30 +00:00
|
|
|
|
|
|
|
test('include - basename + slash', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.svg',
|
|
|
|
'gravida/sociis/erat/ante.svg',
|
|
|
|
'aptent/elementum.svg',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.svg.include = [
|
|
|
|
'dolor.svg',
|
|
|
|
'**/sociis/**/*.svg'
|
|
|
|
]
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
|
|
|
const minPaths = paths.slice(0, 2)
|
|
|
|
const unminPaths = paths.slice(2)
|
|
|
|
|
|
|
|
minPaths.forEach((inpath) => {
|
|
|
|
const output = hexo.route.get(inpath)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
unminPaths.forEach((inpath) => {
|
|
|
|
const output = hexo.route.get(inpath)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - reverse pattern + basename disabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.svg',
|
|
|
|
'gravida/sociis/erat/ante.svg',
|
|
|
|
'aptent/elementum.svg',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.svg.include = [
|
|
|
|
'!dolor.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.svg.globOptions = {
|
|
|
|
basename: false
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await s()
|
|
|
|
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
const output = hexo.route.get(inpath)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - empty route', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const result = await s()
|
|
|
|
expect(result.length).toBe(0)
|
|
|
|
})
|
2019-12-27 04:32:03 +00:00
|
|
|
})
|
2019-12-27 05:39:29 +00:00
|
|
|
|
|
|
|
describe('gzip', () => {
|
|
|
|
const { gzipDefault } = require('../index')
|
|
|
|
const g = require('../lib/filter').gzipFn.bind(hexo)
|
|
|
|
const zlib = require('zlib')
|
|
|
|
const gzip = promisify(zlib.gzip)
|
|
|
|
const unzip = promisify(zlib.unzip)
|
|
|
|
const path = 'foo.txt'
|
|
|
|
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
hexo.config.minify.gzip = Object.assign({}, gzipDefault)
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
routeList.forEach((path) => hexo.route.remove(path))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('default', async () => {
|
|
|
|
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, { level: zlib.constants.Z_BEST_COMPRESSION })
|
|
|
|
const resultUnzip = await unzip(result)
|
|
|
|
const expectedUnzip = await unzip(expected)
|
|
|
|
|
2019-12-28 08:42:14 +00:00
|
|
|
expect(result.equals(expected)).toBe(true)
|
2019-12-27 05:39:29 +00:00
|
|
|
expect(resultUnzip.toString()).toBe(input)
|
|
|
|
expect(expectedUnzip.toString()).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('disable', async () => {
|
|
|
|
hexo.config.minify.gzip.enable = false
|
|
|
|
const result = await g()
|
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2019-12-27 07:50:03 +00:00
|
|
|
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)
|
|
|
|
|
2019-12-28 08:42:14 +00:00
|
|
|
expect(result.equals(expected)).toBe(true)
|
2019-12-27 07:50:03 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', async () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.gzip.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
await g()
|
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`gzip: ${path}`)
|
|
|
|
})
|
|
|
|
|
2019-12-27 07:50:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-27 05:39:29 +00:00
|
|
|
test('include - exclude non-text file by default', async () => {
|
|
|
|
const path = 'foo.jpg'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
await g()
|
|
|
|
|
|
|
|
const result = hexo.route.get(path.concat('.gz'))
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - basename', async () => {
|
|
|
|
hexo.config.minify.gzip.include = 'bar.txt'
|
2019-12-28 10:10:30 +00:00
|
|
|
const path = 'foo/bar.txt'
|
|
|
|
hexo.route.set(path, input)
|
2019-12-27 05:39:29 +00:00
|
|
|
await g()
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const result = hexo.route.get(path.concat('.gz'))
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 05:39:29 +00:00
|
|
|
expect(result).toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - slash in pattern', async () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
hexo.config.minify.gzip.include = '**/lectus/**/*.txt'
|
|
|
|
const path = 'eleifend/lectus/nullam/dapibus/netus.txt'
|
2019-12-28 10:10:30 +00:00
|
|
|
hexo.route.set(path, input)
|
2019-12-27 05:39:29 +00:00
|
|
|
await g()
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const result = hexo.route.get(path.concat('.gz'))
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 05:39:29 +00:00
|
|
|
expect(result).toBeDefined()
|
|
|
|
})
|
2019-12-29 02:33:59 +00:00
|
|
|
|
|
|
|
test('include - basename + slash + basename enabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.gzip.include = [
|
|
|
|
'*.html',
|
|
|
|
'**/sociis/**/*.css'
|
|
|
|
]
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await g()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = [
|
|
|
|
'lorem/ipsum/dolor.html.gz',
|
|
|
|
'gravida/sociis/erat/ante.css.gz'
|
|
|
|
]
|
|
|
|
const notExpected = [
|
|
|
|
'aptent/elementum.js.gz',
|
|
|
|
'felis/blandit/cursus.svg.gz'
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
expect(routeList).toEqual(expect.not.arrayContaining(notExpected))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - basename + slash + basename disabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.gzip.include = [
|
|
|
|
'*.html',
|
|
|
|
'**/sociis/**/*.css'
|
|
|
|
]
|
|
|
|
hexo.config.minify.gzip.globOptions = {
|
|
|
|
basename: false
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await g()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = [
|
|
|
|
'gravida/sociis/erat/ante.css.gz'
|
|
|
|
]
|
|
|
|
const notExpected = [
|
|
|
|
'lorem/ipsum/dolor.html.gz',
|
|
|
|
'aptent/elementum.js.gz',
|
|
|
|
'felis/blandit/cursus.svg.gz'
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
expect(routeList).toEqual(expect.not.arrayContaining(notExpected))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - reverse pattern + basename disabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.gzip.include = [
|
|
|
|
'!dolor.html'
|
|
|
|
]
|
|
|
|
hexo.config.minify.gzip.globOptions = {
|
|
|
|
basename: false
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await g()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = paths.map((path) => path.concat('.gz'))
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
})
|
2019-12-27 05:39:29 +00:00
|
|
|
})
|
2019-12-27 05:59:44 +00:00
|
|
|
|
|
|
|
describe('brotli', () => {
|
|
|
|
const { brotliDefault } = require('../index')
|
|
|
|
const b = require('../lib/filter').brotliFn.bind(hexo)
|
|
|
|
const zlib = require('zlib')
|
|
|
|
const brotli = promisify(zlib.brotliCompress)
|
|
|
|
const unbrotli = promisify(zlib.brotliDecompress)
|
|
|
|
const path = 'foo.txt'
|
|
|
|
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
hexo.config.minify.brotli = Object.assign({}, brotliDefault)
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
routeList.forEach((path) => hexo.route.remove(path))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('default', async () => {
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const output = hexo.route.get(path.concat('.br'))
|
|
|
|
const buf = []
|
|
|
|
output.on('data', (chunk) => (buf.push(chunk)))
|
|
|
|
output.on('end', async () => {
|
|
|
|
const result = Buffer.concat(buf)
|
|
|
|
const expected = await brotli(input)
|
|
|
|
const resultUnbr = await unbrotli(result)
|
|
|
|
const expectedUnbr = await unbrotli(expected)
|
|
|
|
|
2019-12-28 08:42:14 +00:00
|
|
|
expect(result.equals(expected)).toBe(true)
|
2019-12-27 05:59:44 +00:00
|
|
|
expect(resultUnbr.toString()).toBe(input)
|
|
|
|
expect(expectedUnbr.toString()).toBe(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('disable', async () => {
|
|
|
|
hexo.config.minify.brotli.enable = false
|
|
|
|
const result = await b()
|
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2019-12-27 08:09:31 +00:00
|
|
|
test('option', async () => {
|
|
|
|
const level = 1
|
|
|
|
hexo.config.minify.brotli.level = level
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const output = hexo.route.get(path.concat('.br'))
|
|
|
|
const buf = []
|
|
|
|
output.on('data', (chunk) => (buf.push(chunk)))
|
|
|
|
output.on('end', async () => {
|
|
|
|
const result = Buffer.concat(buf)
|
|
|
|
const expected = await brotli(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
|
|
|
|
|
2019-12-28 08:42:14 +00:00
|
|
|
expect(result.equals(expected)).toBe(true)
|
2019-12-27 08:09:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-30 05:53:28 +00:00
|
|
|
test('option - verbose', async () => {
|
2019-12-27 22:54:09 +00:00
|
|
|
hexo.config.minify.brotli.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
|
|
|
await b()
|
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`brotli: ${path}`)
|
|
|
|
})
|
|
|
|
|
2019-12-27 09:12:27 +00:00
|
|
|
test('option - level is string', async () => {
|
2019-12-27 07:50:03 +00:00
|
|
|
const level = 'foo'
|
|
|
|
hexo.config.minify.brotli.level = level
|
2019-12-27 09:12:27 +00:00
|
|
|
await b()
|
2019-12-27 07:50:03 +00:00
|
|
|
|
2019-12-27 09:12:27 +00:00
|
|
|
const output = hexo.route.get(path.concat('.br'))
|
|
|
|
const buf = []
|
|
|
|
output.on('data', (chunk) => (buf.push(chunk)))
|
|
|
|
output.on('end', async () => {
|
|
|
|
const result = Buffer.concat(buf)
|
|
|
|
const expected = await brotli(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY } })
|
|
|
|
|
2019-12-28 08:42:14 +00:00
|
|
|
expect(result.equals(expected)).toBe(true)
|
2019-12-27 09:12:27 +00:00
|
|
|
})
|
2019-12-27 07:50:03 +00:00
|
|
|
})
|
|
|
|
|
2019-12-27 05:59:44 +00:00
|
|
|
test('include - exclude non-text file by default', async () => {
|
|
|
|
const path = 'foo.jpg'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const result = hexo.route.get(path.concat('.br'))
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 05:59:44 +00:00
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - basename', async () => {
|
|
|
|
hexo.config.minify.brotli.include = 'bar.txt'
|
2019-12-28 10:10:30 +00:00
|
|
|
const path = 'foo/bar.txt'
|
|
|
|
hexo.route.set(path, input)
|
2019-12-27 05:59:44 +00:00
|
|
|
await b()
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const result = hexo.route.get(path.concat('.br'))
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 05:59:44 +00:00
|
|
|
expect(result).toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - slash in pattern', async () => {
|
2019-12-28 10:26:25 +00:00
|
|
|
hexo.config.minify.brotli.include = '**/lectus/**/*.txt'
|
|
|
|
const path = 'eleifend/lectus/nullam/dapibus/netus.txt'
|
2019-12-28 10:10:30 +00:00
|
|
|
hexo.route.set(path, input)
|
2019-12-27 05:59:44 +00:00
|
|
|
await b()
|
|
|
|
|
2019-12-28 10:10:30 +00:00
|
|
|
const result = hexo.route.get(path.concat('.br'))
|
2019-12-27 23:12:46 +00:00
|
|
|
|
2019-12-27 05:59:44 +00:00
|
|
|
expect(result).toBeDefined()
|
|
|
|
})
|
2019-12-29 02:33:59 +00:00
|
|
|
|
|
|
|
test('include - basename + slash + basename enabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.brotli.include = [
|
|
|
|
'*.html',
|
|
|
|
'**/sociis/**/*.css'
|
|
|
|
]
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = [
|
|
|
|
'lorem/ipsum/dolor.html.br',
|
|
|
|
'gravida/sociis/erat/ante.css.br'
|
|
|
|
]
|
|
|
|
const notExpected = [
|
|
|
|
'aptent/elementum.js.br',
|
|
|
|
'felis/blandit/cursus.svg.br'
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
expect(routeList).toEqual(expect.not.arrayContaining(notExpected))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - basename + slash + basename disabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.brotli.include = [
|
|
|
|
'*.html',
|
|
|
|
'**/sociis/**/*.css'
|
|
|
|
]
|
|
|
|
hexo.config.minify.brotli.globOptions = {
|
|
|
|
basename: false
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = [
|
|
|
|
'gravida/sociis/erat/ante.css.br'
|
|
|
|
]
|
|
|
|
const notExpected = [
|
|
|
|
'lorem/ipsum/dolor.html.br',
|
|
|
|
'aptent/elementum.js.br',
|
|
|
|
'felis/blandit/cursus.svg.br'
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
expect(routeList).toEqual(expect.not.arrayContaining(notExpected))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - reverse pattern + basename disabled', async () => {
|
|
|
|
hexo.route.remove(path)
|
|
|
|
|
|
|
|
const paths = [
|
|
|
|
'lorem/ipsum/dolor.html',
|
|
|
|
'gravida/sociis/erat/ante.css',
|
|
|
|
'aptent/elementum.js',
|
|
|
|
'felis/blandit/cursus.svg'
|
|
|
|
]
|
|
|
|
hexo.config.minify.brotli.include = [
|
|
|
|
'!dolor.html'
|
|
|
|
]
|
|
|
|
hexo.config.minify.brotli.globOptions = {
|
|
|
|
basename: false
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.forEach((inpath) => {
|
|
|
|
hexo.route.set(inpath, input)
|
|
|
|
})
|
|
|
|
await b()
|
|
|
|
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
const expected = paths.map((path) => path.concat('.br'))
|
|
|
|
|
|
|
|
expect(routeList).toEqual(expect.arrayContaining(expected))
|
|
|
|
})
|
2019-12-27 05:59:44 +00:00
|
|
|
})
|