pinafore/bin/build-svg.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

import svgs from './svgs'
import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import SVGO from 'svgo'
import $ from 'cheerio'
2018-01-27 20:48:22 +00:00
const svgo = new SVGO()
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
async function readSvg (svg) {
2019-08-03 20:49:37 +00:00
const filepath = path.join(__dirname, '../', svg.src)
const content = await readFile(filepath, 'utf8')
const optimized = (await svgo.optimize(content))
const $optimized = $(optimized.data)
const $path = $optimized.find('path, circle').removeAttr('fill')
2019-08-03 20:49:37 +00:00
const $symbol = $('<symbol></symbol>')
.attr('id', svg.id)
.attr('viewBox', $optimized.attr('viewBox'))
.append($path)
return $.xml($symbol)
}
2018-01-27 20:48:22 +00:00
export async function buildSvg () {
2019-08-03 20:49:37 +00:00
const inlineSvgs = svgs.filter(_ => _.inline)
const regularSvgs = svgs.filter(_ => !_.inline)
2019-08-03 20:49:37 +00:00
const inlineSvgStrings = (await Promise.all(inlineSvgs.map(readSvg))).join('')
const regularSvgStrings = (await Promise.all(regularSvgs.map(readSvg))).join('')
2019-08-03 20:49:37 +00:00
const inlineOutput = `<svg xmlns="http://www.w3.org/2000/svg" style="display:none">${inlineSvgStrings}</svg>`
const 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
return inlineOutput
2018-01-27 20:48:22 +00:00
}