2021-07-05 05:19:04 +02:00
|
|
|
import svgs from './svgs.js'
|
2018-12-18 02:21:29 +01:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2019-01-20 00:06:25 +01:00
|
|
|
import { promisify } from 'util'
|
2021-05-15 22:28:42 +02:00
|
|
|
import { optimize } from 'svgo'
|
2021-07-05 05:19:04 +02:00
|
|
|
import cheerioPackage from 'cheerio'
|
2018-01-27 21:48:22 +01:00
|
|
|
|
2021-07-05 05:19:04 +02:00
|
|
|
const { default: $ } = cheerioPackage
|
|
|
|
|
|
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
2019-01-20 00:06:25 +01:00
|
|
|
const readFile = promisify(fs.readFile)
|
2019-03-03 04:02:06 +01:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
|
|
|
async function readSvg (svg) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const filepath = path.join(__dirname, '../', svg.src)
|
|
|
|
const content = await readFile(filepath, 'utf8')
|
2021-05-15 22:28:42 +02:00
|
|
|
const optimized = (await optimize(content, { multipass: true }))
|
2019-08-03 22:49:37 +02:00
|
|
|
const $optimized = $(optimized.data)
|
2019-11-01 07:01:44 +01:00
|
|
|
const $path = $optimized.find('path, circle').removeAttr('fill')
|
2020-08-26 01:36:41 +02:00
|
|
|
const viewBox = $optimized.attr('viewBox') || `0 0 ${$optimized.attr('width')} ${$optimized.attr('height')}`
|
2019-08-03 22:49:37 +02:00
|
|
|
const $symbol = $('<symbol></symbol>')
|
2019-03-03 04:02:06 +01:00
|
|
|
.attr('id', svg.id)
|
2020-08-26 01:36:41 +02:00
|
|
|
.attr('viewBox', viewBox)
|
2019-03-03 04:02:06 +01:00
|
|
|
.append($path)
|
|
|
|
return $.xml($symbol)
|
|
|
|
}
|
2018-01-27 21:48:22 +01:00
|
|
|
|
2018-12-18 02:21:29 +01:00
|
|
|
export async function buildSvg () {
|
2019-08-03 22:49:37 +02:00
|
|
|
const inlineSvgs = svgs.filter(_ => _.inline)
|
|
|
|
const regularSvgs = svgs.filter(_ => !_.inline)
|
2019-03-03 04:02:06 +01:00
|
|
|
|
2019-08-03 22:49:37 +02:00
|
|
|
const inlineSvgStrings = (await Promise.all(inlineSvgs.map(readSvg))).join('')
|
|
|
|
const regularSvgStrings = (await Promise.all(regularSvgs.map(readSvg))).join('')
|
2019-03-03 04:02:06 +01:00
|
|
|
|
2019-08-03 22:49:37 +02: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>`
|
2019-03-03 04:02:06 +01:00
|
|
|
|
|
|
|
await writeFile(path.resolve(__dirname, '../static/icons.svg'), regularOutput, 'utf8')
|
2018-01-27 21:48:22 +01:00
|
|
|
|
2019-03-03 04:02:06 +01:00
|
|
|
return inlineOutput
|
2018-01-27 21:48:22 +01:00
|
|
|
}
|