【问题标题】:How to compress images with ImageMinimizerWebpackPlugin in Next.js (webpack 5)?如何在 Next.js(webpack 5)中使用 ImageMinimizerWebpackPlugin 压缩图像?
【发布时间】:2021-09-28 20:01:21
【问题描述】:

我想使用这个插件来压缩图片,但是我遇到了构建通过但图片没有被压缩的问题。

我是 webpack 的新手,不明白出了什么问题(也许我需要一个将压缩文件作为文件夹的出口点?)

next.config.js

const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");

const nextConfig = {
  future: {
    webpack5: true
  },

  webpack: (config, options) => {

    config.module.rules.push({
      test: /\.(jpe?g|png|svg)$/i,
      type: "asset",
    })

    config.plugins.push(new ImageMinimizerPlugin({
      minimizerOptions: {
        // Lossless optimization with custom option
        // Feel free to experiment with options for better result for you
        plugins: [
          ["jpegtran", { progressive: true }],
          ["optipng", { optimizationLevel: 5 }],
          // Svgo configuration here https://github.com/svg/svgo#configuration
          [
            "svgo",
              {
                plugins: 
                  {
                    name: "removeViewBox",
                    active: false,
                  },
              },
            ],
          ],
        },
      })
    )
    return config
  },
};

module.exports = nextConfig

【问题讨论】:

    标签: javascript reactjs webpack next.js


    【解决方案1】:

    遇到了同样的问题,无法使用 imagemin 解决。唯一有效的是当我切换到使用squoosh 时,如Plugin docs 中所述。

    如果你想使用imagemin,我目前唯一推荐的解决方案是使用image-webpack-loader,而不是Webpack 5 的新asset 功能。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2016-08-08
    • 2021-05-13
    • 1970-01-01
    • 2018-04-20
    • 2021-10-25
    • 2018-09-10
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多