Skip to content

Commit 94566d3

Browse files
committed
more complete configuration
1 parent b9ab572 commit 94566d3

File tree

3 files changed

+79
-75
lines changed

3 files changed

+79
-75
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ If you want your SVG assets uncompressed, you can temporarily disable the Plugin
1919

2020
If for some reason you’re not happy with the default settings we’ve chosen, you can select **Plugins › SVGO Compressor › About SVGO Compressor** and then click the **Edit SVGO Settings…** button. You default editor will open the `svgo.json` file, where you’ll be able to tweak the settings.
2121

22-
## SVGO Plugins: what they do
22+
### Top level settings
23+
24+
- `pretty`: Make SVG pretty printed
25+
- `indent`: Indent number when pretty printing SVGs
26+
- `multipass`: Run the optimization 10 times
27+
- `floatPrecision`: The precision of the numbers to use in all the plugins
28+
29+
### SVGO Plugins
2330

2431
#### addAttributesToSVGElement
2532

src/defaultConfig.js

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,7 @@
55
// So, for every SVGO Plugin you want to use, you'll have to add an object to svgoJSON.plugins,
66
// with a 'name' key and a 'params' key (optional, only needed if you want to change any of the default params for a plugin)
77
//
8-
// This is the list of SVGO Plugins available as of 2016-05-26:
9-
// TODO: keep this list updated automatically
10-
//
11-
// - addAttributesToSVGElement - adds attributes to an outer <svg> element
12-
// - addClassesToSVGElement - add classnames to an outer <svg> element
13-
// - cleanupAttrs - cleanup attributes from newlines, trailing and repeating spaces
14-
// - cleanUpEnableBackground - remove or cleanup enable-background attribute when possible
15-
// - cleanupIDs - remove unused and minify used IDs
16-
// - cleanupListOfValues - round numeric values in attributes that take a list of numbers, like viewBox or enableBackground
17-
// - cleanupNumericValues - round numeric values to the fixed precision, remove default 'px' units
18-
// - collapseGroups - collapse useless groups
19-
// - convertColors - convert colors (from rgb() to #rrggbb, from #rrggbb to #rgb)
20-
// - convertPathData - convert Path data to relative or absolute whichever is shorter, convert one segment to another, trim useless delimiters, smart rounding and much more
21-
// - convertShapeToPath - convert some basic shapes to path
22-
// - convertStyleToAttrs - convert styles into attributes
23-
// - convertTransform - collapse multiple transforms into one, convert matrices to the short aliases and much more
24-
// - mergePaths - merge multiple Paths into one
25-
// - minifyStyles - minify <style> elements content with CSSO
26-
// - moveElemsAttrsToGroup - move elements attributes to the existing group wrapper
27-
// - moveGroupAttrsToElems - move some group attributes to the content elements
28-
// - removeAttrs - remove attributes by pattern
29-
// - removeComments - remove comments
30-
// - removeDesc - remove <desc> (only non-meaningful by default)
31-
// - removeDimensions - remove width/height attributes if viewBox is present
32-
// - removeDoctype - remove doctype declaration
33-
// - removeEditorsNSData - remove editors namespaces, elements and attributes
34-
// - removeEmptyAttrs - remove empty attributes
35-
// - removeEmptyContainers - remove empty Container elements
36-
// - removeEmptyText - remove empty Text elements
37-
// - removeHiddenElems - remove hidden elements
38-
// - removeMetadata - remove <metadata>
39-
// - removeNonInheritableGroupAttrs - remove non-inheritable group's "presentation" attributes
40-
// - removeRasterImages - remove raster images
41-
// - removeStyleElement - remove <style> elements
42-
// - removeTitle - remove <title>
43-
// - removeUnknownsAndDefaults - remove unknown elements content and attributes, remove attrs with default values
44-
// - removeUnusedNS - remove unused namespaces declaration
45-
// - removeUselessDefs - remove elements of <defs> without id
46-
// - removeUselessStrokeAndFill - remove useless stroke and fill attrs
47-
// - removeViewBox - remove viewBox attribute when possible
48-
// - removeXMLProcInst - remove XML processing instructions
49-
// - sortAttrs - sort element attributes for epic readability
50-
// - transformsWithOnePath - apply transforms, crop by real width, center vertical alignment and resize SVG with one Path inside
8+
// This is the list of SVGO Plugins available taken from https://github.com/svg/svgo/blob/master/.svgo.yml. The order is important!s
519
//
5210
// These are some sane defaults we've found to work reasonably well, compressing your SVG without
5311
// altering the shapes or the look. Your milleage may vary, so feel free to tweak these…
@@ -56,31 +14,51 @@ module.exports = {
5614
"comment": "This is the settings file for the SVGO Compressor Plugin. For more info, please check <https://github.com/BohemianCoding/svgo-compressor>",
5715
"pretty": true,
5816
"indent": 2,
17+
"floatPrecision": 3,
5918
"plugins":[
60-
{ "name": "cleanupIDs" },
61-
{ "name": "cleanupListOfValues" },
62-
{ "name": "cleanupNumericValues" },
63-
{ "name": "collapseGroups" },
64-
{ "name": "convertColors" },
65-
{ "name": "convertStyleToAttrs" },
66-
{ "name": "convertTransform" },
67-
{ "name": "mergePaths" },
68-
{ "name": "minifyStyles" },
69-
{ "name": "removeComments" },
70-
{ "name": "removeDesc", "params": { "removeAny": true } },
71-
{ "name": "removeDoctype" },
72-
{ "name": "removeEditorsNSData" },
73-
{ "name": "removeEmptyAttrs" },
74-
{ "name": "removeEmptyContainers" },
75-
{ "name": "removeEmptyText" },
76-
{ "name": "removeMetadata" },
77-
{ "name": "removeNonInheritableGroupAttrs" },
78-
{ "name": "removeTitle" },
79-
{ "name": "removeUnknownsAndDefaults" },
80-
{ "name": "removeUnusedNS" },
81-
{ "name": "removeUselessDefs" },
82-
{ "name": "removeUselessStrokeAndFill" },
83-
{ "name": "removeXMLProcInst" },
84-
{ "name": "sortAttrs" }
19+
{ "name": "removeDoctype", enabled: true },
20+
{ "name": "removeXMLProcInst", enabled: true },
21+
{ "name": "removeComments", enabled: true },
22+
{ "name": "removeMetadata", enabled: true },
23+
{ "name": "removeXMLNS", enabled: false },
24+
{ "name": "removeEditorsNSData", enabled: true },
25+
{ "name": "cleanupAttrs", enabled: false },
26+
{ "name": "inlineStyles", enabled: false },
27+
{ "name": "minifyStyles", enabled: true },
28+
{ "name": "convertStyleToAttrs", enabled: true },
29+
{ "name": "cleanupIDs", enabled: true },
30+
{ "name": "prefixIds", enabled: false },
31+
{ "name": "removeRasterImages", enabled: false },
32+
{ "name": "removeUselessDefs", enabled: true },
33+
{ "name": "cleanupNumericValues", enabled: true },
34+
{ "name": "cleanupListOfValues", enabled: true },
35+
{ "name": "convertColors", enabled: true },
36+
{ "name": "removeUnknownsAndDefaults", enabled: true },
37+
{ "name": "removeNonInheritableGroupAttrs", enabled: true },
38+
{ "name": "removeUselessStrokeAndFill", enabled: true },
39+
{ "name": "removeViewBox", enabled: false },
40+
{ "name": "cleanupEnableBackground", enabled: false },
41+
{ "name": "removeHiddenElems", enabled: false },
42+
{ "name": "removeEmptyText", enabled: true },
43+
{ "name": "convertShapeToPath", enabled: false },
44+
{ "name": "moveElemsAttrsToGroup", enabled: false },
45+
{ "name": "moveGroupAttrsToElems", enabled: false },
46+
{ "name": "collapseGroups", enabled: true },
47+
{ "name": "convertPathData", enabled: false },
48+
{ "name": "convertTransform", enabled: true },
49+
{ "name": "removeEmptyAttrs", enabled: true },
50+
{ "name": "removeEmptyContainers", enabled: true },
51+
{ "name": "mergePaths", enabled: true },
52+
{ "name": "removeUnusedNS", enabled: true },
53+
{ "name": "sortAttrs", enabled: true },
54+
{ "name": "removeTitle", enabled: true },
55+
{ "name": "removeDesc", enabled: true, "params": { "removeAny": true } },
56+
{ "name": "removeDimensions", enabled: false },
57+
{ "name": "removeAttrs", enabled: false },
58+
{ "name": "removeElementsByAttr", enabled: false },
59+
{ "name": "addClassesToSVGElement", enabled: false },
60+
{ "name": "removeStyleElement", enabled: false },
61+
{ "name": "removeScriptElement", enabled: false },
62+
{ "name": "addAttributesToSVGElement", enabled: false },
8563
]
8664
}

src/plugin.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ export function openSettings() {
1111

1212
// Plugin was run from the menu, so let's open the settings window
1313
const response = dialog.showMessageBox({
14-
buttons: ['Edit SVGO Settings…', 'Cancel'],
14+
buttons: ['Edit SVGO Settings…', 'Reset SVGO Settings', 'Cancel'],
1515
message: "About SVGO Compressor",
1616
detail: "This Plugin uses SVGO to compress SVG assets exported from Sketch.\n\nIt works automatically whenever you export to SVG, so you don’t need to do anything special. Just work on your design as always, and enjoy smaller & cleaner SVG files.\n\nIf for some reason you’re not happy with the default options, you can edit the svgo.json file in the Application Support folder for Sketch.\n"
1717
})
1818

1919
if (response === 0) {
2020
// open the config
2121
spawnSync("/usr/bin/open", [svgoJSONFilePath])
22+
} else if (response === 1) {
23+
// reset config
24+
fs.writeFileSync(svgoJSONFilePath, JSON.stringify(require('./defaultConfig'), null, ' '), 'utf8')
2225
}
2326
}
2427

@@ -28,8 +31,15 @@ export function compress(context) {
2831
return
2932
}
3033

34+
const floatPrecision = typeof svgoJSON.floatPrecision !== 'undefined'
35+
? Number(svgoJSON.floatPrecision)
36+
: undefined;
37+
3138
const parsedSVGOPlugins = []
3239
svgoJSON.plugins.forEach(item => {
40+
if (typeof item.enabled !== 'undefined' && !item.enabled) {
41+
return
42+
}
3343
const plugin = svgoPlugins[item.name]
3444
if (!plugin) {
3545
log('Plugin not found: ' + item.name)
@@ -40,10 +50,14 @@ export function compress(context) {
4050
plugin.active = true
4151
if (plugin.params) {
4252
// Plugin supports params
53+
54+
// Set floatPrecision across all the plugins
55+
if (floatPrecision && 'floatPrecision' in plugin.params) {
56+
plugin.params.floatPrecision = floatPrecision
57+
}
4358
log('—› default params: ' + JSON.stringify(plugin.params, null, 2))
4459
}
4560
if (item.params != null) {
46-
log('—› new params: ' + JSON.stringify(item.params, null, 2))
4761
if (typeof plugin.params === 'undefined') {
4862
plugin.params = {}
4963
}
@@ -68,17 +82,18 @@ export function compress(context) {
6882
log('Let‘s go…')
6983
let originalTotalSize = 0
7084
let compressedTotalSize = 0
85+
if (typeof svgoJSON.full === 'undefined') { svgoJSON.full = true }
86+
if (typeof svgoJSON.multipass === 'undefined') { svgoJSON.multipass = true }
7187
if (typeof svgoJSON.pretty === 'undefined') { svgoJSON.pretty = true }
7288
if (typeof svgoJSON.indent === 'undefined') { svgoJSON.indent = 2 }
7389
const svgCompressor = new svgo({
74-
full: true,
90+
full: svgoJSON.full,
7591
js2svg: {
7692
pretty: svgoJSON.pretty,
7793
indent: svgoJSON.indent
7894
},
79-
plugins: parsedSVGOPlugins
80-
// multipass: true
81-
// floatPrecision: 1
95+
plugins: parsedSVGOPlugins,
96+
multipass: svgoJSON.multipass
8297
})
8398
Promise.all(filesToCompress.map(currentFile => {
8499
const svgString = fs.readFileSync(currentFile, 'utf8')
@@ -105,5 +120,9 @@ export function compress(context) {
105120
log(msg)
106121
UI.message(msg)
107122
})
123+
.catch((err) => {
124+
log(err)
125+
UI.message(err.message)
126+
})
108127
}
109128
}

0 commit comments

Comments
 (0)