@@ -6,6 +6,7 @@ import { rgbToHex, calculateImageSizes, getSrcSet, getSizes } from "./utils"
6
6
import { traceSVG , getImageSizeAsync , base64 , batchQueueImageResizing } from "."
7
7
import sharp from "./safe-sharp"
8
8
import { createTransformObject } from "./plugin-options"
9
+ import { reportError } from "./report-error"
9
10
10
11
const DEFAULT_BLURRED_IMAGE_WIDTH = 20
11
12
@@ -49,7 +50,7 @@ const metadataCache = new Map<string, IImageMetadata>()
49
50
export async function getImageMetadata (
50
51
file : FileNode ,
51
52
getDominantColor ?: boolean
52
- ) : Promise < IImageMetadata > {
53
+ ) : Promise < IImageMetadata | undefined > {
53
54
if ( ! getDominantColor ) {
54
55
// If we don't need the dominant color we can use the cheaper size function
55
56
const { width, height, type } = await getImageSizeAsync ( file )
@@ -59,18 +60,24 @@ export async function getImageMetadata(
59
60
if ( metadata && process . env . NODE_ENV !== `test` ) {
60
61
return metadata
61
62
}
62
- const pipeline = sharp ( file . absolutePath )
63
63
64
- const { width, height, density, format } = await pipeline . metadata ( )
64
+ try {
65
+ const pipeline = sharp ( file . absolutePath )
65
66
66
- const { dominant } = await pipeline . stats ( )
67
- // Fallback in case sharp doesn't support dominant
68
- const dominantColor = dominant
69
- ? rgbToHex ( dominant . r , dominant . g , dominant . b )
70
- : `#000000`
67
+ const { width, height, density, format } = await pipeline . metadata ( )
68
+
69
+ const { dominant } = await pipeline . stats ( )
70
+ // Fallback in case sharp doesn't support dominant
71
+ const dominantColor = dominant
72
+ ? rgbToHex ( dominant . r , dominant . g , dominant . b )
73
+ : `#000000`
74
+
75
+ metadata = { width, height, density, format, dominantColor }
76
+ metadataCache . set ( file . internal . contentDigest , metadata )
77
+ } catch ( err ) {
78
+ reportError ( `Failed to process image ${ file . absolutePath } ` , err )
79
+ }
71
80
72
- metadata = { width, height, density, format, dominantColor }
73
- metadataCache . set ( file . internal . contentDigest , metadata )
74
81
return metadata
75
82
}
76
83
@@ -149,7 +156,7 @@ export async function generateImageData({
149
156
150
157
let primaryFormat : ImageFormat | undefined
151
158
if ( useAuto ) {
152
- primaryFormat = normalizeFormat ( metadata . format || file . extension )
159
+ primaryFormat = normalizeFormat ( metadata ? .format || file . extension )
153
160
} else if ( formats . has ( `png` ) ) {
154
161
primaryFormat = `png`
155
162
} else if ( formats . has ( `jpg` ) ) {
@@ -339,7 +346,7 @@ export async function generateImageData({
339
346
imageProps . placeholder = {
340
347
fallback,
341
348
}
342
- } else if ( metadata . dominantColor ) {
349
+ } else if ( metadata ? .dominantColor ) {
343
350
imageProps . backgroundColor = metadata . dominantColor
344
351
}
345
352
0 commit comments