Skip to content

Commit 5cb3eb2

Browse files
author
AJ Moon
committed
add auto image generation support, update docs
1 parent 10a1faf commit 5cb3eb2

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed

packages/gatsby-plugin-manifest/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ the created manifest.json.
1919

2020
## How to use
2121

22+
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.
23+
24+
### Manual configuration
25+
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:
26+
2227
```javascript
2328
// In your gatsby-config.js
2429
plugins: [
@@ -52,4 +57,62 @@ plugins: [
5257
];
5358
```
5459

60+
### Hybrid configuration
61+
62+
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:
63+
64+
```javascript
65+
// In your gatsby-config.js
66+
plugins: [
67+
{
68+
resolve: `gatsby-plugin-manifest`,
69+
options: {
70+
name: "GatsbyJS",
71+
short_name: "GatsbyJS",
72+
start_url: "/",
73+
background_color: "#f7f0eb",
74+
theme_color: "#a2466c",
75+
display: "minimal-ui",
76+
icon: src/images/icon.png
77+
icons: [
78+
{
79+
src: `/favicons/android-chrome-192x192.png`,
80+
sizes: `192x192`,
81+
type: `image/png`,
82+
},
83+
{
84+
src: `/favicons/android-chrome-512x512.png`,
85+
sizes: `512x512`,
86+
type: `image/png`,
87+
},
88+
],
89+
},
90+
},
91+
];
92+
```
93+
94+
### Automatic configuration
95+
96+
In the automatic configuration you are responsible for defining the entire web app manifest ecept for the icons, you only provide a hi-res source icon to be used to auto generate the default set of icons. See the example below:
97+
98+
```javascript
99+
// In your gatsby-config.js
100+
plugins: [
101+
{
102+
resolve: `gatsby-plugin-manifest`,
103+
options: {
104+
name: "GatsbyJS",
105+
short_name: "GatsbyJS",
106+
start_url: "/",
107+
background_color: "#f7f0eb",
108+
theme_color: "#a2466c",
109+
display: "minimal-ui",
110+
icon: src/images/icon.png
111+
},
112+
},
113+
];
114+
```
115+
55116
To create `manifest.json`, you need to run `gatsby build`.
117+
118+

packages/gatsby-plugin-manifest/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"dependencies": {
1010
"babel-runtime": "^6.26.0",
11-
"bluebird": "^3.5.0"
11+
"bluebird": "^3.5.0",
12+
"sharp": "^0.20.1"
1213
},
1314
"devDependencies": {
1415
"babel-cli": "^6.26.0",
Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,98 @@
11
const fs = require(`fs`)
22
const Promise = require(`bluebird`)
3+
const sharp = require(`sharp`)
4+
5+
// default icons for generating icons
6+
const defaultIcons = [
7+
{
8+
"src": `icons/icon-48x48.png`,
9+
"sizes": `48x48`,
10+
"type": `image/png`,
11+
},
12+
{
13+
"src": `icons/icon-72x72.png`,
14+
"sizes": `72x72`,
15+
"type": `image/png`,
16+
},
17+
{
18+
"src": `icons/icon-96x96.png`,
19+
"sizes": `96x96`,
20+
"type": `image/png`,
21+
},
22+
{
23+
"src": `icons/icon-144x144.png`,
24+
"sizes": `144x144`,
25+
"type": `image/png`,
26+
},
27+
{
28+
"src": `icons/icon-192x192.png`,
29+
"sizes": `192x192`,
30+
"type": `image/png`,
31+
},
32+
{
33+
"src": `icons/icon-256x256.png`,
34+
"sizes": `256x256`,
35+
"type": `image/png`,
36+
},
37+
{
38+
"src": `icons/icon-384x384.png`,
39+
"sizes": `384x384`,
40+
"type": `image/png`,
41+
},
42+
{
43+
"src": `icons/icon-512x512.png`,
44+
"sizes": `512x512`,
45+
"type": `image/png`,
46+
},
47+
]
48+
49+
sharp.simd(true)
50+
51+
function generateIcons(icons, srcIcon) {
52+
return Promise.map(icons, icon => {
53+
const size = parseInt(icon.sizes.substring(0, icon.sizes.lastIndexOf(`x`)))
54+
const imgPath = `./public/` + icon.src
55+
56+
return sharp(srcIcon)
57+
.resize(size)
58+
.toFile(imgPath)
59+
.then(() => {
60+
})
61+
})
62+
}
363

464
exports.onPostBuild = (args, pluginOptions) =>
565
new Promise(resolve => {
66+
const { icon } = pluginOptions
667
const manifest = { ...pluginOptions }
68+
69+
//cleanup non manifest config from manifest
770
delete manifest.plugins
8-
fs.writeFileSync(`./public/manifest.json`, JSON.stringify(manifest))
9-
resolve()
71+
delete manifest.icon
72+
73+
//if icons are not manually defined use default icon set
74+
if (!manifest.icons) {
75+
manifest.icons = defaultIcons
76+
}
77+
78+
//determine destination path for icons and
79+
const iconPath = `./public/` + manifest.icons[0].src.substring(0, manifest.icons[0].src.lastIndexOf(`/`))
80+
81+
//create destination directory if it doesn't exist
82+
if (!fs.existsSync(iconPath)){
83+
fs.mkdirSync(iconPath)
84+
}
85+
86+
fs.writeFileSync(`${iconPath}/manifest.json`, JSON.stringify(manifest))
87+
88+
// only auto-generate if a src icon is defined
89+
if (icon !== undefined) {
90+
generateIcons(manifest.icons, icon).then(() => {
91+
//images have been generated
92+
console.log(`done`)
93+
resolve()
94+
})
95+
} else {
96+
resolve()
97+
}
1098
})

packages/gatsby-plugin-manifest/src/gatsby-ssr.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import React from "react"
22
import { withPrefix } from "gatsby-link"
33

44
exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
5+
6+
const { icons } = pluginOptions
7+
const iconPath = icons[0].src.substring(0, icons[0].src.lastIndexOf(`/`))
8+
59
setHeadComponents([
610
<link
711
key={`gatsby-plugin-manifest-link`}
812
rel="manifest"
9-
href={withPrefix(`/manifest.json`)}
13+
href={withPrefix(`${iconPath}/manifest.json`)}
1014
/>,
1115
<meta
1216
key={`gatsby-plugin-manifest-meta`}

0 commit comments

Comments
 (0)