Skip to content

Commit

Permalink
Implement default plugins and plugins list extending
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Feb 15, 2021
1 parent c9c7871 commit b1dafc6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 81 deletions.
75 changes: 0 additions & 75 deletions .svgo.yml

This file was deleted.

17 changes: 12 additions & 5 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
* @license MIT https://raw.githubusercontent.com/svg/svgo/master/LICENSE
*/

const { resolvePluginConfig } = require('./svgo/config.js');
const {
defaultPlugins,
resolvePluginConfig,
extendDefaultPlugins
} = require('./svgo/config.js');
const svg2js = require('./svgo/svg2js.js');
const js2svg = require('./svgo/js2svg.js');
const invokePlugins = require('./svgo/plugins.js');
const JSAPI = require('./svgo/jsAPI.js');
const { encodeSVGDatauri } = require('./svgo/tools.js');

exports.extendDefaultPlugins = extendDefaultPlugins;

const optimize = (svgstr, config = {}) => {
if (typeof config !== 'object') {
throw Error('Config should be an object')
Expand All @@ -30,12 +36,13 @@ const optimize = (svgstr, config = {}) => {
}
for (let i = 0; i < maxPassCount; i += 1) {
svgjs = svg2js(svgstr);
if (svgjs.error == null && config.plugins) {
if (Array.isArray(config.plugins) === false) {
if (svgjs.error == null) {
const plugins = config.plugins || defaultPlugins;
if (Array.isArray(plugins) === false) {
throw Error('Invalid plugins list. Provided \'plugins\' in config should be an array.');
}
const plugins = config.plugins.map(plugin => resolvePluginConfig(plugin, config))
svgjs = invokePlugins(svgjs, info, plugins);
const resolvedPlugins = plugins.map(plugin => resolvePluginConfig(plugin, config))
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
}
svgjs = js2svg(svgjs, config.js2svg);
if (svgjs.error) {
Expand Down
69 changes: 69 additions & 0 deletions lib/svgo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,75 @@

const pluginsMap = require('../../plugins/plugins.js');

const pluginsOrder = [
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeMetadata',
'removeXMLNS',
'removeEditorsNSData',
'cleanupAttrs',
'inlineStyles',
'minifyStyles',
'convertStyleToAttrs',
'cleanupIDs',
'prefixIds',
'removeRasterImages',
'removeUselessDefs',
'cleanupNumericValues',
'cleanupListOfValues',
'convertColors',
'removeUnknownsAndDefaults',
'removeNonInheritableGroupAttrs',
'removeUselessStrokeAndFill',
'removeViewBox',
'cleanupEnableBackground',
'removeHiddenElems',
'removeEmptyText',
'convertShapeToPath',
'convertEllipseToCircle',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'collapseGroups',
'convertPathData',
'convertTransform',
'removeEmptyAttrs',
'removeEmptyContainers',
'mergePaths',
'removeUnusedNS',
'sortAttrs',
'sortDefsChildren',
'removeTitle',
'removeDesc',
'removeDimensions',
'removeAttrs',
'removeAttributesBySelector',
'removeElementsByAttr',
'addClassesToSVGElement',
'removeStyleElement',
'removeScriptElement',
'addAttributesToSVGElement',
'removeOffCanvasPaths',
'reusePaths',
];
const defaultPlugins = pluginsOrder.filter(name => pluginsMap[name].active);
exports.defaultPlugins = defaultPlugins;

const extendDefaultPlugins = (plugins) => {
const extendedPlugins = pluginsOrder.map(name => ({ name, ...pluginsMap[name] }));
for (const plugin of plugins) {
const resolvedPlugin = resolvePluginConfig(plugin, {});
const index = pluginsOrder.indexOf(resolvedPlugin.name);
if (index === -1) {
extendedPlugins.push(plugin);
} else {
extendedPlugins[index] = plugin;
}
}
return extendedPlugins;
}
exports.extendDefaultPlugins = extendDefaultPlugins;

const resolvePluginConfig = (plugin, config) => {
let configParams = {};
if ('floatPrecision' in config) {
Expand Down
43 changes: 42 additions & 1 deletion test/config/_index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

const { expect } = require('chai');
const { resolvePluginConfig } = require('../../lib/svgo/config.js');
const {
resolvePluginConfig,
extendDefaultPlugins
} = require('../../lib/svgo/config.js');

describe('config', function() {

Expand Down Expand Up @@ -141,6 +144,44 @@ describe('config', function() {

});

describe('allows to extend default plugins list', () => {
const extendedPlugins = extendDefaultPlugins([
{
name: 'customPlugin',
fn: () => {},
},
{
name: 'removeAttrs',
params: { atts: ['aria-label'] },
},
{
name: 'cleanupIDs',
params: { remove: false },
},
]);
const removeAttrsIndex = extendedPlugins.findIndex(item => item.name === 'removeAttrs');
const cleanupIDsIndex = extendedPlugins.findIndex(item => item.name === 'cleanupIDs');
it('should preserve internal plugins order', () => {
expect(removeAttrsIndex).to.equal(40);
expect(cleanupIDsIndex).to.equal(10);
});
it('should activate inactive by default plugins', () => {
const removeAttrsPlugin = resolvePluginConfig(extendedPlugins[removeAttrsIndex], {});
const cleanupIDsPlugin = resolvePluginConfig(extendedPlugins[cleanupIDsIndex], {});
expect(removeAttrsPlugin.active).to.equal(true);
expect(cleanupIDsPlugin.active).to.equal(true);
});
it('should leave not extended inactive plugins to be inactive', () => {
const inactivePlugin = resolvePluginConfig(
extendedPlugins.find(item => item.name === 'addClassesToSVGElement'),
{},
);
expect(inactivePlugin.active).to.equal(false);
});
it('should put custom plugins in the end', () => {
expect(extendedPlugins[extendedPlugins.length - 1].name).to.equal('customPlugin');
});
});
});

function getPlugin(name, plugins) {
Expand Down

0 comments on commit b1dafc6

Please sign in to comment.