diff --git a/lib/svgo/plugins.js b/lib/svgo/plugins.js index 21a850280..26656d126 100644 --- a/lib/svgo/plugins.js +++ b/lib/svgo/plugins.js @@ -11,32 +11,31 @@ * @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) { + if (prev && plugin.type == prev[0].type) { + prev.push(plugin); + } else { + prev = [plugin]; + groups.push(prev); + } + } + for (const group of groups) { + switch(group[0].type) { case 'perItem': - perItemPlugins.push(plugin); + data = perItem(data, info, group); break; case 'perItemReverse': - perItemReversePlugins.push(plugin); + data = perItem(data, info, group, true); break; case 'full': - fullPlugins.push(plugin); + data = full(data, info, group); break; } } - 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); - } return data; }; diff --git a/package-lock.json b/package-lock.json index c03e9692f..587aad8db 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 b2fd2169f..c4d9868d1 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 424324b0b..b206be32e 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 000000000..aecd05c02 --- /dev/null +++ b/test/svgo/plugins-order.svg @@ -0,0 +1,11 @@ + + + + + +@@@ + +