Skip to content

Commit

Permalink
Drop plugin by path support and load builtins statically
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Feb 15, 2021
1 parent 8899f68 commit 2965e8c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
35 changes: 6 additions & 29 deletions lib/svgo/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

var FS = require('fs');
var PATH = require('path');
var yaml = require('js-yaml');
const FS = require('fs');
const PATH = require('path');
const yaml = require('js-yaml');
const pluginsMap = require('../../plugins/plugins.js');

/**
* Read and/or extend/replace default config file,
Expand Down Expand Up @@ -79,7 +80,7 @@ function preparePluginsArray(config, plugins) {
} else {

plugin = setPluginActiveState(
loadPlugin(config, key, item[key].path),
{ ...pluginsMap[key] },
item,
key
);
Expand All @@ -89,7 +90,7 @@ function preparePluginsArray(config, plugins) {
// name
} else {

plugin = loadPlugin(config, item);
plugin = { ...pluginsMap[item] };
plugin.name = item;
if (typeof plugin.params === 'object') {
plugin.params = Object.assign({}, plugin.params);
Expand Down Expand Up @@ -132,10 +133,6 @@ function extendConfig(defaults, config) {
if (typeof item[key] === 'object' && item[key].fn && typeof item[key].fn === 'function') {
defaults.plugins.push(setupCustomPlugin(key, item[key]));

// plugin defined via path
} else if (typeof item[key] === 'object' && item[key].path) {
defaults.plugins.push(setPluginActiveState(loadPlugin(config, undefined, item[key].path), item, key));

} else {
defaults.plugins.forEach(function(plugin) {

Expand Down Expand Up @@ -228,23 +225,3 @@ function setPluginActiveState(plugin, item, key) {

return plugin;
}

/**
* Loads default plugin using name or custom plugin defined via path in config.
*
* @param {Object} config
* @param {Object} name
* @param {Object} path
* @return {Object} plugin
*/
function loadPlugin(config, name, path) {
var plugin;

if (!path) {
plugin = require('../../plugins/' + name);
} else {
plugin = require(PATH.resolve(config.__DIR, path));
}

return Object.assign({}, plugin);
}
49 changes: 49 additions & 0 deletions plugins/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
exports.addAttributesToSVGElement = require('./addAttributesToSVGElement.js');
exports.addClassesToSVGElement = require('./addClassesToSVGElement.js');
exports.cleanupAttrs = require('./cleanupAttrs.js');
exports.cleanupEnableBackground = require('./cleanupEnableBackground.js');
exports.cleanupIDs = require('./cleanupIDs.js');
exports.cleanupListOfValues = require('./cleanupListOfValues.js');
exports.cleanupNumericValues = require('./cleanupNumericValues.js');
exports.collapseGroups = require('./collapseGroups.js');
exports.convertColors = require('./convertColors.js');
exports.convertEllipseToCircle = require('./convertEllipseToCircle.js');
exports.convertPathData = require('./convertPathData.js');
exports.convertShapeToPath = require('./convertShapeToPath.js');
exports.convertStyleToAttrs = require('./convertStyleToAttrs.js');
exports.convertTransform = require('./convertTransform.js');
exports.inlineStyles = require('./inlineStyles.js');
exports.mergePaths = require('./mergePaths.js');
exports.minifyStyles = require('./minifyStyles.js');
exports.moveElemsAttrsToGroup = require('./moveElemsAttrsToGroup.js');
exports.moveGroupAttrsToElems = require('./moveGroupAttrsToElems.js');
exports.prefixIds = require('./prefixIds.js');
exports.removeAttributesBySelector = require('./removeAttributesBySelector.js');
exports.removeAttrs = require('./removeAttrs.js');
exports.removeComments = require('./removeComments.js');
exports.removeDesc = require('./removeDesc.js');
exports.removeDimensions = require('./removeDimensions.js');
exports.removeDoctype = require('./removeDoctype.js');
exports.removeEditorsNSData = require('./removeEditorsNSData.js');
exports.removeElementsByAttr = require('./removeElementsByAttr.js');
exports.removeEmptyAttrs = require('./removeEmptyAttrs.js');
exports.removeEmptyContainers = require('./removeEmptyContainers.js');
exports.removeEmptyText = require('./removeEmptyText.js');
exports.removeHiddenElems = require('./removeHiddenElems.js');
exports.removeMetadata = require('./removeMetadata.js');
exports.removeNonInheritableGroupAttrs = require('./removeNonInheritableGroupAttrs.js');
exports.removeOffCanvasPaths = require('./removeOffCanvasPaths.js');
exports.removeRasterImages = require('./removeRasterImages.js');
exports.removeScriptElement = require('./removeScriptElement.js');
exports.removeStyleElement = require('./removeStyleElement.js');
exports.removeTitle = require('./removeTitle.js');
exports.removeUnknownsAndDefaults = require('./removeUnknownsAndDefaults.js');
exports.removeUnusedNS = require('./removeUnusedNS.js');
exports.removeUselessDefs = require('./removeUselessDefs.js');
exports.removeUselessStrokeAndFill = require('./removeUselessStrokeAndFill.js');
exports.removeViewBox = require('./removeViewBox.js');
exports.removeXMLNS = require('./removeXMLNS.js');
exports.removeXMLProcInst = require('./removeXMLProcInst.js');
exports.reusePaths = require('./reusePaths.js');
exports.sortAttrs = require('./sortAttrs.js');
exports.sortDefsChildren = require('./sortDefsChildren.js');
7 changes: 4 additions & 3 deletions plugins/removeOffCanvasPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ exports.active = false;

exports.description = 'removes elements that are drawn outside of the viewbox (disabled by default)';

var SVGO = require('../lib/svgo.js'),
_path = require('./_path.js'),
const JSAPI = require('../lib/svgo/jsAPI.js');

var _path = require('./_path.js'),
intersects = _path.intersects,
path2js = _path.path2js,
viewBox,
Expand Down Expand Up @@ -97,7 +98,7 @@ function parseViewBox(svg)
bottom: parseFloat(m[2]) + parseFloat(m[4])
};

var path = new SVGO().createContentItem({
var path = new JSAPI({
elem: 'path',
prefix: '',
local: 'path'
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('plugins tests', function() {

file = PATH.resolve(__dirname, file);

it(name + '.' + index, function() {
it.only(name + '.' + index, function() {

return readFile(file)
.then(function(data) {
Expand Down

0 comments on commit 2965e8c

Please sign in to comment.