From cb7e9be623b6e2fbbfcb9b67c4c85131e1477925 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 2 Nov 2021 10:30:05 +0300 Subject: [PATCH] Add warning about enabling plugins in presets --- lib/svgo.test.js | 32 ++++++++++++++++++++++++++++++++ lib/svgo/plugins.js | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/svgo.test.js b/lib/svgo.test.js index 21be404c4..7fece445d 100644 --- a/lib/svgo.test.js +++ b/lib/svgo.test.js @@ -66,6 +66,38 @@ test('allow to disable and customize plugins in preset', () => { `); }); +test('warn when user tries enable plugins in preset', () => { + const svg = ` + + `; + const warn = jest.spyOn(console, 'warn'); + optimize(svg, { + plugins: [ + { + name: 'preset-default', + params: { + overrides: { + cleanupListOfValues: true, + }, + }, + }, + ], + js2svg: { pretty: true, indent: 2 }, + }); + expect(warn) + .toBeCalledWith(`You are trying to enable cleanupListOfValues which is not part of preset. +Try to put it before or after preset, for example + +plugins: [ + { + name: 'preset-default', + }, + 'cleanupListOfValues' +] +`); + warn.mockRestore(); +}); + describe('allow to configure EOL', () => { test('should respect EOL set to LF', () => { const svg = ` diff --git a/lib/svgo/plugins.js b/lib/svgo/plugins.js index 0d55efbf0..db15ea7ad 100644 --- a/lib/svgo/plugins.js +++ b/lib/svgo/plugins.js @@ -86,6 +86,22 @@ const createPreset = ({ name, plugins }) => { if (floatPrecision != null) { globalOverrides.floatPrecision = floatPrecision; } + if (overrides) { + for (const [pluginName, override] of Object.entries(overrides)) { + if (override === true) { + console.warn( + `You are trying to enable ${pluginName} which is not part of preset.\n` + + `Try to put it before or after preset, for example\n\n` + + `plugins: [\n` + + ` {\n` + + ` name: 'preset-default',\n` + + ` },\n` + + ` 'cleanupListOfValues'\n` + + `]\n` + ); + } + } + } return invokePlugins(ast, info, plugins, overrides, globalOverrides); }, };