Skip to content

Commit 7582d31

Browse files
authoredJan 9, 2022
fix(plugin-svgo): handle potential errors from optimize (#663)
1 parent 0927303 commit 7582d31

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎packages/plugin-svgo/src/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ describe('svgo', () => {
5151
expect(result).toMatchSnapshot()
5252
})
5353

54+
it('throws error on invalid svg input', () => {
55+
const errorSvg = `<?xml version="1.0" encoding="UTF-8"?>
56+
<svg width="88px" height="88px" viewBox="0 0 88 88" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
57+
<path d=M51,37 L37,51" id="Shape" class="shape"></path>
58+
</svg>`
59+
60+
expect(() =>
61+
svgo(
62+
errorSvg,
63+
{ svgo: true, runtimeConfig: true },
64+
{ ...state, filePath: path.join(__dirname, '../__fixtures__/svgo') },
65+
),
66+
).toThrowError()
67+
})
68+
5469
it('uses `state.filePath` to detect configuration', () => {
5570
const result = svgo(
5671
baseSvg,

‎packages/plugin-svgo/src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import type { Plugin } from '@svgr/core'
55
const svgoPlugin: Plugin = (code, config, state) => {
66
if (!config.svgo) return code
77
const svgoConfig = getSvgoConfig(config, state)
8-
const { data } = optimize(code, { ...svgoConfig, path: state.filePath })
9-
return data
8+
const result = optimize(code, { ...svgoConfig, path: state.filePath })
9+
10+
if (result.modernError) {
11+
throw result.modernError
12+
}
13+
14+
return result.data
1015
}
1116

1217
export default svgoPlugin

1 commit comments

Comments
 (1)

vercel[bot] commented on Jan 9, 2022

@vercel[bot]
Please sign in to comment.