From dfdadc7547e3b026da161731d47b0935c477dea2 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Wed, 27 Sep 2023 15:53:00 +0100 Subject: [PATCH] fix: warn and do not crash on null plugin --- lib/svgo.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/svgo.js b/lib/svgo.js index 8c5ca130f..6dba24523 100644 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -64,13 +64,22 @@ const optimize = (input, config) => { for (let i = 0; i < maxPassCount; i += 1) { info.multipassCount = i; const ast = parseSvg(input, config.path); + /** @type {unknown[]|unknown} */ const plugins = config.plugins || ['preset-default']; - if (Array.isArray(plugins) === false) { + if (!Array.isArray(plugins)) { throw Error( - "Invalid plugins list. Provided 'plugins' in config should be an array." + 'malformed config, `plugins` property must be an array.\nSee more info here: https://github.com/svg/svgo#configuration' + ); + } + const resolvedPlugins = plugins + .filter((plugin) => plugin != null) + .map(resolvePluginConfig); + + if (resolvedPlugins.length < plugins.length) { + console.warn( + 'Warning: plugins list includes null or undefined elements, these will be ignored.' ); } - const resolvedPlugins = plugins.map(resolvePluginConfig); const globalOverrides = {}; if (config.floatPrecision != null) { globalOverrides.floatPrecision = config.floatPrecision;