Reputation: 8683
I have 2 files app.js
& math.js
It contains the following code -
import { sum } from "./math";
console.log("💩");
console.log(sum(2, 3));
export const sum = (a, b) => a + b;
export const multiply = (m, n) => m * n;
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
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 -
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