Wrapper of the Sharp image manipulation library for Gatsby plugins
Build an image optimization tool that converts input images to multiple output formats with format-specific compression settings to minimize file size while maintaining acceptable quality.
Different image formats have different strengths: JPEG excels at photographs, PNG handles transparency well, WebP offers better compression than JPEG, and AVIF provides the best compression ratios. Modern web applications serve multiple formats to maximize browser compatibility while optimizing performance.
Create a Node.js module (src/optimizer.js) with a function processImage(inputPath, outputDir) that:
The function should return an object with this structure:
{
original: {
path: "path/to/original.jpg",
format: "jpeg",
width: 2000,
height: 1500
},
optimized: [
{
format: "jpeg",
path: "output/image.jpg",
size: 125430,
width: 800,
height: 600
},
{
format: "png",
path: "output/image.png",
size: 450120,
width: 800,
height: 600
},
{
format: "webp",
path: "output/image.webp",
size: 98760,
width: 800,
height: 600
},
{
format: "avif",
path: "output/image.avif",
size: 67890,
width: 800,
height: 600
}
]
}Provides image processing and format conversion capabilities with format-specific optimization options.
Create tests in src/optimizer.test.js using your preferred testing framework.
Given: A sample JPEG image (1200x900 pixels) at test-images/photo.jpg
When: Calling processImage('test-images/photo.jpg', 'output/')
Then:
output/photo.jpg, output/photo.png, output/photo.webp, output/photo.avifGiven: A sample PNG image at test-images/sample.png
When: Calling processImage('test-images/sample.png', 'output/')
Then:
Given: A large image (3000x2000 pixels) at test-images/large.jpg
When: Calling processImage('test-images/large.jpg', 'output/')
Then:
tessl i tessl/npm-gatsby-plugin-sharp@5.15.0docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10