我使用 imagemin-webpack-plugin 来最小化我项目中的图像。它只在我运行时运行npm build.
但是,每次我这样做时,它都会再次最小化所有图像,这是不必要的。因此,我希望它只是最小化已更改的图像。我以前用 gulp 做过,但我是 webpack 的新手,所以我不太确定该怎么做。
另外,当我开始使用这个插件时,我注意到当我在开发过程中添加一个新图像时,它不会显示,直到我运行构建命令。不知道为什么会这样,可能是一个不相关的问题......
const merge = require("webpack-merge");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const common = require("./webpack.common.js");
const imageminMozjpeg = require('imagemin-mozjpeg');
const CopyWebpackPlugin = require('copy-webpack-plugin');
var ImageminPlugin = require('imagemin-webpack-plugin').default
module.exports = merge(common, {
mode: "production",
output: {
filename: "[name].[hash:5].js",
chunkFilename: "[id].[hash:5].css"
},
plugins: [
new CopyWebpackPlugin([{
from: 'src/images/',
to: 'images/'
}]),
new ImageminPlugin({
test: /\.(jpe?g|png|gif|svg)$/i,
pngquant: ({quality: 80}),
plugins: [imageminMozjpeg({quality: 50})]
})
]
});