From 082aee027ddc2033bc3fdb48de5ba2a086eaed26 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 18 Feb 2021 17:46:28 +0300 Subject: [PATCH] Fix plugins order (#1334) Ref https://github.com/svg/svgo/issues/1333 While refactoring broke plugin order. --- lib/svgo/plugins.js | 41 ++++++++++++++++++------------------- package-lock.json | 2 +- test/svgo/_index.js | 5 +++++ test/svgo/multipass.svg | 2 +- test/svgo/plugins-order.svg | 11 ++++++++++ 5 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 test/svgo/plugins-order.svg diff --git a/lib/svgo/plugins.js b/lib/svgo/plugins.js index 21a85028..26656d12 100644 --- a/lib/svgo/plugins.js +++ b/lib/svgo/plugins.js @@ -11,31 +11,30 @@ * @return {Object} output data */ module.exports = function(data, info, plugins) { - const perItemPlugins = []; - const perItemReversePlugins = []; - const fullPlugins = []; // Try to group sequential elements of plugins array + // to optimize ast traversing + const groups = []; + let prev; for (const plugin of plugins) { - switch(plugin.type) { - case 'perItem': - perItemPlugins.push(plugin); - break; - case 'perItemReverse': - perItemReversePlugins.push(plugin); - break; - case 'full': - fullPlugins.push(plugin); - break; + if (prev && plugin.type == prev[0].type) { + prev.push(plugin); + } else { + prev = [plugin]; + groups.push(prev); } } - if (perItemPlugins.length !== 0) { - data = perItem(data, info, perItemPlugins); - } - if (perItemReversePlugins.length !== 0) { - data = perItem(data, info, perItemReversePlugins, true); - } - if (fullPlugins.length !== 0) { - data = full(data, info, fullPlugins); + for (const group of groups) { + switch(group[0].type) { + case 'perItem': + data = perItem(data, info, group); + break; + case 'perItemReverse': + data = perItem(data, info, group, true); + break; + case 'full': + data = full(data, info, group); + break; + } } return data; }; diff --git a/package-lock.json b/package-lock.json index c03e9692..587aad8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "svgo", - "version": "1.3.2", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/svgo/_index.js b/test/svgo/_index.js index b2fd2169..c4d9868d 100644 --- a/test/svgo/_index.js +++ b/test/svgo/_index.js @@ -46,4 +46,9 @@ describe('svgo', () => { }); expect(normalize(result.data)).to.equal(expected); }); + it('should handle plugins order properly', async () => { + const [original, expected] = await parseFixture('plugins-order.svg'); + const result = optimize(original, { input: 'file', path: 'input.svg' }); + expect(normalize(result.data)).to.equal(expected); + }); }); diff --git a/test/svgo/multipass.svg b/test/svgo/multipass.svg index 424324b0..b206be32 100644 --- a/test/svgo/multipass.svg +++ b/test/svgo/multipass.svg @@ -1,7 +1,7 @@ - + diff --git a/test/svgo/plugins-order.svg b/test/svgo/plugins-order.svg new file mode 100644 index 00000000..aecd05c0 --- /dev/null +++ b/test/svgo/plugins-order.svg @@ -0,0 +1,11 @@ + + + + + +@@@ + +