From 6d4d132b5ed03cafa7fed072dda4bb1c8e0da1ee Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Fri, 7 Jan 2022 09:35:59 +0100 Subject: [PATCH 1/2] breaking-change(plugin-svgo): handle potential errors from optimize --- packages/plugin-svgo/src/index.test.ts | 15 +++++++++++++++ packages/plugin-svgo/src/index.ts | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) 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..1a992736 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 ('error' in result) { + throw result.modernError + } + + return result.data } export default svgoPlugin From 8a3eaf6cbae293ec4f4219da08d36ee6e1cbad19 Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Fri, 7 Jan 2022 14:02:42 +0100 Subject: [PATCH 2/2] refactor: change to updated types from DT --- packages/plugin-svgo/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-svgo/src/index.ts b/packages/plugin-svgo/src/index.ts index 1a992736..f36c58b5 100644 --- a/packages/plugin-svgo/src/index.ts +++ b/packages/plugin-svgo/src/index.ts @@ -7,7 +7,7 @@ const svgoPlugin: Plugin = (code, config, state) => { const svgoConfig = getSvgoConfig(config, state) const result = optimize(code, { ...svgoConfig, path: state.filePath }) - if ('error' in result) { + if (result.modernError) { throw result.modernError }