-
Notifications
You must be signed in to change notification settings - Fork 15
/
svgr.config.js
168 lines (138 loc) · 3.36 KB
/
svgr.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @type {import('svgo').Config}
*/
const ICON_CLEANUP_CONFIG = {
'/logo.svg': {
mergePaths: false,
removeFill: false,
replaceColors: {
'#262d3d': 'var(--logo-text-color)',
'#204ecf': 'var(--logo-emblem-color)',
},
},
'/picasso-pictograms/': {
skip: true,
},
}
const getCleanupConfig = svgPath => {
for (const [key, value] of Object.entries(ICON_CLEANUP_CONFIG)) {
if (svgPath.indexOf(key) > -1) {
return value
}
}
return {}
}
const findChildWithName = (node, name) => {
if (node.type === 'element' && node.name === name) {
return node
}
if (node.children) {
for (const child of node.children) {
const found = findChildWithName(child, name)
if (found) {
return found
}
}
}
return null
}
const findAllChildrenWithName = (node, name) => {
let result = []
if (node.type === 'element' && node.name === name) {
result.push(node)
}
if (node.children) {
for (const child of node.children) {
const found = findAllChildrenWithName(child, name)
result = result.concat(found)
}
}
return result
}
const updateAttributes = (node, attributeModifier) => {
if (node.type === 'element') {
node.attributes = attributeModifier(node.attributes)
}
if (node.children) {
for (const child of node.children) {
updateAttributes(child, attributeModifier)
}
}
}
const removeAttribute = attributeName => attributes => {
// Make a copy of the attributes object to avoid mutating the original one
const newAttributes = { ...attributes }
// Remove the specified attribute from the copy
delete newAttributes[attributeName]
return newAttributes
}
const cleanupAttributes = config => node => {
updateAttributes(node, removeAttribute('id'))
if (config.removeFill !== false) {
updateAttributes(node, removeAttribute('fill'))
} else {
// attempt to replace colors
const replaceColors = attributes => {
const color = attributes['fill']
const currentColor = color && color.toLowerCase()
if (color && config.replaceColors && config.replaceColors[currentColor]) {
return {
...attributes,
fill: config.replaceColors[currentColor],
}
}
return attributes
}
updateAttributes(node, replaceColors)
}
}
const cleanupSketch = (root, params, info) => {
const config = getCleanupConfig(info.path)
if (config.skip) {
return root
}
const svg = root.children[0]
let paths = null
if (config.mergePaths !== false) {
paths = findChildWithName(svg, 'path')
} else {
paths = findAllChildrenWithName(svg, 'path')
}
if (config.mergePaths !== false) {
cleanupAttributes(config)(paths)
svg.children = [paths]
} else {
paths.forEach(cleanupAttributes(config))
svg.children = paths
}
return root
}
module.exports = {
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
{
name: 'removeDimensions',
},
{
name: 'removeAttrs',
params: {
attrs: '(stroke|width|height|xmlns.*)',
},
},
{
name: 'cleanupSketch',
type: 'full',
description: 'Cleanup svg after export from sketch',
fn: cleanupSketch,
},
],
},
}