Skip to content

Commit

Permalink
Add generating webmanifest
Browse files Browse the repository at this point in the history
  • Loading branch information
firefoxic committed Apr 5, 2024
1 parent 6bb2775 commit 200ec91
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ conjure <command> [options]

- `images` — Optimize SVGs and convert raster images to modern formats (Webp and Avif by default).
- `icons` — Optimize SVG icon files.
- `favicons` — Optimize SVG favicon and generate from it raster favicons, including ICO format and all necessary PNG and Webp.
- `favicons` — Optimize SVG favicon and generate from it raster favicons, including ICO format and all necessary PNG and Webp, and also generate a webmanifest.
- `all` — Run all the above commands.

> **Notice**: Individual commands handle the contents of the specified directory. But the general `all` command expects the path to the directory containing the `images`, `icons` and `favicons` directories (see [examples](#examples) below).
Expand Down Expand Up @@ -71,6 +71,7 @@ conjure <command> [options]
- `<origin_name>-180.png` in size `180×180` for old iPhones
- `<origin_name>-192.png` and `<origin_name>-192.webp` in size `192×192`
- `<origin_name>-512.png` and `<origin_name>-512.webp` in size `512×512`
- `<origin_name>.webmanifest` with the `name` and `description` fields from your `package.json` and the `icons` field for the `192` and `512` files

```shell
conjure favicons -i assets/favicons
Expand Down
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Commands
Optimize SVG icon files.
favicons
Optimize SVG favicon and generate from it raster favicons, including ICO format and all necessary PNG and Webp.
Optimize SVG favicon and generate from it raster favicons, including ICO format and all necessary PNG and Webp, and also generate a webmanifest.
all
Run all the above commands.
Expand Down
64 changes: 50 additions & 14 deletions lib/createRasterFavicons.js
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)
}
}
}

0 comments on commit 200ec91

Please sign in to comment.