From 7e043896b5ef2d7c62eec73b1e6b041bbe7b028b Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Tue, 23 Jan 2018 21:18:05 +0100 Subject: [PATCH 1/3] add publicURL field to File node to allow copying assets to static directory and getting their paths --- .../src/extend-file-node.js | 44 +++++++++++++++++++ .../src/gatsby-node.js | 2 + 2 files changed, 46 insertions(+) create mode 100644 packages/gatsby-source-filesystem/src/extend-file-node.js diff --git a/packages/gatsby-source-filesystem/src/extend-file-node.js b/packages/gatsby-source-filesystem/src/extend-file-node.js new file mode 100644 index 0000000000000..d8aa1ceb00466 --- /dev/null +++ b/packages/gatsby-source-filesystem/src/extend-file-node.js @@ -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}` + }, + }, + } +} diff --git a/packages/gatsby-source-filesystem/src/gatsby-node.js b/packages/gatsby-source-filesystem/src/gatsby-node.js index 7cb1a9e8b3cea..0ef8ee9436cdd 100644 --- a/packages/gatsby-source-filesystem/src/gatsby-node.js +++ b/packages/gatsby-source-filesystem/src/gatsby-node.js @@ -95,3 +95,5 @@ Please pick a path to an existing directory. }) }) } + +exports.setFieldsOnGraphQLNodeType = require(`./extend-file-node`) From 6d8f17bb2f42159d4bae25650f767e4668bd037a Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 24 Jan 2018 17:05:16 +0100 Subject: [PATCH 2/3] [gatsby-transformer-sharp] add more details when reporting file copy error in ImageSharp.original field, honor path prefix when returning public url, use image name as part of image created in static directory --- .../src/extend-node-type.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index ace0414f57fd0..779d2f136f463 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -115,17 +115,25 @@ 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 + ) } }) } @@ -133,7 +141,7 @@ module.exports = ({ type, pathPrefix, getNodeAndSavePathDependency }) => { return { width: dimensions.width, height: dimensions.height, - src: `/static/` + imageName, + src: `${pathPrefix}/static/${imageName}`, } }, }, From eca5cb06d0dc16c840c8de4f1da73c1f014dd53b Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Thu, 25 Jan 2018 16:32:43 -0800 Subject: [PATCH 3/3] Format --- packages/gatsby-source-filesystem/src/extend-file-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-source-filesystem/src/extend-file-node.js b/packages/gatsby-source-filesystem/src/extend-file-node.js index d8aa1ceb00466..99bf81a29a2ad 100644 --- a/packages/gatsby-source-filesystem/src/extend-file-node.js +++ b/packages/gatsby-source-filesystem/src/extend-file-node.js @@ -2,7 +2,7 @@ const { GraphQLString } = require(`graphql`) const fs = require(`fs-extra`) const path = require(`path`) -module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = "" }) => { +module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => { if (type.name !== `File`) { return {} }