Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Added support for non-square icon sizes #130

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
697 changes: 436 additions & 261 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"license": "MIT",
"dependencies": {
"css-color-names": "^1.0.0",
"jimp": "^0.6.0",
"jimp": "^0.14.0",
"mime": "^2.4.0"
},
"devDependencies": {
Expand Down
27 changes: 23 additions & 4 deletions src/icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function sanitizeIcon (iconSnippet) {
const arr = parseArray(iconSnippet.size || iconSnippet.sizes)
if (!arr) throw new IconError('Unknown icon sizes.')
const sizes = []
for (let size of arr) sizes.push(+size || parseInt(size))

for (let size of arr) {
typeof arr[0] === 'number' ? sizes.push(+size || parseInt(size)) : sizes.push(size)
}

return {
src: iconSnippet.src,
sizes,
Expand All @@ -28,7 +32,12 @@ function sanitizeIcon (iconSnippet) {
}

function processIcon (currentSize, icon, buffer, mimeType, publicPath, shouldFingerprint) {
const dimensions = `${currentSize}x${currentSize}`
const dimensions = typeof currentSize === 'number' ? `${currentSize}x${currentSize}` : currentSize

if (dimensions.split('x').length === 0) {
throw new IconError(`Unexpected size format: ${currentSize}. Must be a number or string "WxH"'`)
}

const fileName = shouldFingerprint ? `icon_${dimensions}.${generateFingerprint(buffer)}.${mime.getExtension(mimeType)}` : `icon_${dimensions}.${mime.getExtension(mimeType)}`
const iconOutputDir = icon.destination ? joinURI(icon.destination, fileName) : fileName
const iconPublicUrl = joinURI(publicPath, iconOutputDir)
Expand Down Expand Up @@ -63,7 +72,7 @@ function process (sizes, icon, cachedIconsCopy, icons, assets, fingerprint, publ
}

const size = sizes.pop()
if (size > 0) {
if (size > 0 || typeof size === 'string') {
const mimeType = mime.getType(icon.src)
if (!supportedMimeTypes.includes(mimeType)) {
let buffer
Expand All @@ -80,7 +89,17 @@ function process (sizes, icon, cachedIconsCopy, icons, assets, fingerprint, publ

jimp.read(icon.src, (err, img) => {
if (err) throw new IconError(`It was not possible to read '${icon.src}'.`)
img.resize(size, size).getBuffer(mimeType, (err, buffer) => {

let x = size, y = size, tmp

if (typeof size === 'string'){
tmp = size.split('x')
x = Number(tmp[0])
y = Number(tmp[1])
}

img.background(img.getPixelColor(1,1))
img.contain(x, y, jimp.HORIZONTAL_ALIGN_CENTER | jimp.VERTICAL_ALIGN_MIDDLE).getBuffer(mimeType, (err, buffer) => {
if (err) throw new IconError(`It was not possible to retrieve buffer of '${icon.src}'.`)
const processedIcon = processIcon(size, icon, buffer, mimeType, publicPath, fingerprint)
icons.push(processedIcon.manifestIcon)
Expand Down
2 changes: 1 addition & 1 deletion tests/basic/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.b71cbf253213cc7cedb8b846c4e8ea0a.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.b71cbf253213cc7cedb8b846c4e8ea0a.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
2 changes: 1 addition & 1 deletion tests/complex/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-startup-image sizes=1024x1024 href=/icons/ios/startup/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=apple-touch-icon sizes=1024x1024 href=/icons/ios/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=apple-touch-icon sizes=180x180 href=/icons/ios/icon_180x180.ff39561a8e4bf67913868a5caa412712.png><link rel=apple-touch-icon sizes=167x167 href=/icons/ios/icon_167x167.26d2d10196ca0a32493134fb5a4510ba.png><link rel=apple-touch-icon sizes=152x152 href=/icons/ios/icon_152x152.8f7dadbad509ff949e7da5488f44b686.png><link rel=apple-touch-icon sizes=120x120 href=/icons/ios/icon_120x120.463a6f38bcc879bee49facdf208464c4.png><link rel=manifest href=/manifest.aa33c28d24e1121925cfa150e2ec52a9.json crossorigin=use-credentials></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-startup-image sizes=1024x1024 href=/icons/ios/startup/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=apple-touch-icon sizes=1024x1024 href=/icons/ios/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=apple-touch-icon sizes=180x180 href=/icons/ios/icon_180x180.ff39561a8e4bf67913868a5caa412712.png><link rel=apple-touch-icon sizes=167x167 href=/icons/ios/icon_167x167.26d2d10196ca0a32493134fb5a4510ba.png><link rel=apple-touch-icon sizes=152x152 href=/icons/ios/icon_152x152.8f7dadbad509ff949e7da5488f44b686.png><link rel=apple-touch-icon sizes=120x120 href=/icons/ios/icon_120x120.463a6f38bcc879bee49facdf208464c4.png><link rel=manifest href=/manifest.aa33c28d24e1121925cfa150e2ec52a9.json crossorigin=use-credentials></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
2 changes: 1 addition & 1 deletion tests/issue-84/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.81200e1d24c270411245b5a63a4b91e7.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.81200e1d24c270411245b5a63a4b91e7.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/issue-87/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-startup-image sizes=768x768 href=/icons/icon_768x768.f85b060e02c421c52e530e23fe1df50e.png><link rel=apple-touch-startup-image sizes=750x750 href=/icons/icon_750x750.6d93014782b03254bb1f640a67ed0590.png><link rel=apple-touch-startup-image sizes=640x640 href=/icons/icon_640x640.3e80720306149c1cca44f6fa41d3dffd.png><link rel=manifest href=/manifest.24abfacd4846fab249f8fe94967260b9.json></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-startup-image sizes=768x1024 href=/icons/icon_768x1024.9d45001b732385e2214068e3865acb9a.png><link rel=apple-touch-startup-image sizes=750x1334 href=/icons/icon_750x1334.63fbc0bb0bc6036b480cde5d3fb93701.png><link rel=apple-touch-startup-image sizes=640x1136 href=/icons/icon_640x1136.f1e18738dea948159627c5df01731830.png><link rel=manifest href=/manifest.9b84f25c3f21e630a942dd5d41ed5d77.json></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"icons": [
{
"src": "/icons/icon_768x768.f85b060e02c421c52e530e23fe1df50e.png",
"sizes": "768x768",
"src": "/icons/icon_768x1024.9d45001b732385e2214068e3865acb9a.png",
"sizes": "768x1024",
"type": "image/png"
},
{
"src": "/icons/icon_750x750.6d93014782b03254bb1f640a67ed0590.png",
"sizes": "750x750",
"src": "/icons/icon_750x1334.63fbc0bb0bc6036b480cde5d3fb93701.png",
"sizes": "750x1334",
"type": "image/png"
},
{
"src": "/icons/icon_640x640.3e80720306149c1cca44f6fa41d3dffd.png",
"sizes": "640x640",
"src": "/icons/icon_640x1136.f1e18738dea948159627c5df01731830.png",
"sizes": "640x1136",
"type": "image/png"
}
],
Expand Down
2 changes: 1 addition & 1 deletion tests/rgb-background/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.836308dd4105e7779e96d575bbcb3984.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.836308dd4105e7779e96d575bbcb3984.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
2 changes: 1 addition & 1 deletion tests/rgba-background/test/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.82c3c467821f8c1b613bc6f741480697.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.c2a880b33508651abef9.bundle.js></script></body></html>
<!DOCTYPE html><html><head><meta charset=UTF-8><title>Webpack App</title><meta name=apple-mobile-web-app-title content="My Progressive Web App"><meta name=apple-mobile-web-app-capable content=yes><meta name=apple-mobile-web-app-status-bar-style content=default><link rel=apple-touch-icon sizes=1024x1024 href=/icons/icon_1024x1024.35f4891d98af6eb0c3dee2853ea0f190.png><link rel=manifest href=/manifest.82c3c467821f8c1b613bc6f741480697.json><link rel=mask-icon href=/icons/icon_512x512.dd60b11e9762b6e25f6d4ce981a1eeae.svg color=#ffffff></head><body><script type=text/javascript src=/main.1883bc7755d680d6d162.bundle.js></script></body></html>
3 changes: 2 additions & 1 deletion tests/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function run (name, next) {
const outputFilename = outputToTest.substring(outputPathLength)
const testAgainst = testContent.indexOf(outputFilename)

const success = testAgainst > -1 && fs.readFileSync(outputToTest).equals(fs.readFileSync(testTree[testAgainst]))
const isDirectory = fs.existsSync(outputToTest) && fs.lstatSync(outputToTest).isDirectory()
const success = isDirectory || testAgainst > -1 && fs.readFileSync(outputToTest).equals(fs.readFileSync(testTree[testAgainst]))

// if (!success) {
// console.log(`FAILED: "${outputFilename}" AGAINST "${testTree[testAgainst]}..."`)
Expand Down