build-icons.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. /**
  3. * Build icons.svg — one inline SVG sprite combined from the Font Awesome Pro kit's
  4. * pre-built per-style sprites under node_modules/@awesome.me/kit-<id>/icons/sprites/.
  5. *
  6. * Symbol ids are namespaced `dw-<name>` (e.g. dw-house). Consumed via the dw_icon()
  7. * helper (inc/template.php) or directly: <svg class="dw-icon"><use href="#dw-house"></use></svg>.
  8. *
  9. * Run: npm run icons (also chained into `npm run build`).
  10. * The kit is the curated icon set, so the sprite is exactly the icons we use (~40, ~40 KB)
  11. * — replacing the 689 KB Font Awesome webfont.
  12. */
  13. import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
  14. import { resolve } from 'node:path';
  15. const scope = 'node_modules/@awesome.me';
  16. let kit;
  17. try {
  18. kit = readdirSync(scope).find((d) => d.startsWith('kit-'));
  19. } catch {
  20. /* scope missing */
  21. }
  22. if (!kit) {
  23. console.error('build-icons: no @awesome.me/kit-* installed — skipping (run `npm install` your FA kit first)');
  24. process.exit(0);
  25. }
  26. const spritesDir = resolve(scope, kit, 'icons/sprites');
  27. const symbols = [];
  28. const seen = new Set();
  29. for (const file of readdirSync(spritesDir).filter((f) => f.endsWith('.svg'))) {
  30. const svg = readFileSync(resolve(spritesDir, file), 'utf8');
  31. for (const m of svg.matchAll(/<symbol id="([^"]+)"([\s\S]*?)<\/symbol>/g)) {
  32. const name = m[1];
  33. if (seen.has(name)) continue; // first style wins on a name clash
  34. seen.add(name);
  35. symbols.push(`<symbol id="dw-${name}"${m[2]}</symbol>`);
  36. }
  37. }
  38. symbols.sort();
  39. const sprite =
  40. '<svg xmlns="http://www.w3.org/2000/svg" style="display:none" aria-hidden="true">\n' +
  41. symbols.join('\n') +
  42. '\n</svg>\n';
  43. writeFileSync('icons.svg', sprite);
  44. console.log(`build-icons: wrote icons.svg — ${symbols.length} icons from ${kit}`);