Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-filesystem] add publicURL field #3669

Merged
merged 3 commits into from
Jan 26, 2018
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