The optimized configuration is an object in JSON format and currently supports three attributes: rules, params, and browsers.
Here is an example of using optimized configuration:
const svgSlim = require('svg-slim');
const userConfig = {
"rules": {
"collapse-g": false,
"combine-transform": false,
"rm-attribute": [true, {
"keepAria": true,
"keepEvent": false
}]
},
"params": {
"trifuncDigit": 3,
"sizeDigit": 2,
"angelDigit": 2
},
"browsers": ["> 1%", "not ie 11", "not firefox < 99"]
};
svgSlim(svgcode, userConfig);
params currently supports the following configuration items
Property | Type | Default Value | Remarks |
---|---|---|---|
sizeDigit | number | 2 | Accuracy of displacement data |
angelDigit | number | 2 | Accuracy of angle data |
trifuncDigit | number | 3 | Accuracy of trigonometric function data |
opacityDigit | number | 3 | Accuracy of opacity data |
thinning | number | 0 | Optimize the path by thinning out nodes, 0 means no thinning out, and greater than 0 means thinning out the node threshold |
straighten | number | 0 | Convert a small-size curve to a straight line, 0 means no optimization is performed, and greater than 0 means the threshold of the curve to a straight line |
mergePoint | number | 0 | Whether to merge points with similar distances in the path, 0 means that this optimization is not performed |
rmAttrEqDefault | boolean | true | Whether to remove the same style as the default value |
ignoreKnownCSS | boolean | false | Whether to retain the CSS style that cannot be used as property (Warning!, many CSS3 properties are not in this list, but they are still valid for SVG elements in some modern browsers) |
Browsers configuration depends on browserslist, you can pass in a string/string list here, or configure the "browserslist" property in package.json
The following is a complete list of optimization rules. Some optimization rules contain additional configuration items.
All rules are turned on by default, and you can turn them off by optimizing the configured rules.
Name | Configuration Item | Remarks |
---|---|---|
apply-style | None | Apply style to target element |
collapse-g | None | Collapse unnecessary g elements |
collapse-textwrap | None | Collapse unnecessary text nodes |
combine-path | { disregardFill: boolean, disregardOpacity: boolean } | Combine adjacent path elements |
combine-transform | None | Combine transform function |
compute-path | None | Calculate and optimize the path |
rm-attribute | { keepAria: boolean, keepEvent: boolean } | Remove unnecessary attributes |
rm-comments | None | Remove comment |
rm-doctype | None | Remove DocType |
rm-hidden | None | Remove invisible content |
rm-important | None | Remove unnecessary !important in the style sheet |
rm-illegal-style | None | Remove the irregular style attribute |
rm-irregular-nesting | { ignore: string[] } | Remove irregular nesting |
rm-irregular-tag | { ignore: string[] } | Remove irregular tags |
rm-px | None | Remove px unit |
rm-unnecessary | { tags: string[] } | Remove unnecessary svg elements |
rm-version | None | Remove the version attribute of svg |
rm-viewbox | None | Optimize or remove the viewbox attribute of svg |
rm-xml-decl | None | Remove xml declaration node |
rm-xmlns | None | Optimize and remove unnecessary xml namespace |
shorten-animate | { remove: boolean } | Optimize animation elements |
shorten-class | None | Optimization className |
shorten-color | { rrggbbaa: boolean } | Optimize color |
shorten-decimal-digits | None | Optimize numeric attributes |
shorten-defs | None | Optimize the content under the defs node |
shorten-filter | None | Optimize filter elements |
shorten-id | None | Optimization ID |
shorten-shape | None | Optimize graphic elements |
shorten-style-attr | None | Optimize the style attribute |
shorten-style-tag | None | Optimize style tag |
style-to-class | None | Create className for repeated style attributes |