vbenjs / vite-plugin-imagemin

A vite plugin for compressing image assets.
MIT License
207 stars 28 forks source link

Imagemin config is ignored #1

Closed chojnicki closed 3 years ago

chojnicki commented 3 years ago

Hi. Thanks for this plugin :) I was using Imagemin with webpack for years without problems, but cannot force it to work with Vite.

Fresh Vite install with Vue 3:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import viteImagemin from 'vite-plugin-imagemin'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    viteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false,
      },
      optipng: {
        optimizationLevel: 7,
      },
      webp: {
        quality: 75,
      },
      mozjpeg: {
        quality: 10
      },
      pngquant: {
        quality: [0.65, 0.9],
        speed: 4,
      },
      svgo: {
        plugins: [
          {
            removeViewBox: false,
          },
          {
            removeEmptyAttrs: false,
          },
        ],
      },
    })
  ]
})

It does not matter what I type in mozjpeg quality or optipng optimizationLevel because I will always get same assets.

dist/assets/logo.03d6d6da.png -69% 6.69kb / tiny: 2.08kb dist/assets/test.6ece58c0.jpg -83% 3522.61kb / tiny: 616.63kb

So .jpg 10% and 100% results in same size.

Also, it is the same after disabling mozjpeg by

mozjpeg: false

It looks like config is ignored, but this:

    viteImagemin({
      disabled: true
    })

does actually disable compressing, so config is not ignored if it recognizes this option. What is going on?

anncwb commented 3 years ago

I will look at it later

anncwb commented 3 years ago

This is caused by webp。

webp: {
          quality: 75,
 }

imagemin will first read the configuration of webp to compress jpg and png. So just set webp to false You can also upgrade to version 0.2.7. The default webp is set to false, and the webp configuration is deleted in vite.config.ts

chojnicki commented 3 years ago

Thanks!