/
index.js
150 lines (127 loc) · 3.54 KB
/
index.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
/* eslint-disable no-underscore-dangle */
import SVGO from 'svgo'
import { cosmiconfigSync } from 'cosmiconfig'
import mergeDeep from 'merge-deep'
const explorer = cosmiconfigSync('svgo', {
searchPlaces: [
'package.json',
'.svgorc',
'.svgorc.json',
'.svgorc.yaml',
'.svgorc.yml',
'svgo.config.js',
'.svgo.yml',
],
transform: result => result && result.config,
cache: true,
})
function encodeSVGDatauri(str, type) {
let prefix = 'data:image/svg+xml'
// base64
if (!type || type === 'base64') {
prefix += ';base64,'
if (Buffer.from) {
str = prefix + Buffer.from(str).toString('base64')
} else {
// eslint-disable-next-line
str = prefix + new Buffer(str).toString('base64')
}
// URI encoded
} else if (type === 'enc') {
str = `${prefix},${encodeURIComponent(str)}`
// unencoded
} else if (type === 'unenc') {
str = `${prefix},${str}`
}
return str
}
// See https://github.com/svg/svgo/blob/master/lib/svgo.js#L24
// _optimizeOnce is synchronous internally
function optimizeSync(svgstr, info) {
const { config } = this
if (config.error) {
throw config.error
}
const maxPassCount = config.multipass ? 10 : 1
let counter = 0
let prevResultSize = Number.POSITIVE_INFINITY
let result
const optimizeOnceCallback = svgjs => {
if (svgjs.error) {
throw svgjs.error
}
// eslint-disable-next-line no-plusplus
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
prevResultSize = svgjs.data.length
this._optimizeOnce(svgjs.data, info, optimizeOnceCallback)
} else {
if (config.datauri) {
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri)
}
if (info.path) {
svgjs.path = info.path
}
result = svgjs
}
}
this._optimizeOnce(svgstr, info, optimizeOnceCallback)
return result
}
function getBaseSvgoConfig(config) {
const baseSvgoConfig = {
plugins: [{ prefixIds: true }],
}
if (config.icon || config.dimensions === false)
baseSvgoConfig.plugins.push({ removeViewBox: false })
return baseSvgoConfig
}
function getFilePath(state) {
return state.filePath || process.cwd()
}
function getPlugins(config) {
if (!config || !config.plugins) {
return []
}
if (!Array.isArray(config.plugins)) {
throw Error("`svgoConfig.plugins` must be an array")
}
return config.plugins
}
function extendPlugins(...configs) {
const init = [];
let i = configs.length;
while (i-- > 0) {
const plugins = configs[i];
for (let j = 0; j < plugins.length; j++) {
const plugin = plugins[j];
if (!init.some(item => Object.keys(item)[0] === Object.keys(plugin)[0])) {
init.push(plugin);
}
}
}
return init;
}
function createSvgo(config, rcConfig) {
const baseSvgoConfig = getBaseSvgoConfig(config);
const plugins = extendPlugins(getPlugins(baseSvgoConfig), getPlugins(rcConfig), getPlugins(config.svgoConfig));
const mergedConfig = mergeDeep(
baseSvgoConfig,
rcConfig,
config.svgoConfig,
)
mergedConfig.plugins = plugins
return new SVGO(mergedConfig)
}
function getInfo(state) {
return state.filePath
? { input: 'file', path: state.filePath }
: { input: 'string' }
}
export default function svgoPlugin(code, config, state) {
if (!config.svgo) return code
const filePath = getFilePath(state)
const svgoRcConfig = config.runtimeConfig ? explorer.search(filePath) : {}
const svgo = createSvgo(config, svgoRcConfig)
const { data } = optimizeSync.call(svgo, code, getInfo(state))
return data
}