v4.1 - Admin/onboarding
This commit is contained in:
@@ -4,6 +4,7 @@ import { join, extname, basename } from 'node:path';
|
||||
|
||||
const MAX_WIDTH = 1600;
|
||||
const MIN_BYTES_TO_OPTIMISE = 250 * 1024;
|
||||
const OPTIMISE_WEBP = process.env.OPTIMISE_WEBP === '1';
|
||||
|
||||
const dirs = ['src/lib/images', 'static/images'];
|
||||
|
||||
@@ -31,6 +32,7 @@ async function optimiseFile(file) {
|
||||
} else if (ext === '.jpg' || ext === '.jpeg') {
|
||||
buf = await pipeline.jpeg({ quality: 82, mozjpeg: true }).toBuffer();
|
||||
} else if (ext === '.webp') {
|
||||
if (!OPTIMISE_WEBP) return { file, original: (await stat(file)).size, optimised: (await stat(file)).size, skipped: true };
|
||||
buf = await pipeline.webp({ quality: 80, effort: 6 }).toBuffer();
|
||||
} else {
|
||||
return null;
|
||||
@@ -54,6 +56,10 @@ for (const dir of dirs) {
|
||||
const file = join(dir, name);
|
||||
const s = await stat(file);
|
||||
if (s.size < MIN_BYTES_TO_OPTIMISE) continue;
|
||||
if (/\.webp$/i.test(name) && !OPTIMISE_WEBP) {
|
||||
console.log(`${basename(file).padEnd(58)} ${(s.size / 1024).toFixed(0).padStart(5)}KB (skipped: existing WebP)`);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const res = await optimiseFile(file);
|
||||
if (!res) continue;
|
||||
|
||||
Reference in New Issue
Block a user