44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
// Writes the Material Symbols used in the source to icons.css. Icons are found
|
|||
|
|
// by their i_<name> classes and by the icon props of Button, ToggleButton and
|
||
|
|
// TabMenu: icon="name", icon: "name" and icon={a ? "name" : "other"}. Icons
|
||
|
|
// which are already in icons.css are not downloaded again
|
||
|
|
import { globSync, readFileSync, writeFileSync } from "node:fs"
|
||
|
|
|
||
|
|
const css_file = "../res/static/style/icons.css"
|
||
|
|
|
||
|
|
const names = new Set()
|
||
|
|
for (const file of globSync(["src/**/*.svelte", "../res/template/**/*.html"])) {
|
||
|
|
const src = readFileSync(file, "utf8")
|
||
|
|
for (const [, name] of src.matchAll(/\bi_([a-z0-9_]+)/g)) {
|
||
|
|
names.add(name)
|
||
|
|
}
|
||
|
|
for (const [, prop] of src.matchAll(/\bicon(?:_on|_off)?(?:=|:\s*)("[^"]*"|\{[^}]*\})/g)) {
|
||
|
|
for (const [, name] of prop.matchAll(/"([a-z0-9_]+)"/g)) {
|
||
|
|
names.add(name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const cached = new Map(
|
||
|
|
readFileSync(css_file, "utf8").matchAll(/^\.i_([a-z0-9_]+)\{.*$/gm).map(m => [m[1], m[0]])
|
||
|
|
)
|
||
|
|
|
||
|
|
const rules = await Promise.all([...names].sort().map(async name => {
|
||
|
|
if (cached.has(name)) {
|
||
|
|
return cached.get(name)
|
||
|
|
}
|
||
|
|
|
||
|
|
const url = `https://fonts.gstatic.com/s/i/short-term/release/materialsymbolsoutlined/${name}/fill1/24px.svg`
|
||
|
|
const resp = await fetch(url)
|
||
|
|
if (!resp.ok) {
|
||
|
|
throw new Error(`Icon ${name} not found: ${resp.status} ${url}`)
|
||
|
|
}
|
||
|
|
const svg = await resp.text()
|
||
|
|
const path = svg.match(/^<svg [^>]*viewBox="0 -960 960 960"[^>]*><path d="([^"]+)"\/><\/svg>$/)
|
||
|
|
if (path === null) {
|
||
|
|
throw new Error(`Icon ${name} has an unexpected format: ${svg}`)
|
||
|
|
}
|
||
|
|
return `.i_${name}{--icon:url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 -960 960 960'><path d='${path[1]}'/></svg>")}`
|
||
|
|
}))
|
||
|
|
|
||
|
|
writeFileSync(css_file, "/* Generated by svelte/icons.js */\n" + rules.join("\n") + "\n")
|