unplugin-images-cls-optimizer
v0.0.2
Published
Erase CLS by automatically optimizing images.
Readme
unplugin-images-cls-optimizer
Erase CLS by automatically optimizing images.
- ⚡️ Automatic Dimensions: Automatically calculates and injects
widthandheightattributes to prevent layout shifts. - 🖼️ Blurhash Placeholders: Generates and injects a blurhash background for a smooth loading experience.
- 💤 Lazy Loading: Automatically adds
loading="lazy"to images. - 💚 Vue Support: Currently optimized for Vue applications. But I'm sure it could be adapted for other frameworks as well. Feel free to open an issue or a PR to suggest support for other frameworks.
Based on the article Erase CLS by automatically optimizing images with Vite.
Installation
pnpm add -D unplugin-images-cls-optimizerUsage
[!NOTE] Contributions, through issues or pull requests, to extend support for other frameworks are welcome!
Vite
// vite.config.ts
import imagesClsOptimizer from 'unplugin-images-cls-optimizer/vite'
export default defineConfig({
plugins: [
imagesClsOptimizer(),
],
})How it works
This plugin scans your .vue files for <img> tags. When it finds a local image (PNG or JPG/JPEG), it:
- Reads the image file from your public directory.
- Calculates the image dimensions using
@unpic/pixels. - Generates a blurhash of the image.
- Rewrites the
<img>tag with:widthandheightattributes.- A
styleattribute containing the blurhash as a background image. loading="lazy".
Sponsors
License
MIT License © 2025-PRESENT Estéban Soubiran
