From 64740468ec0d4b392b5c5f372e94a282862e894e Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Sat, 12 Sep 2026 02:09:24 +0000 Subject: [PATCH] fix(frontend): address the review findings on the precompress plugin The summary divided by zero and printed "NaN% smaller" when nothing cleared the extension and size filters. The comment on the extension list said it tracks gzip_types, which was wrong twice over: .svg, .wasm, .webmanifest and .map are absent from that directive, and gzip_static serves a .gz sibling without consulting it at all. Someone editing either list on the strength of that comment would have changed behaviour silently. Storybook builds ran the plugin too, gzipping storybook-static that nginx never serves. It is stripped alongside vite-plugin-pwa, which .storybook already does for the same reason. The Python test could only grep vite.config.js for the call, so a gutted closeBundle or apply: "serve" left it green. A vitest suite now runs the hooks against a temporary directory and checks the archive round-trips, that small and already-compressed files are skipped, and that a rerun does not compress its own output. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01HDXxq3vAxJVGhm1pQuK8V7 --- frontend/.storybook/main.ts | 4 +- frontend/scripts/precompress.ts | 10 +++-- frontend/test/precompress.test.ts | 74 +++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 frontend/test/precompress.test.ts diff --git a/frontend/.storybook/main.ts b/frontend/.storybook/main.ts index 218365dca..d5fa99d89 100644 --- a/frontend/.storybook/main.ts +++ b/frontend/.storybook/main.ts @@ -27,7 +27,9 @@ const config: StorybookConfig = { // no place in Storybook. Flatten the plugin tree (vite plugins can be // arrays of plugins) and strip anything PWA related. function isBlocked(name: string) { - return name.startsWith("vite-plugin-pwa"); + // romm:precompress writes .gz siblings for nginx; storybook-static is + // never served by nginx, so they would be dead weight. + return name.startsWith("vite-plugin-pwa") || name === "romm:precompress"; } function keep(plugin: unknown): unknown[] { if (!plugin) return []; diff --git a/frontend/scripts/precompress.ts b/frontend/scripts/precompress.ts index 31db6f82b..49a0475a3 100644 --- a/frontend/scripts/precompress.ts +++ b/frontend/scripts/precompress.ts @@ -13,8 +13,8 @@ import { extname, join, resolve } from "node:path"; import { constants, gzipSync } from "node:zlib"; import type { Plugin } from "vite"; -// Kept in step with gzip_types in docker/nginx/default.conf. Formats that are -// already compressed (png, woff2, ico) only grow, so they are left alone. +// gzip_static serves a .gz sibling whatever gzip_types says, so this list alone +// decides what is precompressed. Already-compressed formats only grow. const COMPRESSIBLE = new Set([ ".css", ".html", @@ -77,8 +77,10 @@ export function precompress(): Plugin { const mib = (bytes: number) => (bytes / 1024 ** 2).toFixed(1); this.info( - `${count} files, ${mib(raw)} MiB -> ${mib(packed)} MiB ` + - `(${Math.round((1 - packed / raw) * 100)}% smaller)`, + count === 0 + ? "nothing to compress" + : `${count} files, ${mib(raw)} MiB -> ${mib(packed)} MiB ` + + `(${Math.round((1 - packed / raw) * 100)}% smaller)`, ); }, }; diff --git a/frontend/test/precompress.test.ts b/frontend/test/precompress.test.ts new file mode 100644 index 000000000..8ca76fe77 --- /dev/null +++ b/frontend/test/precompress.test.ts @@ -0,0 +1,74 @@ +import { mkdtemp, mkdir, readFile, readdir, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { gunzipSync } from "node:zlib"; +import { describe, expect, it } from "vitest"; +import { precompress } from "../scripts/precompress"; + +/** Run the plugin's build hooks against a throwaway output directory. */ +async function runOn(outDir: string): Promise { + const plugin = precompress(); + const { configResolved, closeBundle } = plugin as unknown as { + configResolved: (config: unknown) => void; + closeBundle: (this: { info: (msg: string) => void }) => Promise; + }; + configResolved({ root: outDir, build: { outDir } }); + await closeBundle.call({ info: () => {} }); +} + +async function fixture( + files: Record, +): Promise { + const dir = await mkdtemp(join(tmpdir(), "precompress-")); + for (const [name, body] of Object.entries(files)) { + await mkdir(join(dir, name, ".."), { recursive: true }); + await writeFile(join(dir, name), body); + } + return dir; +} + +describe("precompress", () => { + it("only runs on builds, after the other plugins have emitted", () => { + const plugin = precompress(); + expect(plugin.apply).toBe("build"); + expect(plugin.enforce).toBe("post"); + }); + + it("writes a .gz sibling whose contents round-trip", async () => { + const body = "const compressible = 1;\n".repeat(200); + const dir = await fixture({ "assets/app.js": body }); + + await runOn(dir); + + const gz = await readFile(join(dir, "assets/app.js.gz")); + expect(gunzipSync(gz).toString()).toBe(body); + expect(gz.byteLength).toBeLessThan(Buffer.byteLength(body)); + }); + + it("skips files below the gzip_min_length nginx uses", async () => { + const dir = await fixture({ "tiny.css": "a{color:red}" }); + + await runOn(dir); + + expect(await readdir(dir)).not.toContain("tiny.css.gz"); + }); + + it("leaves already-compressed formats alone", async () => { + const dir = await fixture({ "logo.png": Buffer.alloc(4096, 7) }); + + await runOn(dir); + + expect(await readdir(dir)).not.toContain("logo.png.gz"); + }); + + it("does not compress its own output on a second run", async () => { + const dir = await fixture({ + "app.js": "const compressible = 1;\n".repeat(200), + }); + + await runOn(dir); + await runOn(dir); + + expect(await readdir(dir)).not.toContain("app.js.gz.gz"); + }); +});