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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDXxq3vAxJVGhm1pQuK8V7
This commit is contained in:
Sam Dornan
2026-09-12 02:09:24 +00:00
committed by Claude
parent b48f6003b5
commit 64740468ec
3 changed files with 83 additions and 5 deletions

View File

@@ -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 [];

View File

@@ -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)`,
);
},
};

View File

@@ -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<void> {
const plugin = precompress();
const { configResolved, closeBundle } = plugin as unknown as {
configResolved: (config: unknown) => void;
closeBundle: (this: { info: (msg: string) => void }) => Promise<void>;
};
configResolved({ root: outDir, build: { outDir } });
await closeBundle.call({ info: () => {} });
}
async function fixture(
files: Record<string, string | Buffer>,
): Promise<string> {
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");
});
});