-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add auto image generation support, update docs
- Loading branch information
AJ Moon
committed
Mar 28, 2018
1 parent
10a1faf
commit 5cb3eb2
Showing
4 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,98 @@ | ||
const fs = require(`fs`) | ||
const Promise = require(`bluebird`) | ||
const sharp = require(`sharp`) | ||
|
||
// default icons for generating icons | ||
const defaultIcons = [ | ||
{ | ||
"src": `icons/icon-48x48.png`, | ||
"sizes": `48x48`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-72x72.png`, | ||
"sizes": `72x72`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-96x96.png`, | ||
"sizes": `96x96`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-144x144.png`, | ||
"sizes": `144x144`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-192x192.png`, | ||
"sizes": `192x192`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-256x256.png`, | ||
"sizes": `256x256`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-384x384.png`, | ||
"sizes": `384x384`, | ||
"type": `image/png`, | ||
}, | ||
{ | ||
"src": `icons/icon-512x512.png`, | ||
"sizes": `512x512`, | ||
"type": `image/png`, | ||
}, | ||
] | ||
|
||
sharp.simd(true) | ||
|
||
function generateIcons(icons, srcIcon) { | ||
return Promise.map(icons, icon => { | ||
const size = parseInt(icon.sizes.substring(0, icon.sizes.lastIndexOf(`x`))) | ||
const imgPath = `./public/` + icon.src | ||
|
||
return sharp(srcIcon) | ||
.resize(size) | ||
.toFile(imgPath) | ||
.then(() => { | ||
}) | ||
}) | ||
} | ||
|
||
exports.onPostBuild = (args, pluginOptions) => | ||
new Promise(resolve => { | ||
const { icon } = pluginOptions | ||
const manifest = { ...pluginOptions } | ||
|
||
//cleanup non manifest config from manifest | ||
delete manifest.plugins | ||
fs.writeFileSync(`./public/manifest.json`, JSON.stringify(manifest)) | ||
resolve() | ||
delete manifest.icon | ||
|
||
//if icons are not manually defined use default icon set | ||
if (!manifest.icons) { | ||
manifest.icons = defaultIcons | ||
} | ||
|
||
//determine destination path for icons and | ||
const iconPath = `./public/` + manifest.icons[0].src.substring(0, manifest.icons[0].src.lastIndexOf(`/`)) | ||
|
||
//create destination directory if it doesn't exist | ||
if (!fs.existsSync(iconPath)){ | ||
fs.mkdirSync(iconPath) | ||
} | ||
|
||
fs.writeFileSync(`${iconPath}/manifest.json`, JSON.stringify(manifest)) | ||
|
||
// only auto-generate if a src icon is defined | ||
if (icon !== undefined) { | ||
generateIcons(manifest.icons, icon).then(() => { | ||
//images have been generated | ||
console.log(`done`) | ||
resolve() | ||
}) | ||
} else { | ||
resolve() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters