deadcoder0904
deadcoder0904

Reputation: 8683

webpack.optimize.UglifyJSPlugin does not work while uglifyjs-webpack-plugin works

I have 2 files app.js & math.js

It contains the following code -

app.js

import { sum } from "./math";

console.log("💩");

console.log(sum(2, 3));

math.js

export const sum = (a, b) => a + b;
export const multiply = (m, n) => m * n;

webpack.config.js

const path = require("path");
const webpack = require("webpack");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const config = {
    entry: "./app.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js"
    },
    plugins: [
        new UglifyJsPlugin()
        // new webpack.optimize.UglifyJsPlugin() doesn't work
    ]
};

module.exports = config;

The complete code is available at https://github.com/deadcoder0904/webpack-treeshake

The problem is UglifyJsPlugin works but webpack.optimize.UglifyJsPlugin() doesn't work

My webpack version is v3.11.0

Upvotes: 3

Views: 11239

Answers (1)

deadcoder0904
deadcoder0904

Reputation: 8683

Posting answer to my own question. Thanks @str for helping out.

I was using different versions of Uglify JS, not sure if that was the problem.

So I tried installing webpack version 4 which is now in beta so I had to do install webpack@next. Also, webpack-cli is now its own package so I had to install it too. Earlier, it was bundled with webpack itself.

After installing both webpack@next & webpack-cli, I changed my webpack.config,js to look like -

webpack.config,js

const path = require("path");
const webpack = require("webpack");

const config = {
    entry: "./app.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js"
    }
};

module.exports = config;

And ran the following to generate bundle with TreeShaking

webpack --optimize-minimize --mode production

The complete code is available at https://github.com/deadcoder0904/webpack-treeshake

Checkout my other answer to TreeShake with Lodash which does all the treeshaking

Upvotes: 1

Related Questions