diff --git a/index.css b/index.css index b15b300ce..ebbc6efd4 100644 --- a/index.css +++ b/index.css @@ -16,7 +16,7 @@ (http://localhost:5173) and packaged (app://netcatty), regardless of where the bundled CSS file ends up. A relative ./fonts/... would be resolved against dist/assets/index-*.css in production and 404. */ - src: url("/fonts/SymbolsNerdFontMono-Regular.ttf") format("truetype"); + src: url("/fonts/SymbolsNerdFontMono-Regular.woff2") format("woff2"); font-weight: normal; font-style: normal; font-display: block; diff --git a/public/fonts/SymbolsNerdFontMono-Regular.ttf b/public/fonts/SymbolsNerdFontMono-Regular.ttf deleted file mode 100644 index d898eea9d..000000000 Binary files a/public/fonts/SymbolsNerdFontMono-Regular.ttf and /dev/null differ diff --git a/public/fonts/SymbolsNerdFontMono-Regular.woff2 b/public/fonts/SymbolsNerdFontMono-Regular.woff2 new file mode 100644 index 000000000..cd140e13b Binary files /dev/null and b/public/fonts/SymbolsNerdFontMono-Regular.woff2 differ diff --git a/vite.config.test.ts b/vite.config.test.ts index 91c419c7d..20b801869 100644 --- a/vite.config.test.ts +++ b/vite.config.test.ts @@ -1,6 +1,10 @@ import assert from "node:assert/strict"; -import { readFileSync } from "node:fs"; +import { existsSync, readFileSync } from "node:fs"; import test from "node:test"; +import { + isFontsourceStylesheetId, + stripFontsourceWoffFallbacks, +} from "./vite.config.ts"; test("vite dev CSP allows eval only for development runtime", () => { const source = readFileSync(new URL("./vite.config.ts", import.meta.url), "utf8"); @@ -10,3 +14,48 @@ test("vite dev CSP allows eval only for development runtime", () => { assert.match(source, /transformIndexHtml/); assert.doesNotMatch(source, /index\.html.*unsafe-eval/); }); + +test("isFontsourceStylesheetId matches POSIX, Windows, and query-suffixed @fontsource CSS paths", () => { + assert.equal(isFontsourceStylesheetId("/repo/node_modules/@fontsource/mona-sans/400.css"), true); + assert.equal(isFontsourceStylesheetId("C:\\repo\\node_modules\\@fontsource\\jetbrains-mono\\600.css?used"), true); + assert.equal(isFontsourceStylesheetId("/repo/index.css"), false); + assert.equal(isFontsourceStylesheetId("/repo/node_modules/@fontsource/mona-sans/files/font.woff2"), false); +}); + +test("stripFontsourceWoffFallbacks removes redundant .woff fallbacks while preserving .woff2 and woff-only declarations", () => { + const input = [ + "@font-face {", + " font-family: 'Mona Sans';", + " src: url(./files/mona-sans-latin-400-normal.woff2) format('woff2'), url(./files/mona-sans-latin-400-normal.woff) format('woff');", + "}", + "@font-face {", + ' font-family: "Reversed";', + ' src: url("./rev.woff") format("woff"), url("./rev.woff2") format("woff2");', + "}", + "@font-face {", + " font-family: 'LegacyOnly';", + " src: url(./legacy.woff) format('woff');", + "}", + ].join("\n"); + + const output = stripFontsourceWoffFallbacks(input); + assert.match(output, /src: url\(\.\/files\/mona-sans-latin-400-normal\.woff2\) format\('woff2'\);/); + assert.doesNotMatch(output, /mona-sans-latin-400-normal\.woff\b/); + assert.match(output, /src: url\("\.\/rev\.woff2"\) format\("woff2"\);/); + assert.doesNotMatch(output, /rev\.woff"\)/); + assert.match(output, /src: url\(\.\/legacy\.woff\) format\('woff'\);/); +}); + +test("bundled Nerd Font uses valid WOFF2 binary and retains its license", () => { + const woff2Url = new URL("./public/fonts/SymbolsNerdFontMono-Regular.woff2", import.meta.url); + const ttfUrl = new URL("./public/fonts/SymbolsNerdFontMono-Regular.ttf", import.meta.url); + const licenseUrl = new URL("./public/fonts/SymbolsNerdFont-LICENSE.txt", import.meta.url); + const indexCss = readFileSync(new URL("./index.css", import.meta.url), "utf8"); + + assert.equal(existsSync(woff2Url), true); + assert.equal(existsSync(ttfUrl), false); + assert.equal(existsSync(licenseUrl), true); + assert.equal(readFileSync(woff2Url).subarray(0, 4).toString("ascii"), "wOF2"); + assert.match(indexCss, /url\("\/fonts\/SymbolsNerdFontMono-Regular\.woff2"\) format\("woff2"\)/); + assert.doesNotMatch(indexCss, /SymbolsNerdFontMono-Regular\.ttf/); +}); diff --git a/vite.config.ts b/vite.config.ts index 8e041a68b..40c4cfce0 100755 --- a/vite.config.ts +++ b/vite.config.ts @@ -65,6 +65,42 @@ const warnOnOutdatedOptimizeDep = (): Plugin => ({ }, }); +const WOFF2_SOURCE_RE = /url\(\s*['"]?[^)'"]+\.woff2(?:[?#][^)'"]*)?['"]?\s*\)/i; +const TRAILING_WOFF_FALLBACK_RE = /,\s*url\(\s*['"]?[^)'"]+\.woff(?:[?#][^)'"]*)?['"]?\s*\)\s*format\(\s*['"]woff['"]\s*\)/gi; +const LEADING_WOFF_FALLBACK_RE = /url\(\s*['"]?[^)'"]+\.woff(?:[?#][^)'"]*)?['"]?\s*\)\s*format\(\s*['"]woff['"]\s*\)\s*,\s*/gi; +const FONTSOURCE_CSS_ID_RE = /(?:^|[\\/])node_modules[\\/]@fontsource[\\/].+\.css(?:\?.*)?$/i; + +export function isFontsourceStylesheetId(id: string): boolean { + return FONTSOURCE_CSS_ID_RE.test(id); +} + +/** + * Electron/Chromium always supports WOFF2; strip redundant legacy `.woff` + * fallbacks from `@fontsource/*` stylesheets when a `.woff2` source is present + * so Vite does not emit unused `.woff` assets into `dist/assets/`. + */ +export function stripFontsourceWoffFallbacks(css: string): string { + return css.replace(/src\s*:[^;]+;/gi, (declaration) => { + if (!WOFF2_SOURCE_RE.test(declaration)) { + return declaration; + } + return declaration + .replace(TRAILING_WOFF_FALLBACK_RE, '') + .replace(LEADING_WOFF_FALLBACK_RE, ''); + }); +} + +const stripFontsourceWoffPlugin = (): Plugin => ({ + name: 'strip-fontsource-woff-fallbacks', + enforce: 'pre', + transform(code, id) { + if (!isFontsourceStylesheetId(id) || !code.includes('.woff')) return null; + const next = stripFontsourceWoffFallbacks(code); + if (next === code) return null; + return { code: next, map: null }; + }, +}); + const VENDOR_CHUNK_PACKAGES: Array<[chunk: string, packages: string[]]> = [ ['vendor-radix', ['@radix-ui/']], ['vendor-xterm', ['@xterm/']], @@ -134,7 +170,7 @@ export default defineConfig(() => { }, }, }, - plugins: [suppressMonacoSourcemapWarning(), devContentSecurityPolicyPlugin(), warnOnOutdatedOptimizeDep(), tailwindcss(), react()], + plugins: [suppressMonacoSourcemapWarning(), devContentSecurityPolicyPlugin(), warnOnOutdatedOptimizeDep(), stripFontsourceWoffPlugin(), tailwindcss(), react()], optimizeDeps: { include: [ // Keep the renderer alias pre-bundled so dev serves ESM instead of raw CommonJS.