Skip to content

Commit

Permalink
fix(gatsby-plugin-manifest): allow multiple icon paths (#13059)
Browse files Browse the repository at this point in the history
## Description

gatsby-plugin-manifest fails to create images when paths are different.
~~gatsby-plugin-manifest does not delete the include_favicon option when creating the manifest file.~~

## Related Issues

Fixes #13055
  • Loading branch information
me4502 authored and DSchau committed Apr 9, 2019
1 parent 1e74779 commit 5dcde0d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
42 changes: 42 additions & 0 deletions packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ jest.mock(`fs`, () => {
return {
existsSync: jest.fn().mockImplementation(() => true),
writeFileSync: jest.fn(),
mkdirSync: jest.fn(),
readFileSync: jest.fn().mockImplementation(() => `someIconImage`),
statSync: jest.fn(),
}
Expand Down Expand Up @@ -82,6 +83,8 @@ const manifestOptions = {
describe(`Test plugin manifest options`, () => {
beforeEach(() => {
fs.writeFileSync.mockReset()
fs.mkdirSync.mockReset()
fs.existsSync.mockReset()
sharp.mockClear()
})

Expand All @@ -105,6 +108,45 @@ describe(`Test plugin manifest options`, () => {
expect(contents).toMatchSnapshot()
})

it(`correctly works with multiple icon paths`, async () => {
fs.existsSync.mockReturnValue(false)

const size = 48

const pluginSpecificOptions = {
icons: [
{
src: `icons/icon-48x48.png`,
sizes: `${size}x${size}`,
type: `image/png`,
},
{
src: `other-icons/icon-48x48.png`,
sizes: `${size}x${size}`,
type: `image/png`,
},
],
}

await onPostBootstrap(apiArgs, {
...manifestOptions,
...pluginSpecificOptions,
})

const firstIconPath = path.join(
`public`,
path.dirname(`icons/icon-48x48.png`)
)
const secondIconPath = path.join(
`public`,
path.dirname(`other-icons/icon-48x48.png`)
)

const calls = fs.mkdirSync.mock.calls
expect(calls[0][0]).toEqual(firstIconPath)
expect(calls[1][0]).toEqual(secondIconPath)
})

it(`invokes sharp if icon argument specified`, async () => {
fs.statSync.mockReturnValueOnce({ isFile: () => true })

Expand Down
18 changes: 12 additions & 6 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ exports.onPostBootstrap = async ({ reporter }, pluginOptions) => {
}

// Determine destination path for icons.
const iconPath = path.join(`public`, path.dirname(manifest.icons[0].src))

//create destination directory if it doesn't exist
if (!fs.existsSync(iconPath)) {
fs.mkdirSync(iconPath)
}
let paths = {}
manifest.icons.forEach(icon => {
const iconPath = path.join(`public`, path.dirname(icon.src))
if (!paths[iconPath]) {
const exists = fs.existsSync(iconPath)
//create destination directory if it doesn't exist
if (!exists) {
fs.mkdirSync(iconPath)
}
paths[iconPath] = true
}
})

// Only auto-generate icons if a src icon is defined.
if (icon !== undefined) {
Expand Down

0 comments on commit 5dcde0d

Please sign in to comment.