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