Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/gatsby-source-filesystem/src/extend-file-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { GraphQLString } = require(`graphql`)
const fs = require(`fs-extra`)
const path = require(`path`)

module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = "" }) => {
if (type.name !== `File`) {
return {}
}

return {
publicURL: {
type: GraphQLString,
args: {},
resolve: (file, fieldArgs, context) => {
const details = getNodeAndSavePathDependency(file.id, context.path)
const fileName = `${file.name}-${file.internal.contentDigest}${
details.ext
}`

const publicPath = path.join(
process.cwd(),
`public`,
`static`,
fileName
)

if (!fs.existsSync(publicPath)) {
fs.copy(details.absolutePath, publicPath, err => {
if (err) {
console.error(
`error copying file from ${
details.absolutePath
} to ${publicPath}`,
err
)
}
})
}

return `${pathPrefix}/static/${fileName}`
},
},
}
}
2 changes: 2 additions & 0 deletions packages/gatsby-source-filesystem/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ Please pick a path to an existing directory.
})
})
}

exports.setFieldsOnGraphQLNodeType = require(`./extend-file-node`)
16 changes: 12 additions & 4 deletions packages/gatsby-transformer-sharp/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,33 @@ module.exports = ({ type, pathPrefix, getNodeAndSavePathDependency }) => {
async resolve(image, fieldArgs, context) {
const details = getNodeAndSavePathDependency(image.parent, context.path)
const dimensions = sizeOf(details.absolutePath)
const imageName = `${image.internal.contentDigest}${details.ext}`
const imageName = `${details.name}-${image.internal.contentDigest}${
details.ext
}`
const publicPath = path.join(
process.cwd(),
`public`,
`static/${imageName}`
`static`,
imageName
)

if (!fsExtra.existsSync(publicPath)) {
fsExtra.copy(details.absolutePath, publicPath, err => {
if (err) {
console.error(`error copying file`, err)
console.error(
`error copying file from ${
details.absolutePath
} to ${publicPath}`,
err
)
}
})
}

return {
width: dimensions.width,
height: dimensions.height,
src: `/static/` + imageName,
src: `${pathPrefix}/static/${imageName}`,
}
},
},
Expand Down