Files
romm/frontend/scripts/trimMdiIcons.ts
Stu Anderson 8e1ea1155d fix(frontend): catch mdi icon names the build would drop (#5562)
* perf(frontend): subset the mdi icon font to the icons the app uses

#5118 trimmed the icon CSS, but the font file still held every glyph
(403 KB) and the build still copied the eot, ttf and woff formats.
The build plugin now also cuts the woff2 down to the code points of the
rules it keeps, and the stylesheet's only font source is that file.

Build only: dev keeps the full font, so a newly used icon renders
without a restart. Font in the build: 403,216 B -> 25,504 B, and the
three unused formats are no longer emitted.

Adds subset-font and @types/subset-font as dev dependencies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* chore(frontend): lint against building mdi icon names at runtime

The build keeps only the icon glyphs whose names appear in full in
src/, so `mdi-${name}` or "mdi-" + name would render an empty square
in production. romm/no-dynamic-mdi-name reports both forms in scripts
and Vue templates.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* chore(frontend): drop the mdi font subsetting, keep the lint rule

The browser caches the full icon font after the first load, so
subsetting it only saves about 360 KB on a cold cache. That doesn't
justify two new dev dependencies and a build step that writes into
node_modules. The rule that rejects runtime-built icon names still
protects the existing CSS trim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(frontend): flag mdi icon names split across string literals

Addresses Greptile's review: "mdi-" + "home" passed the rule, but the
build only finds icon names written in full, so that icon lost its CSS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(frontend): type isStringLiteral as a type guard

vue-tsc rejected reading .value off the narrowed BinaryExpression side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(frontend): catch mdi names split across + chains and templates

pr-ready step 2 (code-review xhigh). The rule now reads the static text
at each end of a join, so `base + " mdi-" + name` and `mdi-` + name are
reported, while "mdi-home" + " " + tone stays valid. The word boundary
no longer treats `_` as a separator, and the test reuses a shared
sfcTemplate helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(frontend): share the SFC test wrapper, inline isStringLiteral

pr-ready step 3 (simplify). sfc and sfcTemplate now build their case
through one vueCase helper, isStringLiteral is inlined into its only
caller, and the rule points at the build's ICON_NAME so the two
character classes stay in step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(frontend): fail the build on mdi names @mdi/font does not define

The icon trim keeps only names written in full in src/, so a name built
at runtime, or one @mdi/font never had, renders blank in production.
The build now lists each mdi-* token outside tests that names no class
in the stylesheet, with its file and line, and stops. That covers what
the lint rule cannot see: names joined through a variable, .concat, or
+=, and plain typos.

The check found mdi-playstation in the RSelect story, which @mdi/font 7
does not have; it is now mdi-sony-playstation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-17 08:13:26 -05:00

72 lines
2.5 KiB
TypeScript

// Drops the @mdi/font icon rules nothing in src/ names, and fails the build on
// an mdi-* name the stylesheet does not define.
import { readdirSync, readFileSync } from "node:fs";
import { extname, join } from "node:path";
import type { Plugin } from "vite";
const ICON_NAME = /mdi-[a-z0-9-]+/g;
// Also matches the bare "mdi-" left where a name is joined at runtime.
const ICON_TOKEN = /(?<![\w-])mdi-[a-z0-9-]*/g;
const ICON_RULE = /\.(mdi-[a-z0-9-]+)::before\s*\{[^}]*\}/g;
const ICON_CLASS = /\.(mdi-[a-z0-9-]+)/g;
function sourceFiles(root: string): { path: string; text: string }[] {
return readdirSync(join(root, "src"), { recursive: true, encoding: "utf8" })
.filter((path) => [".vue", ".ts"].includes(extname(path)))
.map((path) => ({
path: join("src", path),
text: readFileSync(join(root, "src", path), "utf8"),
}));
}
export function usedIconNames(root: string): Set<string> {
return new Set(
sourceFiles(root).flatMap(({ text }) => text.match(ICON_NAME) ?? []),
);
}
/** Each mdi-* token in src/ outside tests that `css` defines no class for, as "path:line token". */
export function unknownIconNames(root: string, css: string): string[] {
const classes = new Set(
[...css.matchAll(ICON_CLASS)].map(([, name]) => name),
);
return sourceFiles(root)
.filter(({ path }) => !path.endsWith(".test.ts"))
.flatMap(({ path, text }) =>
[...text.matchAll(ICON_TOKEN)]
.filter(([token]) => !classes.has(token))
.map(({ 0: token, index }) => {
const line = text.slice(0, index).split("\n").length;
return `${path}:${line} ${token}`;
}),
);
}
export function trimIconCss(css: string, used: Set<string>): string {
return css.replace(ICON_RULE, (rule, name) => (used.has(name) ? rule : ""));
}
export function trimMdiIcons(): Plugin {
let root = "";
let used = new Set<string>();
return {
name: "romm:trim-mdi-icons",
apply: "build",
enforce: "pre",
configResolved(config) {
root = config.root;
used = usedIconNames(root);
},
transform(code, id) {
if (!id.endsWith("@mdi/font/css/materialdesignicons.css")) return null;
const unknown = unknownIconNames(root, code);
if (unknown.length) {
this.error(
`@mdi/font has no class for these names. Fix the name, or write it in full instead of building it at runtime:\n${unknown.join("\n")}`,
);
}
return { code: trimIconCss(code, used), map: null };
},
};
}