-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat(gatsby-sharp): create more resilient wrapper around sharp #34339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aab24c9
feat(gatsby-sharp): add gatsby-sharp as sharp abstraction
wardpeet ae84e46
move safe-sharp to gatsby/sharp when available
wardpeet 02b94f3
fix yarn.lock and snapshots because of deps
wardpeet 19d8ee9
only require sharp once + typescript fixes
wardpeet 2cc31ac
fix fallback safe-sharp
wardpeet dd9ea5b
update sharp
wardpeet 19ca39f
fix jest babel resolution
wardpeet a8c7c6a
Apply suggestions from code review
wardpeet 867cfd6
Merge branch 'master' into feat/gatsby-sharp
wardpeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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`, | ||
wardpeet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows jest to use the babelrc inside a package instead of only the global one.