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 Jun 23, 2021
1 parent 06dfa9c commit 78ed287
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 110 deletions.
85 changes: 0 additions & 85 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,10 +14,11 @@ const {
GraphQLJSON,
GraphQLList,
} = require(`gatsby/graphql`)
const qs = require(`qs`)
const { fetchRemoteFile } = require(`gatsby-core-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 @@ -666,7 +667,7 @@ const fluidNodeType = ({ name, getTracedSVG, reporter }) => {
}
}

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

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, reporter)
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 Down Expand Up @@ -712,7 +719,23 @@ exports.extendNodeType = ({ type, store, reporter }) => {
}

try {
const absolutePath = await cacheImage(store, image, options, reporter)
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,
})

if (!(`getDominantColor` in pluginSharp)) {
console.error(
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/extend-node-type`)
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 78ed287

Please sign in to comment.