Skip to content

Commit

Permalink
Merge pull request #51 from curbengh/svgo-2
Browse files Browse the repository at this point in the history
chore(deps): bump svgo from 1.3.2 to 2.3.0 (BREAKING CHANGE)
  • Loading branch information
curbengh authored Apr 25, 2021
2 parents 4f03371 + 3819909 commit 6c21cc6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ minify:
- **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- Exclude `*.min.svg` by default.
- **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).
- **globOptions** - See [globbing](#globbing) section.

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global hexo */
'use strict'

const { extendDefaultPlugins } = require('svgo')

hexo.config.minify = Object.assign({
enable: true
}, hexo.config.minify)
Expand Down Expand Up @@ -48,7 +50,7 @@ hexo.config.minify.svg = Object.assign({
priority: 10,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
plugins: extendDefaultPlugins([]),
globOptions: { basename: true }
}, hexo.config.minify.svg)

Expand Down
11 changes: 6 additions & 5 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { minify: htmlMinify } = require('html-minifier')
const CleanCSS = require('clean-css')
const { minify: terserMinify } = require('terser')
const Svgo = require('svgo')
const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')
const zlib = require('zlib')
const { promisify } = require('util')
const gzip = promisify(zlib.gzip)
Expand Down Expand Up @@ -136,6 +136,7 @@ function minifySvg () {
const { route } = hexo
const routeList = route.list()
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 new Promise((resolve, reject) => {
Expand All @@ -144,12 +145,12 @@ function minifySvg () {
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => {
if (assetTxt.length) {
try {
const { data } = await new Svgo(options).optimize(assetTxt)
const { data, error } = svgOptimize(assetTxt, { ...options, plugins })
if (data) {
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
resolve(route.set(path, data))
} catch (err) {
reject(new Error(`Path: ${path}\n${err}`))
} else if (error) {
reject(new Error(`Path: ${path}\n${error}`))
}
}
resolve()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"html-minifier": "^4.0.0",
"micromatch": "^4.0.2",
"minify-xml": "^2.1.1",
"svgo": "^1.2.2",
"svgo": "^2.3.0",
"terser": "^5.3.0"
},
"devDependencies": {
Expand Down
47 changes: 30 additions & 17 deletions test/svg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict'

const Hexo = require('hexo')
const Svgo = require('svgo')
const { optimize: svgOptimize, extendDefaultPlugins } = require('svgo')

describe('svg', () => {
const hexo = new Hexo(__dirname)
Expand All @@ -16,7 +16,7 @@ describe('svg', () => {
enable: true,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
plugins: extendDefaultPlugins([]),
globOptions: { basename: true }
}
}
Expand All @@ -30,7 +30,7 @@ describe('svg', () => {

test('default', async () => {
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)
let result = ''
Expand All @@ -57,10 +57,13 @@ describe('svg', () => {
})

test('option', async () => {
const customOpt = [{ cleanupIDs: false }]
const customOpt = [{
name: 'cleanupIDs',
active: false
}]
hexo.config.minify.svg.plugins = customOpt
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)
let result = ''
Expand All @@ -79,17 +82,27 @@ describe('svg', () => {
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 () => {
const input = '{}'
hexo.route.set(path, input)
let expected
try {
await new Svgo(hexo.config.minify.svg).optimize(input)
} catch (err) {
expected = err
}
expect(expected).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${expected}`)

const { error } = svgOptimize(input, hexo.config.minify.svg)

expect(error).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${error}`)
})

test('include - exclude *.min.svg by default', async () => {
Expand All @@ -110,7 +123,7 @@ describe('svg', () => {
const path = 'foo/bar.svg'
hexo.route.set(path, input)
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)
let result = ''
Expand All @@ -125,7 +138,7 @@ describe('svg', () => {
const path = 'eleifend/lectus/nullam/dapibus/netus.svg'
hexo.route.set(path, input)
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)
let result = ''
Expand Down Expand Up @@ -153,7 +166,7 @@ describe('svg', () => {
hexo.route.set(inpath, input)
})
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 unminPaths = paths.slice(2)
Expand Down Expand Up @@ -197,7 +210,7 @@ describe('svg', () => {
hexo.route.set(inpath, input)
})
await s()
const { data } = await new Svgo(hexo.config.minify.svg).optimize(input)
const { data } = svgOptimize(input, hexo.config.minify.svg)

paths.forEach((inpath) => {
const output = hexo.route.get(inpath)
Expand Down

0 comments on commit 6c21cc6

Please sign in to comment.