forked from gregberge/svgr
/
svgo.js
33 lines (30 loc) 路 894 Bytes
/
svgo.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
import SVGO from 'svgo'
import cosmiconfig from 'cosmiconfig'
import mergeConfigs from './mergeConfigs'
const explorer = cosmiconfig('svgo', {
searchPlaces: [
'package.json',
`.svgorc`,
`.svgorc.json`,
`.svgorc.yaml`,
`.svgorc.yml`,
`svgo.config.js`,
'.svgo.yml',
],
transform: result => result && result.config,
})
function getBaseSvgoConfig(config) {
const baseSvgoConfig = { plugins: [] }
if (config.icon) baseSvgoConfig.plugins.push({ removeViewBox: false })
return baseSvgoConfig
}
export default async (code, config = {}, state = {}) => {
if (!config.svgo) return code
const filePath = state.filePath || process.cwd()
const svgoRcConfig = await explorer.search(filePath)
const svgo = new SVGO(
mergeConfigs(getBaseSvgoConfig(config), svgoRcConfig, config.svgoConfig),
)
const { data } = await svgo.optimize(code)
return data
}