forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating gatsby-plugin-manifest (gatsbyjs#4382)
* add auto image generation support, update docs * fix spelling error and clarify wording * Update docs with shannon's feedback * rewrite since I forgot to save * Update README.md * Update gatsby-node.js * I made some of the language consistent like shannon requested but using the "mode" idea. made some formatting more consistent. Fixed the Hybrid description to be more clear. Hybrid only gerates icons from the provided `icons` array, not a amalgamation of both. * Update package.json Add some more keywords so this plugin will show up for searches for those in our plugin library.
- Loading branch information
1 parent
dbd2267
commit 3e6b0cb
Showing
4 changed files
with
221 additions
and
8 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 } | ||
|
||
// Delete options we won't pass to the manifest.json. | ||
delete manifest.plugins | ||
fs.writeFileSync(`./public/manifest.json`, JSON.stringify(manifest)) | ||
resolve() | ||
delete manifest.icon | ||
|
||
// If icons are not manually defined, use the default icon set. | ||
if (!manifest.icons) { | ||
manifest.icons = defaultIcons | ||
} | ||
|
||
// Determine destination path for icons. | ||
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 icons 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