我正在使用imagemin-webpack-plugin来最小化项目中的图像。它仅在我运行npm build.
时运行,但是每次执行此操作时,它将再次最小化所有图像,这是不必要的。因此,我希望它仅使更改的图像最小化。我以前已经用过gulp,但是我是webpack的新手,所以我不确定该怎么做。
此外,当我开始使用此插件时,我注意到当我在开发时添加新图像时,在运行build命令之前,该图像不会显示。不知道为什么会这样,这可能是一个不相关的问题...
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})]
})
]
});
答案 0 :(得分:0)
在撰写本文时,您可以为插件使用cacheFolder
选项。
https://github.com/Klathmon/imagemin-webpack-plugin#optionscachefolder
new ImageminPlugin({
cacheFolder: './.cache',
// ...
})
此外,您不需要使用CopyWebpackPlugin
插件,当Imagemin插件已经具有此内置插件时,我发现它是多余的。
https://github.com/Klathmon/imagemin-webpack-plugin#optionsexternalimages
new ImageminPlugin({
externalImages: {
context: '.',
sources: glob.sync('./src/images/**/*.{jpg,png}'),
destination: 'images',
fileName: '[path][name].[ext]'
}
// ...
})