Skip to content

Commit

Permalink
refactor(gatsby-plugin-manifest): don't import fs in ssr code (#29306)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Feb 3, 2021
1 parent 7b6454b commit 563e555
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
15 changes: 1 addition & 14 deletions packages/gatsby-plugin-manifest/src/__tests__/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require(`path`)
const { defaultIcons, doesIconExist, addDigestToPath } = require(`../common`)
const { defaultIcons, addDigestToPath } = require(`../common`)

describe(`gatsby-plugin-manifest`, () => {
describe(`defaultIcons`, () => {
Expand All @@ -8,18 +7,6 @@ describe(`gatsby-plugin-manifest`, () => {
})
})

describe(`doesIconExist`, () => {
it(`returns true if image exists on filesystem`, () => {
const iconSrc = path.resolve(__dirname, `./images/gatsby-logo.png`)
expect(doesIconExist(iconSrc)).toBeTruthy()
})

it(`returns false if image does not exist on filesystem`, () => {
const iconSrc = path.resolve(__dirname, `./images/non-existent-logo.png`)
expect(doesIconExist(iconSrc)).toBeFalsy()
})
})

describe(`addDigestToPath`, () => {
it(`returns unmodified path`, () => {
expect(
Expand Down
16 changes: 16 additions & 0 deletions packages/gatsby-plugin-manifest/src/__tests__/node-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require(`path`)
const { doesIconExist } = require(`../node-helpers`)

describe(`gatsby-plugin-manifest`, () => {
describe(`doesIconExist`, () => {
it(`returns true if image exists on filesystem`, () => {
const iconSrc = path.resolve(__dirname, `./images/gatsby-logo.png`)
expect(doesIconExist(iconSrc)).toBeTruthy()
})

it(`returns false if image does not exist on filesystem`, () => {
const iconSrc = path.resolve(__dirname, `./images/non-existent-logo.png`)
expect(doesIconExist(iconSrc)).toBeFalsy()
})
})
})
18 changes: 0 additions & 18 deletions packages/gatsby-plugin-manifest/src/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "fs"
import sysPath from "path"

exports.favicons = [
Expand Down Expand Up @@ -53,23 +52,6 @@ exports.defaultIcons = [
},
]

/**
* Check if the icon exists on the filesystem
*
* @param {String} srcIcon Path of the icon
*/
exports.doesIconExist = function doesIconExist(srcIcon) {
try {
return fs.statSync(srcIcon).isFile()
} catch (e) {
if (e.code !== `ENOENT`) {
throw e
}

return false
}
}

/**
* @param {string} path The generic path to an icon
* @param {string} digest The digest of the icon provided in the plugin's options.
Expand Down
8 changes: 2 additions & 6 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import * as fs from "fs"
import * as path from "path"
import sharp from "./safe-sharp"
import { createContentDigest, cpuCoreCount, slash } from "gatsby-core-utils"
import {
defaultIcons,
doesIconExist,
addDigestToPath,
favicons,
} from "./common"
import { defaultIcons, addDigestToPath, favicons } from "./common"
import { doesIconExist } from "./node-helpers"

import pluginOptionsSchema from "./pluginOptionsSchema"

Expand Down
18 changes: 18 additions & 0 deletions packages/gatsby-plugin-manifest/src/node-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fs from "fs"

/**
* Check if the icon exists on the filesystem
*
* @param {String} srcIcon Path of the icon
*/
export function doesIconExist(srcIcon) {
try {
return fs.statSync(srcIcon).isFile()
} catch (e) {
if (e.code !== `ENOENT`) {
throw e
}

return false
}
}

0 comments on commit 563e555

Please sign in to comment.