forgejo/tools/generate-images.js
Gusted b86aefc038
Some checks failed
testing / backend-checks (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / frontend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / security-check (push) Has been skipped
Integration tests for the release process / release-simulation (push) Has been cancelled
/ release (push) Has been cancelled
chore: use sharp to generate images (#7512)
- `tools/generate-images.js` is used to convert SVGs to resized optimized SVGs and resized optimized PNG. Although it would be best to drop generating images from SVG, the usage of these images do not accept a SVG.
- The script relied on two dependencies being installed on-the-fly, this is suboptimal as it means its integrity was not saved in package-lock.json and no specific version was specified which makes reproducible builds harder. `imagemin-zopfli` was not updated in 4 years and seems to use dependency that generate funny message about memory leaks and using no longer maintained dependencies.
- Use [`sharp`](https://sharp.pixelplumbing.com/) to do the image conversion, this installs two binaries on Linux (glibc/musl) and are responsible for 5% of the `node_modules` directory size. Add this to package.json as a dev dependencies to ensure the integrity can be verified and help reproducible builds.
- Drop the `gitea` conversion, I cannot find this being used within Forgejo (my best guess is that e20cd83bc5 dropped the usage of it).
- Resolves forgejo/forgejo#7232

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7512
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-04-11 15:12:50 +00:00

55 lines
1.8 KiB
JavaScript
Executable file

import {optimize} from 'svgo';
import {readFile, writeFile} from 'node:fs/promises';
import {exit} from 'node:process';
import SharpConstructor from 'sharp';
import {fileURLToPath} from 'node:url';
function doExit(err) {
if (err) console.error(err);
exit(err ? 1 : 0);
}
async function generate(svg, path, {size, bg}) {
const outputFile = new URL(path, import.meta.url);
if (String(outputFile).endsWith('.svg')) {
const {data} = optimize(svg, {
plugins: [
'preset-default',
'removeDimensions',
{
name: 'addAttributesToSVGElement',
params: {attributes: [{width: size}, {height: size}]},
},
],
});
await writeFile(outputFile, data);
return;
}
let sharp = (new SharpConstructor(Buffer.from(svg))).resize(size, size).png({compressionLevel: 9, palette: true, effort: 10, quality: 80});
if (bg) {
sharp = sharp.flatten({background: 'white'});
}
sharp.toFile(fileURLToPath(outputFile), (err) => err !== null && console.error(err) && exit(1));
}
async function main() {
const logoSvg = await readFile(new URL('../assets/logo.svg', import.meta.url), 'utf8');
const faviconSvg = await readFile(new URL('../assets/favicon.svg', import.meta.url), 'utf8');
await Promise.all([
generate(logoSvg, '../public/assets/img/logo.svg', {size: 32}),
generate(logoSvg, '../public/assets/img/logo.png', {size: 512}),
generate(faviconSvg, '../public/assets/img/favicon.svg', {size: 32}),
generate(faviconSvg, '../public/assets/img/favicon.png', {size: 180}),
generate(logoSvg, '../public/assets/img/avatar_default.png', {size: 200}),
generate(logoSvg, '../public/assets/img/apple-touch-icon.png', {size: 180, bg: true}),
]);
}
try {
doExit(await main());
} catch (err) {
doExit(err);
}