From a16706626b397fffe47b192b835104e01b2977a7 Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 19 Jul 2017 08:26:54 -0400 Subject: [PATCH 1/5] Added the option to use the original image --- .../gatsby-transformer-sharp/package.json | 4 ++- .../src/extend-node-type.js | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-transformer-sharp/package.json b/packages/gatsby-transformer-sharp/package.json index 0e70643b602e5..2bfefdeef3c6b 100644 --- a/packages/gatsby-transformer-sharp/package.json +++ b/packages/gatsby-transformer-sharp/package.json @@ -15,7 +15,9 @@ "author": "Kyle Mathews ", "license": "MIT", "dependencies": { - "bluebird": "^3.5.0" + "bluebird": "^3.5.0", + "image-size": "^0.6.0", + "ncp": "^2.0.0" }, "devDependencies": { "babel-cli": "^6.24.1" diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index 12087f9a8225c..0fb50c1e387f5 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -16,6 +16,11 @@ const { } = require(`gatsby-plugin-sharp`) const sharp = require(`sharp`) +const {promisify} = require('util'); +const ncp = require('ncp'); +const ncpAsync = promisify(ncp); +const sizeOf = require('image-size'); +const path = require('path'); const ImageFormatType = new GraphQLEnumType({ name: `ImageFormat`, @@ -60,6 +65,36 @@ module.exports = ({ type, pathPrefix, getNodeAndSavePathDependency }) => { } return { + original: { + type: new GraphQLObjectType({ + name: `ImageSharpOriginal`, + fields: { + width: { type: GraphQLFloat }, + height: { type: GraphQLFloat }, + src: { type: GraphQLString }, + }, + }), + args: { + }, + 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 publicPath = path.join( + process.cwd(), + `public`, + `static/${imageName}` + ); + + await ncpAsync(details.absolutePath, publicPath); + + return { + width: dimensions.width, + height: dimensions.height, + src: '/static/' + imageName, + } + }, + }, responsiveResolution: { type: new GraphQLObjectType({ name: `ImageSharpResponsiveResolution`, From 681cfedc7091183422e9df33bb557a0f0a8202ab Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 19 Jul 2017 14:08:13 -0400 Subject: [PATCH 2/5] Added util.promisify --- packages/gatsby-transformer-sharp/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-transformer-sharp/package.json b/packages/gatsby-transformer-sharp/package.json index 2bfefdeef3c6b..24eacadec6069 100644 --- a/packages/gatsby-transformer-sharp/package.json +++ b/packages/gatsby-transformer-sharp/package.json @@ -17,7 +17,8 @@ "dependencies": { "bluebird": "^3.5.0", "image-size": "^0.6.0", - "ncp": "^2.0.0" + "ncp": "^2.0.0", + "util.promisify": "^1.0.0" }, "devDependencies": { "babel-cli": "^6.24.1" From a81d9725d69e9d5b6bf5166d49c901fd6cd16600 Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 19 Jul 2017 17:41:39 -0400 Subject: [PATCH 3/5] Typo --- packages/gatsby-transformer-sharp/src/extend-node-type.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index 0fb50c1e387f5..b02e609c6a86a 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -16,7 +16,7 @@ const { } = require(`gatsby-plugin-sharp`) const sharp = require(`sharp`) -const {promisify} = require('util'); +const promisify = require('util.promisify'); const ncp = require('ncp'); const ncpAsync = promisify(ncp); const sizeOf = require('image-size'); From 188426f57c9cba13ce2d0a4268b41f40386e8950 Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 19 Jul 2017 17:52:31 -0400 Subject: [PATCH 4/5] Made sure to not do the work copying if already done --- packages/gatsby-transformer-sharp/package.json | 5 ++--- .../gatsby-transformer-sharp/src/extend-node-type.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/gatsby-transformer-sharp/package.json b/packages/gatsby-transformer-sharp/package.json index 24eacadec6069..c34035f65b848 100644 --- a/packages/gatsby-transformer-sharp/package.json +++ b/packages/gatsby-transformer-sharp/package.json @@ -16,9 +16,8 @@ "license": "MIT", "dependencies": { "bluebird": "^3.5.0", - "image-size": "^0.6.0", - "ncp": "^2.0.0", - "util.promisify": "^1.0.0" + "fs-extra": "^4.0.0", + "image-size": "^0.6.0" }, "devDependencies": { "babel-cli": "^6.24.1" diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index b02e609c6a86a..d989befa4404d 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -17,8 +17,7 @@ const { const sharp = require(`sharp`) const promisify = require('util.promisify'); -const ncp = require('ncp'); -const ncpAsync = promisify(ncp); +const fsExtra = require('fs-extra'); const sizeOf = require('image-size'); const path = require('path'); @@ -86,7 +85,13 @@ module.exports = ({ type, pathPrefix, getNodeAndSavePathDependency }) => { `static/${imageName}` ); - await ncpAsync(details.absolutePath, publicPath); + if (!fsExtra.existsSync(publicPath)) { + fsExtra.copy(details.absolutePath, publicPath, err => { + if (err) { + console.error(`error copying file`, err) + } + }) + } return { width: dimensions.width, From f7597876761247753665fdd4fed4f8f68886198b Mon Sep 17 00:00:00 2001 From: Chiedo Date: Wed, 19 Jul 2017 17:54:55 -0400 Subject: [PATCH 5/5] Removed empty ref --- packages/gatsby-transformer-sharp/src/extend-node-type.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index d989befa4404d..2be6852bf36c9 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -16,7 +16,6 @@ const { } = require(`gatsby-plugin-sharp`) const sharp = require(`sharp`) -const promisify = require('util.promisify'); const fsExtra = require('fs-extra'); const sizeOf = require('image-size'); const path = require('path');