imagemin-module icon indicating copy to clipboard operation
imagemin-module copied to clipboard

Cannot override the default `minimizerOptions.plugins`, I can only add new plugins

Open FreekVR opened this issue 3 years ago • 1 comments

I was trying to override the default plugins in my project like so from nuxt.config.js;

[...]
imagemin: {
  mimizerOptions: {
    plugins: [
      [ 'gifsicle', { interlaced: false} ],
      [ 'jpegtran', { progressive: false} ],
      [ 'optipng', { optimizationLevel: 9 } ],
    ]
  }
}

However, I was having issues with these changes not applying and it appears to be the case that custom imagemin plugins are getting pushed to the defaults, instead of overriding them. I was able to verify this by adding the following debug code in dist/module.js line 103 (right after defining the options)

const util = require('util')
console.log(util.inspect(options, false, null, true /* enable colors */))

Given the config from my project, this debug code outputs the following;

  minimizerOptions: {
    plugins: [
      [ 'gifsicle', { interlaced: true } ],
      [ 'jpegtran', { progressive: true } ],
      [ 'optipng', { optimizationLevel: 5 } ],
      [
        'svgo',
        { plugins: [ { removeViewBox: false } ] }
      ],
      [ 'gifsicle', { interlaced: false} ],
      [ 'jpegtran', { progressive: false} ],
      [ 'optipng', { optimizationLevel: 9 } ],

I would expect that I would be able to override or remove the default plugins and their options, but as it stands, it only appears I can add new ones? Or if I'm missing something here please let me know where I went wrong :)

FreekVR avatar Apr 15 '21 08:04 FreekVR

Hi, There is a way to trick it, use a function:

[...]
imagemin: {
  mimizerOptions: {
    plugins: () => [
      [ 'gifsicle', { interlaced: false} ],
      [ 'jpegtran', { progressive: false} ],
      [ 'optipng', { optimizationLevel: 9 } ],
    ]
  }
}

Otherwise it just concatenates arrays.

This should be documented, but it's the default behaviour of https://github.com/unjs/defu which is used to merge configs.

tex0l avatar Jul 26 '21 16:07 tex0l