Skip to content

Commit 7582d31

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

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 7 additions & 2 deletions
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

0 commit comments

Comments
 (0)