From 7582d3130e5b6eb0f962e283f956a84552f839a6 Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Sun, 9 Jan 2022 22:56:41 +0100 Subject: [PATCH] fix(plugin-svgo): handle potential errors from optimize (#663) --- 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..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