-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
import { access, mkdir } from "node:fs/promises" | ||
import { access, mkdir, readFile, writeFile } from "node:fs/promises" | ||
import { basename, extname, dirname, join, resolve } from "node:path" | ||
|
||
import sharp from "sharp" | ||
|
||
export async function createRasterFavicons({ vectorPaths, inputDirectory, outputDirectory }) { | ||
let packageInfo = {} | ||
|
||
try { | ||
const packageJson = await readFile(resolve(process.cwd(), `package.json`), `utf8`) | ||
packageInfo = JSON.parse(packageJson) | ||
} catch (error) { | ||
console.error(`Error reading package.json:`, error) | ||
} | ||
|
||
for (const filePath of vectorPaths) { | ||
const fileName = basename(filePath, extname(filePath)) | ||
const baseName = basename(fileName, extname(fileName)) | ||
const subfolder = dirname(filePath.substring(inputDirectory.length)) | ||
const destSubfolder = join(outputDirectory, subfolder) | ||
const rootRelativePath = destSubfolder.split(`/`).slice(1).join(`/`) | ||
const sizes = [180, 192, 512] | ||
const formats = [`png`, `webp`] | ||
let icons = [] | ||
|
||
try { | ||
await access(destSubfolder) | ||
} catch { | ||
await mkdir(destSubfolder, { recursive: true }) | ||
} | ||
|
||
for (const format of [`png`, `webp`]) { | ||
for (const size of [512, 192, 180]) { | ||
if (!(format === `webp` && size === 180)) { | ||
try { | ||
const image = sharp(filePath) | ||
.resize(size) | ||
.toFormat(format, { lossless: true }) | ||
|
||
const outputPath = resolve(destSubfolder, `${baseName}-${size}.${format}`) | ||
await image.toFile(outputPath) | ||
} catch (error) { | ||
console.error(`Error processing ${filePath}:`, error) | ||
} | ||
for (const format of formats) { | ||
for (const size of sizes) { | ||
if (format === `webp` && size === 180) continue | ||
|
||
try { | ||
const image = sharp(filePath) | ||
.resize(size) | ||
.toFormat(format, { lossless: true }) | ||
|
||
const outputPath = resolve(destSubfolder, `${baseName}-${size}.${format}`) | ||
await image.toFile(outputPath) | ||
} catch (error) { | ||
console.error(`Error processing ${filePath}:`, error) | ||
} | ||
|
||
if (size === 180) continue | ||
|
||
icons.push( | ||
{ | ||
src: `/${rootRelativePath}${baseName}-${size}.${format}`, | ||
sizes: `${size}x${size}`, | ||
type: `image/${format}`, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
const webmanifest = { | ||
...(packageInfo.name && { name: packageInfo.name }), | ||
...(packageInfo.description && { description: packageInfo.description }), | ||
icons, | ||
} | ||
|
||
const webmanifestPath = resolve(destSubfolder, `${baseName}.webmanifest`) | ||
try { | ||
await writeFile(webmanifestPath, JSON.stringify(webmanifest, null, `\t`)) | ||
} catch (error) { | ||
console.error(`Error writing ${baseName}.webmanifest file:`, error) | ||
} | ||
} | ||
} |