Skip to content

Commit

Permalink
fix(plugin-svgo): handle potential errors from optimize (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr authored Jan 9, 2022
1 parent 0927303 commit 7582d31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/plugin-svgo/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ describe('svgo', () => {
expect(result).toMatchSnapshot()
})

it('throws error on invalid svg input', () => {
const errorSvg = `<?xml version="1.0" encoding="UTF-8"?>
<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">
<path d=M51,37 L37,51" id="Shape" class="shape"></path>
</svg>`

expect(() =>
svgo(
errorSvg,
{ svgo: true, runtimeConfig: true },
{ ...state, filePath: path.join(__dirname, '../__fixtures__/svgo') },
),
).toThrowError()
})

it('uses `state.filePath` to detect configuration', () => {
const result = svgo(
baseSvg,
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-svgo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import type { Plugin } from '@svgr/core'
const svgoPlugin: Plugin = (code, config, state) => {
if (!config.svgo) return code
const svgoConfig = getSvgoConfig(config, state)
const { data } = optimize(code, { ...svgoConfig, path: state.filePath })
return data
const result = optimize(code, { ...svgoConfig, path: state.filePath })

if (result.modernError) {
throw result.modernError
}

return result.data
}

export default svgoPlugin

1 comment on commit 7582d31

@vercel
Copy link

@vercel vercel bot commented on 7582d31 Jan 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.