From 7946f9ff2f0de63fc82c48d24f12706483640b4d Mon Sep 17 00:00:00 2001 From: Derek Lewis Date: Wed, 26 Aug 2026 03:45:53 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=F0=9F=9A=80=EF=BC=9Aminif?= =?UTF-8?q?y=20the=20script=20the=20site=20serves?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The counter shipped as it is written upstream, comments and all. It is squeezed on the way out instead: 9,213 bytes to 5,774, and 3,327 to 2,263 once gzipped, which is the figure that counts. The copy in the repository is untouched, which is what lets both things be true at once: a reader downloads the small one, and the one here stays byte for byte what upstream serves, so `nps verify.vendored` can still tell whether it has fallen behind. A minifier drops comments, and the two lines this one opens with are its terms rather than decoration -- ISC asks for the notice in every copy. They are kept, along with anything marked `@license` or `@preserve`. Images and scripts are both copied rather than rendered, so neither meets a transform; the pass over the output now knows what to do with each. That leaves nothing served here that is not squeezed: markup, styles, marks and now this. Signed-off-by: Derek Lewis Assisted-by: Claude-Code:claude-opus-5 Fixes: https://github.com/OpenINF/openinf.github.io/issues/1548 --- eleventy.config.mjs | 44 ++++++++++++++++++++++++++++++++++++-------- package.json | 1 + pnpm-lock.yaml | 3 +++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/eleventy.config.mjs b/eleventy.config.mjs index 91dc34b46..64997a5fb 100644 --- a/eleventy.config.mjs +++ b/eleventy.config.mjs @@ -1,6 +1,6 @@ import { execFileSync } from 'node:child_process'; import { readdir, readFile, writeFile } from 'node:fs/promises'; -import { join, parse as pathParse } from 'node:path'; +import { extname, join, parse as pathParse } from 'node:path'; import { EleventyI18nPlugin } from '@11ty/eleventy'; import { PATHS } from '@openinf/portal/build/constants'; import { hasViewBox, replaceInlineSvg } from '@openinf/portal/build/inline-svg'; @@ -13,6 +13,7 @@ import markdownItGitHubAlerts from 'markdown-it-github-alerts'; import postcss from 'postcss'; import { compileString } from 'sass'; import { optimize as optimizeSvg } from 'svgo'; +import { minify as minifyJs } from 'terser'; // skipcq: JS-0116 export default async function (eleventyConfig) { @@ -221,7 +222,32 @@ export default async function (eleventyConfig) { return replaceInlineSvg(content, (svg) => shrinkSvg(svg, inline)); }); - // Images are copied rather than rendered, so no transform reaches them. + // What a script says about its own license has to reach whoever receives + // it. The vendored counter is ISC, which asks for the notice in every + // copy, and a minifier drops comments unless told which ones are not + // decoration. + const License = /@license|@preserve|ISC license|GoatCounter:/; + + /** + * Minifies a script, keeping whatever states its terms. + * @param {string} code The script. + * @returns {Promise} The minified script. + */ + const shrinkJs = async (code) => { + const { code: minified } = await minifyJs(code, { + format: { comments: (_node, comment) => License.test(comment.value) }, + }); + + return minified ?? code; + }; + + /** What is squeezed on the way out, and how. */ + const squeezers = { + '.svg': shrinkSvg, + '.js': shrinkJs, + }; + + // Assets are copied rather than rendered, so no transform reaches them. eleventyConfig.on('eleventy.after', async ({ dir }) => { const entries = await readdir(dir.output, { recursive: true, @@ -229,13 +255,15 @@ export default async function (eleventyConfig) { }); await Promise.all( - entries - .filter((entry) => entry.isFile() && entry.name.endsWith('.svg')) - .map(async (entry) => { - const file = join(entry.parentPath, entry.name); + entries.map(async (entry) => { + const squeeze = squeezers[extname(entry.name)]; + + if (!entry.isFile() || squeeze === undefined) return; + + const file = join(entry.parentPath, entry.name); - await writeFile(file, shrinkSvg(await readFile(file, 'utf8'))); - }) + await writeFile(file, await squeeze(await readFile(file, 'utf8'))); + }) ); }); diff --git a/package.json b/package.json index 9a9e356ac..55fb4d1f5 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "stylelint-config-recess-order": "7.8.0", "stylelint-config-standard-scss": "17.0.0", "svgo": "4.0.2", + "terser": "5.50.0", "typescript": "7.0.2", "unified": "11.0.5", "vnu-jar": "26.8.15", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5760cd320..aa3b4d782 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -209,6 +209,9 @@ importers: svgo: specifier: 4.0.2 version: 4.0.2 + terser: + specifier: 5.50.0 + version: 5.50.0 typescript: specifier: 7.0.2 version: 7.0.2