sprites 逆向生成 svg 文件
javascript
const fs = require('fs-extra')
const { parse } = require('svg-parser')
fs.readFile('sprites.svg', 'utf-8', (err, contents) => {
if (err) return console.error(err)
const parsed = parse(contents)
const symbols = parsed.children[0].children
symbols.forEach((symbol) => {
const name = symbol.properties.id
let paths = ''
symbol.children.forEach(path => {
paths += `<path d="${path.properties.d}" />`
})
const svg = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
+ `<svg class='icon' viewBox='${symbol.properties.viewBox}' version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`
+ paths
+ '</svg>'
fs.outputFile(`icons/${name}.svg`, svg, (err) => {
if (err) return console.error(err)
console.log(`${name}.svg`)
})
})
})