I am using webpack's CssMinimizerWebpackPlugin
(docs). I initially configured it as recommended on the documentation
optimization: {
minimize: true,
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// `...`,
new CssMinimizerPlugin(),
],
},
But it seems that doing so prevents my main.js
file from being minified too. I decided to change it and place it under the plugins
tag, simply removing the optimization one completely like this:
plugins: [
//other plugins
new MiniCssExtractPlugin(),
],
And this way it seems to work, both the .css and .js files are being minified. I didn't find any reference to this under the docs provided above, so I would like to know, why is this happening? Why is it not working when using the documented set up?
For reference, please find my full working webpack configuration file below (note that I am passing the development or production mode through the --mode flag):
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = {
entry: path.join(__dirname, 'src/main.js'),
module: {
rules: [{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: false,
},
},
{
loader: 'css-loader',
options: {
// 0 => no loaders (default);
// 1 => postcss-loader;
// 2 => postcss-loader, sass-loader
importLoaders: 1
}
},
'postcss-loader'
]
},
]
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'public/index.html')
}),
new MiniCssExtractPlugin(),
new CssMinimizerPlugin(),
],
devServer: {
watchContentBase: true,
open: true,
writeToDisk: true,
},
// COMMENTED SINCE IT IS NOT WORKING CORRECTLY
// optimization: {
// minimize: true,
// minimizer: [
// // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
// // `...`,
// new CssMinimizerPlugin(),
// ],
// },
}