I’m working on a React project using Vite as the build tool.

I’d like to automatically compress PNG and JPEG images when building for production, without changing the image import paths (so that imports like import logo from './logo.png' still work).

I tried using:

  • vite-plugin-imagemin → works locally but fails in CI due to missing binaries.

  • sharp in a custom script → changes file names, which breaks imports.

Goal:

  • Run during vite build

  • Maintain image filenames and relative paths

  • Work on Linux CI runners (no GUI dependencies)

Question:
Is there a way to configure Vite (or another plugin) to compress images automatically during build while preserving the original filenames and paths?

1 Reply 1

I recommend using vite-plugin-image-optimizer which is CI friendly

Then you also have to configure vite. All configurations are also available on the npm page.

//example 
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer';
import { defineConfig } from 'vite';

export default defineConfig(() => {
  return {
    plugins: [
      ViteImageOptimizer({
         png: {
          quality: 100,
         },
      }),
    ],
  };
});

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.