It appears the other answers are outdated. With webpack 4, you can set mode: 'production'
in your webpack config.
In your code, check for development mode like this:
if (process.env.NODE_ENV === 'development') {
if(info instanceof LogModel)
throw new Error("not a instance of LogModel");
}
When webpack creates a bundle with mode: 'production'
, all the code inside these if clauses, along with the if clauses themselves, will be automatically removed from the bundle.
There is no need to use the define plugin explicitly (it is used by webpack “under the hood”), and it's not necessary to use something like webpack-unassert-loader
or webpack-strip-block
mentioned in other answers.
Check out this little demo repo I have made to try this out: https://github.com/pahund/webpack-devprod-experiment
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…