This project helps you convert images in your project folder to WebP format using imagemin and imagemin-webp.
WebP is an open-source image format developed by Google that offers better compression and performance for websites.
Learn more: https://developers.google.com/speed/webp
Install the required packages using npm:
npm install imagemin imagemin-webp @types/imagemin @types/imagemin-webpCreate a file named imagemin.mjs in the root of your project and add the following code:
import imagemin from "imagemin";
import imageminWebp from "imagemin-webp";
await imagemin(["public/assets/images/*.{jpg,png,jpeg}"], {
destination: "public/assets/images/build",
plugins: [
imageminWebp({
quality: 50,
}),
],
});In your package.json, add:
"scripts": {
"images": "node imagemin.mjs"
}npm run imagesAll images from public/assets/images/ will be converted to WebP format and saved in:
📁 public/assets/images/build
- Converts all
.jpg,.jpeg, and.pngimages to.webp - Optimized images are stored in a separate
/buildfolder - Original images remain untouched
- Smaller file sizes for faster load times
- Better image quality at lower sizes
- Supported by most modern browsers
- Make sure you are using Node.js 14 or higher.
- The output directory must exist or will be created automatically.