🚀 A Vite plugin that automatically converts .jpg
, .jpeg
, and .png
image references to .webp
format in both source code and built files, but only applies in production mode. Now supports configurable output directory!
- Replaces image references in source code to use
.webp
- Converts actual image files in the
dist
directory (or custom directory) to.webp
- Recursively processes directories (excluding
/assets
) - Applies only in production mode
- Supports customizable output directory
npm install vite-plugin-optimize-image --save-dev
or
yarn add vite-plugin-optimize-image -D
Add the plugin to your Vite configuration:
import { defineConfig } from "vite";
import optimizeImagePlugin from "vite-plugin-optimize-image";
export default defineConfig({
plugins: [optimizeImagePlugin()],
});
You can specify a custom output directory instead of the default dist
:
export default defineConfig({
plugins: [optimizeImagePlugin({ distDir: "custom-output" })],
});
- The plugin only applies in production mode to avoid modifying images during development.
- During the transformation phase, the plugin updates image file references in the source code from
.jpg
,.jpeg
, and.png
to.webp
. - During the
closeBundle
phase, it recursively scans the specified output directory (dist
by default) and renames actual image files to.webp
. - The
/assets
directory is ignored to avoid unnecessary modifications.
const imgSrc = "./images/sample.png";
const imgSrc = "./images/sample.webp";
dist/
images/
sample.png
dist/
images/
sample.webp
Option | Type | Default | Description |
---|---|---|---|
distDir |
string |
"dist" |
Custom output directory for built files |
MIT
Feel free to open issues or pull requests if you have suggestions or improvements! 🚀
- Maintainer: yunseul lee
- Email: yun.zzang9.me@gmail.com