Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is CssMinimizerWebpackPlugin preventing my main js file from being minified?

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(),
    //     ],
    //   },
}
like image 673
Aridez Avatar asked Mar 14 '21 23:03

Aridez


People also ask

What does webpack use to minify?

Webpack performs minification in production mode using Terser by default. Besides JavaScript, it's possible to minify other assets, such as CSS and HTML too.

Does webpack automatically minify?

In webpack 4, the bundle-level minification is enabled automatically – both in the production mode and without one. It uses the UglifyJS minifier under the hood. (If you ever need to disable minification, just use the development mode or pass false to the optimization. minimize option.)

Does webpack use terser?

Webpack v5 comes with the latest terser-webpack-plugin out of the box. If you are using Webpack v5 or above and wish to customize the options, you will still need to install terser-webpack-plugin . Using Webpack v4, you have to install terser-webpack-plugin v4.


1 Answers

Since version 4 webpack runs optimizations for you depending on the chosen mode, still all optimizations are available for manual configuration and overrides.

In particular, for js minification it uses TerserWebpackPlugin. When the optimization is overridden then the defaults of webpack do not take effect, using only the provided ones. In this case it only ran through the Css plugin.

To fix this, webpack offers the possibility of keeping existing minimizers by using '...', so it should look like this:

const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

module.exports = {
    // other config
    optimization: {
        minimizer: [
          // For webpack@5 you can use the `...` syntax to extend existing minimizers
          '...',
          new CssMinimizerPlugin(),
        ],
      },
}

Webpack includes webpack-terser-plugin by default in order to minify the .js files too. The configuration above would be similar to the following one:

// This serves just as an example, webpack provides more optimization steps when using '...'
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
    // other config
    optimization: {
        minimizer: [
          // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
          new TerserPlugin(),
          new CssMinimizerPlugin(),
        ],
      },
}
like image 171
Aridez Avatar answered Oct 29 '22 00:10

Aridez