// Writes the Material Symbols used in the source to icons.css. Icons are found // by their i_ 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(/^]*viewBox="0 -960 960 960"[^>]*><\/svg>$/) if (path === null) { throw new Error(`Icon ${name} has an unexpected format: ${svg}`) } return `.i_${name}{--icon:url("data:image/svg+xml,")}` })) writeFileSync(css_file, "/* Generated by svelte/icons.js */\n" + rules.join("\n") + "\n")