When you bundle the application without transformations (like Babel) and minification (like UglifyJS), you get: unused harmony export.
Now Webpack 2+ only marks code unused and doesn’t export it inside the module. It pulls everything and leaves unused code for minification libraries.
You can use UglifyJS with babel for this.
UglifyJS doesn’t support the new language features of Javascript ES2015+ yet. You will need Babel to transpile the code to ES5 and then use UglifyJS to clean up the unused code.
You will need .babelrc file with:
We have to tell the preset (in our case babel-preset-env) to skip the module transpilation.
{
"presets": [
["env", {
"loose": true,
"modules": false
}]
]
}
and corresponding webpack config:
module: {
rules: [
{ test: /\.js$/, loader: 'babel-loader' }
]
},
plugins: [
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: true
},
output: {
comments: false
},
sourceMap: false
})
]
OR
Babili is a better option since Babili will remove unused code before transpilation. It is much easier to spot unused classes before downleveled to ES5. Tree-shaking will also work for class declarations, not just functions.
You will need:
npm install [babili][3] [babili-webpack-plugin][4] --save-dev
Use the following plugin in your webpack config, like so:
plugins: [
new BabiliPlugin()
]
There is also an optimzied way to use babili as a preset. You can refer their site to use it as a preset for babel-loader.