Skip to content

Commit

Permalink
Merge f7a7a12 into fe8a370
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Feb 24, 2021
2 parents fe8a370 + f7a7a12 commit b36c537
Show file tree
Hide file tree
Showing 17 changed files with 892 additions and 1,515 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [8, 10, 12, 14]
node: [10, 12, 14, 15]

steps:
- name: Clone repository
Expand All @@ -36,6 +36,9 @@ jobs:
- name: Install npm dependencies
run: npm ci

- name: Run lint
run: npm run lint

- name: Run tests
run: npm test
if: matrix.node != env.NODE_COV
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,9 +1,9 @@
*.md.html
**/*.md.html
/.idea
/node_modules
/node_modules/
/npm-debug.log
/coverage
/tmp
/coverage/
/tmp/
/FEATURES.md
/dev-scripts/
14 changes: 8 additions & 6 deletions bin/svg-sprite.js
Expand Up @@ -107,14 +107,16 @@ function addConfigMap(store, path, value) {
*/
function mergeConfig(from, to) {
for (var f in from) {
if (_.isObject(from[f])) {
if (!_.isObject(to[f])) {
to[f] = from[f];
if (Object.prototype.hasOwnProperty.call(from, f)) {
if (Object.prototype.hasOwnProperty.call(to, f) && _.isObject(from[f])) {
if (!_.isObject(to[f])) {
to[f] = from[f];
} else {
mergeConfig(from[f], to[f]);
}
} else {
mergeConfig(from[f], to[f]);
to[f] = from[f];
}
} else {
to[f] = from[f];
}
}
}
Expand Down
75 changes: 22 additions & 53 deletions lib/svg-sprite/transform/svgo.js
Expand Up @@ -10,54 +10,18 @@
* @license MIT https://raw.github.com/jkphl/svg-sprite/master/LICENSE
*/

var SVGO = require('svgo'),
var svgo = require('svgo'),
_ = require('lodash'),
pretty = require('prettysize'),
defaultPluginConfig = [

// {cleanupAttrs : true}, // cleanup attributes from newlines, trailing, and repeating spaces
// {removeDoctype : true}, // remove doctype declaration
// {removeXMLProcInst : true}, // remove XML processing instructions
// {removeComments : true}, // remove comments
// {removeMetadata : true}, // remove <metadata>
// {removeTitle : true}, // remove <title>
// {removeDesc : true}, // remove <desc>
// {removeUselessDefs : true}, // remove elements of <defs> without id
// {removeXMLNS : true}, // removes xmlns attribute (for inline svg, disabled by default)
// {removeEditorsNSData : true}, // remove editors namespaces, elements, and attributes
// {removeEmptyAttrs : true}, // remove empty attributes
// {removeHiddenElems : true}, // remove hidden elements
// {removeEmptyText : true}, // remove empty Text elements
// {removeEmptyContainers : true}, // remove empty Container elements
// {removeViewBox : true}, // remove viewBox attribute when possible
// {cleanupEnableBackground : true}, // remove or cleanup enable-background attribute when possible
// {minifyStyles : false}, // minify <style> elements content with CSSO
// {convertStyleToAttrs : false}, // convert styles into attributes
{ inlineStyles: false }, // Move <style> definitions to inline style attributes where possible
// {convertColors : true}, // convert colors (from rgb() to #rrggbb, from #rrggbb to #rgb)
// {convertPathData : true}, // convert Path data to relative or absolute (whichever is shorter), convert one segment to another, trim useless delimiters, smart rounding, and much more
// {convertTransform : true}, // collapse multiple transforms into one, convert matrices to the short aliases, and much more
// {removeUnknownsAndDefaults : true}, // remove unknown elements content and attributes, remove attrs with default values
// {removeNonInheritableGroupAttrs : true}, // remove non-inheritable group's "presentation" attributes
// {removeUselessStrokeAndFill : true}, // remove useless stroke and fill attrs
// {removeUnusedNS : true}, // remove unused namespaces declaration
// {cleanupIDs : true}, // remove unused and minify used IDs
// {cleanupNumericValues : true}, // round numeric values to the fixed precision, remove default px units
// {cleanupListOfValues : true}, // round numeric values in attributes that take a list of numbers (like viewBox or enable-background)
// {moveElemsAttrsToGroup : true}, // move elements' attributes to their enclosing group
{ moveGroupAttrsToElems: true }, // move some group attributes to the contained elements
// {collapseGroups : true}, // collapse useless groups
// {removeRasterImages : true}, // remove raster images (disabled by default)
// {mergePaths : true}, // merge multiple Paths into one
// {convertShapeToPath : true}, // convert some basic shapes to <path>
// {sortAttrs : true}, // sort element attributes for epic readability (disabled by default)
// {removeDimensions : true}, // remove width/height attributes if viewBox is present (opposite to removeViewBox, disable it first) (disabled by default)
// {removeAttrs : true}, // remove attributes by pattern (disabled by default)
// {removeElementsByAttr : true}, // remove arbitrary elements by ID or className (disabled by default)
// {addClassesToSVGElement : true}, // add classnames to an outer <svg> element (disabled by default)
// {addAttributesToSVGElement : true}, // adds attributes to an outer <svg> element (disabled by default)
// {removeStyleElement : false}, // remove <style> elements (disabled by default)
// {removeScriptElement : true}, // remove <script> elements (disabled by default)
{
name: 'inlineStyles',
active: false
},
{
name: 'moveGroupAttrsToElems',
active: true
}
];

/**
Expand All @@ -70,15 +34,20 @@ var SVGO = require('svgo'),
*/
module.exports = function (shape, config, spriter, cb) {
config = _.cloneDeep(config);
config.plugins = ('plugins' in config) ? defaultPluginConfig.concat(config.plugins) : defaultPluginConfig;
config.plugins.push({ removeXMLProcInst: !!spriter.config.svg.xmlDeclaration });
config.plugins.push({ removeDoctype: !!spriter.config.svg.doctypeDeclaration });
config.plugins = svgo.extendDefaultPlugins('plugins' in config ? defaultPluginConfig.concat(config.plugins) : defaultPluginConfig);
config.plugins.push({
name: 'removeXMLProcInst',
active: !!spriter.config.svg.xmlDeclaration
}, {
name: 'removeDoctype',
active: !!spriter.config.svg.doctypeDeclaration
});

var svg = shape.getSVG(false),
svgLength = svg.length,
svgoInstance = new SVGO(config);
svgLength = svg.length;

svgoInstance.optimize(svg).then(function (result) {
try {
var result = svgo.optimize(svg, config);
shape.setSVG(result.data);
var optSVGLength = null;
for (var t = 0, tl = spriter.config.log.transports.length; t < tl; ++t) {
Expand All @@ -89,9 +58,9 @@ module.exports = function (shape, config, spriter, cb) {
}

cb(null);
}).catch(function (error) {
} catch (error) {
spriter.error('Optimizing "%s" with SVGO failed with error "%s"', shape.name, error);

cb(error);
});
}
};

0 comments on commit b36c537

Please sign in to comment.