-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenerate-favicons.js
More file actions
47 lines (37 loc) · 1.44 KB
/
generate-favicons.js
File metadata and controls
47 lines (37 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
import sharp from 'sharp';
import pngToIco from 'png-to-ico';
import { readFile, writeFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const imgDir = join(__dirname, '..', 'website', 'static', 'img');
async function generateFavicons() {
console.log('Generating favicons...');
// Read the source SVG and strip dark-mode media query for light-mode raster export
const svgSource = await readFile(join(imgDir, 'favicon-source.svg'), 'utf8');
const lightSvg = svgSource.replace(
/@media\s*\(prefers-color-scheme:\s*dark\)\s*\{[^}]*\}/g,
''
);
const svgBuffer = Buffer.from(lightSvg);
// Generate 32x32 PNG then convert to .ico
const png32 = await sharp(svgBuffer)
.resize(32, 32)
.png()
.toBuffer();
const ico = await pngToIco([png32]);
const icoPath = join(imgDir, 'favicon.ico');
await writeFile(icoPath, ico);
console.log(` favicon.ico (32x32): ${(ico.length / 1024).toFixed(1)}KB`);
// Generate 180x180 apple-touch-icon
const touchPath = join(imgDir, 'apple-touch-icon.png');
await sharp(svgBuffer)
.resize(180, 180)
.png()
.toFile(touchPath);
const { size } = await readFile(touchPath).then(b => ({ size: b.length }));
console.log(` apple-touch-icon.png (180x180): ${(size / 1024).toFixed(1)}KB`);
console.log('Done.');
}
generateFavicons().catch(console.error);