Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking-change(plugin-svgo): handle potential errors from optimize #663

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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