From 93aa12a58526f5d3429b089de5cb6b5b1f25991e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Fri, 11 Dec 2020 14:03:29 +0100 Subject: [PATCH] make gatsby-plugin-image optional --- .../gatsby-source-contentful/package.json | 2 +- .../src/extend-node-type.js | 53 ++++++++++++------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/packages/gatsby-source-contentful/package.json b/packages/gatsby-source-contentful/package.json index f11bf5d49b3a8..5e1993c7ae08a 100644 --- a/packages/gatsby-source-contentful/package.json +++ b/packages/gatsby-source-contentful/package.json @@ -18,7 +18,6 @@ "contentful": "^7.14.12", "fs-extra": "^9.0.1", "gatsby-core-utils": "^1.6.0-next.0", - "gatsby-plugin-image": "^0.2.0-next.0", "gatsby-plugin-utils": "^0.5.0-next.0", "gatsby-source-filesystem": "^2.7.0-next.2", "is-online": "^8.5.1", @@ -43,6 +42,7 @@ "license": "MIT", "peerDependencies": { "gatsby": "^2.12.1", + "gatsby-plugin-image": "^0.3.0-next.0", "gatsby-plugin-sharp": "^2.6.14" }, "repository": { diff --git a/packages/gatsby-source-contentful/src/extend-node-type.js b/packages/gatsby-source-contentful/src/extend-node-type.js index 8ee27b18e007d..0d1f25b57d19c 100644 --- a/packages/gatsby-source-contentful/src/extend-node-type.js +++ b/packages/gatsby-source-contentful/src/extend-node-type.js @@ -1,8 +1,6 @@ const fs = require(`fs`) const path = require(`path`) const crypto = require(`crypto`) -const { generateImageData } = require(`gatsby-plugin-image`) -const { getGatsbyImageFieldConfig } = require(`gatsby-plugin-image/graphql`) const Promise = require(`bluebird`) const { @@ -152,6 +150,7 @@ const fitMap = new Map([ ]) const resolveGatsbyImageData = async (image, options) => { + const { generateImageData } = require(`gatsby-plugin-image`) const { baseUrl, ...sourceMetadata } = getBasicImageProps(image, options) return generateImageData({ @@ -649,30 +648,46 @@ exports.extendNodeType = ({ type, store, cache, getNodesByType }) => { const fluidNode = fluidNodeType({ name: `ContentfulFluid`, getTracedSVG }) const sizesNode = fluidNodeType({ name: `ContentfulSizes`, getTracedSVG }) - const gatsbyImageData = getGatsbyImageFieldConfig(resolveGatsbyImageData, { - jpegProgressive: { - type: GraphQLBoolean, - defaultValue: true, - }, - resizingBehavior: { - type: ImageResizingBehavior, - }, - cropFocus: { - type: ImageCropFocusType, - }, - quality: { - type: GraphQLInt, - defaultValue: 50, - }, - }) sizesNode.deprecationReason = `Sizes was deprecated in Gatsby v2. It's been renamed to "fluid" https://example.com/write-docs-and-fix-this-example-link` + // gatsby-pugin-image + const getGatsbyImageData = () => { + // @todo check if gatsby-plugin-image is enabled + const gatsbyImageEnabled = false + if (!gatsbyImageEnabled) { + return { + type: GraphQLString, + deprecated: true, + deprecationReason: `Enable gatsby-plugin-image to use this field.`, + resolve: () => null, + } + } + const { getGatsbyImageFieldConfig } = require(`gatsby-plugin-image/graphql`) + + return getGatsbyImageFieldConfig(resolveGatsbyImageData, { + jpegProgressive: { + type: GraphQLBoolean, + defaultValue: true, + }, + resizingBehavior: { + type: ImageResizingBehavior, + }, + cropFocus: { + type: ImageCropFocusType, + }, + quality: { + type: GraphQLInt, + defaultValue: 50, + }, + }) + } + return { fixed: fixedNode, resolutions: resolutionsNode, fluid: fluidNode, sizes: sizesNode, - gatsbyImageData, + gatsbyImageData: getGatsbyImageData(), resize: { type: new GraphQLObjectType({ name: `ContentfulResize`,