Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugins order #1334

Merged
merged 1 commit into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.