diff --git a/packages/plugin-svgo/src/index.test.ts b/packages/plugin-svgo/src/index.test.ts index 63e2c712..ca1dd4f6 100644 --- a/packages/plugin-svgo/src/index.test.ts +++ b/packages/plugin-svgo/src/index.test.ts @@ -51,6 +51,21 @@ describe('svgo', () => { expect(result).toMatchSnapshot() }) + it('throws error on invalid svg input', () => { + const errorSvg = ` + + + ` + + 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, diff --git a/packages/plugin-svgo/src/index.ts b/packages/plugin-svgo/src/index.ts index 38a1e26a..f36c58b5 100644 --- a/packages/plugin-svgo/src/index.ts +++ b/packages/plugin-svgo/src/index.ts @@ -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