Files
Louis King 510612d69b feat(web): adopt self-hosted IBM Plex Sans and IBM Plex Mono typography
Replace the OS system font stack with IBM Plex: the variable-weight
sans (100-700, one file per subset) for UI and headings, and Plex Mono
400 for the public keys, packet hashes, and hex that font-mono renders
across the app. Latin + latin-ext subsets only (shipped locales are
en/nl); no italics; mono is never rendered bold here.

Fonts are self-hosted from @fontsource packages via the existing
build.js vendor pipeline (no CDN), copied to static/vendor/fonts/ with
a hard build failure on wrong filenames. Wiring:

- input.css: @theme --font-sans/--font-mono + @font-face rules with
  unicode-ranges taken verbatim from the package CSS; Tailwind v4
  derives the document default from --font-sans, re-fonting daisyUI
  components with no other changes.
- spa.html: preload the latin sans variable woff2 (crossorigin, URL
  identical to the @font-face src) to minimize FOUT.
- charts.js: Chart.defaults.font.family to match (Chart.js otherwise
  uses Helvetica/Arial).
- error.html: name-prepend only; the page stays dependency-free.
- middleware.py: long-term immutable cache for /static/vendor/fonts/
  (stable names referenced from CSS, so ?v= versioning can't apply),
  with a matching test.
- app.css: slight hero-title letter-spacing tightening for Plex at
  display sizes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 21:58:58 +01:00

134 lines
3.7 KiB
JavaScript

import { execSync } from "node:child_process";
import {
cpSync,
existsSync,
mkdirSync,
readFileSync,
readdirSync,
writeFileSync,
} from "node:fs";
import { createHash } from "node:crypto";
import { join } from "node:path";
const STATIC = join("src", "meshcore_hub", "web", "static");
const VENDOR = join(STATIC, "vendor");
const DIST = join(STATIC, "dist");
function vendor(pkg, files, dest) {
const out = join(VENDOR, dest);
mkdirSync(out, { recursive: true });
for (const f of files) {
const src = join("node_modules", pkg, f);
if (!existsSync(src)) {
console.error(` MISSING: ${src}`);
process.exit(1);
}
cpSync(src, join(out, f.split("/").pop()), { recursive: true });
}
}
console.log("Building Tailwind CSS...");
execSync(
`npx @tailwindcss/cli build --input ${join(STATIC, "css", "input.css")} --output ${join(STATIC, "css", "tailwind.css")} --minify`,
{ stdio: "inherit" },
);
console.log("Copying vendor files...");
vendor("leaflet", ["dist/leaflet.css", "dist/leaflet.js", "dist/leaflet.js.map"], "leaflet");
mkdirSync(join(VENDOR, "leaflet", "images"), { recursive: true });
cpSync(
join("node_modules", "leaflet", "dist", "images"),
join(VENDOR, "leaflet", "images"),
{ recursive: true },
);
vendor("chart.js", ["dist/chart.umd.min.js"], "chart.js");
vendor("qrcodejs", ["qrcode.min.js"], "qrcodejs");
vendor(
"@fontsource-variable/ibm-plex-sans",
[
"files/ibm-plex-sans-latin-wght-normal.woff2",
"files/ibm-plex-sans-latin-ext-wght-normal.woff2",
],
"fonts",
);
vendor(
"@fontsource/ibm-plex-mono",
[
"files/ibm-plex-mono-latin-400-normal.woff2",
"files/ibm-plex-mono-latin-ext-400-normal.woff2",
],
"fonts",
);
console.log("Bundling SPA with esbuild...");
mkdirSync(DIST, { recursive: true });
const metafilePath = join(DIST, "meta.json");
execSync(
`npx esbuild ${join(STATIC, "js", "spa", "app.js")}` +
` --bundle --format=esm --splitting --minify` +
` --outdir=${DIST}` +
` --entry-names=[name].[hash]` +
` --chunk-names=chunks/[name].[hash]` +
` --metafile=${metafilePath}`,
{ stdio: "inherit" },
);
console.log("Generating assets manifest...");
const meta = JSON.parse(readFileSync(metafilePath, "utf-8"));
const assets = {};
for (const [outputPath, info] of Object.entries(meta.outputs)) {
if (!info.entryPoint) continue;
const entryName = info.entryPoint.split("/").pop().replace(/\.js$/, ".js");
const fileName = outputPath.split("/").pop();
assets[entryName] = fileName;
}
const vendorFiles = {
"leaflet.css": join(VENDOR, "leaflet", "leaflet.css"),
"leaflet.js": join(VENDOR, "leaflet", "leaflet.js"),
"chart.umd.min.js": join(VENDOR, "chart.js", "chart.umd.min.js"),
"qrcode.min.js": join(VENDOR, "qrcodejs", "qrcode.min.js"),
};
const vendorHashes = {};
for (const [name, path] of Object.entries(vendorFiles)) {
if (existsSync(path)) {
const hash = createHash("sha256")
.update(readFileSync(path))
.digest("hex")
.slice(0, 8);
vendorHashes[name] = hash;
}
}
const localesDir = join(STATIC, "locales");
let localeContent = "";
if (existsSync(localesDir)) {
const localeFiles = readdirSync(localesDir)
.filter((f) => f.endsWith(".json"))
.sort();
for (const f of localeFiles) {
localeContent += readFileSync(join(localesDir, f), "utf-8");
}
}
const localeVersion =
localeContent.length > 0
? createHash("sha256").update(localeContent).digest("hex").slice(0, 8)
: "";
const manifest = {
...assets,
vendor: vendorHashes,
locale_version: localeVersion,
};
writeFileSync(join(DIST, "assets.json"), JSON.stringify(manifest, null, 2));
console.log(" Manifest:", JSON.stringify(manifest, null, 2));
console.log("Done.");