Merge pull request #51 from curbengh/svgo-2

chore(deps): bump svgo from 1.3.2 to 2.3.0 (BREAKING CHANGE)
This commit is contained in:
Ming Di Leom 2021-04-25 18:04:24 +09:30 committed by GitHub
commit 6c21cc6a9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 25 deletions

View File

@ -140,7 +140,16 @@ minify:
- **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array. - **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- Exclude `*.min.svg` by default. - Exclude `*.min.svg` by default.
- **plugins** - Plugin options. - **plugins** - Plugin options.
- Example: to retain comments, `plugins: [{removeComments: false}]`. - Examples:
``` yaml
plugins:
# Retain comments
- name: 'removeComments'
active: false
# Do not remove unused ID attributes
- name: 'cleanupIDs'
active: false
```
- For more options, see [svgo](https://github.com/svg/svgo). - For more options, see [svgo](https://github.com/svg/svgo).
- **globOptions** - See [globbing](#globbing) section. - **globOptions** - See [globbing](#globbing) section.

View File

@ -1,6 +1,8 @@
/* global hexo */ /* global hexo */
'use strict' 'use strict'
const { extendDefaultPlugins } = require('svgo')
hexo.config.minify = Object.assign({ hexo.config.minify = Object.assign({
enable: true enable: true
}, hexo.config.minify) }, hexo.config.minify)
@ -48,7 +50,7 @@ hexo.config.minify.svg = Object.assign({
priority: 10, priority: 10,
verbose: false, verbose: false,
include: ['*.svg', '!*.min.svg'], include: ['*.svg', '!*.min.svg'],
plugins: [], plugins: extendDefaultPlugins([]),
globOptions: { basename: true } globOptions: { basename: true }
}, hexo.config.minify.svg) }, hexo.config.minify.svg)

View File

@ -3,7 +3,7 @@
const { minify: htmlMinify } = require('html-minifier') const { minify: htmlMinify } = require('html-minifier')
const CleanCSS = require('clean-css') const CleanCSS = require('clean-css')
const { minify: terserMinify } = require('terser') const { minify: terserMinify } = require('terser')
const Svgo = require('svgo') const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util') const { promisify } = require('util')
const gzip = promisify(zlib.gzip) const gzip = promisify(zlib.gzip)
@ -136,6 +136,7 @@ function minifySvg () {
const { route } = hexo const { route } = hexo
const routeList = route.list() const routeList = route.list()
const { globOptions, include, verbose } = options const { globOptions, include, verbose } = options
const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([])
return Promise.all((match(routeList, include, globOptions)).map((path) => { return Promise.all((match(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -144,12 +145,12 @@ function minifySvg () {
assetPath.on('data', (chunk) => (assetTxt += chunk)) assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { const { data, error } = svgOptimize(assetTxt, { ...options, plugins })
const { data } = await new Svgo(options).optimize(assetTxt) if (data) {
if (verbose) logFn.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) { } else if (error) {
reject(new Error(`Path: ${path}\n${err}`)) reject(new Error(`Path: ${path}\n${error}`))
} }
} }
resolve() resolve()

View File

@ -27,7 +27,7 @@
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"micromatch": "^4.0.2", "micromatch": "^4.0.2",
"minify-xml": "^2.1.1", "minify-xml": "^2.1.1",
"svgo": "^1.2.2", "svgo": "^2.3.0",
"terser": "^5.3.0" "terser": "^5.3.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -2,7 +2,7 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const Svgo = require('svgo') const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')
describe('svg', () => { describe('svg', () => {
const hexo = new Hexo(__dirname) const hexo = new Hexo(__dirname)
@ -16,7 +16,7 @@ describe('svg', () => {
enable: true, enable: true,
verbose: false, verbose: false,
include: ['*.svg', '!*.min.svg'], include: ['*.svg', '!*.min.svg'],
plugins: [], plugins: extendDefaultPlugins([]),
globOptions: { basename: true } globOptions: { basename: true }
} }
} }
@ -30,7 +30,7 @@ describe('svg', () => {
test('default', async () => { test('default', async () => {
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, hexo.config.minify.svg)
const output = hexo.route.get(path) const output = hexo.route.get(path)
let result = '' let result = ''
@ -57,10 +57,13 @@ describe('svg', () => {
}) })
test('option', async () => { test('option', async () => {
const customOpt = [{ cleanupIDs: false }] const customOpt = [{
name: 'cleanupIDs',
active: false
}]
hexo.config.minify.svg.plugins = customOpt hexo.config.minify.svg.plugins = customOpt
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, { ...hexo.config.minify.svg, plugins: extendDefaultPlugins(customOpt) })
const output = hexo.route.get(path) const output = hexo.route.get(path)
let result = '' let result = ''
@ -79,17 +82,27 @@ describe('svg', () => {
expect(hexo.log.log.mock.calls[0][0]).toContain(`svg: ${path}`) expect(hexo.log.log.mock.calls[0][0]).toContain(`svg: ${path}`)
}) })
test('option - plugins - invalid', async () => {
hexo.config.minify.svg.plugins = 'invalid'
await s()
const { data } = svgOptimize(input, { ...hexo.config.minify.svg, plugins: extendDefaultPlugins([]) })
const output = hexo.route.get(path)
let result = ''
output.on('data', (chunk) => (result += chunk))
output.on('end', () => {
expect(result).toBe(data)
})
})
test('invalid svg', async () => { test('invalid svg', async () => {
const input = '{}' const input = '{}'
hexo.route.set(path, input) hexo.route.set(path, input)
let expected
try { const { error } = svgOptimize(input, hexo.config.minify.svg)
await new Svgo(hexo.config.minify.svg).optimize(input)
} catch (err) { expect(error).toBeDefined()
expected = err await expect(s()).rejects.toThrow(`Path: ${path}\n${error}`)
}
expect(expected).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${expected}`)
}) })
test('include - exclude *.min.svg by default', async () => { test('include - exclude *.min.svg by default', async () => {
@ -110,7 +123,7 @@ describe('svg', () => {
const path = 'foo/bar.svg' const path = 'foo/bar.svg'
hexo.route.set(path, input) hexo.route.set(path, input)
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, hexo.config.minify.svg)
const output = hexo.route.get(path) const output = hexo.route.get(path)
let result = '' let result = ''
@ -125,7 +138,7 @@ describe('svg', () => {
const path = 'eleifend/lectus/nullam/dapibus/netus.svg' const path = 'eleifend/lectus/nullam/dapibus/netus.svg'
hexo.route.set(path, input) hexo.route.set(path, input)
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, hexo.config.minify.svg)
const output = hexo.route.get(path) const output = hexo.route.get(path)
let result = '' let result = ''
@ -153,7 +166,7 @@ describe('svg', () => {
hexo.route.set(inpath, input) hexo.route.set(inpath, input)
}) })
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, hexo.config.minify.svg)
const minPaths = paths.slice(0, 2) const minPaths = paths.slice(0, 2)
const unminPaths = paths.slice(2) const unminPaths = paths.slice(2)
@ -197,7 +210,7 @@ describe('svg', () => {
hexo.route.set(inpath, input) hexo.route.set(inpath, input)
}) })
await s() await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) const { data } = svgOptimize(input, hexo.config.minify.svg)
paths.forEach((inpath) => { paths.forEach((inpath) => {
const output = hexo.route.get(inpath) const output = hexo.route.get(inpath)