fix(svg): compatibility with svgo 2.4+

- overriding default plugins
- https://github.com/svg/svgo/releases/tag/v2.4.0
This commit is contained in:
MDLeom 2021-10-10 09:19:50 +00:00
parent be96f15ceb
commit c0041b0f23
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
4 changed files with 61 additions and 30 deletions

View File

@ -27,22 +27,32 @@ Yet Another Minifier for Hexo. Minify and compress HTML, JS, CSS, SVG, XML and J
- [Globbing](#globbing)
- [HTTP Compression](#http-compression)
## Version 5
In v5, `svg.plugins:` option should follow svgo v2+ syntax:
## Version 6
In v6, `svg.plugins:` option should be the following syntax:
``` diff
minify:
svg:
plugins:
# v6
+ removeComments: false
+ cleanupIDs: false
+ builtinPluginName:
+ optionName: 'optionValue'
# v5
- - name: 'removeComments'
- active: false
- - name: 'cleanupIDs'
- active: false
# v4
- - removeComments: false
- - cleanupIDs: false
+ - name: 'removeComments'
+ active: false
+ - name: 'cleanupIDs'
+ active: false
```
The option only overrides svgo's default plugins, other options are not supported.
## Installation
``` bash
$ npm install hexo-yam --save
@ -148,11 +158,9 @@ minify:
``` yaml
plugins:
# Retain comments
- name: 'removeComments'
active: false
removeComments: false
# Do not remove unused ID attributes
- name: 'cleanupIDs'
active: false
cleanupIDs: false
```
- For more options, see [svgo](https://github.com/svg/svgo).
- **globOptions** - See [globbing](#globbing) section.

View File

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

View File

@ -3,7 +3,7 @@
const { minify: htmlMinify } = require('html-minifier')
const CleanCSS = require('clean-css')
const { minify: terserMinify } = require('terser')
const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')
const { optimize: svgOptimize } = require('svgo')
const zlib = require('zlib')
const { promisify } = require('util')
const gzip = promisify(zlib.gzip)
@ -136,7 +136,14 @@ function minifySvg () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include, verbose } = options
const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([])
// const plugins = Array.isArray(options.plugins) ? extendDefaultPlugins(options.plugins) : extendDefaultPlugins([])
const pluginCfg = Object.prototype.toString.call(options.plugins) === '[object Object]' ? { ...options.plugins } : {}
const plugins = [{
name: 'preset-default',
params: {
overrides: pluginCfg
}
}]
return Promise.all((match(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => {

View File

@ -2,13 +2,20 @@
'use strict'
const Hexo = require('hexo')
const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')
const { optimize: svgOptimize } = require('svgo')
describe('svg', () => {
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'
// svgo's plugins option
let plugins = [{
name: 'preset-default',
params: {
overrides: {}
}
}]
beforeEach(() => {
hexo.config.minify = {
@ -16,10 +23,16 @@ describe('svg', () => {
enable: true,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: extendDefaultPlugins([]),
plugins: {},
globOptions: { basename: true }
}
}
plugins = [{
name: 'preset-default',
params: {
overrides: {}
}
}]
hexo.route.set(path, input)
})
@ -30,7 +43,7 @@ describe('svg', () => {
test('default', async () => {
await s()
const { data } = svgOptimize(input, hexo.config.minify.svg)
const { data } = svgOptimize(input, { plugins })
const output = hexo.route.get(path)
let result = ''
@ -57,13 +70,18 @@ describe('svg', () => {
})
test('option', async () => {
const customOpt = [{
name: 'cleanupIDs',
active: false
}]
const customOpt = {
cleanupIDs: false
}
hexo.config.minify.svg.plugins = customOpt
plugins = [{
name: 'preset-default',
params: {
overrides: customOpt
}
}]
await s()
const { data } = svgOptimize(input, { ...hexo.config.minify.svg, plugins: extendDefaultPlugins(customOpt) })
const { data } = svgOptimize(input, { plugins })
const output = hexo.route.get(path)
let result = ''
@ -85,7 +103,7 @@ describe('svg', () => {
test('option - plugins - invalid', async () => {
hexo.config.minify.svg.plugins = 'invalid'
await s()
const { data } = svgOptimize(input, { ...hexo.config.minify.svg, plugins: extendDefaultPlugins([]) })
const { data } = svgOptimize(input, { plugins })
const output = hexo.route.get(path)
let result = ''
@ -99,7 +117,7 @@ describe('svg', () => {
const input = '{}'
hexo.route.set(path, input)
const { error } = svgOptimize(input, hexo.config.minify.svg)
const { error } = svgOptimize(input, { plugins })
expect(error).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${error}`)
@ -123,7 +141,7 @@ describe('svg', () => {
const path = 'foo/bar.svg'
hexo.route.set(path, input)
await s()
const { data } = svgOptimize(input, hexo.config.minify.svg)
const { data } = svgOptimize(input, { plugins })
const output = hexo.route.get(path)
let result = ''
@ -138,7 +156,7 @@ describe('svg', () => {
const path = 'eleifend/lectus/nullam/dapibus/netus.svg'
hexo.route.set(path, input)
await s()
const { data } = svgOptimize(input, hexo.config.minify.svg)
const { data } = svgOptimize(input, { plugins })
const output = hexo.route.get(path)
let result = ''
@ -166,7 +184,7 @@ describe('svg', () => {
hexo.route.set(inpath, input)
})
await s()
const { data } = svgOptimize(input, hexo.config.minify.svg)
const { data } = svgOptimize(input, { plugins })
const minPaths = paths.slice(0, 2)
const unminPaths = paths.slice(2)
@ -210,7 +228,7 @@ describe('svg', () => {
hexo.route.set(inpath, input)
})
await s()
const { data } = svgOptimize(input, hexo.config.minify.svg)
const { data } = svgOptimize(input, { plugins })
paths.forEach((inpath) => {
const output = hexo.route.get(inpath)