Created
June 18, 2020 14:05
-
-
Save voskresla/658abd243d1718abe56e71a00f68b8e0 to your computer and use it in GitHub Desktop.
prepare-svg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const SVGO = require('svgo') | |
const svgo = new SVGO({ | |
plugins: [ | |
{ removeXMLNS: true }, | |
{ sortAttrs: true }, | |
{ removeDimensions: true }, | |
], | |
}) | |
fs.readdirSync('./icons').forEach(file => { | |
fs.renameSync('./icons/' + file, './icons/' + file.replace(/[ ']/g, '-')) | |
}) | |
const stopList = ['star-half', 'zoom-out', 'zoom'] | |
const stopListFilter = (name) => { | |
const [filename] = name.split('.') | |
if (stopList.includes(filename)) { | |
console.warn(`Иконка ${name} требует ручной обработки. Возможно это dualtone.`) | |
return false | |
} | |
return true | |
} | |
const svgFilter = (file) => { | |
const [name, ext] = file.split('.') | |
return ext === 'svg' | |
} | |
fs.readdirSync('./icons') | |
.filter(stopListFilter) | |
.filter(svgFilter) | |
.forEach(file => { | |
const filepath = './icons/' + file | |
const icon = fs.readFileSync(filepath, 'utf-8') | |
svgo.optimize(icon).then(({ data }) => { | |
fs.writeFileSync( | |
filepath, | |
data | |
.replace(/ fill="[^white|none]\S+"/g, ' fill="currentColor"') | |
.replace(/ stroke="[^white]\S+"/g, ' stroke="currentColor"') | |
) | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment