Skip to content

Commit

Permalink
refactor: replace cache-image with getRemoteFile from gatsby-core-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Mar 22, 2021
1 parent 53a9d5e commit 49dbf9e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 107 deletions.
82 changes: 0 additions & 82 deletions packages/gatsby-source-contentful/src/cache-image.js

This file was deleted.

37 changes: 30 additions & 7 deletions packages/gatsby-source-contentful/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ const {
GraphQLJSON,
GraphQLList,
} = require(`gatsby/graphql`)
const qs = require(`qs`)
const { fetchRemoteFile } = require(`gatsby-core-utils`)
const { generateImageData } = require(`gatsby-plugin-image`)
const {
getGatsbyImageFieldConfig,
} = require(`gatsby-plugin-image/graphql-utils`)

const { stripIndent } = require(`common-tags`)
const qs = require(`qs`)

const cacheImage = require(`./cache-image`)
const downloadWithRetry = require(`./download-with-retry`).default
const {
ImageFormatType,
Expand Down Expand Up @@ -664,7 +665,7 @@ const fluidNodeType = ({ name, getTracedSVG }) => {
}
}

exports.extendNodeType = ({ type, store }) => {
exports.extendNodeType = ({ type, cache }) => {
if (type.name !== `ContentfulAsset`) {
return {}
}
Expand All @@ -674,15 +675,21 @@ exports.extendNodeType = ({ type, store }) => {

const { image, options } = args
const {
file: { contentType },
file: { contentType, url: imgUrl, fileName: name },
} = image

if (contentType.indexOf(`image/`) !== 0) {
return null
}

const absolutePath = await cacheImage(store, image, options)
const extension = path.extname(absolutePath)
const url = createUrl(imgUrl, options)
const extension = path.extname(imgUrl)
const absolutePath = await fetchRemoteFile({
url,
name,
cache,
ext: extension,
})

return traceSVG({
file: {
Expand All @@ -698,7 +705,23 @@ exports.extendNodeType = ({ type, store }) => {

const getDominantColor = async ({ image, options }) => {
try {
const absolutePath = await cacheImage(store, image, options)
const {
file: { contentType, url: imgUrl, fileName: name },
} = image

if (contentType.indexOf(`image/`) !== 0) {
return null
}

const url = createUrl(imgUrl, options)
const extension = path.extname(imgUrl)

const absolutePath = await fetchRemoteFile({
url,
name,
cache,
ext: extension,
})

const pluginSharp = require(`gatsby-plugin-sharp`)
if (!(`getDominantColor` in pluginSharp)) {
Expand Down
41 changes: 23 additions & 18 deletions packages/gatsby-transformer-sqip/src/extend-node-type.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const { resolve } = require(`path`)
const md5File = require(`md5-file`)

const {
DuotoneGradientType,
ImageCropFocusType,
} = require(`gatsby-transformer-sharp/types`)
const { queueImageResizing } = require(`gatsby-plugin-sharp`)
const path = require(`path`)

const Debug = require(`debug`)
const fs = require(`fs-extra`)
const sharp = require(`sharp`)
const md5File = require(`md5-file`)

const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
} = require(`gatsby/graphql`)
const sharp = require(`sharp`)
const { ensureDir } = require(`fs-extra`)
const { queueImageResizing } = require(`gatsby-plugin-sharp`)
const { fetchRemoteFile } = require(`gatsby-core-utils`)
const { createUrl } = require(`gatsby-source-contentful`)
const {
DuotoneGradientType,
ImageCropFocusType,
} = require(`gatsby-transformer-sharp/types`)

const generateSqip = require(`./generate-sqip`)

Expand Down Expand Up @@ -44,11 +45,11 @@ module.exports = async args => {

async function sqipSharp({ type, cache, getNodeAndSavePathDependency, store }) {
const program = store.getState().program
const cacheDir = resolve(
const cacheDir = path.resolve(
`${program.directory}/node_modules/.cache/gatsby-transformer-sqip/`
)

await ensureDir(cacheDir)
await fs.ensureDir(cacheDir)

return {
sqip: {
Expand Down Expand Up @@ -140,14 +141,12 @@ async function sqipContentful({ type, cache, store }) {
schemes: { ImageResizingBehavior, ImageCropFocusType },
} = require(`gatsby-source-contentful`)

const cacheImage = require(`gatsby-source-contentful/cache-image`)

const program = store.getState().program
const cacheDir = resolve(
const cacheDir = path.resolve(
`${program.directory}/node_modules/.cache/gatsby-transformer-sqip/`
)

await ensureDir(cacheDir)
await fs.ensureDir(cacheDir)

return {
sqip: {
Expand Down Expand Up @@ -192,7 +191,7 @@ async function sqipContentful({ type, cache, store }) {
},
async resolve(asset, fieldArgs, context) {
const {
file: { contentType },
file: { contentType, url: imgUrl },
} = asset

if (!contentType.includes(`image/`)) {
Expand Down Expand Up @@ -223,7 +222,13 @@ async function sqipContentful({ type, cache, store }) {
background,
}

const absolutePath = await cacheImage(store, asset, options)
const url = createUrl(imgUrl, options)
const extension = path.extname(imgUrl)
const absolutePath = await fetchRemoteFile({
url,
cache,
ext: extension,
})

const contentDigest = await md5File(absolutePath)

Expand Down

0 comments on commit 49dbf9e

Please sign in to comment.