Skip to content

Commit

Permalink
move safe-sharp to gatsby/sharp when available
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Jan 4, 2022
1 parent 40b5e89 commit 128c5c8
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 222 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"npm-packlist": "^2.1.5",
"npm-run-all": "4.1.5",
"plop": "^1.9.1",
"prettier": "2.5.1",
"prettier": "^2.5.1",
"remark": "^13.0.0",
"remark-cli": "^9.0.0",
"remark-frontmatter": "^3.0.0",
Expand Down
10 changes: 4 additions & 6 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as fs from "fs"
import * as path from "path"
import sharp from "./safe-sharp"
// TODO V5 use gatsby/sharp
import getSharpInstance from "./safe-sharp"
import { createContentDigest, slash } from "gatsby-core-utils"
import { defaultIcons, addDigestToPath, favicons } from "./common"
import { doesIconExist } from "./node-helpers"

import pluginOptionsSchema from "./pluginOptionsSchema"

sharp.simd(true)

// force it to be 1 as we only resize one image
sharp.concurrency(1)

async function generateIcon(icon, srcIcon) {
const imgPath = path.join(`public`, icon.src)

Expand All @@ -28,6 +24,7 @@ async function generateIcon(icon, srcIcon) {
// Sharp accept density from 1 to 2400
const density = Math.min(2400, Math.max(1, size))

const sharp = await getSharpInstance()
return sharp(srcIcon, { density })
.resize({
width: size,
Expand Down Expand Up @@ -194,6 +191,7 @@ const makeManifest = async ({
)
}

const sharp = await getSharpInstance()
const sharpIcon = sharp(icon)

const metadata = await sharpIcon.metadata()
Expand Down
13 changes: 12 additions & 1 deletion packages/gatsby-plugin-manifest/src/safe-sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,18 @@ try {
originalConsoleError(msg, ...args)
handleMessage(msg)
}
sharp = require(`sharp`)
try {
sharp = require(`gatsby/sharp`)
} catch (e) {
sharp = () =>
Promise.resolve(() => {
const sharp = require(`sharp`)
sharp.simd()
sharp.concurrency(1)

return sharp
})
}
} catch (e) {
handleMessage(e.toString())
throw e
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/duotone.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO V5 use gatsby/sharp
const sharp = require(`./safe-sharp`)
const { reportError } = require(`./report-error`)

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/image-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Reporter } from "gatsby/reporter"
import fs from "fs-extra"
import { rgbToHex, calculateImageSizes, getSrcSet, getSizes } from "./utils"
import { traceSVG, getImageSizeAsync, base64, batchQueueImageResizing } from "."
// TODO V5 use gatsby/sharp
import sharp from "./safe-sharp"
import {
createTransformObject,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO V5 use gatsby/sharp
const sharp = require(`./safe-sharp`)
const { generateImageData } = require(`./image-data`)
const imageSize = require(`probe-image-size`)
Expand Down
10 changes: 1 addition & 9 deletions packages/gatsby-plugin-sharp/src/process-file.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO V5 use gatsby/sharp
const sharp = require(`./safe-sharp`)
const fs = require(`fs-extra`)
const path = require(`path`)
Expand All @@ -7,15 +8,6 @@ const { healOptions } = require(`./plugin-options`)
const { SharpError } = require(`./sharp-error`)
const { createContentDigest } = require(`gatsby-core-utils`)

// Try to enable the use of SIMD instructions. Seems to provide a smallish
// speedup on resizing heavy loads (~10%). Sharp disables this feature by
// default as there's been problems with segfaulting in the past but we'll be
// adventurous and see what happens with it on.
sharp.simd(true)

// Concurrency is handled in gatsby-worker queue instead
sharp.concurrency(1)

/**
* @typedef DuotoneArgs
* @property {string} highlight
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-sharp/src/safe-sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ try {
originalConsoleError(msg, ...args)
handleMessage(msg)
}
sharp = require(`sharp`)
try {
sharp = require(`gatsby/sharp`)
} catch (e) {
sharp = require(`sharp`)
}
} catch (e) {
handleMessage(e.toString())
throw e
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/trace-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require(`fs-extra`)
const _ = require(`lodash`)
const tmpDir = require(`os`).tmpdir()
const path = require(`path`)
// TODO V5 use gatsby/sharp
const sharp = require(`./safe-sharp`)
const filenamify = require(`filenamify`)
const duotone = require(`./duotone`)
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO V5 use gatsby/sharp
import sharp from "./safe-sharp"
import fs from "fs-extra"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ jest.mock(`sharp`, () => {
const pipeline = {
metadata: metadataMock,
}

return pipeline
}

sharp.simd = jest.fn()
sharp.concurrency = jest.fn()
sharp.metadataMock = metadataMock

return sharp
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-remark-images-contentful/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { selectAll } = require(`unist-util-select`)
const sharp = require(`./safe-sharp`)
// TODO V5 use gatsby/sharp
const getSharpInstance = require(`./safe-sharp`)
const axios = require(`axios`)
const _ = require(`lodash`)
const Promise = require(`bluebird`)
Expand Down Expand Up @@ -62,6 +63,7 @@ module.exports = async (
if (cachedRawHTML) {
return cachedRawHTML
}
const sharp = await getSharpInstance()
const metaReader = sharp()

// @todo to increase reliablility, this should use the asset downloading function from gatsby-source-contentful
Expand All @@ -86,6 +88,7 @@ module.exports = async (
try {
metadata = await metaReader.metadata()
} catch (error) {
console.log(error)
reporter.panic(
`The image "${node.url}" (with alt text: "${node.alt}") doesn't appear to be a supported image format.`,
error
Expand Down
13 changes: 12 additions & 1 deletion packages/gatsby-remark-images-contentful/src/safe-sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,18 @@ try {
originalConsoleError(msg, ...args)
handleMessage(msg)
}
sharp = require(`sharp`)
try {
sharp = require(`gatsby/sharp`)
} catch (e) {
sharp = () =>
Promise.resolve(() => {
const sharp = require(`sharp`)
sharp.simd()
sharp.concurrency(1)

return sharp
})
}
} catch (e) {
handleMessage(e.toString())
throw e
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-sharp/src/customize-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
traceSVG,
generateImageData,
} = require(`gatsby-plugin-sharp`)

// TODO V5 use gatsby/sharp
const sharp = require(`./safe-sharp`)
const fs = require(`fs-extra`)
const imageSize = require(`probe-image-size`)
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-transformer-sharp/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Potrace } from "potrace"
import type Sharp from "sharp"

// TODO V5 use gatsby/sharp
const sharp: typeof Sharp = require(`./safe-sharp`)
const DEFAULT_PNG_COMPRESSION_SPEED = 4

Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"@types/reach__router": "^1.3.5",
"@types/react-dom": "^17.0.9",
"@types/semver": "^7.3.9",
"@types/sharp": "^0.29.5",
"@types/signal-exit": "^3.0.0",
"@types/string-similarity": "^4.0.0",
"@types/tmp": "^0.2.0",
Expand All @@ -188,6 +189,9 @@
"zipkin-javascript-opentracing": "^3.0.0",
"zipkin-transport-http": "^0.22.0"
},
"optionalDependencies": {
"gatsby-sharp": "^0.0.1-next.0"
},
"engines": {
"node": ">=14.15.0"
},
Expand Down
Loading

0 comments on commit 128c5c8

Please sign in to comment.