Skip to content

Commit

Permalink
fix(gatsby-plugin-image): Conditionally require dependencies and give…
Browse files Browse the repository at this point in the history
… better warnings (#28921)

* Better error handling for missing dependencies

* fix eslint

* /reporter
  • Loading branch information
LB committed Jan 11, 2021
1 parent d33793e commit c82c3a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"gatsby": ">=2",
"gatsby-image": "*",
"gatsby-plugin-sharp": "*",
"gatsby-source-filesystem": "*",
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
},
Expand Down
50 changes: 41 additions & 9 deletions packages/gatsby-plugin-image/src/node-apis/image-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import {
Actions,
Store,
} from "gatsby"
import * as PluginSharp from "gatsby-plugin-sharp"
import { createFileNode } from "gatsby-source-filesystem/create-file-node"
import fs from "fs-extra"
import path from "path"
import { ImageProps, SharpProps } from "../utils"
import { watchImage } from "./watcher"
import { createRemoteFileNode, FileSystemNode } from "gatsby-source-filesystem"
import type { FileSystemNode } from "gatsby-source-filesystem"

const supportedTypes = new Set([`image/png`, `image/jpeg`, `image/webp`])
export interface IImageMetadata {
Expand All @@ -25,15 +23,27 @@ export async function createImageNode({
fullPath,
createNodeId,
createNode,
reporter,
}: {
fullPath: string
createNodeId: ParentSpanPluginArgs["createNodeId"]
createNode: Actions["createNode"]
reporter: Reporter
}): Promise<FileSystemNode | undefined> {
if (!fs.existsSync(fullPath)) {
return undefined
}
const file: FileSystemNode = await createFileNode(fullPath, createNodeId, {})

let file: FileSystemNode
try {
const {
createFileNode,
} = require(`gatsby-source-filesystem/create-file-node`)
file = await createFileNode(fullPath, createNodeId, {})
} catch (e) {
reporter.panic(`Please install gatsby-source-filesystem`)
return undefined
}

if (!file) {
return undefined
Expand Down Expand Up @@ -75,6 +85,13 @@ export async function writeImages({
let file: FileSystemNode | undefined
let fullPath
if (process.env.GATSBY_EXPERIMENTAL_REMOTE_IMAGES && isRemoteURL(src)) {
let createRemoteFileNode
try {
;({ createRemoteFileNode } = require(`gatsby-source-filesystem`))
} catch (e) {
reporter.panic(`Please install gatsby-source-filesystem`)
}

try {
file = await createRemoteFileNode({
url: src,
Expand All @@ -89,12 +106,12 @@ export async function writeImages({
return
}
if (
!file.internal.mediaType ||
!file?.internal.mediaType ||
!supportedTypes.has(file.internal.mediaType)
) {
reporter.error(
`The file loaded from ${src} is not a valid image type. Found "${
file.internal.mediaType || `unknown`
file?.internal.mediaType || `unknown`
}"`
)
return
Expand All @@ -106,7 +123,16 @@ export async function writeImages({
reporter.warn(`Could not find image "${src}". Looked for ${fullPath}`)
return
}
file = await createFileNode(fullPath, createNodeId, {})

try {
const {
createFileNode,
} = require(`gatsby-source-filesystem/create-file-node`)

file = await createFileNode(fullPath, createNodeId, {})
} catch (e) {
reporter.panic(`Please install gatsby-source-filesystem`)
}
}

if (!file) {
Expand Down Expand Up @@ -161,15 +187,21 @@ export async function writeImage(
cache: GatsbyCache,
filename: string
): Promise<void> {
let generateImageData
try {
generateImageData = require(`gatsby-plugin-sharp`).generateImageData
} catch (e) {
reporter.panic(`Please install gatsby-plugin-sharp`)
}
try {
const options = { file, args, pathPrefix, reporter, cache }

if (!PluginSharp.generateImageData) {
if (!generateImageData) {
reporter.warn(`Please upgrade gatsby-plugin-sharp`)
return
}
// get standard set of fields from sharp
const sharpData = await PluginSharp.generateImageData(options)
const sharpData = await generateImageData(options)

if (sharpData) {
// Write the image properties to the cache
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-image/src/node-apis/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chokidar, { FSWatcher } from "chokidar"
import { Actions, ParentSpanPluginArgs, GatsbyCache, Reporter } from "gatsby"
import { createImageNode, IImageMetadata, writeImage } from "./image-processing"
import { FileSystemNode } from "gatsby-source-filesystem"
import type { FileSystemNode } from "gatsby-source-filesystem"

let watcher: FSWatcher | undefined

Expand Down Expand Up @@ -34,6 +34,7 @@ export function watchImage({
fullPath: path,
createNodeId,
createNode,
reporter,
})
if (!node) {
reporter.warn(`Could not process image ${path}`)
Expand Down

0 comments on commit c82c3a6

Please sign in to comment.