Skip to content

Commit

Permalink
fix(svg): svgo@3 now throws error
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 6, 2022
1 parent 9c8a228 commit a6e2ea0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ minify:
# Retain comments
removeComments: false
# Do not remove unused ID attributes
cleanupIDs: false
cleanupIds: false
```
- For more options, see [svgo](https://github.com/svg/svgo).
- **globOptions** - See [globbing](#globbing) section.
Expand Down
16 changes: 10 additions & 6 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ function minifySvg () {
assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => {
if (assetTxt.length) {
const { data, error } = svgOptimize(assetTxt, { ...options, plugins })
if (data) {
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
resolve(route.set(path, data))
} else if (error) {
reject(new Error(`Path: ${path}\n${error}`))
try {
const { data } = svgOptimize(assetTxt, { ...options, plugins })
if (data) {
if (verbose) logFn.call(this, assetTxt, data, path, 'svg')
resolve(route.set(path, data))
} else {
reject(new Error(`Path: ${path}\nEmpty svg input`))
}
} catch (err) {
reject(new Error(`Path: ${path}\n${err}`))
}
}
resolve()
Expand Down
14 changes: 9 additions & 5 deletions test/svg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('svg', () => {

test('option', async () => {
const customOpt = {
cleanupIDs: false
cleanupIds: false
}
hexo.config.minify.svg.plugins = customOpt
plugins = [{
Expand Down Expand Up @@ -117,10 +117,14 @@ describe('svg', () => {
const input = '{}'
hexo.route.set(path, input)

const { error } = svgOptimize(input, { plugins })

expect(error).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${error}`)
let expected
try {
svgOptimize(input, { plugins })
} catch (err) {
expected = err
}
expect(expected).toBeDefined()
await expect(s()).rejects.toThrow(`Path: ${path}\n${expected}`)
})

test('include - exclude *.min.svg by default', async () => {
Expand Down

0 comments on commit a6e2ea0

Please sign in to comment.