mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Drop extendDefaultPlugins
This commit is contained in:
@ -4,13 +4,8 @@ const os = require('os');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { pathToFileURL } = require('url');
|
const { pathToFileURL } = require('url');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {
|
const { optimize: optimizeAgnostic, createContentItem } = require('./svgo.js');
|
||||||
extendDefaultPlugins,
|
|
||||||
optimize: optimizeAgnostic,
|
|
||||||
createContentItem,
|
|
||||||
} = require('./svgo.js');
|
|
||||||
|
|
||||||
exports.extendDefaultPlugins = extendDefaultPlugins;
|
|
||||||
exports.createContentItem = createContentItem;
|
exports.createContentItem = createContentItem;
|
||||||
|
|
||||||
const importConfig = async (configFile) => {
|
const importConfig = async (configFile) => {
|
||||||
|
@ -1,18 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const { defaultPlugins, resolvePluginConfig } = require('./svgo/config.js');
|
||||||
defaultPlugins,
|
|
||||||
resolvePluginConfig,
|
|
||||||
extendDefaultPlugins,
|
|
||||||
} = require('./svgo/config.js');
|
|
||||||
const { parseSvg } = require('./parser.js');
|
const { parseSvg } = require('./parser.js');
|
||||||
const { stringifySvg } = require('./stringifier.js');
|
const { stringifySvg } = require('./stringifier.js');
|
||||||
const { invokePlugins } = require('./svgo/plugins.js');
|
const { invokePlugins } = require('./svgo/plugins.js');
|
||||||
const JSAPI = require('./svgo/jsAPI.js');
|
const JSAPI = require('./svgo/jsAPI.js');
|
||||||
const { encodeSVGDatauri } = require('./svgo/tools.js');
|
const { encodeSVGDatauri } = require('./svgo/tools.js');
|
||||||
|
|
||||||
exports.extendDefaultPlugins = extendDefaultPlugins;
|
|
||||||
|
|
||||||
const optimize = (input, config) => {
|
const optimize = (input, config) => {
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
config = {};
|
config = {};
|
||||||
|
@ -57,42 +57,6 @@ const pluginsOrder = [
|
|||||||
const defaultPlugins = pluginsOrder.filter((name) => pluginsMap[name].active);
|
const defaultPlugins = pluginsOrder.filter((name) => pluginsMap[name].active);
|
||||||
exports.defaultPlugins = defaultPlugins;
|
exports.defaultPlugins = defaultPlugins;
|
||||||
|
|
||||||
const extendDefaultPlugins = (plugins) => {
|
|
||||||
console.warn(
|
|
||||||
'\n"extendDefaultPlugins" utility is deprecated.\n' +
|
|
||||||
'Use "preset-default" plugin with overrides instead.\n' +
|
|
||||||
'For example:\n' +
|
|
||||||
`{\n` +
|
|
||||||
` name: 'preset-default',\n` +
|
|
||||||
` params: {\n` +
|
|
||||||
` overrides: {\n` +
|
|
||||||
` // customize plugin options\n` +
|
|
||||||
` convertShapeToPath: {\n` +
|
|
||||||
` convertArcs: true\n` +
|
|
||||||
` },\n` +
|
|
||||||
` // disable plugins\n` +
|
|
||||||
` convertPathData: false\n` +
|
|
||||||
` }\n` +
|
|
||||||
` }\n` +
|
|
||||||
`}\n`
|
|
||||||
);
|
|
||||||
const extendedPlugins = pluginsOrder.map((name) => ({
|
|
||||||
name,
|
|
||||||
active: pluginsMap[name].active,
|
|
||||||
}));
|
|
||||||
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) => {
|
const resolvePluginConfig = (plugin) => {
|
||||||
let configParams = {};
|
let configParams = {};
|
||||||
if (typeof plugin === 'string') {
|
if (typeof plugin === 'string') {
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const { resolvePluginConfig } = require('../../lib/svgo/config.js');
|
||||||
resolvePluginConfig,
|
|
||||||
extendDefaultPlugins,
|
|
||||||
} = require('../../lib/svgo/config.js');
|
|
||||||
|
|
||||||
describe('config', function () {
|
describe('config', function () {
|
||||||
describe('extend config with object', function () {
|
describe('extend config with object', function () {
|
||||||
@ -123,54 +120,6 @@ 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).toEqual(41);
|
|
||||||
expect(cleanupIDsIndex).toEqual(11);
|
|
||||||
});
|
|
||||||
it('should activate inactive by default plugins', () => {
|
|
||||||
const removeAttrsPlugin = resolvePluginConfig(
|
|
||||||
extendedPlugins[removeAttrsIndex]
|
|
||||||
);
|
|
||||||
const cleanupIDsPlugin = resolvePluginConfig(
|
|
||||||
extendedPlugins[cleanupIDsIndex]
|
|
||||||
);
|
|
||||||
expect(removeAttrsPlugin.active).toEqual(true);
|
|
||||||
expect(cleanupIDsPlugin.active).toEqual(true);
|
|
||||||
});
|
|
||||||
it('should leave not extended inactive plugins to be inactive', () => {
|
|
||||||
const inactivePlugin = resolvePluginConfig(
|
|
||||||
extendedPlugins.find((item) => item.name === 'addClassesToSVGElement')
|
|
||||||
);
|
|
||||||
expect(inactivePlugin.active).toEqual(false);
|
|
||||||
});
|
|
||||||
it('should put custom plugins in the end', () => {
|
|
||||||
expect(extendedPlugins[extendedPlugins.length - 1].name).toEqual(
|
|
||||||
'customPlugin'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function getPlugin(name, plugins) {
|
function getPlugin(name, plugins) {
|
||||||
|
Reference in New Issue
Block a user