UglifyJS and webpack v5

29.7k views Asked by At

We've been using UglifyJS and webpack v4 for our react code, but then just updated to webpack v5. It appears that UglifyJS does not work with webpack v5. Is there an alternative? We need something that works with babel-loader.

Thanks

2

There are 2 answers

1
Sam Chen On

Webpack 5 comes with terser-webpack-plugin out of the box, hence you can just import it and configure as you wish.

0
8ctopus On

As Sam said, Webpack 5 comes with the Terser plugin which does what UglifyJS used to do. Here's an example on how to use the Terser plugin for webpack.

// webpack.config.js
const TerserPlugin = require("terser-webpack-plugin");

const config = {
    ...
    optimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin(),
        ],
    }
};