diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index c7f7b0dad3aca..bdd40fa64cb10 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -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(), } @@ -82,6 +83,8 @@ const manifestOptions = { describe(`Test plugin manifest options`, () => { beforeEach(() => { fs.writeFileSync.mockReset() + fs.mkdirSync.mockReset() + fs.existsSync.mockReset() sharp.mockClear() }) @@ -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 }) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index f735497c28109..5139b49a91ced 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -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) {