This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
site/plugins/manifest-icons/index.js
Maxim Slipenko a557f77d9c
All checks were successful
continuous-integration/drone/push Build is passing
исправляет проблемы с PWA из cписка Lighthouse
2022-05-20 16:36:26 +03:00

39 lines
1.1 KiB
JavaScript

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'
}
}))
res[res.length - 1].purpose = 'maskable'
return JSON.stringify(res)
})
}