diff --git a/packages/gatsby-transformer-sharp/package.json b/packages/gatsby-transformer-sharp/package.json index 0e70643b602e5..c34035f65b848 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", + "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 12087f9a8225c..2be6852bf36c9 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -16,6 +16,9 @@ const { } = require(`gatsby-plugin-sharp`) const sharp = require(`sharp`) +const fsExtra = require('fs-extra'); +const sizeOf = require('image-size'); +const path = require('path'); const ImageFormatType = new GraphQLEnumType({ name: `ImageFormat`, @@ -60,6 +63,42 @@ 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}` + ); + + if (!fsExtra.existsSync(publicPath)) { + fsExtra.copy(details.absolutePath, publicPath, err => { + if (err) { + console.error(`error copying file`, err) + } + }) + } + + return { + width: dimensions.width, + height: dimensions.height, + src: '/static/' + imageName, + } + }, + }, responsiveResolution: { type: new GraphQLObjectType({ name: `ImageSharpResponsiveResolution`,