From a9132a53eac37f713e8cb8a8246c62f4f8d8f142 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 07:49:53 +0200 Subject: [PATCH] chore(deps): update sharp (#35539) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Lennart --- packages/gatsby-plugin-manifest/package.json | 2 +- packages/gatsby-plugin-sharp/README.md | 10 +- packages/gatsby-plugin-sharp/package.json | 4 +- .../src/__tests__/__snapshots__/index.js.snap | 42 ++--- .../src/__tests__/plugin-options.ts | 8 +- .../gatsby-plugin-sharp/src/gatsby-node.js | 25 +++ .../gatsby-plugin-sharp/src/image-data.ts | 2 +- packages/gatsby-plugin-sharp/src/index.js | 2 +- .../{plugin-options.js => plugin-options.ts} | 147 ++++++++++++++---- .../src/{process-file.js => process-file.ts} | 82 ++++------ .../placeholder-handler.ts | 2 +- .../package.json | 2 +- packages/gatsby-sharp/package.json | 4 +- packages/gatsby-source-shopify/package.json | 4 +- packages/gatsby-source-wordpress/package.json | 2 +- .../gatsby-transformer-sharp/package.json | 4 +- packages/gatsby/package.json | 2 +- yarn.lock | 48 +++--- 18 files changed, 240 insertions(+), 152 deletions(-) rename packages/gatsby-plugin-sharp/src/{plugin-options.js => plugin-options.ts} (55%) rename packages/gatsby-plugin-sharp/src/{process-file.js => process-file.ts} (70%) diff --git a/packages/gatsby-plugin-manifest/package.json b/packages/gatsby-plugin-manifest/package.json index edf8819d17f60..3283b1ee73c76 100644 --- a/packages/gatsby-plugin-manifest/package.json +++ b/packages/gatsby-plugin-manifest/package.json @@ -11,7 +11,7 @@ "gatsby-core-utils": "^3.21.0-next.2", "gatsby-plugin-utils": "^3.15.0-next.2", "semver": "^7.3.7", - "sharp": "^0.30.3" + "sharp": "^0.30.7" }, "devDependencies": { "@babel/cli": "^7.15.4", diff --git a/packages/gatsby-plugin-sharp/README.md b/packages/gatsby-plugin-sharp/README.md index a9f30f6843982..6d6cbeba925d9 100644 --- a/packages/gatsby-plugin-sharp/README.md +++ b/packages/gatsby-plugin-sharp/README.md @@ -20,7 +20,9 @@ of PNGs then it can significantly reduce build times. ## Install -`npm install gatsby-plugin-sharp` +```shell +npm install gatsby-plugin-sharp +``` ## How to use @@ -32,8 +34,8 @@ plugins: [ options: { // Defaults used for gatsbyImageData and StaticImage defaults: {}, - // Set to false to allow builds to continue on image errors - failOnError: true, + // Set to none to allow builds to continue on image errors + failOn: `none`, // deprecated options and their defaults: base64Width: 20, forceBase64Format: ``, // valid formats: png,jpg,webp @@ -50,7 +52,7 @@ plugins: [ - `defaults`: default values used for `gatsbyImageData` and `StaticImage` from [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image). Available options are: `formats`,`placeholder`,`quality`,`breakpoints`,`backgroundColor`,`tracedSVGOptions`,`blurredOptions`,`jpgOptions`,`pngOptions`,`webpOptions`,`avifOptions`. For details of these, see [the reference guide](https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image). -- `failOnError`: default = `true`. By default builds will fail if there is a corrupted image. Set to false to continue the build on error. The image will return `undefined`. +- `failOn`: default = `warning`. By default builds will fail if there is a corrupted image. Set to `none` to continue the build on error. The image will return `undefined`. You can customize this option, see [`options.failOn`](https://sharp.pixelplumbing.com/api-constructor#parameters). Other options are deprecated, and should only be used for the legacy `fixed` and `fluid` functions. diff --git a/packages/gatsby-plugin-sharp/package.json b/packages/gatsby-plugin-sharp/package.json index f5a23f1b1ae18..1068956b84219 100644 --- a/packages/gatsby-plugin-sharp/package.json +++ b/packages/gatsby-plugin-sharp/package.json @@ -23,13 +23,13 @@ "probe-image-size": "^7.2.3", "progress": "^2.0.3", "semver": "^7.3.7", - "sharp": "^0.30.3", + "sharp": "^0.30.7", "svgo": "1.3.2" }, "devDependencies": { "@babel/cli": "^7.15.4", "@babel/core": "^7.15.5", - "@types/sharp": "^0.30.0", + "@types/sharp": "^0.30.5", "babel-preset-gatsby-package": "^2.21.0-next.0", "cross-env": "^7.0.3", "gatsby-plugin-image": "^2.21.0-next.2" diff --git a/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap index 3429438aaca65..ea17e500aba82 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap @@ -85,7 +85,7 @@ exports[`gatsby-plugin-sharp fixed correctly infers the width when only the heig "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -142,7 +142,7 @@ exports[`gatsby-plugin-sharp fixed does not warn when the requested width is equ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -185,7 +185,7 @@ exports[`gatsby-plugin-sharp fixed warns when the requested width is greater tha "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -256,7 +256,7 @@ exports[`gatsby-plugin-sharp fluid accepts srcSet breakpoints 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -299,7 +299,7 @@ exports[`gatsby-plugin-sharp fluid adds pathPrefix if defined 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -382,7 +382,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -464,7 +464,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -546,7 +546,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -628,7 +628,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -688,7 +688,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -752,7 +752,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -816,7 +816,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -880,7 +880,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -916,7 +916,7 @@ exports[`gatsby-plugin-sharp fluid does not change the arguments object it is gi "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -987,7 +987,7 @@ exports[`gatsby-plugin-sharp fluid ensure maxWidth is in srcSet breakpoints 1`] "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1082,7 +1082,7 @@ exports[`gatsby-plugin-sharp fluid infers the maxWidth if only maxHeight is give "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1125,7 +1125,7 @@ exports[`gatsby-plugin-sharp fluid keeps original file name 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1189,7 +1189,7 @@ exports[`gatsby-plugin-sharp fluid prevents duplicate breakpoints 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1260,7 +1260,7 @@ exports[`gatsby-plugin-sharp fluid reject any breakpoints larger than the origin "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1318,7 +1318,7 @@ exports[`gatsby-plugin-sharp queueImageResizing file name works with spaces & sp "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1361,7 +1361,7 @@ exports[`gatsby-plugin-sharp queueImageResizing should round height when auto-ca "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, - "failOnError": true, + "failOn": "warning", "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, diff --git a/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts b/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts index e9d74afa5fcf5..02f0376579477 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts +++ b/packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts @@ -1,15 +1,15 @@ import { testPluginOptionsSchema } from "gatsby-plugin-utils" import { pluginOptionsSchema } from "../../gatsby-node" -import { doMergeDefaults } from "../plugin-options" +import { doMergeDefaults, PluginOptionsDefaults } from "../plugin-options" -const defaults = { +const defaults: PluginOptionsDefaults = { formats: [`auto`, `webp`], placeholder: `dominantColor`, quality: 50, breakpoints: [100, 200], backgroundColor: `rebeccapurple`, tracedSVGOptions: {}, - blurredOptions: { quality: 20 }, + blurredOptions: { width: 20 }, jpgOptions: { quality: 20 }, pngOptions: { quality: 20 }, webpOptions: { quality: 20 }, @@ -74,7 +74,7 @@ describe(`plugin defaults`, () => { }, "backgroundColor": "rebeccapurple", "blurredOptions": Object { - "quality": 20, + "width": 20, }, "breakpoints": Array [ 100, diff --git a/packages/gatsby-plugin-sharp/src/gatsby-node.js b/packages/gatsby-plugin-sharp/src/gatsby-node.js index 919d9bee0c4f0..e5abb86aac8ae 100644 --- a/packages/gatsby-plugin-sharp/src/gatsby-node.js +++ b/packages/gatsby-plugin-sharp/src/gatsby-node.js @@ -231,7 +231,12 @@ exports.pluginOptionsSchema = ({ Joi }) => ), stripMetadata: Joi.boolean().default(true), defaultQuality: Joi.number().default(50), + // TODO(v5): Remove deprecated failOnError option failOnError: Joi.boolean().default(true), + failOn: Joi.any() + .valid(`none`, `truncated`, `error`, `warning`) + .default(`warning`) + .description(`Level of sensitivity to invalid images`), defaults: Joi.object({ formats: Joi.array().items( Joi.string().valid(`auto`, `png`, `jpg`, `webp`, `avif`) @@ -255,4 +260,24 @@ exports.pluginOptionsSchema = ({ Joi }) => }).description( `Default options used by gatsby-plugin-image. \nSee https://gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image/` ), + }).custom(value => { + const shouldNotFailOnError = !value.failOnError + + if (shouldNotFailOnError) { + // show this warning only once in main process + if (!process.env.GATSBY_WORKER_ID) { + console.warn( + `[gatsby-plugin-sharp]: The "failOnError" option is deprecated. Please use "failOn" instead.` + ) + } + + return { + ...value, + failOn: `none`, + } + } + + return { + ...value, + } }) diff --git a/packages/gatsby-plugin-sharp/src/image-data.ts b/packages/gatsby-plugin-sharp/src/image-data.ts index 5b6de4cf2e1d3..a6c34cb11ea02 100644 --- a/packages/gatsby-plugin-sharp/src/image-data.ts +++ b/packages/gatsby-plugin-sharp/src/image-data.ts @@ -62,7 +62,7 @@ export async function getImageMetadata( } try { - const pipeline = sharp({ failOnError: !!getPluginOptions().failOnError }) + const pipeline = sharp({ failOn: getPluginOptions().failOn }) fs.createReadStream(file.absolutePath).pipe(pipeline) diff --git a/packages/gatsby-plugin-sharp/src/index.js b/packages/gatsby-plugin-sharp/src/index.js index 1e30b2445e0f5..f770efd3f5912 100644 --- a/packages/gatsby-plugin-sharp/src/index.js +++ b/packages/gatsby-plugin-sharp/src/index.js @@ -262,7 +262,7 @@ async function generateBase64({ file, args = {}, reporter }) { }) let pipeline try { - pipeline = !options.failOnError ? sharp({ failOnError: false }) : sharp() + pipeline = sharp({ failOn: pluginOptions.failOn }) if (!options.rotate) { pipeline.rotate() diff --git a/packages/gatsby-plugin-sharp/src/plugin-options.js b/packages/gatsby-plugin-sharp/src/plugin-options.ts similarity index 55% rename from packages/gatsby-plugin-sharp/src/plugin-options.js rename to packages/gatsby-plugin-sharp/src/plugin-options.ts index 5c0f4d7844fb6..84e5663ed9f84 100644 --- a/packages/gatsby-plugin-sharp/src/plugin-options.js +++ b/packages/gatsby-plugin-sharp/src/plugin-options.ts @@ -1,4 +1,68 @@ -const _ = require(`lodash`) +import type { ISharpGatsbyImageArgs, Fit } from "gatsby-plugin-image" +import { pickBy, defaults, mergeWith, omitBy, isNil, identity } from "lodash" +import type { FailOnOptions, SharpOptions } from "sharp" + +export type PluginOptionsDefaults = Pick< + ISharpGatsbyImageArgs, + | "formats" + | "placeholder" + | "quality" + | "breakpoints" + | "backgroundColor" + | "tracedSVGOptions" + | "blurredOptions" + | "jpgOptions" + | "pngOptions" + | "webpOptions" + | "avifOptions" +> + +export interface ISharpPluginOptions { + base64Width?: number + forceBase64Format?: "png" | "webp" | "jpg" | string + useMozJpeg?: boolean + stripMetadata?: boolean + lazyImageGeneration?: boolean + defaultQuality: number + failOn?: SharpOptions["failOn"] + defaults?: PluginOptionsDefaults +} + +interface IDuotoneArgs { + highlight: string + shadow: string + opacity?: number +} + +export interface ITransformArgs { + height: number + width: number + cropFocus?: number | string + toFormat: string + pngCompressionLevel?: number + quality?: number + jpegQuality?: number + pngQuality?: number + webpQuality?: number + jpegProgressive?: boolean + grayscale?: boolean + rotate?: number + trim?: number + duotone?: IDuotoneArgs + background?: string + fit?: Fit + pathPrefix?: string + maxHeight?: number + maxWidth?: number + base64Width?: number +} + +interface IGeneralArgs extends ITransformArgs { + base64: boolean + pathPrefix: string + toFormatBase64: string + pngCompressionSpeed?: number +} // Plugin options are loaded onPreBootstrap in gatsby-node const pluginDefaults = { @@ -8,44 +72,47 @@ const pluginDefaults = { stripMetadata: true, lazyImageGeneration: true, defaultQuality: 50, - failOnError: true, // matches default of the sharp api constructor (https://sharp.pixelplumbing.com/api-constructor) + failOn: `warning` as FailOnOptions, } -const generalArgs = { +const generalArgs: Partial = { quality: 50, - jpegQuality: null, - pngQuality: null, - webpQuality: null, + jpegQuality: undefined, + pngQuality: undefined, + webpQuality: undefined, jpegProgressive: true, pngCompressionLevel: 9, // default is 4 (https://github.com/kornelski/pngquant/blob/4219956d5e080be7905b5581314d913d20896934/rust/bin.rs#L61) pngCompressionSpeed: 4, base64: true, grayscale: false, - duotone: false, + duotone: undefined, pathPrefix: ``, toFormat: ``, toFormatBase64: ``, rotate: 0, } -let pluginOptions = Object.assign({}, pluginDefaults) -exports.setPluginOptions = opts => { +let pluginOptions: ISharpPluginOptions = Object.assign({}, pluginDefaults) +export const setPluginOptions = ( + opts: Record +): ISharpPluginOptions => { pluginOptions = Object.assign({}, pluginOptions, opts) generalArgs.quality = pluginOptions.defaultQuality return pluginOptions } -exports.getPluginOptions = () => pluginOptions -exports.getPluginOptionsDefaults = () => pluginDefaults +export const getPluginOptions = (): ISharpPluginOptions => pluginOptions +export const getPluginOptionsDefaults = (): ISharpPluginOptions => + pluginDefaults /** * Creates a transform object - * - * @param {Partial} args */ -exports.createTransformObject = args => { +export const createTransformObject = ( + args: ITransformArgs +): Partial => { const options = { height: args.height, width: args.width, @@ -67,38 +134,50 @@ exports.createTransformObject = args => { } // get all non falsey values - return _.pickBy(options, _.identity) + return pickBy(options, identity) } /** * Used for gatsbyImageData and StaticImage only */ -exports.mergeDefaults = args => doMergeDefaults(args, pluginOptions.defaults) +export const mergeDefaults = ( + args: ISharpGatsbyImageArgs +): PluginOptionsDefaults & ISharpGatsbyImageArgs => + doMergeDefaults(args, pluginOptions.defaults) -const customizer = (objValue, srcValue) => +const customizer = (objValue: unknown, srcValue: T): T | undefined => Array.isArray(objValue) ? srcValue : undefined -function doMergeDefaults(args, defaults) { +export function doMergeDefaults( + args: ISharpGatsbyImageArgs, + defaults?: PluginOptionsDefaults +): PluginOptionsDefaults & ISharpGatsbyImageArgs { if (!defaults) { return args } - return _.mergeWith({}, defaults, args, customizer) + return mergeWith({}, defaults, args, customizer) } -exports.doMergeDefaults = doMergeDefaults - -exports.healOptions = ( - { defaultQuality: quality, base64Width }, - args, +export const healOptions = ( + { + defaultQuality: quality, + base64Width, + }: Pick, + args: ITransformArgs, fileExtension = ``, defaultArgs = {} -) => { - const options = _.defaults({}, args, { quality }, defaultArgs, generalArgs) +): Partial & { + quality: number +} & ITransformArgs => { + const options = defaults({}, args, { quality }, defaultArgs, generalArgs) + // @ts-ignore - parseInt as safeguard, expects string tho options.quality = parseInt(options.quality, 10) + // @ts-ignore - parseInt as safeguard, expects string tho options.pngCompressionLevel = parseInt(options.pngCompressionLevel, 10) + // @ts-ignore - parseInt as safeguard, expects string tho options.pngCompressionSpeed = parseInt(options.pngCompressionSpeed, 10) options.toFormat = options.toFormat.toLowerCase() - options.toFormatBase64 = options.toFormatBase64.toLowerCase() + options.toFormatBase64 = options.toFormatBase64?.toLowerCase() options.base64Width = options.base64Width || base64Width // when toFormat is not set we set it based on fileExtension @@ -120,9 +199,11 @@ exports.healOptions = ( options.width = 400 } if (options.width !== undefined) { + // @ts-ignore - parseInt as safeguard, expects string tho options.width = parseInt(options.width, 10) } if (options.height !== undefined) { + // @ts-ignore - parseInt as safeguard, expects string tho options.height = parseInt(options.height, 10) } @@ -130,8 +211,10 @@ exports.healOptions = ( if (options.maxWidth === undefined && options.maxHeight === undefined) { options.maxWidth = 800 } else if (options.maxWidth !== undefined) { + // @ts-ignore - parseInt as safeguard, expects string tho options.maxWidth = parseInt(options.maxWidth, 10) } else if (options.maxHeight !== undefined) { + // @ts-ignore - parseInt as safeguard, expects string tho options.maxHeight = parseInt(options.maxHeight, 10) } @@ -147,11 +230,11 @@ exports.healOptions = ( /** * Removes all default values so we have the smallest transform args - * - * @param {Partial} args - * @param {{defaultQuality: number }} pluginOptions */ -exports.removeDefaultValues = (args, pluginOptions) => { +export const removeDefaultValues = ( + args: ITransformArgs, + pluginOptions: ISharpPluginOptions +): Partial => { const options = { height: args.height, width: args.width, @@ -179,5 +262,5 @@ exports.removeDefaultValues = (args, pluginOptions) => { background: args.background, } - return _.omitBy(options, _.isNil) + return omitBy(options, isNil) } diff --git a/packages/gatsby-plugin-sharp/src/process-file.js b/packages/gatsby-plugin-sharp/src/process-file.ts similarity index 70% rename from packages/gatsby-plugin-sharp/src/process-file.js rename to packages/gatsby-plugin-sharp/src/process-file.ts index 94587f907d262..be848464b1f48 100644 --- a/packages/gatsby-plugin-sharp/src/process-file.js +++ b/packages/gatsby-plugin-sharp/src/process-file.ts @@ -1,13 +1,17 @@ -const sharp = require(`./safe-sharp`) -const fs = require(`fs-extra`) -const path = require(`path`) -const debug = require(`debug`)(`gatsby:gatsby-plugin-sharp`) -const duotone = require(`./duotone`) -const { healOptions } = require(`./plugin-options`) -const { SharpError } = require(`./sharp-error`) -const { - createContentDigest, -} = require(`gatsby-core-utils/create-content-digest`) +import sharp from "./safe-sharp" +import fs from "fs-extra" +import path from "path" +import debug from "debug" +import { createContentDigest } from "gatsby-core-utils/create-content-digest" +import duotone from "./duotone" +import { + healOptions, + ITransformArgs, + ISharpPluginOptions, +} from "./plugin-options" +import { SharpError } from "./sharp-error" + +const log = debug(`gatsby:gatsby-plugin-sharp`) // Try to enable the use of SIMD instructions. Seems to provide a smallish // speedup on resizing heavy loads (~10%). Sharp disables this feature by @@ -18,50 +22,20 @@ sharp.simd(true) // Concurrency is handled in gatsby-worker queue instead sharp.concurrency(1) -/** - * @typedef DuotoneArgs - * @property {string} highlight - * @property {string} shadow - * @property {number} opacity - */ - -/** - * @typedef {Object} TransformArgs - * @property {number} height - * @property {number} width - * @property {number} cropFocus - * @property {string} toFormat - * @property {number} pngCompressionLevel - * @property {number} quality - * @property {number} jpegQuality - * @property {number} pngQuality - * @property {number} webpQuality - * @property {boolean} jpegProgressive - * @property {boolean} grayscale - * @property {number} rotate - * @property {number} trim - * @property {DuotoneArgs} duotone - * @property {string} background - * @property {import('sharp').FitEnum} fit - */ - -/** + - * @typedef {Object} Transform - * @property {string} outputPath - * @property {TransformArgs} args - */ - -/** - * @param {String} file - * @param {Transform[]} transforms - */ -exports.processFile = async (file, transforms, options = {}) => { +interface ITransform { + outputPath: string + args: ITransformArgs +} + +export const processFile = async ( + file: string, + transforms: Array, + options = {} as ISharpPluginOptions +): Promise> => { let pipeline try { const inputBuffer = await fs.readFile(file) - pipeline = !options.failOnError - ? sharp(inputBuffer, { failOnError: false }) - : sharp(inputBuffer) + pipeline = sharp(inputBuffer, { failOn: options.failOn }) // Keep Metadata if (!options.stripMetadata) { @@ -75,11 +49,11 @@ exports.processFile = async (file, transforms, options = {}) => { transforms.map(async transform => { try { const { outputPath, args } = transform - debug(`Start processing ${outputPath}`) + log(`Start processing ${outputPath}`) await fs.ensureDir(path.dirname(outputPath)) const transformArgs = healOptions( - { defaultQuality: options.defaultQuality }, + { defaultQuality: options.defaultQuality as number }, args ) @@ -177,7 +151,7 @@ exports.processFile = async (file, transforms, options = {}) => { ) } -exports.createArgsDigest = args => { +export const createArgsDigest = (args: unknown): string => { const argsDigest = createContentDigest(args) return argsDigest.slice(-5) diff --git a/packages/gatsby-plugin-utils/src/polyfill-remote-file/placeholder-handler.ts b/packages/gatsby-plugin-utils/src/polyfill-remote-file/placeholder-handler.ts index f206a546a78bd..f1ac0303349fd 100644 --- a/packages/gatsby-plugin-utils/src/polyfill-remote-file/placeholder-handler.ts +++ b/packages/gatsby-plugin-utils/src/polyfill-remote-file/placeholder-handler.ts @@ -88,7 +88,7 @@ const queue = Queue< } case PlaceholderType.DOMINANT_COLOR: { const fileStream = createReadStream(filePath) - const pipeline = sharp({ failOnError: false }) + const pipeline = sharp({ failOn: `none` }) fileStream.pipe(pipeline) const { dominant } = await pipeline.stats() diff --git a/packages/gatsby-remark-images-contentful/package.json b/packages/gatsby-remark-images-contentful/package.json index e7f8af5eb9c79..6fb6ab35d6df1 100644 --- a/packages/gatsby-remark-images-contentful/package.json +++ b/packages/gatsby-remark-images-contentful/package.json @@ -22,7 +22,7 @@ "is-relative-url": "^3.0.0", "lodash": "^4.17.21", "semver": "^7.3.7", - "sharp": "^0.30.3", + "sharp": "^0.30.7", "unist-util-select": "^3.0.4" }, "devDependencies": { diff --git a/packages/gatsby-sharp/package.json b/packages/gatsby-sharp/package.json index 7ee956efc59f1..46e20ad54dc3a 100644 --- a/packages/gatsby-sharp/package.json +++ b/packages/gatsby-sharp/package.json @@ -14,8 +14,8 @@ "types": "dist/index.d.ts", "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-sharp#readme", "dependencies": { - "@types/sharp": "^0.30.0", - "sharp": "^0.30.3" + "@types/sharp": "^0.30.5", + "sharp": "^0.30.7" }, "devDependencies": { "@babel/cli": "^7.15.5", diff --git a/packages/gatsby-source-shopify/package.json b/packages/gatsby-source-shopify/package.json index 9eae628d05b15..04cf326309119 100644 --- a/packages/gatsby-source-shopify/package.json +++ b/packages/gatsby-source-shopify/package.json @@ -28,13 +28,13 @@ "gatsby-plugin-utils": "^3.15.0-next.2", "gatsby-source-filesystem": "^4.21.0-next.3", "node-fetch": "^2.6.7", - "sharp": "^0.30.3", + "sharp": "^0.30.7", "shift-left": "^0.1.5" }, "devDependencies": { "@types/node": "^14.17.34", "@types/node-fetch": "^2.5.12", - "@types/sharp": "^0.30.0", + "@types/sharp": "^0.30.5", "cross-env": "^7.0.3", "gatsby-plugin-image": "^2.21.0-next.2", "msw": "^0.38.2", diff --git a/packages/gatsby-source-wordpress/package.json b/packages/gatsby-source-wordpress/package.json index e8fbcaaf74653..b1aaf7f2f6f0a 100644 --- a/packages/gatsby-source-wordpress/package.json +++ b/packages/gatsby-source-wordpress/package.json @@ -40,7 +40,7 @@ "read-chunk": "^3.2.0", "replaceall": "^0.1.6", "semver": "^7.3.7", - "sharp": "^0.30.3", + "sharp": "^0.30.7", "valid-url": "^1.0.9" }, "devDependencies": { diff --git a/packages/gatsby-transformer-sharp/package.json b/packages/gatsby-transformer-sharp/package.json index 2313a336639ea..7ddd63063eaca 100644 --- a/packages/gatsby-transformer-sharp/package.json +++ b/packages/gatsby-transformer-sharp/package.json @@ -15,12 +15,12 @@ "gatsby-plugin-utils": "^3.15.0-next.2", "probe-image-size": "^7.2.3", "semver": "^7.3.7", - "sharp": "^0.30.3" + "sharp": "^0.30.7" }, "devDependencies": { "@babel/cli": "^7.15.4", "@babel/core": "^7.15.5", - "@types/sharp": "^0.30.0", + "@types/sharp": "^0.30.5", "babel-preset-gatsby-package": "^2.21.0-next.0", "cross-env": "^7.0.3" }, diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index d841e848c69e3..278b70dde73a3 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -184,7 +184,7 @@ "@types/reach__router": "^1.3.5", "@types/react-dom": "^17.0.9", "@types/semver": "^7.3.10", - "@types/sharp": "^0.30.0", + "@types/sharp": "^0.30.5", "@types/signal-exit": "^3.0.0", "@types/string-similarity": "^4.0.0", "@types/tmp": "^0.2.0", diff --git a/yarn.lock b/yarn.lock index e026c8a1d7bf1..be35112aa660e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5127,10 +5127,10 @@ dependencies: "@types/node" "*" -"@types/sharp@^0.30.0": - version "0.30.0" - resolved "https://registry.yarnpkg.com/@types/sharp/-/sharp-0.30.0.tgz#58cb016c8fdc558b4c5771ad1f3668336685c843" - integrity sha512-bZ0Y/JVlrOyqwlBMJ2taEgnwFavjLnyZmLOLecmOesuG5kR2Lx9b2fM4osgfVjLJi8UlE+t3R1JzRVMxF6MbfA== +"@types/sharp@^0.30.5": + version "0.30.5" + resolved "https://registry.yarnpkg.com/@types/sharp/-/sharp-0.30.5.tgz#d75d91f7acf5260525aeae229845046dcff6d17a" + integrity sha512-EhO29617AIBqxoVtpd1qdBanWpspk/kD2B6qTFRJ31Q23Rdf+DNU1xlHSwtqvwq1vgOqBwq1i38SX+HGCymIQg== dependencies: "@types/node" "*" @@ -7972,10 +7972,10 @@ color@^3.0.0, color@^3.1.1: color-convert "^1.9.1" color-string "^1.5.4" -color@^4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/color/-/color-4.2.1.tgz#498aee5fce7fc982606c8875cab080ac0547c884" - integrity sha512-MFJr0uY4RvTQUKvPq7dh9grVOTYSFeXja2mBXioCGjnjJoXrAp9jJ1NQTDR73c9nwBSAQiNKloKl5zq9WB9UPw== +color@^4.2.3: + version "4.2.3" + resolved "https://registry.yarnpkg.com/color/-/color-4.2.3.tgz#d781ecb5e57224ee43ea9627560107c0e0c6463a" + integrity sha512-1rXeuUUiGGrykh+CeBdu5Ie7OJwinCgQY0bc7GCRxy5xVHy+moaqkpL/jqQq0MtQOeYcrqEz4abc5f0KtU7W4A== dependencies: color-convert "^2.0.1" color-string "^1.9.0" @@ -18012,6 +18012,11 @@ node-addon-api@^4.3.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-4.3.0.tgz#52a1a0b475193e0928e98e0426a0d1254782b77f" integrity sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ== +node-addon-api@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-5.0.0.tgz#7d7e6f9ef89043befdb20c1989c905ebde18c501" + integrity sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA== + node-cleanup@^2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/node-cleanup/-/node-cleanup-2.1.2.tgz#7ac19abd297e09a7f72a71545d951b517e4dde2c" @@ -18327,7 +18332,7 @@ npm-run-path@^4.0.0, npm-run-path@^4.0.1: dependencies: path-key "^3.0.0" -npmlog@^4.0.1, npmlog@^4.1.2: +npmlog@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b" dependencies: @@ -20156,10 +20161,10 @@ preact@^10.6.4: resolved "https://registry.yarnpkg.com/preact/-/preact-10.6.4.tgz#ad12c409ff1b4316158486e0a7b8d43636f7ced8" integrity sha512-WyosM7pxGcndU8hY0OQlLd54tOU+qmG45QXj2dAYrL11HoyU/EzOSTlpJsirbBr1QW7lICxSsVJJmcmUglovHQ== -prebuild-install@^7.0.1: - version "7.0.1" - resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.0.1.tgz#c10075727c318efe72412f333e0ef625beaf3870" - integrity sha512-QBSab31WqkyxpnMWQxubYAHR5S9B2+r81ucocew34Fkl98FhvKIF50jIJnNOBmAZfyNV7vE5T6gd3hTVWgY6tg== +prebuild-install@^7.1.1: + version "7.1.1" + resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.1.1.tgz#de97d5b34a70a0c81334fd24641f2a1702352e45" + integrity sha512-jAXscXWMcCK8GgCoHOfIr0ODh5ai8mj63L2nWrjuAgXE6tDyYGnx4/8o/rCgU+B4JSyZBKbeZqzhtwtC3ovxjw== dependencies: detect-libc "^2.0.0" expand-template "^2.0.3" @@ -20168,7 +20173,6 @@ prebuild-install@^7.0.1: mkdirp-classic "^0.5.3" napi-build-utils "^1.0.1" node-abi "^3.3.0" - npmlog "^4.0.1" pump "^3.0.0" rc "^1.2.7" simple-get "^4.0.0" @@ -23099,16 +23103,16 @@ shallow-copy@~0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/shallow-copy/-/shallow-copy-0.0.1.tgz#415f42702d73d810330292cc5ee86eae1a11a170" -sharp@^0.30.3: - version "0.30.3" - resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.30.3.tgz#315a1817423a4d1cde5119a21c99c234a7a6fb37" - integrity sha512-rjpfJFK58ZOFSG8sxYSo3/JQb4ej095HjXp9X7gVu7gEn1aqSG8TCW29h/Rr31+PXrFADo1H/vKfw0uhMQWFtg== +sharp@^0.30.7: + version "0.30.7" + resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.30.7.tgz#7862bda98804fdd1f0d5659c85e3324b90d94c7c" + integrity sha512-G+MY2YW33jgflKPTXXptVO28HvNOo9G3j0MybYAHeEmby+QuD2U98dT6ueht9cv/XDqZspSpIhoSW+BAKJ7Hig== dependencies: - color "^4.2.1" + color "^4.2.3" detect-libc "^2.0.1" - node-addon-api "^4.3.0" - prebuild-install "^7.0.1" - semver "^7.3.5" + node-addon-api "^5.0.0" + prebuild-install "^7.1.1" + semver "^7.3.7" simple-get "^4.0.1" tar-fs "^2.1.1" tunnel-agent "^0.6.0"