-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Updating gatsby-plugin-manifest #4382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
5cb3eb2
ccc5a76
590a14f
348e96e
ef7e10a
04070fa
acb7374
07f8c96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,11 @@ the created manifest.json. | |
|
|
||
| ## How to use | ||
|
|
||
| There are three configurations in which this plugin will function: manual, hybrid, and auto. What config options you include will determine how the plugin functions. | ||
|
|
||
| ### Manual configuration | ||
| In the manual configuration you are responsible for defining the entire web app manifest and providing the defined icons in the static directory. See the example below: | ||
|
||
|
|
||
| ```javascript | ||
| // In your gatsby-config.js | ||
| plugins: [ | ||
|
|
@@ -52,4 +57,62 @@ plugins: [ | |
| ]; | ||
| ``` | ||
|
|
||
| ### Hybrid configuration | ||
|
||
|
|
||
| In the hybrid configuration you are responsible for defining the entire web app manifest but instead of manually generating the defined icons you provide a hi-res source icon to be used to auto generate your defined icons. See the example below: | ||
|
||
|
|
||
| ```javascript | ||
| // In your gatsby-config.js | ||
| plugins: [ | ||
| { | ||
| resolve: `gatsby-plugin-manifest`, | ||
| options: { | ||
| name: "GatsbyJS", | ||
| short_name: "GatsbyJS", | ||
| start_url: "/", | ||
| background_color: "#f7f0eb", | ||
| theme_color: "#a2466c", | ||
| display: "minimal-ui", | ||
| icon: src/images/icon.png | ||
| icons: [ | ||
| { | ||
| src: `/favicons/android-chrome-192x192.png`, | ||
| sizes: `192x192`, | ||
| type: `image/png`, | ||
| }, | ||
| { | ||
| src: `/favicons/android-chrome-512x512.png`, | ||
| sizes: `512x512`, | ||
| type: `image/png`, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ### Automatic configuration | ||
|
|
||
| In the automatic configuration you are responsible for defining the entire web app manifest except for the icons. You only provide a hi-res source icon. The icons themselves and the needed configuration will be generated. See the example below: | ||
|
|
||
| ```javascript | ||
| // In your gatsby-config.js | ||
| plugins: [ | ||
| { | ||
| resolve: `gatsby-plugin-manifest`, | ||
| options: { | ||
| name: "GatsbyJS", | ||
| short_name: "GatsbyJS", | ||
| start_url: "/", | ||
| background_color: "#f7f0eb", | ||
| theme_color: "#a2466c", | ||
| display: "minimal-ui", | ||
| icon: src/images/icon.png | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| To create `manifest.json`, you need to run `gatsby build`. | ||
|
|
||
|
|
||
| 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() | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd stay consistent with naming things, so either auto here and in the header below, or automatic in both cases. Not a huge deal, just makes it easier to scan the document for relevant information.
Might also make sense to stick with config or configuration and stay consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, will people know generally what this plugin is about? A brief description before the how to use section could be handy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about also rewording the last sentence to read "These three configuration options are explained below. The plugin functions differently depending on which of the three you choose." This would clearly link this paragraph with the following paragraph, and linking is nearly always a great thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All sound good