Related issue: mishoo/UglifyJS#3110
Bug report
What is the current behavior?
webpack include dev module in production bundle.
If the current behavior is a bug, please provide the steps to reproduce.
Re-pro: https://github.com/dangh/test-webpack-treeshaking
The dev.js
is included in the bundle with the following code:
const __DEV__ = process.env.NODE_ENV === 'development'
if (__DEV__) {
require('./dev.js')
}
But not this one:
if (process.env.NODE_ENV === 'development') {
require('./dev.js')
}
UglifyJS/BabelMinify doesn’t help since it can optimize the calling site but doesn’t touch the bundled module.
See the output: https://github.com/dangh/test-webpack-treeshaking/blob/master/dist/main.js#L74-L79
What is the expected behavior?
The dev.js
module shouldn’t be included in the bundle.
Other relevant information:
webpack version: 4.7.0
Node.js version: 9.11.1
Operating System: macOS
Additional tools:
webpack is able to evaluate expressions but it does not memoize the bindings’ value.
tl;dr: webpack only optimizes constant expressions. Use
DefinePlugin
to replace variables.