Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use latest terser-webpack-plugin with Webpack5

According to this link (Terser documentation) if you are using latest Webpack 5, you don't need to install the Terser plugin as it is included in Webpack 5 out of the box. However, I am having a hard time to get this working.

If I remove the terser-webpack-plugin from my packages.json file and I try to use it like this (see below webpack.production.js), I get build errors like this:

[webpack-cli] Failed to load 'D:\Project\React\MyApp\config\webpack.production.js' config

[webpack-cli] Error: Cannot find module 'terser-webpack-plugin'

webpack.production.js

const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const { merge } = require('webpack-merge');

module.exports = merge(commonCfg, {
        ......
        optimization: {
            minimize: true,
            minimizer: [new TerserPlugin({
                cache: false,
                parallel: false,
                sourceMap: true,
            })]
        },

Now, if I include the latest terser-webpack-plugin version (5.1.1) on my package.json and run my build, I get the following error message:

[webpack-cli] Failed to load 'D:\Project\React\MyApp\config\webpack.production.js' config

[webpack-cli] Invalid options object. Terser Plugin has been initialized using an options object that does not match the API schema.

  • options has an unknown property 'sourceMap'. These properties are valid: object { test?, include?, exclude?, terserOptions?, extractComments?, parallel?, minify? }

The only way I can make this work is keeping terser-webpack-plugin on version 4.2.X.

Is there a way I can make this work with latest Terser version? Or maybe I misunderstood the documentation?

like image 630
user3587624 Avatar asked Feb 24 '21 01:02

user3587624


People also ask

What does terser webpack plugin do?

Allows you to override default minify function. By default plugin uses terser package. Useful for using and testing unpublished versions or forks. Always use require inside minify function when parallel option enabled.

What is copy webpack plugin?

webpack.config.js ℹ️ webpack-copy-plugin is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process.


1 Answers

Hi here is how i resolved the Terserof Webpack 5

Before Webpack 5:

minimizer: [
  new TerserPlugin({
    terserOptions: {
      mangle: {
        compress: {},
      },
    }
  })
]

After Webpack 5:

minimizer: [
  (compiler) => {
    const TerserPlugin = require('terser-webpack-plugin');
    new TerserPlugin({
      terserOptions: {
        compress: {},
      }
    }).apply(compiler);
  },
]

you can read more about it here https://webpack.js.org/configuration/optimization/ and to check the terser option check this url https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions

here is link to my article with more migration error problem solved https://medium.com/@arianpopalyar/webpack-4-to-webpack-5-migration-9bc683d2bc72

like image 63
Arian Popalyar Avatar answered Sep 27 '22 20:09

Arian Popalyar