-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-sharp): create more resilient wrapper around sharp (#34339)
* feat(gatsby-sharp): add gatsby-sharp as sharp abstraction * move safe-sharp to gatsby/sharp when available * fix yarn.lock and snapshots because of deps * only require sharp once + typescript fixes * fix fallback safe-sharp * update sharp * fix jest babel resolution * Apply suggestions from code review
- Loading branch information
Showing
17 changed files
with
258 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
console.log('hi there'); | ||
module.exports = { | ||
"presets": [["babel-preset-gatsby-package"]], | ||
"plugins": ["babel-plugin-replace-ts-export-assignment"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "gatsby-sharp", | ||
"version": "0.0.1-next.0", | ||
"sideEffects": false, | ||
"keywords": [ | ||
"gatsby", | ||
"sharp" | ||
], | ||
"main": "dist/index.js", | ||
"source": "src/index.ts", | ||
"files": [ | ||
"dist/*" | ||
], | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-sharp#readme", | ||
"dependencies": { | ||
"@types/sharp": "^0.29.5", | ||
"sharp": "^0.29.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.15.5", | ||
"@babel/core": "^7.15.5", | ||
"babel-plugin-replace-ts-export-assignment": "^0.0.2", | ||
"cross-env": "^7.0.3" | ||
}, | ||
"engines": { | ||
"node": ">=14.15.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gatsbyjs/gatsby.git", | ||
"directory": "packages/gatsby-sharp" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "babel src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\"", | ||
"typegen": "tsc --emitDeclarationOnly --declaration --declarationDir dist/", | ||
"prepare": "cross-env NODE_ENV=production npm-run-all -s build typegen", | ||
"watch": "babel -w src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\"" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** @jest-environment node */ | ||
import { exec } from "child_process" | ||
|
||
jest.mock(`child_process`, () => { | ||
return { | ||
exec: jest.fn(async (command, options, cb) => { | ||
setImmediate(() => { | ||
try { | ||
return cb( | ||
null, | ||
`> sharp@0.29.3 install C:\\Users\\Ward\\projects\\gatsby\\gatsby\\node_modules\\sharp\n`, | ||
`` | ||
) | ||
} catch (err) { | ||
return cb(true, ``, err.message) | ||
} | ||
}) | ||
}), | ||
} | ||
}) | ||
|
||
function getSharpInstance(): typeof import("../index") { | ||
let getSharpInstance | ||
jest.isolateModules(() => { | ||
getSharpInstance = require(`../index`) | ||
}) | ||
|
||
return getSharpInstance() | ||
} | ||
|
||
describe(`getSharpInstance`, () => { | ||
beforeEach(() => { | ||
exec.mockClear() | ||
}) | ||
|
||
// jest mocking is making this impossible to test | ||
it(`should give you the bare sharp module`, async () => { | ||
const sharpInstance = await getSharpInstance() | ||
|
||
expect(exec).not.toHaveBeenCalled() | ||
expect(sharpInstance).toBeDefined() | ||
expect(sharpInstance.versions).toBeDefined() | ||
}) | ||
|
||
it( | ||
`should rebuild sharp when binaries not found for current arch`, | ||
async () => { | ||
expect.assertions(3) | ||
|
||
let called = false | ||
jest.doMock(`sharp`, () => { | ||
if (!called) { | ||
called = true | ||
throw new Error(`sharp failed to load`) | ||
} | ||
|
||
return jest.requireActual(`sharp`) | ||
}) | ||
|
||
try { | ||
const sharpInstance = await getSharpInstance() | ||
expect(sharpInstance).toBeDefined() | ||
expect(sharpInstance.versions).toBeDefined() | ||
} catch (err) { | ||
// ignore | ||
} | ||
|
||
expect(exec).toHaveBeenCalledWith( | ||
`npm rebuild sharp`, | ||
expect.anything(), | ||
expect.anything() | ||
) | ||
}, | ||
60 * 1000 | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { exec } = require(`child_process`) | ||
const { createRequire } = require(`module`) | ||
|
||
let sharpInstance: typeof import("sharp") | ||
|
||
export = async function getSharpInstance(): Promise<typeof import("sharp")> { | ||
try { | ||
return importSharp() | ||
} catch (err) { | ||
await rebuildSharp() | ||
|
||
// Try importing again now we have rebuilt sharp | ||
return importSharp() | ||
} | ||
} | ||
|
||
function importSharp(): typeof import("sharp") { | ||
if (!sharpInstance) { | ||
const cleanRequire = createRequire(__filename) | ||
const sharp = cleanRequire(`sharp`) | ||
|
||
sharp.simd(true) | ||
// Concurrency is handled by gatsby | ||
sharp.concurrency(1) | ||
|
||
sharpInstance = sharp | ||
} | ||
|
||
return sharpInstance | ||
} | ||
|
||
function rebuildSharp(): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
exec( | ||
`npm rebuild sharp`, | ||
{ | ||
timeout: 60 * 1000, | ||
}, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
if (error.killed) { | ||
console.log(`timeout reached`) | ||
} | ||
|
||
return reject(stderr) | ||
} | ||
|
||
return setImmediate(() => resolve(stdout)) | ||
} | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"target": "ES5", | ||
"module": "CommonJS" | ||
}, | ||
"exclude": [ | ||
"src/__tests__", | ||
"src/__mocks__", | ||
"dist", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import getSharpInstance from "gatsby-sharp" | ||
|
||
export = getSharpInstance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict" | ||
|
||
module.exports = require('gatsby-sharp'); |
Oops, something went wrong.