2020-01-03 00:51:45 +00:00
|
|
|
/* eslint-env jest */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Hexo = require('hexo')
|
2021-10-10 09:19:50 +00:00
|
|
|
const { optimize: svgOptimize } = require('svgo')
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
describe('svg', () => {
|
2020-09-05 11:18:35 +00:00
|
|
|
const hexo = new Hexo(__dirname)
|
|
|
|
const s = require('../lib/filter').minifySvg.bind(hexo)
|
|
|
|
const input = '<svg><rect x="1" y="2" width="3" height="4" id="a"/></svg>'
|
|
|
|
const path = 'foo.svg'
|
2021-10-10 09:19:50 +00:00
|
|
|
// svgo's plugins option
|
|
|
|
let plugins = [{
|
|
|
|
name: 'preset-default',
|
|
|
|
params: {
|
|
|
|
overrides: {}
|
|
|
|
}
|
|
|
|
}]
|
2020-09-05 11:18:35 +00:00
|
|
|
|
2020-01-03 00:51:45 +00:00
|
|
|
beforeEach(() => {
|
2020-09-05 11:18:35 +00:00
|
|
|
hexo.config.minify = {
|
|
|
|
svg: {
|
|
|
|
enable: true,
|
|
|
|
verbose: false,
|
|
|
|
include: ['*.svg', '!*.min.svg'],
|
2021-10-10 09:19:50 +00:00
|
|
|
plugins: {},
|
2020-09-05 11:18:35 +00:00
|
|
|
globOptions: { basename: true }
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 09:19:50 +00:00
|
|
|
plugins = [{
|
|
|
|
name: 'preset-default',
|
|
|
|
params: {
|
|
|
|
overrides: {}
|
|
|
|
}
|
|
|
|
}]
|
2020-01-03 00:51:45 +00:00
|
|
|
hexo.route.set(path, input)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
const routeList = hexo.route.list()
|
|
|
|
routeList.forEach((path) => hexo.route.remove(path))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('default', async () => {
|
|
|
|
await s()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('empty file', async () => {
|
|
|
|
hexo.route.set(path, '')
|
|
|
|
const result = await s()
|
|
|
|
|
|
|
|
// empty file resolves to an array of undefined
|
|
|
|
expect(result).toBeDefined()
|
|
|
|
expect(result[0]).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('option', async () => {
|
2021-10-10 09:19:50 +00:00
|
|
|
const customOpt = {
|
2022-11-06 02:54:55 +00:00
|
|
|
cleanupIds: false
|
2021-10-10 09:19:50 +00:00
|
|
|
}
|
2020-01-03 00:51:45 +00:00
|
|
|
hexo.config.minify.svg.plugins = customOpt
|
2021-10-10 09:19:50 +00:00
|
|
|
plugins = [{
|
|
|
|
name: 'preset-default',
|
|
|
|
params: {
|
|
|
|
overrides: customOpt
|
|
|
|
}
|
|
|
|
}]
|
2020-01-03 00:51:45 +00:00
|
|
|
await s()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
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"')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('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}`)
|
|
|
|
})
|
|
|
|
|
2021-04-25 08:25:32 +00:00
|
|
|
test('option - plugins - invalid', async () => {
|
|
|
|
hexo.config.minify.svg.plugins = 'invalid'
|
|
|
|
await s()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2021-04-25 08:25:32 +00:00
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-01-03 00:51:45 +00:00
|
|
|
test('invalid svg', async () => {
|
|
|
|
const input = '{}'
|
|
|
|
hexo.route.set(path, input)
|
2021-04-25 08:25:32 +00:00
|
|
|
|
2022-11-06 02:54:55 +00:00
|
|
|
let expected
|
|
|
|
try {
|
|
|
|
svgOptimize(input, { plugins })
|
|
|
|
} catch (err) {
|
|
|
|
expected = err
|
|
|
|
}
|
|
|
|
expect(expected).toBeDefined()
|
|
|
|
await expect(s()).rejects.toThrow(`Path: ${path}\n${expected}`)
|
2020-01-03 00:51:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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'
|
|
|
|
const path = 'foo/bar.svg'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
await s()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('include - slash in pattern', async () => {
|
|
|
|
hexo.config.minify.svg.include = '**/lectus/**/*.svg'
|
|
|
|
const path = 'eleifend/lectus/nullam/dapibus/netus.svg'
|
|
|
|
hexo.route.set(path, input)
|
|
|
|
await s()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
const output = hexo.route.get(path)
|
|
|
|
let result = ''
|
|
|
|
output.on('data', (chunk) => (result += chunk))
|
|
|
|
output.on('end', () => {
|
|
|
|
expect(result).toBe(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
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()
|
2021-10-10 09:19:50 +00:00
|
|
|
const { data } = svgOptimize(input, { plugins })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|