2018-12-18 01:21:29 +00:00
|
|
|
import svgs from './svgs'
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2019-01-19 23:06:25 +00:00
|
|
|
import { promisify } from 'util'
|
2018-12-18 01:21:29 +00:00
|
|
|
import SVGO from 'svgo'
|
|
|
|
import $ from 'cheerio'
|
2018-01-27 20:48:22 +00:00
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
const svgo = new SVGO()
|
2019-01-19 23:06:25 +00:00
|
|
|
const readFile = promisify(fs.readFile)
|
2019-03-03 03:02:06 +00:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
|
|
|
async function readSvg (svg) {
|
|
|
|
let filepath = path.join(__dirname, '../', svg.src)
|
|
|
|
let content = await readFile(filepath, 'utf8')
|
|
|
|
let optimized = (await svgo.optimize(content))
|
|
|
|
let $optimized = $(optimized.data)
|
|
|
|
let $path = $optimized.find('path').removeAttr('fill')
|
|
|
|
let $symbol = $('<symbol></symbol>')
|
|
|
|
.attr('id', svg.id)
|
|
|
|
.attr('viewBox', `0 0 ${optimized.info.width} ${optimized.info.height}`)
|
|
|
|
.append($path)
|
|
|
|
return $.xml($symbol)
|
|
|
|
}
|
2018-01-27 20:48:22 +00:00
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
export async function buildSvg () {
|
2019-03-03 03:02:06 +00:00
|
|
|
let inlineSvgs = svgs.filter(_ => _.inline)
|
|
|
|
let regularSvgs = svgs.filter(_ => !_.inline)
|
|
|
|
|
|
|
|
let inlineSvgStrings = (await Promise.all(inlineSvgs.map(readSvg))).join('')
|
|
|
|
let regularSvgStrings = (await Promise.all(regularSvgs.map(readSvg))).join('')
|
|
|
|
|
|
|
|
let inlineOutput = `<svg xmlns="http://www.w3.org/2000/svg" style="display:none">${inlineSvgStrings}</svg>`
|
|
|
|
let regularOutput = `<svg xmlns="http://www.w3.org/2000/svg">${regularSvgStrings}</svg>`
|
|
|
|
|
|
|
|
await writeFile(path.resolve(__dirname, '../static/icons.svg'), regularOutput, 'utf8')
|
2018-01-27 20:48:22 +00:00
|
|
|
|
2019-03-03 03:02:06 +00:00
|
|
|
return inlineOutput
|
2018-01-27 20:48:22 +00:00
|
|
|
}
|