Skip to content

Commit

Permalink
Fix plugins order (#1334)
Browse files Browse the repository at this point in the history
Ref #1333

While refactoring broke plugin order.
  • Loading branch information
TrySound committed Feb 18, 2021
1 parent efa62c8 commit 082aee0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
31 changes: 15 additions & 16 deletions lib/svgo/plugins.js
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/svgo/_index.js
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion test/svgo/multipass.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/svgo/plugins-order.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 082aee0

Please sign in to comment.