I need compress images like a TinyPNG and save compressed images in dist folder. I use webpack 4 and found imagemin-webpack.But I don't understand what use: plugin or loader? Please, help to do configuration for this task.
let path = require('path');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
let {imageminLoader} = require("imagemin-webpack");
let imageminGifsicle = require("imagemin-gifsicle");
let conf = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './dist/'),
filename: 'main.js',
publicPath: 'dist/'
},
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
minimize: true,
sourceMap: true
}
}
]
})
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
{
loader: "file-loader"
},
{
loader: imageminLoader,
options: {
cache: true,
bail: false,
imageminOptions: {
plugins: [imageminGifsicle()]
},
name: "[hash]-compressed.[ext]"
}
}
]
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
This is my config file. After run build command nothing happens with images.
For compressing images in Webpack 4 I am using "img-loader".
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
{
loader: 'file-loader',
options: {
name: '' + imgPath + '[name].[ext]'
}
},
{
loader: 'img-loader',
options: {
plugins: [
imageminGifsicle({
interlaced: false
}),
imageminMozjpeg({
progressive: true,
arithmetic: false
}),
imageminPngquant({
floyd: 0.5,
speed: 2
}),
imageminSvgo({
plugins: [
{ removeTitle: true },
{ convertPathData: false }
]
})
]
}
}
]
}
Of course, these are required variables:
const imgPath = './assets/img/';
const imageminGifsicle = require("imagemin-gifsicle");
const imageminPngquant = require("imagemin-pngquant");
const imageminSvgo = require("imagemin-svgo");
const imageminMozjpeg = require('imagemin-mozjpeg');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With