pinafore/bin/build-svg.js
Nolan Lawson bb7fe6e30a
chore: make build process faster/simpler (#833)
This gets rid of the awkward checking-in of `template.html` to git (when
it's a built file) and also makes the rebuilds faster and more
consistent by running everything through the same pipeline. So inline
CSS, SVG, and JS are all partially built on-the-fly.

I've basically reinvented gulp, but it's pretty lightweight and
zero-dep, so I'm happy with it.
2018-12-17 17:21:29 -08:00

29 lines
890 B
JavaScript
Executable file

#!/usr/bin/env node
import svgs from './svgs'
import path from 'path'
import fs from 'fs'
import pify from 'pify'
import SVGO from 'svgo'
import $ from 'cheerio'
const svgo = new SVGO()
const readFile = pify(fs.readFile.bind(fs))
export async function buildSvg () {
let result = (await Promise.all(svgs.map(async 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)
}))).join('\n')
return `<svg xmlns="http://www.w3.org/2000/svg" style="display:none;">\n${result}\n</svg>`
}