This commit is contained in:
36
plugins/manifest-icons/index.js
Normal file
36
plugins/manifest-icons/index.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const sharp = require('sharp')
|
||||
const fs = require('fs').promises
|
||||
const getDirName = require('path').dirname
|
||||
|
||||
async function writeFile (path, contents, cb) {
|
||||
await fs.mkdir(getDirName(path), { recursive: true })
|
||||
fs.writeFile(path, contents, cb)
|
||||
}
|
||||
|
||||
function generatePngFavicon ({ density, width, height }, sourcePath, dimension) {
|
||||
return sharp(sourcePath, {
|
||||
density: (dimension / Math.max(width, height)) * density
|
||||
})
|
||||
.resize(dimension, dimension)
|
||||
.png()
|
||||
.toBuffer()
|
||||
}
|
||||
|
||||
module.exports = (eleventyConfig) => {
|
||||
eleventyConfig.addAsyncShortcode('manifest_icons', async function (faviconFile, ...dimensions) {
|
||||
const metadata = await sharp(faviconFile).metadata()
|
||||
|
||||
const res = await Promise.all(dimensions.map(async dimension => {
|
||||
const favicon = await generatePngFavicon(metadata, faviconFile, dimension)
|
||||
writeFile(`dist/icons/${dimension}/favicon.png`, favicon)
|
||||
|
||||
return {
|
||||
src: `/icons/${dimension}/favicon.png`,
|
||||
sizes: `${dimension}x${dimension}`,
|
||||
type: 'image/png'
|
||||
}
|
||||
}))
|
||||
|
||||
return JSON.stringify(res)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user