Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
fix(icon): better unusual input icon handling
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
kevinmarrec committed Sep 29, 2022
1 parent 8c52b11 commit 8c1f8e2
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/generate.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const consola = require('consola')
const { remove, mkdirp } = require('fs-extra')
const { join } = require('pathe')
const sharp = require('sharp')
Expand All @@ -13,7 +14,7 @@ async function generate ({ input, distDir, sizes, maskablePadding, splash, hash
// Resized icons (purpose: any)
await Promise.all(sizes.map(size =>
sharp(input)
.resize(Math.floor(size))
.resize(Math.floor(size), Math.floor(size))
.toFile(join(distDir, `${size}x${size}${hash}.png`))
))

Expand All @@ -28,7 +29,7 @@ async function generate ({ input, distDir, sizes, maskablePadding, splash, hash
}
}).composite([{
input: await sharp(input)
.resize(Math.floor(size * (1 - maskablePadding / 100)))
.resize(Math.floor(size * (1 - maskablePadding / 100)), Math.floor(size * (1 - maskablePadding / 100)))
.toBuffer()
}])
.toFile(join(distDir, `${size}x${size}.maskable${hash}.png`))
Expand All @@ -39,17 +40,21 @@ async function generate ({ input, distDir, sizes, maskablePadding, splash, hash
if (splash) {
distDir = join(distDir, `../${splash.targetDir}`)
await ensureDir(distDir)
await Promise.all(splash.devices.map(device =>
sharp({
create: {
width: device.width,
height: device.height,
channels: 4,
background: splash.backgroundColor
}
}).composite([{ input }])
.toFile(join(distDir, `${device.width}x${device.height}${hash}.png`))
))
await Promise.all(splash.devices.map(async (device) => {
try {
await sharp({
create: {
width: device.width,
height: device.height,
channels: 4,
background: splash.backgroundColor
}
}).composite([{ input }])
.toFile(join(distDir, `${device.width}x${device.height}${hash}.png`))
} catch (err) {
consola.warn(`[PWA] Failed to generate splash (${device.width}x${device.height}) because icon input has larger dimensions`)
}
}))
}
}

Expand Down

0 comments on commit 8c1f8e2

Please sign in to comment.